-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenUtilIndices.js
62 lines (54 loc) · 1.34 KB
/
genUtilIndices.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const fs = require( 'fs/promises' );
const path = require( 'path' );
/**
* Generates `util` and `mw/util` indices.
*/
( async () => {
/**
*
* @param subpath
*/
async function genIndex( subpath ) {
const utilPath = path.join( process.cwd(), 'src', subpath );
const modules = [];
const hasNoDefault = [];
const dir = ( await fs.opendir( utilPath ) );
for await ( const fse of dir ) {
const moduleName = fse.name.replace( /\.tsx?$/, '' );
if ( fse.isFile() && fse.name !== 'index.ts' ) {
modules.push( moduleName );
if (
!( await fs.readFile( path.join( utilPath, fse.name ) ) )
.toString( 'utf8' )
.includes( 'export default' )
) {
hasNoDefault.push( moduleName );
}
}
}
await fs.writeFile(
path.join( utilPath, 'index.ts' ),
modules
.map( v => `import ${
hasNoDefault.includes( v ) ? `* as ${v}` : v
} from './${v}';` )
.join( '\n' ) +
'\n' +
'export default {\n' +
modules
.map( ( v, i ) => `\t${v}: ${v}` + ( i === modules.length - 1 ? '' : ',' ) )
.join( '\n' ) + '\n' +
'};\n'
);
}
try {
await genIndex( 'util' );
} catch ( e ) {
console.error( 'Failed to generate indices for src/util.', e );
}
try {
await genIndex( 'wiki/util' );
} catch ( e ) {
console.error( 'Failed to generate indices for src/wiki/util.', e );
}
} )();