refactor: use template (#178)

This commit is contained in:
AIsouler
2024-04-21 18:18:03 +08:00
committed by GitHub
parent 022f6ccbf6
commit 384d0c7f50
1469 changed files with 2335 additions and 17772 deletions

View File

@@ -1,4 +1,7 @@
import subsConfig from '../src/config';
import { writeConfig } from '../src/file';
import { updateDist } from '@gkd-kit/tools';
import { updateReadMeMd } from './updateReadMeMd';
import subscription from './check';
await writeConfig(subsConfig);
await updateDist(subscription);
await updateReadMeMd();

View File

@@ -1,7 +1,9 @@
import subsConfig from '../src/config';
import { checkConfig } from '../src/file';
import picocolors from 'picocolors';
import subscription from '../src/subscription';
import { checkSubscription } from '@gkd-kit/tools';
import { checkDeprecatedGroupKeys } from '../src/appDeprecatedKeys';
checkConfig(subsConfig);
checkSubscription(subscription);
console.log(picocolors.green('校验成功, 合法订阅'));
checkDeprecatedGroupKeys(subscription.apps!);
export default subscription;

View File

@@ -1,6 +0,0 @@
import fs from 'node:fs/promises';
import type PkgT from '../package.json';
const pkg: typeof PkgT = JSON.parse(
await fs.readFile(process.cwd() + '/package.json', 'utf-8'),
);
process.stdout.write(pkg.version);

42
scripts/updateReadMeMd.ts Normal file
View File

@@ -0,0 +1,42 @@
import fs from 'node:fs/promises';
// 从 README.md 中解析出 GROUP_SIZE、GLOBALGROUP_SIZE 和 VERSION 的值
const parseReadMeMd = async () => {
const readmePath = process.cwd() + '/dist/README.md';
const readmeContent = await fs.readFile(readmePath, 'utf-8');
// 使用正则表达式匹配需要的值
const groupSizeMatch = readmeContent.match(/\|应用规则\|(\d+)\|/);
const globalGroupSizeMatch = readmeContent.match(/\|全局规则\|(\d+)\|/);
const appSizeMatch = readmeContent.match(/\|应用\|(\d+)\|/);
const versionMatch = readmeContent.match(/v(\d+)/);
const APP_SIZE = appSizeMatch ? appSizeMatch[1] : '';
const GROUP_SIZE = groupSizeMatch ? groupSizeMatch[1] : '';
const GLOBALGROUP_SIZE = globalGroupSizeMatch ? globalGroupSizeMatch[1] : '';
const VERSION = versionMatch ? versionMatch[1] : '';
return { APP_SIZE, GROUP_SIZE, GLOBALGROUP_SIZE, VERSION };
};
// 更新 README.md 的模板内容并写入文件
export const updateReadMeMd = async () => {
const { APP_SIZE, GROUP_SIZE, GLOBALGROUP_SIZE, VERSION } =
await parseReadMeMd();
const mdTemplatePath = process.cwd() + '/Template.md';
const readmeMdPath = process.cwd() + '/README.md';
// 读取模板文件
const mdTemplate = await fs.readFile(mdTemplatePath, 'utf-8');
// 替换模板中的占位符
const readMeMdText = mdTemplate
.replace('--APP_SIZE--', APP_SIZE)
.replace('--GROUP_SIZE--', GROUP_SIZE)
.replace('--GLOBALGROUP_SIZE--', GLOBALGROUP_SIZE)
.replaceAll('--VERSION--', VERSION);
// 写入 README.md 文件
await fs.writeFile(readmeMdPath, readMeMdText);
};

View File

@@ -1,61 +0,0 @@
import fs from 'node:fs/promises';
import url from 'node:url';
import type { RawApp } from '@gkd-kit/api';
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: { default: RawApp } = 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);
}