forked from axrl/common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatePackageVersion.ts
69 lines (63 loc) · 2.82 KB
/
updatePackageVersion.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
import {readFile, writeFile} from 'fs/promises';
import {extname, join} from 'path';
const libraryPackageJsonPath = process.argv[2];
if (libraryPackageJsonPath === null || libraryPackageJsonPath === undefined) {
throw new Error('Не передан путь к файлу package.json, который требуется обновить.');
} else {
if (typeof libraryPackageJsonPath !== 'string') {
throw new Error('Путь к обновляемому файлу package.json должен быть строкой.');
} else {
if (extname(libraryPackageJsonPath) !== '.json') {
throw new Error('Указанный файл - не json.');
}
}
}
const mainPackageJsonPath = join(process.cwd(), 'package.json');
async function main(): Promise<void> {
try {
const libraryPackageJson = JSON.parse(
await readFile(libraryPackageJsonPath, {encoding: 'utf-8'}),
);
const mainPackageJson = JSON.parse(
await readFile(mainPackageJsonPath, {encoding: 'utf-8'}),
);
const versionArray = libraryPackageJson?.version?.split('.');
const libraryDependencies = libraryPackageJson.peerDependencies;
let haveChanges: boolean = false;
const checkDep = (main: string, library: string): boolean => {
return !!main && !library.includes('>=') && main !== library;
};
Object.keys(libraryDependencies).forEach(key => {
if (checkDep(mainPackageJson.dependencies[key], libraryDependencies[key])) {
libraryDependencies[key] = mainPackageJson.dependencies[key];
if (!haveChanges) {
haveChanges = true;
}
} else {
if (checkDep(mainPackageJson.devDependencies[key], libraryDependencies[key])) {
libraryDependencies[key] = mainPackageJson.devDependencies[key];
if (!haveChanges) {
haveChanges = true;
}
}
}
});
if (haveChanges || (versionArray && versionArray?.[2])) {
if (versionArray && versionArray?.[2]) {
versionArray[2] = String(+versionArray[2] + 1);
libraryPackageJson.version = versionArray.join('.');
}
await writeFile(libraryPackageJsonPath, JSON.stringify(libraryPackageJson, null, 2));
// eslint-disable-next-line no-console
console.log('Successfully update library package.json file!.');
} else {
throw new Error(
`Error on update version in package.json file!. Current value (need fix) = ${libraryPackageJson.version}.`,
);
}
} catch (message) {
// eslint-disable-next-line no-console
console.log(message);
}
}
main();