-
Notifications
You must be signed in to change notification settings - Fork 21
/
set-version.cjs
61 lines (50 loc) · 1.55 KB
/
set-version.cjs
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
const fs = require('fs');
const path = require('path');
const version = process.argv[2];
if (!version || !(/^\d+\.\d+\.\d+$/.test(version))) {
console.log('Usage: node set-version.js 1.2.3');
return;
}
const dependencies = [
'mini-canvas-core',
'mini-canvas-editor'
];
function resolvePath(filePath) {
return path.join(__dirname, '..', filePath);
}
function updateDependencies(deps) {
if (!deps) {
return;
}
for (const name in deps) {
if (dependencies.includes(name)) {
deps[name] = `^${version}`;
}
}
}
function updatePackage(filePath, setVersion) {
filePath = resolvePath(filePath);
const json = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
if (setVersion) {
json['version'] = version;
}
updateDependencies(json['dependencies']);
updateDependencies(json['peerDependencies']);
updateDependencies(json['devDependencies']);
fs.writeFileSync(filePath, JSON.stringify(json, null, '\t'), 'utf-8');
}
function updateJsdelivrUrl(filePath) {
filePath = resolvePath(filePath);
let text = fs.readFileSync(filePath, 'utf-8');
text = text.replace(/\/\/cdn\.jsdelivr\.net\/npm\/mini-canvas-(editor|core)@\d+\.\d+\.\d+/g, (found) => {
const parts = found.split('@');
return `${parts[0]}@${version}`;
});
fs.writeFileSync(filePath, text, 'utf-8');
}
updatePackage('core/package.json', true);
updatePackage('editor/package.json', true);
updatePackage('demos/webpack-app/package.json', false);
updatePackage('demos/svelte-app/package.json', false);
updateJsdelivrUrl('README.md');
updateJsdelivrUrl('demos/webpack-app/public/vanilla-javascript.html');