-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild-board-index.js
43 lines (36 loc) · 1.54 KB
/
build-board-index.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
import { writeFileSync } from 'node:fs';
import { join, parse, relative, basename, extname } from 'node:path';
import { globSync } from 'glob';
const srcPath = join(import.meta.dirname, 'packages/components/src');
const boardIndexPath = join(srcPath, 'board-index.ts');
const boardPaths = globSync('**/*.board.tsx');
const nameCounters = {};
const imports = [];
for (const boardPath of boardPaths) {
const parsedBoardPath = parse(boardPath);
const relativePath = relative(srcPath, boardPath);
const relativeSpecifier = `./${relativePath.replace(/\\/g, '/')}`.slice(0, -parsedBoardPath.ext.length) + '.js';
const nameWithoutDotBoard = basename(parsedBoardPath.name, extname(parsedBoardPath.name));
const escapedName = nameWithoutDotBoard.replace(/-/g, '_');
const localName = nameCounters[escapedName] ? `${escapedName}${nameCounters[escapedName]}` : escapedName;
nameCounters[escapedName] ??= 0;
nameCounters[escapedName]++;
const importStatement = `import ${localName} from '${relativeSpecifier}';`;
imports.push({
statement: importStatement,
export: localName,
});
}
imports.sort((a, b) => a.statement.localeCompare(b.statement));
const boardIndexSource = `/**
* This file is auto generated when the project is built
* to rebuild only the file 'npm run build:boards'
* generated at 'build-board-index.js'
* do no edit manually
*/
${imports.map((i) => i.statement).join('\n')}
export default [
${imports.map((i) => ` ${i.export},`).join('\n')}
];
`;
writeFileSync(boardIndexPath, boardIndexSource);