-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolkadot-dev-version.cjs
executable file
·71 lines (55 loc) · 2.19 KB
/
polkadot-dev-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
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
// Copyright 2017-2021 @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('./execSync.cjs');
const TYPES = ['major', 'minor', 'patch', 'pre'];
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;
}, {});
}
function updatePackage (version, others, 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 if (key !== 'stableVersion') {
result[key] = json[key];
}
return result;
}, {});
fs.writeFileSync(pkgPath, `${JSON.stringify(updated, null, 2)}\n`);
}
console.log('$ polkadot-dev-version', process.argv.slice(2).join(' '));
execSync(`yarn version ${type === 'pre' ? 'prerelease' : type}`);
const rootPath = path.join(process.cwd(), 'package.json');
const rootJson = JSON.parse(fs.readFileSync(rootPath, 'utf8'));
updatePackage(rootJson.version, [], rootPath, rootJson);
// yarn workspaces does an OOM, manual looping takes ages
if (fs.existsSync('packages')) {
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]) => {
updatePackage(rootJson.version, others, pkgPath, json);
});
}
execSync('yarn');