|
| 1 | +import fs from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import url from 'node:url' |
| 4 | + |
| 5 | +const __dirname: string = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '..') |
| 6 | + |
| 7 | +const workspaceNames: string[] = ['packages'] |
| 8 | +for (const workspaceName of workspaceNames) { |
| 9 | + const packagesDir: string = path.resolve(__dirname, workspaceName) |
| 10 | + if (!fs.existsSync(packagesDir) || !fs.statSync(packagesDir).isDirectory()) continue |
| 11 | + |
| 12 | + const packageNames: string[] = fs.readdirSync(packagesDir) |
| 13 | + for (const packageName of packageNames) { |
| 14 | + const projectPath: string = path.resolve(packagesDir, packageName, 'project.json') |
| 15 | + if (fs.existsSync(projectPath) && !fs.statSync(projectPath).isFile()) continue |
| 16 | + |
| 17 | + const testsPath: string = path.resolve(packagesDir, packageName, '__test__') |
| 18 | + const hasTests: boolean = fs.existsSync(testsPath) && fs.statSync(testsPath).isDirectory() |
| 19 | + |
| 20 | + const isTool: boolean = /^tool-|-tool$/.test(packageName) |
| 21 | + const content: string = genProjectJSON({ workspaceName, packageName, hasTests, isTool }) |
| 22 | + fs.writeFileSync(projectPath, content, 'utf8') |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function genProjectJSON(data: { |
| 27 | + workspaceName: string |
| 28 | + packageName: string |
| 29 | + hasTests: boolean |
| 30 | + isTool: boolean |
| 31 | +}): string { |
| 32 | + const { workspaceName, packageName, hasTests, isTool } = data |
| 33 | + const rollupConfigFilename: string = isTool ? 'rollup.config.cli.mjs' : 'rollup.config.mjs' |
| 34 | + const cwd: string = `${workspaceName}/${packageName}` |
| 35 | + const sourceRoot: string = `${cwd}/src` |
| 36 | + |
| 37 | + const json = { |
| 38 | + $schema: '../../node_modules/nx/schemas/project-schema.json', |
| 39 | + name: packageName, |
| 40 | + sourceRoot, |
| 41 | + projectType: 'library', |
| 42 | + tags: [], |
| 43 | + targets: { |
| 44 | + clean: { |
| 45 | + executor: 'nx:run-commands', |
| 46 | + options: { |
| 47 | + cwd, |
| 48 | + parallel: false, |
| 49 | + commands: ['rimraf lib'], |
| 50 | + }, |
| 51 | + }, |
| 52 | + build: { |
| 53 | + executor: 'nx:run-commands', |
| 54 | + dependsOn: ['clean', '^build'], |
| 55 | + options: { |
| 56 | + cwd, |
| 57 | + parallel: false, |
| 58 | + commands: [`rollup -c ../../${rollupConfigFilename}`], |
| 59 | + env: { |
| 60 | + NODE_ENV: 'production', |
| 61 | + ROLLUP_SHOULD_SOURCEMAP: 'true', |
| 62 | + }, |
| 63 | + }, |
| 64 | + configurations: { |
| 65 | + production: { |
| 66 | + env: { |
| 67 | + NODE_ENV: 'production', |
| 68 | + ROLLUP_SHOULD_SOURCEMAP: 'false', |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + watch: { |
| 74 | + executor: 'nx:run-commands', |
| 75 | + options: { |
| 76 | + cwd, |
| 77 | + parallel: false, |
| 78 | + commands: [`rollup -c ../../${rollupConfigFilename} -w`], |
| 79 | + env: { |
| 80 | + NODE_ENV: 'development', |
| 81 | + ROLLUP_SHOULD_SOURCEMAP: 'true', |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + test: hasTests |
| 86 | + ? { |
| 87 | + executor: 'nx:run-commands', |
| 88 | + options: { |
| 89 | + cwd, |
| 90 | + commands: [ |
| 91 | + 'node --experimental-vm-modules ../../node_modules/.bin/jest --config ../../jest.config.mjs --rootDir .', |
| 92 | + ], |
| 93 | + }, |
| 94 | + configurations: { |
| 95 | + coverage: { |
| 96 | + commands: [ |
| 97 | + 'node --experimental-vm-modules ../../node_modules/.bin/jest --config ../../jest.config.mjs --rootDir . --coverage', |
| 98 | + ], |
| 99 | + }, |
| 100 | + update: { |
| 101 | + commands: [ |
| 102 | + 'node --experimental-vm-modules ../../node_modules/.bin/jest --config ../../jest.config.mjs --rootDir . -u', |
| 103 | + ], |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | + : undefined, |
| 108 | + }, |
| 109 | + } |
| 110 | + return JSON.stringify(json, null, 2) |
| 111 | +} |
0 commit comments