This repository was archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathupdate-version.js
More file actions
executable file
·102 lines (86 loc) · 3.45 KB
/
Copy pathupdate-version.js
File metadata and controls
executable file
·102 lines (86 loc) · 3.45 KB
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
#!/usr/bin/env node
// Copyright 2017-2020 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0
const fs = require('fs');
const path = require('path');
const [type] = require('yargs').demandCommand(1).argv._;
const execSync = require('child_process').execSync;
const TYPES = ['major', 'minor', 'patch', 'prerelease'];
if (!TYPES.includes(type)) {
throw new Error(`Invalid version bump "${type}", expected one of ${TYPES.join(', ')}`);
}
function updateDependencies(dependencies, others, version) {
return Object.entries(dependencies)
.sort((a, b) => a[0].localeCompare(b[0]))
.reduce((result, [key, value]) => {
result[key] = others.includes(key) && value !== '*' ? (value.startsWith('^') ? `^${version}` : version) : value;
return result;
}, {});
}
console.log('$ update versions', process.argv.slice(2).join(' '));
console.log(`yarn version ${type}`);
execSync(`yarn version ${type}`);
function deleteStableVersion() {
const packageJsonPath = path.join(process.cwd(), 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
delete packageJson.stableVersion;
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`);
}
deleteStableVersion();
// yarn workspaces does an OOM, manual looping takes ages
if (fs.existsSync('packages')) {
const { version } = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8'));
console.log(`Updating packages version ${version}`);
const packages = fs
.readdirSync('packages')
.map((dir) => path.join(process.cwd(), 'packages', dir, 'package.json'))
.filter((pkgPath) => fs.existsSync(pkgPath))
.map((pkgPath) => [pkgPath, JSON.parse(fs.readFileSync(pkgPath, 'utf8'))]);
const others = packages.map(([, json]) => json.name);
packages.forEach(([pkgPath, json]) => {
const updated = Object.keys(json).reduce((result, key) => {
if (key === 'version') {
result[key] = version;
} else if (
['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies', 'resolutions'].includes(key)
) {
result[key] = updateDependencies(json[key], others, version);
} else {
result[key] = json[key];
}
return result;
}, {});
fs.writeFileSync(pkgPath, `${JSON.stringify(updated, null, 2)}\n`);
});
}
if (fs.existsSync('examples')) {
const { version } = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8'));
console.log(`Updating examples version ${version}`);
const packages = fs
.readdirSync('examples')
.map((dir) => path.join(process.cwd(), 'examples', dir, 'package.json'))
.filter((pkgPath) => fs.existsSync(pkgPath))
.map((pkgPath) => [pkgPath, JSON.parse(fs.readFileSync(pkgPath, 'utf8'))]);
const others = packages.map(([, json]) => json.name);
packages.forEach(([pkgPath, json]) => {
const updated = Object.keys(json).reduce((result, key) => {
if (key === 'version') {
result[key] = version;
} else if (
['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies', 'resolutions'].includes(key)
) {
result[key] = updateDependencies(json[key], others, version);
} else {
result[key] = json[key];
}
return result;
}, {});
fs.writeFileSync(pkgPath, `${JSON.stringify(updated, null, 2)}\n`);
});
}
try {
execSync('yarn');
} catch (e) {
console.log('! $YARN failed');
console.log(e);
}