|
| 1 | +import { findWorkspacePackages } from '@pnpm/workspace.find-packages'; |
| 2 | +import { spawnSync } from 'child_process'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as process from 'process'; |
| 6 | + |
| 7 | +enum ResultType { |
| 8 | + SUCCESS, |
| 9 | + WARNING, |
| 10 | + FAILED |
| 11 | +} |
| 12 | + |
| 13 | +type ProcessResult = |
| 14 | + | { |
| 15 | + demoName: string; |
| 16 | + resultType: ResultType.SUCCESS; |
| 17 | + } |
| 18 | + | { |
| 19 | + demoName: string; |
| 20 | + resultType: ResultType.WARNING; |
| 21 | + message: string; |
| 22 | + } |
| 23 | + | { |
| 24 | + demoName: string; |
| 25 | + resultType: ResultType.FAILED; |
| 26 | + message: string; |
| 27 | + }; |
| 28 | + |
| 29 | +type Flags = { |
| 30 | + '--ignore-workspace': boolean; |
| 31 | + '--pattern': boolean; |
| 32 | + '--dry-run': boolean; |
| 33 | +}; |
| 34 | + |
| 35 | +const DEMOS_DIR = 'demos'; |
| 36 | + |
| 37 | +const workspacePackages = await findWorkspacePackages(path.resolve('.'), { |
| 38 | + patterns: ['./packages/*'] |
| 39 | +}); |
| 40 | + |
| 41 | +const defaultFlags = (): Flags => { |
| 42 | + return { |
| 43 | + '--ignore-workspace': false, |
| 44 | + '--pattern': false, |
| 45 | + '--dry-run': false |
| 46 | + }; |
| 47 | +}; |
| 48 | + |
| 49 | +const parseArgs = (processArgs: string[]): [string, string[], Flags] => { |
| 50 | + // processArgs[0] == 'node' |
| 51 | + // processArgs[1] == 'bump-demo-package.ts' |
| 52 | + const name: string = processArgs[1]; |
| 53 | + const args: string[] = []; |
| 54 | + const flags: Flags = defaultFlags(); |
| 55 | + |
| 56 | + // Boolean flags |
| 57 | + for (const arg of processArgs.slice(2)) { |
| 58 | + if (arg in flags) { |
| 59 | + flags[arg] = true; |
| 60 | + } else { |
| 61 | + args.push(arg); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return [name, args, flags]; |
| 66 | +}; |
| 67 | + |
| 68 | +let [_programName, programArgs, programOpts]: [string, string[], Flags] = parseArgs(process.argv); |
| 69 | + |
| 70 | +const processDemo = (demoPath: string): ProcessResult => { |
| 71 | + const demoName = path.basename(demoPath); |
| 72 | + const demoSrc = path.resolve(demoPath); |
| 73 | + |
| 74 | + console.log(`Processing ${demoName}`); |
| 75 | + if (!fs.lstatSync(demoSrc).isDirectory()) { |
| 76 | + return { |
| 77 | + demoName, |
| 78 | + resultType: ResultType.WARNING, |
| 79 | + message: `'${demoSrc}' is not a directory.` |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + const packageJsonPath = path.join(demoSrc, 'package.json'); |
| 84 | + let packageJson: any; |
| 85 | + |
| 86 | + try { |
| 87 | + packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
| 88 | + } catch (e) { |
| 89 | + if (e instanceof SyntaxError) { |
| 90 | + return { |
| 91 | + demoName, |
| 92 | + resultType: ResultType.FAILED, |
| 93 | + message: `Error parsing package.json: ${e.message}` |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + if (!('code' in e)) { |
| 98 | + return { |
| 99 | + demoName, |
| 100 | + resultType: ResultType.FAILED, |
| 101 | + message: `Unknown error: ${e}` |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + return { |
| 106 | + demoName, |
| 107 | + resultType: ResultType.FAILED, |
| 108 | + message: `Error reading package.json: ${e.message}` |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + let result: ProcessResult = { |
| 113 | + demoName, |
| 114 | + resultType: ResultType.SUCCESS |
| 115 | + }; |
| 116 | + const processDeps = (deps: [string, string][]) => { |
| 117 | + for (const workspacePackage of workspacePackages) { |
| 118 | + const packageName = workspacePackage.manifest.name!; |
| 119 | + if (packageName in deps) { |
| 120 | + const originalVersion = deps[packageName]; |
| 121 | + const latestVersion = workspacePackage.manifest.version!; |
| 122 | + |
| 123 | + if (result.resultType !== ResultType.WARNING && originalVersion.startsWith('workspace')) { |
| 124 | + if (!programOpts['--ignore-workspace']) { |
| 125 | + result = { |
| 126 | + demoName, |
| 127 | + resultType: ResultType.WARNING, |
| 128 | + message: `Package '${packageName}' had version '${originalVersion}' which is unexpected.` |
| 129 | + }; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + console.log(`> ${packageName}: ${originalVersion} => ${latestVersion}`); |
| 134 | + deps[packageName] = latestVersion; |
| 135 | + } |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | + if (packageJson.dependencies) { |
| 140 | + processDeps(packageJson.dependencies); |
| 141 | + } |
| 142 | + |
| 143 | + if (packageJson.peerDependencies) { |
| 144 | + processDeps(packageJson.peerDependencies); |
| 145 | + } |
| 146 | + |
| 147 | + if (packageJson.devDependencies) { |
| 148 | + processDeps(packageJson.devDependencies); |
| 149 | + } |
| 150 | + |
| 151 | + if (!programOpts['--dry-run']) { |
| 152 | + try { |
| 153 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n'); |
| 154 | + } catch (e) { |
| 155 | + result = { |
| 156 | + demoName, |
| 157 | + resultType: ResultType.FAILED, |
| 158 | + message: `Error writing package.json: ${e.message}` |
| 159 | + }; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + console.log('Done\n'); |
| 164 | + return result; |
| 165 | +}; |
| 166 | + |
| 167 | +const main = () => { |
| 168 | + let demos: string[] = []; |
| 169 | + |
| 170 | + if (programOpts['--pattern']) { |
| 171 | + const pattern = programArgs.join(' '); |
| 172 | + console.log(`Finding demos using pattern '${pattern}'`); |
| 173 | + |
| 174 | + const process = spawnSync('find', [DEMOS_DIR, '-maxdepth', '1', '-name', pattern], { |
| 175 | + encoding: 'utf8' |
| 176 | + }); |
| 177 | + demos = process.stdout.split('\n').filter((d) => d.trim().length > 0); |
| 178 | + |
| 179 | + console.log('Done\n'); |
| 180 | + } else { |
| 181 | + const allDemos = fs.readdirSync(path.resolve('./demos')); |
| 182 | + const givenDemos = programArgs; |
| 183 | + |
| 184 | + if (givenDemos.length > 0) { |
| 185 | + console.log('Bumping given demos'); |
| 186 | + demos = givenDemos; |
| 187 | + } else { |
| 188 | + console.log('Bumping all demos'); |
| 189 | + demos = allDemos; |
| 190 | + } |
| 191 | + |
| 192 | + demos = demos.map((d) => path.join(DEMOS_DIR, d)); |
| 193 | + } |
| 194 | + |
| 195 | + const results = demos.map((demo) => processDemo(demo)); |
| 196 | + |
| 197 | + const warnings = results.filter((r) => r.resultType == ResultType.WARNING); |
| 198 | + const errors = results.filter((r) => r.resultType == ResultType.FAILED); |
| 199 | + |
| 200 | + if (warnings.length > 0) { |
| 201 | + console.log('Warnings:'); |
| 202 | + } |
| 203 | + |
| 204 | + if (errors.length > 0) { |
| 205 | + console.log('Failures:'); |
| 206 | + } |
| 207 | +}; |
| 208 | + |
| 209 | +main(); |
0 commit comments