forked from seanlandsman/ag-grid-vue3-test-cases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversionModules.js
130 lines (104 loc) · 4.11 KB
/
versionModules.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Note: Assumes working directory is the root of the mono-repo
const fs = require('fs');
const path = require('path');
const pipe =
(...fns) =>
(x) =>
fns.reduce((v, f) => f(v), x);
const getDirectories = source =>
fs.readdirSync(source, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => `${source}/${dirent.name}`)
const packageDirectories = require('./lerna.json').packages.map(package => package.replace('/*', ''))
.map(getDirectories)
.flat();
if (process.argv.length < 3) {
console.log('Usage: node scripts/deployments/versionModules.js [Grid Version]');
console.log('For example: node scripts/deployments/versionModules.js 19.1.0');
console.log('Note: This script should be run from the root of the monorepo');
process.exit(1);
}
const [exec, scriptPath, gridNewVersion] = process.argv;
if (!gridNewVersion) {
console.error('ERROR: Invalid grid or charts version supplied');
process.exit(1);
}
console.log('************************************************************************************************');
console.log(`Setting Grid Version to ${gridNewVersion} `);
console.log('************************************************************************************************');
function main() {
updatePackageJsonFiles();
}
function updatePackageJsonFiles() {
console.log('Updating package.json files');
const CWD = process.cwd();
packageDirectories.forEach((packageDirectory) => {
// update all package.json files
const packageJsonFile = `${CWD}/${packageDirectory}/package.json`;
updateFileWithNewVersions(packageJsonFile);
// update version.ts file
const currentVersionFile = `${CWD}/${packageDirectory}/src/version.ts`;
updateVersionFile(currentVersionFile);
});
}
function updateFileWithNewVersions(currentFile) {
const packageJson = JSON.parse(fs.readFileSync(currentFile, 'utf8'));
const updatedPackageJson = pipe(
updateVersion,
updateDependencies,
updateDevDependencies,
updatePeerDependencies,
updateOptionalDependencies
)(packageJson);
fs.writeFileSync(currentFile, JSON.stringify(updatedPackageJson, null, 2), 'utf8');
}
/**
* Update `version.ts` file with version number if it exists
*/
function updateVersionFile(currentFile) {
if (!fs.existsSync(currentFile)) {
return;
}
fs.readFile(currentFile, 'utf8', (err, contents) => {
const regex = /(export const VERSION =)(.*)$/m;
const substitute = `$1 '${gridNewVersion}';`;
const replacement = contents.replace(regex, substitute);
fs.writeFileSync(currentFile, replacement, 'utf8');
});
}
function updateVersion(packageJson) {
packageJson.version = gridNewVersion;
return packageJson;
}
function updateDependencies(fileContents) {
return updateDependency(fileContents, 'dependencies', gridNewVersion);
}
function updateDevDependencies(fileContents) {
return updateDependency(fileContents, 'devDependencies', gridNewVersion);
}
function updatePeerDependencies(fileContents) {
return updateDependency(fileContents, 'peerDependencies', gridNewVersion);
}
function updateOptionalDependencies(fileContents) {
return updateDependency(fileContents, 'optionalDependencies', gridNewVersion);
}
function updateDependency(fileContents, property, dependencyVersion) {
if (!fileContents[property]) {
return fileContents;
}
const dependencyContents = fileContents[property];
const gridDependency = function (key) {
return key.startsWith('ag-grid') || key.startsWith('@ag-grid');
};
const chartDependency = function (key) {
return key.startsWith('ag-charts') || key.startsWith('@ag-charts');
};
Object.entries(dependencyContents)
.filter(([key, value]) => gridDependency(key) || chartDependency(key))
.filter(([key, value]) => key !== 'ag-grid-testing')
.forEach(([key, value]) => {
dependencyContents[key] = dependencyVersion;
});
return fileContents;
}
main();