chore(actions): build_release

This commit is contained in:
lisonge
2023-11-05 02:03:27 +08:00
parent b1a3310841
commit 0da957ee31
8 changed files with 61 additions and 1846 deletions

View File

@@ -2,6 +2,7 @@ import _ from 'lodash';
import fs from 'node:fs/promises';
import path from 'node:path';
import { parseSelector } from './selector';
import type PkgT from '../package.json';
import type { AppConfig, IArray, SubscriptionConfig } from './types';
const iArrayToArray = <T>(array: IArray<T> = []): T[] => {
@@ -18,6 +19,26 @@ const sortKeys: (keyof SubscriptionConfig)[] = [
'apps',
];
const orderdStringify = (
obj: any,
keys: string[],
replacer?: (this: any, key: string, value: any) => any,
space?: string | number,
) => {
const map = new Map<string, unknown>();
keys.forEach((k) => {
if (obj[k] === undefined) return;
map.set(k, obj[k]);
});
return (
JSON.stringify(Object.fromEntries(map.entries()), replacer, space) + '\n'
);
};
const pkg: typeof PkgT = JSON.parse(
await fs.readFile(process.cwd() + '/package.json', 'utf-8'),
);
const pkgKeys = Object.keys(pkg);
export const writeConfig = async (fp: string, config: SubscriptionConfig) => {
const oldConfig: SubscriptionConfig = JSON.parse(
await fs.readFile(fp, 'utf-8').catch(() => '{}'),
@@ -31,33 +52,30 @@ export const writeConfig = async (fp: string, config: SubscriptionConfig) => {
checkConfig(newConfig);
const hasUpdate = !_.isEqual(newConfig, oldConfig);
if (hasUpdate) {
newConfig.version++;
}
// update md
await updateReadMeMd(newConfig, oldConfig);
console.log('更新文档');
if (!hasUpdate) {
console.log('没有检测到规则变化,跳过更新JSON');
console.log('暂无规则变化');
return;
}
// keep json key sort by map
const map = new Map<string, unknown>();
sortKeys.forEach((k) => {
if (newConfig[k] === undefined) return;
map.set(k, newConfig[k]);
});
const buffer = Buffer.from(
JSON.stringify(Object.fromEntries(map.entries())),
newConfig.version++;
// update md
await updateReadMeMd(newConfig, oldConfig);
// update package.json
pkg.version = `1.${newConfig.version}.0`;
await fs.writeFile(
process.cwd() + '/package.json',
orderdStringify(pkg, pkgKeys, undefined, 2),
'utf-8',
);
// update gkd.json
const buffer = Buffer.from(orderdStringify(newConfig, sortKeys), 'utf-8');
await fs.writeFile(fp, buffer);
console.log(
`更新订阅: 版本:${newConfig.version}, 文件大小: ${
`更新订阅: v${newConfig.version}, 文件大小: ${
(buffer.length / 1024).toFixed(3) + 'KB'
}`,
);
@@ -283,6 +301,7 @@ export const updateReadMeMd = async (
}),
);
if (changeCount <= 0) return;
console.log('更新文档: ' + changeCount);
const appListText =
'| 名称 | ID | 规则组 |\n| - | - | - |\n' +