chore: 使用命令更新内存订阅

This commit is contained in:
lisonge 2023-09-27 15:11:28 +08:00
parent 5681a481a0
commit 987397bd2f
3 changed files with 70 additions and 1 deletions

View File

@ -17,7 +17,8 @@
"format": "prettier --cache --write .",
"lint": "eslint --cache --fix ./**/*.{js,ts,jsx,tsx}",
"build": "tsc --noEmit && tsx ./scripts/build.ts",
"check": "tsc --noEmit && tsx ./scripts/check.ts"
"check": "tsc --noEmit && tsx ./scripts/check.ts",
"updateSubs": "tsx ./scripts/updateSubs.ts"
},
"devDependencies": {
"@commitlint/cli": "^17.7.1",

61
scripts/updateSubs.ts Normal file
View File

@ -0,0 +1,61 @@
import fs from 'node:fs/promises';
import url from 'node:url';
import type { AppConfigMudule } from '../src/types';
import { tryRun } from '../src/utils';
// 使用命令更新内存订阅
// 使用格式: pnpm updateSubs deviceUrl appId
// 使用示例-1: pnpm updateSubs http://10.2.156.1:8888 android.zhibo8
// 使用示例-2: pnpm updateSubs 10.2.156.1:8888 android.zhibo8
if (!process.argv[2]) {
throw new Error('miss parameter: deviceUrl');
}
const deviceUrlRaw = process.argv[2].includes('://')
? process.argv[2]
: 'http://' + process.argv[2];
const deviceUrl = tryRun(
() => new url.URL(deviceUrlRaw),
() => void 0,
);
if (!deviceUrl) {
throw new Error('invalid URL: ' + process.argv[2]);
}
deviceUrl.pathname = '/';
if (!(await fetch(deviceUrl).catch(() => false))) {
throw new Error('connect device failed: ' + process.argv[2]);
}
if (!process.argv[3]) {
throw new Error('miss parameter: appId');
}
const appId = process.argv[3];
const tsFp = process.cwd() + `/src/apps/${appId}.ts`;
if (!(await fs.stat(tsFp).catch(() => false))) {
throw new Error('invalid appId: ' + appId);
}
const getAppConfig = async () => {
const mod: AppConfigMudule = await import(url.pathToFileURL(tsFp).href);
return mod.default;
};
const appConfig = await getAppConfig();
const resp = await fetch(deviceUrl.origin + '/api/updateSubsApps', {
method: 'post',
body: JSON.stringify([appConfig]),
headers: {
'Content-Type': 'application/json',
},
}).catch(console.error);
if (!resp) {
throw new Error('更新订阅失败');
}
const r: any = await resp.json();
if (r.__error) {
console.log('更新订阅失败');
console.log(r.message || r);
} else {
console.log('更新订阅成功: ' + appConfig.name);
}

7
src/utils.ts Normal file
View File

@ -0,0 +1,7 @@
export const tryRun = <T>(fc: () => T, fallbackFc: (e: unknown) => T) => {
try {
return fc();
} catch (e) {
return fallbackFc(e);
}
};