-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
change-package-version.ts
101 lines (90 loc) · 2.52 KB
/
change-package-version.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import glob from "glob";
import fse from "fs-extra";
import {program} from "commander";
(function findPackageJsonFiles() {
setupCommander();
const options = {
ignore: [
"../../**/node_modules/**",
"../../**/dist/**",
"../../**/build/**",
"../../**/scripts/**",
"../../**/wwwroot/**"
]
};
const workingDir = "../../";
glob(`${workingDir}**/package.json`, options, (err, files) => {
if (err) throw err;
//Todo @masumulu28: check options value and throw error if not provided
const {packageName,targetVersion}= program.opts();
for (const file of files) {
readPackageJsonFile(file, packageName, targetVersion);
}
});
})();
function readPackageJsonFile(path, key, newVersion) {
const replace = (block, key, newVersion) => {
const founded = Object.keys(block).filter(x => x === key);
if (founded.length > 0) {
let value = block[key];
value = value.replace(semverRegex, newVersion);
return [true, {
...block,
[key]:value
}];
}
return [false, block];
};
fse.readJson(path, (err, packageObj) => {
if (err) throw err;
const { dependencies, peerDependencies, devDependencies } = packageObj;
const results = [];
let result = { ...packageObj };
if (dependencies) {
const [founded, d] = replace(dependencies, key, newVersion);
results.push(founded);
result = {
...result, dependencies: d
};
}
if (peerDependencies) {
const [founded, p] = replace(peerDependencies, key, newVersion);
results.push(founded);
result = {
...result, peerDependencies: p
};
}
if (devDependencies) {
const [founded, d] = replace(devDependencies, key, newVersion);
results.push(founded);
result = {
...result, devDependencies: d
};
}
const anyChanges = !results.some(x => x);
if (anyChanges) {
return;
}
console.log("changed", path);
writeFile(path, result);
}
)
;
}
export const semverRegex =
/\d+\.\d+\.\d+(?:-[a-zA-Z0-9]+(?:\.[a-zA-Z0-9-]+)*)?(?:\+[a-zA-Z0-9]+(?:\.[a-zA-Z0-9-]+)*)?$/;
function writeFile(path, result) {
return fse.writeJson(path, result, { spaces: 2 });
}
function setupCommander() {
program
.option(
"-n, --packageName <packageName>",
"Package name"
)
.option(
"-v, --targetVersion <targetVersion>",
"Version number of the package"
);
program.parse(process.argv);
}