11import axios from 'axios'
22import YAML from 'yaml'
3- import fs from 'fs'
4- import path from 'path'
3+ import * as fs from 'fs' ;
4+ import * as path from 'path' ;
55
6- import { LocalCollection , LocalRepository , LocalSidecar , RemoteIndex } from './types'
6+ import { LocalCollection , LocalRepository , LocalSidecar , RemoteIndex , RemotePlugin } from './types'
77import { Plugin } from './plugin'
88import { infoLog , warnLog } from './utils'
9+ import { debuglog } from 'util' ;
910
1011// iterate over folder
1112async function searchRepository ( pathName : string = "plugins" ) : Promise < Plugin [ ] > {
@@ -18,6 +19,8 @@ async function searchRepository(pathName: string = "plugins"): Promise<Plugin[]>
1819 if ( file . endsWith ( ".yml" ) ) {
1920 const fileData = fs . readFileSync ( `${ repoPath } /${ file } ` , 'utf8' )
2021 const localRepo : LocalRepository = YAML . parse ( fileData )
22+ // set name to filename if not defined
23+ if ( ! localRepo . name ) localRepo . name = file . replace ( ".yml" , "" )
2124 repositories . push ( localRepo )
2225 }
2326 } )
@@ -27,13 +30,19 @@ async function searchRepository(pathName: string = "plugins"): Promise<Plugin[]>
2730 const plugin = await parseRepository ( repo )
2831 plugins . push ( ...plugin )
2932 }
33+ // fetch all readmes of plugins
34+ const readmePromises = plugins . map ( plugin => plugin . checkReadme ( ) )
35+ await Promise . all ( readmePromises )
3036 // sort plugins and print to md
3137 const sortedPlugins = plugins
3238 . sort ( ( a , b ) => a . name . toLowerCase ( ) . localeCompare ( b . name . toLowerCase ( ) ) )
3339 return sortedPlugins
3440}
3541
3642function printPlugins ( outputName : string , sortedPlugins : Plugin [ ] ) {
43+ // create folder if not exists
44+ if ( ! fs . existsSync ( `./dist` ) ) fs . mkdirSync ( `./dist` )
45+ if ( ! fs . existsSync ( `./dist/${ outputName } ` ) ) fs . mkdirSync ( `./dist/${ outputName } ` )
3746 // print to file
3847 const outputPath = `./dist/${ outputName } /list.md`
3948 const stream = fs . createWriteStream ( outputPath )
@@ -55,23 +64,37 @@ async function parseRepository(localRepository: LocalRepository): Promise<Plugin
5564 . then ( res => YAML . parse ( res . data ) )
5665 // iterate over remote index and match with sidecars
5766 const indexPlugins : Plugin [ ] = [ ]
67+ const idxMissingScar : Set < RemotePlugin > = new Set ( )
68+ const allIdx : Set < RemotePlugin > = new Set ( )
5869 for ( const index of indexData ) {
5970 const sidecarMatch = repoSidecars . find ( sidecar => sidecar . id == index . id )
60- if ( sidecarMatch ?. hide ) {
61- // skip if hidden, but warn
62- infoLog ( `Skipping hidden plugin: ${ index . name } ` )
63- continue
71+ if ( sidecarMatch ) {
72+ if ( sidecarMatch . id == "example" ) continue // if example, skip
73+ else if ( sidecarMatch . hide ) { // skip but warn if hidden
74+ debuglog ( `Skipping hidden plugin: ${ index . name } ` )
75+ continue
76+ } else allIdx . add ( index ) // add to sidecars
77+ } else { // sidecar not found
78+ if ( repoDefaults . exclusive ) continue // if exclusive, skip
79+ idxMissingScar . add ( index ) // add to missing
6480 }
65- if ( repoDefaults . exclusive && ! sidecarMatch ) continue // if exclusive, skip if no sidecar
6681 const plugin = new Plugin ( repoDefaults , sidecarMatch , index )
6782 indexPlugins . push ( plugin )
6883 }
6984 // check if there are leftover sidecars
70- const extraSidecars = repoSidecars . filter ( sidecar => ! indexPlugins . find ( plugin => plugin . id == sidecar . id && ! sidecar . hide ) )
85+ // not named example
86+ // not in indexPlugins and not hidden
87+ const extraSidecars = repoSidecars . filter ( sidecar => sidecar . id != "example" && ! sidecar . hide && ! indexPlugins . find ( plugin => plugin . id == sidecar . id ) )
7188 if ( extraSidecars . length > 0 ) {
7289 warnLog ( `Found ${ extraSidecars . length } extra sidecars in ${ localRepository . name } ` )
7390 extraSidecars . forEach ( sidecar => warnLog ( ` ${ sidecar . id } ` ) )
7491 }
92+ // check for plugins without sidecars
93+ const missingSCars = Array . from ( idxMissingScar ) . filter ( plugin => allIdx . has ( plugin ) )
94+ if ( missingSCars . length > 0 ) {
95+ infoLog ( `Found ${ missingSCars . length } missing sidecars in ${ localRepository . name } ` )
96+ missingSCars . forEach ( sidecar => infoLog ( ` ${ sidecar . name } ` ) )
97+ }
7598 return indexPlugins
7699}
77100
@@ -84,6 +107,7 @@ async function run() {
84107 // remove themes from plugins
85108 const filteredPlugins = plugins . filter ( plugin => ! themes . some ( theme => theme . id == plugin . id ) )
86109 printPlugins ( "plugins" , filteredPlugins )
110+ console . log ( "finished building plugin index" )
87111}
88112
89113run ( )
0 commit comments