Skip to content

Commit a617a65

Browse files
committed
feat: updater scripts
1 parent c0e6f4a commit a617a65

File tree

10 files changed

+415
-269
lines changed

10 files changed

+415
-269
lines changed

.github/workflows/release.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ jobs:
104104
uses: actions/setup-node@v1
105105
with:
106106
node-version: 16
107-
- run: yarn add -D minimist esno typescript
108-
# refs/tags/v0.0.0 -> 0.0.0
109-
- run: yarn updater -v=${GITHUB_REF:11}
107+
- run: yarn add -D node-fetch @actions/github
108+
- run: yarn updater
109+
env:
110+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
111+
# refs/tags/v0.0.0 -> 0.0.0
112+
TAG: ${GITHUB_REF:11}
110113

111114
- name: Deploy install.json
112115
uses: peaceiris/actions-gh-pages@v3

UPDATE_LOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Updater Log
2+
3+
## v0.1.6
4+
5+
Development toolbox, and more...

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{
22
"name": "omb",
33
"private": true,
4-
"version": "0.0.0",
4+
"version": "0.1.5",
55
"author": "lencx <[email protected]>",
66
"scripts": {
77
"dev": "vite --port=4096",
88
"build": "rsw build && tsc && vite build",
99
"preview": "vite preview",
1010
"tauri": "tauri",
1111
"rsw": "rsw",
12-
"updater": "esno ./scripts/updater.ts"
12+
"updater": "node scripts/updater.mjs",
13+
"release": "node scripts/release.mjs"
1314
},
1415
"dependencies": {
1516
"@floating-ui/react-dom-interactions": "^0.6.0",
@@ -39,18 +40,17 @@
3940
"uuid": "^8.3.2"
4041
},
4142
"devDependencies": {
43+
"@actions/github": "^5.1.0",
4244
"@tauri-apps/cli": "^1.0.5",
4345
"@types/highlight.js": "^10.1.0",
4446
"@types/lodash": "^4.14.182",
4547
"@types/markdown-it": "^12.2.3",
46-
"@types/minimist": "^1.2.2",
4748
"@types/node": "^18.7.23",
4849
"@types/react": "^18.0.0",
4950
"@types/react-dom": "^18.0.0",
5051
"@types/uuid": "^8.3.4",
5152
"@vitejs/plugin-react": "^2.0.0",
52-
"esno": "^0.16.3",
53-
"minimist": "^1.2.6",
53+
"node-fetch": "^3.2.10",
5454
"sass": "^1.53.0",
5555
"typescript": "^4.8.4",
5656
"unocss": "^0.33.2",

scripts/release.mjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { createRequire } from 'module';
2+
import { execSync } from 'child_process';
3+
import fs from 'fs';
4+
5+
import { resolveUpdateLog } from './updatelog.mjs';
6+
7+
const require = createRequire(import.meta.url);
8+
9+
async function publish() {
10+
const flag = process.argv[2] ?? 'patch';
11+
const packageJson = require('../package.json');
12+
let [a, b, c] = packageJson.version.split('.').map(Number);
13+
14+
if (flag === 'major') {
15+
a += 1;
16+
b = 0;
17+
c = 0;
18+
} else if (flag === 'minor') {
19+
b += 1;
20+
c = 0;
21+
} else if (flag === 'patch') {
22+
c += 1;
23+
} else {
24+
console.log(`Invalid flag "${flag}"`);
25+
process.exit(1);
26+
}
27+
28+
const nextVersion = `${a}.${b}.${c}`;
29+
packageJson.version = nextVersion;
30+
31+
const nextTag = `v${nextVersion}`;
32+
await resolveUpdateLog(nextTag);
33+
34+
fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
35+
36+
execSync('git add ./package.json');
37+
execSync(`git commit -m "v${nextVersion}"`);
38+
execSync(`git tag -a v${nextVersion} -m "v${nextVersion}"`);
39+
execSync(`git push`);
40+
execSync(`git push origin v${nextVersion}`);
41+
console.log(`Publish Successfully...`);
42+
}
43+
44+
publish().catch(console.error);

scripts/updatelog.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
const UPDATE_LOG = 'UPDATE_LOG.md';
5+
6+
export function resolveUpdateLog(tag) {
7+
const reTag = /## v[\d\.]+/;
8+
9+
const file = path.join(process.cwd(), UPDATE_LOG);
10+
11+
if (!fs.existsSync(file)) {
12+
console.log('Could not found UPDATE_LOG.md');
13+
process.exit(1);
14+
}
15+
16+
let _tag;
17+
const tagMap = {};
18+
const content = fs.readFileSync(file, { encoding: 'utf8' }).split('\n');
19+
20+
content.forEach((line, index) => {
21+
if (reTag.test(line)) {
22+
_tag = line.slice(3).trim();
23+
if (!tagMap[_tag]) {
24+
tagMap[_tag] = [];
25+
return;
26+
}
27+
}
28+
if (_tag) {
29+
tagMap[_tag].push(line);
30+
}
31+
if (reTag.test(content[index + 1])) {
32+
_tag = null;
33+
}
34+
});
35+
36+
if (!tagMap?.[tag]) {
37+
console.log(`Tag ${tag} does not exist`);
38+
process.exit(1);
39+
}
40+
41+
return tagMap[tag].join('\n').trim() || '';
42+
}

scripts/updater.mjs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import fetch from 'node-fetch';
2+
import { getOctokit, context } from '@actions/github';
3+
import fs from 'fs';
4+
5+
import { resolveUpdateLog } from './updatelog.mjs';
6+
7+
const token = process.env.GITHUB_TOKEN;
8+
const version = process.env.TAG || '0.0.0';
9+
10+
async function updater() {
11+
if (!token) {
12+
console.log('GITHUB_TOKEN is required');
13+
process.exit(1);
14+
}
15+
// const options = { owner: 'lencx', repo: 'OhMyBox' };
16+
const options = { owner: context.repo.owner, repo: context.repo.repo };
17+
const github = getOctokit(token);
18+
19+
const { data: tags } = await github.rest.repos.listTags({
20+
...options,
21+
per_page: 10,
22+
page: 1,
23+
});
24+
25+
const tag = tags.find((t) => t.name.startsWith('v'));
26+
// console.log(`${JSON.stringify(tag, null, 2)}`);
27+
28+
if (!tag) return;
29+
30+
const { data: latestRelease } = await github.rest.repos.getReleaseByTag({
31+
...options,
32+
tag: tag.name,
33+
});
34+
35+
const updateData = {
36+
name: tag.name,
37+
notes: resolveUpdateLog(tag.name), // use UPDATE_LOG.md
38+
pub_date: new Date().toISOString(),
39+
version,
40+
platforms: {
41+
win64: { signature: '', url: '' }, // compatible with older formats
42+
linux: { signature: '', url: '' }, // compatible with older formats
43+
darwin: { signature: '', url: '' }, // compatible with older formats
44+
'darwin-aarch64': { signature: '', url: '' },
45+
'darwin-intel': { signature: '', url: '' },
46+
'linux-x86_64': { signature: '', url: '' },
47+
'windows-x86_64': { signature: '', url: '' },
48+
// 'windows-i686': { signature: '', url: '' }, // no supported
49+
},
50+
};
51+
52+
const setAsset = async (asset, reg, platforms) => {
53+
let sig = '';
54+
if (/.sig$/.test(asset.name)) {
55+
sig = await getSignature(asset.browser_download_url);
56+
}
57+
platforms.forEach((platform) => {
58+
if (reg.test(asset.name)) {
59+
// platform signature
60+
if (sig) {
61+
updateData.platforms[platform].signature = sig;
62+
return;
63+
}
64+
// platform url
65+
updateData.platforms[platform].url = asset.browser_download_url;
66+
}
67+
});
68+
};
69+
70+
const promises = latestRelease.assets.map(async (asset) => {
71+
// windows
72+
await setAsset(asset, /.msi.zip/, ['win64', 'windows-x86_64']);
73+
74+
// darwin
75+
await setAsset(asset, /.app.tar.gz/, [
76+
'darwin',
77+
'darwin-aarch64',
78+
'darwin-intel',
79+
]);
80+
81+
// linux
82+
await setAsset(asset, /.AppImage.tar.gz/, ['linux', 'linux-x86_64']);
83+
});
84+
await Promise.allSettled(promises);
85+
if (!fs.existsSync('updater')) {
86+
fs.mkdirSync('updater');
87+
}
88+
fs.writeFileSync(
89+
'./updater/install.json',
90+
JSON.stringify(updateData, null, 2)
91+
);
92+
console.log('Generate updater/install.json');
93+
}
94+
95+
updater().catch(console.error);
96+
97+
// get the signature file content
98+
async function getSignature(url) {
99+
try {
100+
const response = await fetch(url, {
101+
method: 'GET',
102+
headers: { 'Content-Type': 'application/octet-stream' },
103+
});
104+
return response.text();
105+
} catch (_) {
106+
return '';
107+
}
108+
}

scripts/updater.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

src-tauri/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "omb"
3-
version = "0.1.5"
3+
version = "0.0.0"
44
description = "OhMyBox"
55
authors = ["lencx <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)