-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-version-numbers.ts
77 lines (67 loc) · 1.92 KB
/
update-version-numbers.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
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
import { readFileSync, writeFileSync } from 'fs';
import { EOL } from 'os';
import * as readline from 'readline';
import pkg from 'glob';
const { glob } = pkg;
import { packages } from './config';
import { createBuilder } from './utils';
const [newVersion] = process.argv.slice(2);
const packagesName = '@house-of-angular';
if (newVersion) {
updateVersions(newVersion);
} else {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(`What's the new version? `, (version) => {
rl.close();
updateVersions(version);
});
}
function updateVersions(version: string) {
const publishNext = createBuilder([
['Update package.json', createPackageJsonBuilder(version)],
]);
publishNext({
scope: packagesName,
packages,
}).catch((err) => {
console.error(err);
process.exit(1);
});
}
/**
* Updates package versions in package.json files
* Updates peerDependencies versions of house-of-angular packages in package.json files
*/
function createPackageJsonBuilder(version: string) {
return async () => {
glob
.sync('**/package.json', { ignore: '**/node_modules/**' })
.map((file: any) => {
const content = readFileSync(file, 'utf-8');
const pkg = JSON.parse(content);
let saveFile = false;
if (pkg?.version && pkg?.name?.startsWith(packagesName)) {
pkg.version = version;
saveFile = true;
}
if (pkg?.peerDependencies) {
Object.keys(pkg.peerDependencies).forEach((key) => {
if (key.startsWith(packagesName)) {
pkg.peerDependencies[key] = version;
saveFile = true;
}
});
}
if (saveFile) {
writeAsJson(file, pkg);
}
});
};
}
function writeAsJson(path: string, json: object) {
const content = JSON.stringify(json, null, 2);
writeFileSync(path, `${content}${EOL}`);
}