forked from angular/dev-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
74 lines (61 loc) · 2.39 KB
/
cli.ts
File metadata and controls
74 lines (61 loc) · 2.39 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @license
* Copyright Google LLC
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Argv, CommandModule} from 'yargs';
import {readFileSync, writeFileSync} from 'node:fs';
import {join} from 'node:path';
import {determineRepoBaseDirFromCwd} from '../../utils/repo-directory';
import {PackageJson, syncNodeJs, syncPnpm, syncTypeScript} from './sync-module-bazel';
import {ChildProcess} from '../../utils/child-process';
import {formatFiles} from '../../format/format';
async function builder(argv: Argv) {
return argv;
}
async function handler() {
const rootDir = determineRepoBaseDirFromCwd();
const packageJsonPath = join(rootDir, 'package.json');
const moduleBazelPath = join(rootDir, 'MODULE.bazel');
const nvmrcPath = join(rootDir, '.nvmrc');
// Read package.json
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) as PackageJson;
const pnpmVersion = packageJson.engines?.pnpm;
const tsVersion = packageJson.dependencies?.typescript || packageJson.devDependencies?.typescript;
// Read .nvmrc
let nvmrcVersion: string | undefined;
try {
nvmrcVersion = readFileSync(nvmrcPath, 'utf8').trim().replace(/^v/, '');
} catch {
// .nvmrc is optional.
}
if (!pnpmVersion) {
throw new Error('Could find engines.pnpm in package.json');
}
if (!tsVersion) {
throw new Error('Could not find typescript in dependencies or devDependencies in package.json');
}
// Read MODULE.bazel
const originalBazelContent = readFileSync(moduleBazelPath, 'utf8');
let moduleBazelContent = originalBazelContent;
moduleBazelContent = await syncPnpm(moduleBazelContent, pnpmVersion);
moduleBazelContent = await syncTypeScript(moduleBazelContent, tsVersion);
moduleBazelContent = await syncNodeJs(moduleBazelContent, nvmrcVersion);
if (originalBazelContent !== moduleBazelContent) {
writeFileSync(moduleBazelPath, moduleBazelContent);
await formatFiles(['MODULE.bazel']);
ChildProcess.spawnSync('pnpm', ['bazel', 'mod', 'deps', '--lockfile_mode=update'], {
suppressErrorOnFailingExitCode: true,
});
}
}
/** CLI command module. */
export const SyncModuleBazelModule: CommandModule = {
builder,
handler,
command: 'sync-module-bazel',
describe:
'Sync node.js, pnpm and typescript versions in MODULE.bazel with package.json and .nvmrc.',
};