|
| 1 | +/** |
| 2 | + * Script to move tsconfig.json's 'references' field into a 'tsconfig.override.json' file. |
| 3 | + */ |
| 4 | + |
| 5 | +import { execSync } from 'child_process'; |
| 6 | +import * as fs from 'fs'; |
| 7 | +import * as path from 'path'; |
| 8 | + |
| 9 | +const demosDir = path.resolve('demos'); |
| 10 | + |
| 11 | +// Function to split user-provided demos into found and not found demos |
| 12 | +const filterDemos = (allDemos: string[], providedDemos: string[]): [string[], string[]] => { |
| 13 | + const found: string[] = []; |
| 14 | + const notFound: string[] = []; |
| 15 | + |
| 16 | + providedDemos.forEach((demo) => { |
| 17 | + if (allDemos.includes(demo)) { |
| 18 | + found.push(demo); |
| 19 | + } else { |
| 20 | + notFound.push(demo); |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | + return [found, notFound]; |
| 25 | +}; |
| 26 | + |
| 27 | +// Function to replace workspace package versions with latest published versions |
| 28 | +const linkDemo = async (demoName: string) => { |
| 29 | + const demoSrc = path.join(demosDir, demoName); |
| 30 | + console.log(`\nProcessing ${demoName}`); |
| 31 | + |
| 32 | + // Update tsconfig.json using tsconfig.override.json |
| 33 | + const tsConfigPath = path.join(demoSrc, 'tsconfig.json'); |
| 34 | + const tsConfigOverridePath = path.join(demoSrc, 'tsconfig.override.json'); |
| 35 | + |
| 36 | + const tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf8')); |
| 37 | + let tsConfigOverride = {}; |
| 38 | + if (fs.existsSync(tsConfigOverridePath)) { |
| 39 | + tsConfigOverride = JSON.parse(fs.readFileSync(tsConfigOverridePath, 'utf8')); |
| 40 | + } |
| 41 | + |
| 42 | + const fieldsToRemove = ['references']; |
| 43 | + |
| 44 | + let changes = 0; |
| 45 | + for (const field of fieldsToRemove) { |
| 46 | + if (tsConfig.hasOwnProperty(field)) { |
| 47 | + tsConfigOverride[field] = tsConfig[field]; |
| 48 | + delete tsConfig[field]; |
| 49 | + changes++; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (changes) { |
| 54 | + fs.writeFileSync(tsConfigPath, `${JSON.stringify(tsConfig, null, 2)}\n`, 'utf8'); |
| 55 | + fs.writeFileSync(tsConfigOverridePath, `${JSON.stringify(tsConfigOverride, null, 2)}\n`, 'utf8'); |
| 56 | + } else { |
| 57 | + console.log('- No changes'); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +// Main function to read demos directory and process each demo |
| 62 | +const main = () => { |
| 63 | + const args: string[] = process.argv.slice(2); |
| 64 | + |
| 65 | + const allDemos = fs.readdirSync(demosDir); |
| 66 | + let demoNames: string[]; |
| 67 | + |
| 68 | + if (args.length > 0) { |
| 69 | + const [foundDemos, notFoundDemos] = filterDemos(allDemos, args); |
| 70 | + |
| 71 | + if (notFoundDemos.length > 0) { |
| 72 | + console.log('⚠️ Warning: Failed to locate some demos:'); |
| 73 | + for (const demo of notFoundDemos) { |
| 74 | + console.log(`- ${demo}`); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + demoNames = foundDemos; |
| 79 | + } else { |
| 80 | + demoNames = allDemos; |
| 81 | + } |
| 82 | + |
| 83 | + console.log('Upgrading demos...'); |
| 84 | + for (const demoName of demoNames) { |
| 85 | + linkDemo(demoName); |
| 86 | + } |
| 87 | + console.log('\nDone.'); |
| 88 | +}; |
| 89 | + |
| 90 | +main(); |
0 commit comments