-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathversion-apply.ts
45 lines (40 loc) · 1.91 KB
/
version-apply.ts
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
import { execSync } from "child_process";
import { existsSync, writeFileSync } from "fs";
import { join } from "path";
import { clean } from "semver";
// Get the version passed as argument
const version = process.argv[2];
if (!version) throw new Error("Please provide a version");
// Clean it (eg: remove `v` from the beginning)
const cleanVersion = clean(version);
if (!cleanVersion) throw new Error("Provided version does not follow semver format");
console.log(`Applying version ${version} ...`);
// Capture any lerna flags if there's any
const args = process.argv.slice(3);
// List all dependencies in the repo
const lernaScript = `lerna list --include-dependencies --include-dependents --json --all --loglevel silent ${args.join(
" ",
)}`;
console.log(`running:\n${lernaScript}`);
const stdout = execSync(lernaScript);
const dependencies = JSON.parse(stdout.toString()) as Array<{ name: string; location: string }>;
const modifiedFilePaths: string[] = [];
// Apply the version to all package.json and app.json files
dependencies.forEach(({ location }) => {
const packageJsonPath = join(location, "package.json");
if (existsSync(packageJsonPath)) {
const packageJsonContent = require(packageJsonPath); // eslint-disable-line @typescript-eslint/no-require-imports
packageJsonContent.version = cleanVersion;
writeFileSync(packageJsonPath, JSON.stringify(packageJsonContent, null, 2));
console.log(`Applied version ${version} to ${packageJsonPath}`);
modifiedFilePaths.push(packageJsonPath);
}
});
// Run prettier to make sure the modified files are properly formatted
const prettierScript = `prettier --config ./packages/tooling/.prettierrc --ignore-path ./packages/tooling/.prettierignore --write ${modifiedFilePaths.join(
" ",
)}`;
console.log(`running:\n${prettierScript}`);
const prettierStdout = execSync(prettierScript);
console.log("\n" + String(prettierStdout));
console.log(`Done applying version ${version}`);