-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-release
88 lines (73 loc) · 2.55 KB
/
create-release
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
#!/usr/bin/env node
import fs from 'fs/promises';
import path from 'path';
import process from 'process';
import semver from 'semver';
import { parse, stringify } from 'smol-toml';
const allowedTypes = ['major', 'minor', 'patch', 'prerelease', 'preminor', 'premajor', 'prepatch'];
const lastArg = process.argv.slice(-1)[0];
const incrementType = allowedTypes.includes(lastArg) ? lastArg : 'patch';
const verFiles = [
['package.json', ['version']],
['src-tauri/Cargo.toml', ['package', 'version']],
['src-tauri/tauri.conf.json', ['package', 'version']],
];
const parsers = {
'.json': JSON.parse,
'.toml': parse,
};
const serializers = {
'.json': (o) => JSON.stringify(o, null, 2),
'.toml': stringify,
};
function findKeyPath(obj, path) {
const next = obj[path[0]];
if (typeof(next) === 'object' && !Array.isArray(next)) {
return findKeyPath(next, path.slice(1));
}
return [next, (newVal) => {
// setter
obj[path[0]] = newVal;
}];
}
const loadedVerFiles = await Promise.all(verFiles.map(async ([filePath, verFieldKeyPath]) => {
const contents = (await fs.readFile(filePath)).toString('utf-8');
const pathParsed = path.parse(filePath);
if (!parsers[pathParsed.ext]) {
console.error(pathParsed);
throw new Error(`no parser for ${pathParsed.ext}`);
}
const parsed = parsers[pathParsed.ext](contents);
return {
filePath,
parsed,
serializer: serializers[pathParsed.ext],
version: findKeyPath(parsed, verFieldKeyPath),
};
}));
// SHOULD check that these are all equal!
const currentVer = loadedVerFiles[0].version[0];
const nextVer = semver.inc(currentVer, incrementType);
console.log(`Incrementing v${currentVer} -> v${nextVer} (${incrementType})\n\n`);
loadedVerFiles.forEach(({ version: [_ver, setter]}) => setter(nextVer));
const written = (await Promise.all(loadedVerFiles.map(({ parsed, filePath, serializer }) =>
fs.writeFile(filePath, serializer(parsed)))))
.every((x) => x === undefined);
if (!written) {
throw new Error("!written");
}
const postCommands = [
'npm install',
'npm run tauri build',
`git commit -am 'chore: bump version to v${nextVer}'`,
`git tag v${nextVer}`,
'git push',
'git push --tags'
];
console.log('Done! Now run:\n');
console.log(` $ ${postCommands.join(' && \\\n ')}`);
console.log('\n... and wait for the build to finish!');
console.log('\n------\n');
console.log('Or, if something went wrong, just run:\n');
console.log(` $ git checkout -- .`);
console.log('\n... to start over.');