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
84 lines (71 loc) · 2.8 KB
/
Copy pathcli.ts
File metadata and controls
84 lines (71 loc) · 2.8 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
75
76
77
78
79
80
81
82
83
84
/**
* @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 {existsSync, 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';
import {getBazelBin} from '../../utils/bazel-bin';
async function builder(argv: Argv) {
return argv;
}
async function handler() {
// TODO: Implement a marker-based root discovery (e.g., .ng-secondary-base).
// This should traverse upward to find the secondary base before falling back to `git rev-parse --show-toplevel`.
// Necessary to support non-standard repository structures where workflows are triggered outside the traditional rootDir.
const rootDir = process.cwd();
const packageJsonPath = join(rootDir, 'package.json');
const moduleBazelPath = join(rootDir, 'MODULE.bazel');
let nvmrcPath = join(rootDir, '.nvmrc');
if (!existsSync(nvmrcPath)) {
nvmrcPath = join(determineRepoBaseDirFromCwd(), '.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 ||
packageJson.dependencies?.['typescript-local']?.replace('npm:typescript@', '');
// Read .nvmrc
let nvmrcVersion: string | undefined;
try {
nvmrcVersion = readFileSync(nvmrcPath, 'utf8').trim().replace(/^v/, '');
} catch {
// .nvmrc is optional.
}
// Read MODULE.bazel
const originalBazelContent = readFileSync(moduleBazelPath, 'utf8');
let moduleBazelContent = originalBazelContent;
if (pnpmVersion) {
moduleBazelContent = await syncPnpm(moduleBazelContent, pnpmVersion);
}
if (tsVersion) {
moduleBazelContent = await syncTypeScript(moduleBazelContent, tsVersion);
}
if (nvmrcVersion) {
moduleBazelContent = await syncNodeJs(moduleBazelContent, nvmrcVersion);
}
if (originalBazelContent !== moduleBazelContent) {
writeFileSync(moduleBazelPath, moduleBazelContent);
await formatFiles([moduleBazelPath]);
ChildProcess.spawnSync(getBazelBin(), ['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.',
};