mirror of
https://github.com/AIsouler/GKD_subscription.git
synced 2025-12-20 00:35:17 +08:00
chore: sync
This commit is contained in:
182
src/file.ts
182
src/file.ts
@@ -15,6 +15,16 @@ const iArrayToArray = <T>(array: IArray<T> = []): T[] => {
|
||||
return Array<T>().concat(array);
|
||||
};
|
||||
|
||||
const sortKeys: (keyof SubscriptionConfig)[] = [
|
||||
`id`,
|
||||
`name`,
|
||||
`version`,
|
||||
`author`,
|
||||
'supportUri',
|
||||
`updateUrl`,
|
||||
`apps`,
|
||||
];
|
||||
|
||||
export const writeConfig = async (fp: string, config: SubscriptionConfig) => {
|
||||
const filePath = relativePath(fp);
|
||||
const newConfig: SubscriptionConfig = { ...config };
|
||||
@@ -28,7 +38,54 @@ export const writeConfig = async (fp: string, config: SubscriptionConfig) => {
|
||||
return;
|
||||
}
|
||||
newConfig.version++;
|
||||
checkConfig(newConfig);
|
||||
|
||||
// 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()), void 0, 2),
|
||||
'utf-8',
|
||||
);
|
||||
await fs.writeFile(filePath, buffer);
|
||||
|
||||
const newPkg = { ...selfPkg, version: `0.0.` + newConfig.version };
|
||||
await fs.writeFile(
|
||||
relativePath('../package.json'),
|
||||
JSON.stringify(newPkg, void 0, 2) + `\n`,
|
||||
);
|
||||
|
||||
await updateReadMeMd(newConfig);
|
||||
|
||||
console.log({
|
||||
mtime: dayjs().format(`HH:mm:ss`),
|
||||
name: newConfig.name,
|
||||
size: (buffer.length / 1024).toFixed(3) + `KB`,
|
||||
version: newConfig.version,
|
||||
});
|
||||
};
|
||||
|
||||
export async function* walk(dirPath: string) {
|
||||
const pathnames = (await fs.readdir(dirPath)).map((s) =>
|
||||
path.join(dirPath, s),
|
||||
);
|
||||
while (pathnames.length > 0) {
|
||||
const pathname = pathnames.pop()!;
|
||||
const state = await fs.lstat(pathname);
|
||||
if (state.isFile()) {
|
||||
yield pathname;
|
||||
} else if (state.isDirectory()) {
|
||||
pathnames.push(
|
||||
...(await fs.readdir(pathname)).map((s) => path.join(pathname, s)),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const checkConfig = (newConfig: SubscriptionConfig) => {
|
||||
// check duplicated group key
|
||||
newConfig.apps?.forEach((app) => {
|
||||
const keys = new Set<number>();
|
||||
@@ -76,16 +133,6 @@ export const writeConfig = async (fp: string, config: SubscriptionConfig) => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const sortKeys: (keyof SubscriptionConfig)[] = [
|
||||
`id`,
|
||||
`name`,
|
||||
`version`,
|
||||
`author`,
|
||||
'supportUri',
|
||||
`updateUrl`,
|
||||
`apps`,
|
||||
];
|
||||
const newKeys = Object.keys(newConfig) as (keyof SubscriptionConfig)[];
|
||||
if (newKeys.some((s) => !sortKeys.includes(s))) {
|
||||
console.log({
|
||||
@@ -94,45 +141,80 @@ export const writeConfig = async (fp: string, config: SubscriptionConfig) => {
|
||||
});
|
||||
throw new Error(`sortKeys miss some new key`);
|
||||
}
|
||||
// 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()), void 0, 2),
|
||||
'utf-8',
|
||||
);
|
||||
await fs.writeFile(filePath, buffer);
|
||||
|
||||
const newPkg = { ...selfPkg, version: `0.0.` + newConfig.version };
|
||||
await fs.writeFile(
|
||||
relativePath('../package.json'),
|
||||
JSON.stringify(newPkg, void 0, 2) + `\n`,
|
||||
);
|
||||
|
||||
console.log({
|
||||
mtime: dayjs().format(`HH:mm:ss`),
|
||||
name: newConfig.name,
|
||||
size: (buffer.length / 1024).toFixed(3) + `KB`,
|
||||
version: newConfig.version,
|
||||
});
|
||||
};
|
||||
|
||||
export async function* walk(dirPath: string) {
|
||||
const pathnames = (await fs.readdir(dirPath)).map((s) =>
|
||||
path.join(dirPath, s),
|
||||
);
|
||||
while (pathnames.length > 0) {
|
||||
const pathname = pathnames.pop()!;
|
||||
const state = await fs.lstat(pathname);
|
||||
if (state.isFile()) {
|
||||
yield pathname;
|
||||
} else if (state.isDirectory()) {
|
||||
pathnames.push(
|
||||
...(await fs.readdir(pathname)).map((s) => path.join(pathname, s)),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
export const updateReadMeMd = async (newConfig: SubscriptionConfig) => {
|
||||
const mdTemplate = await fs.readFile(relativePath('../Template.md'), 'utf-8');
|
||||
const appListText = newConfig.apps
|
||||
.map((app) => {
|
||||
const appMdText = `### [${app.id}](/src/apps/${app.id}.ts) - ${app.name}\n`;
|
||||
const groupMdText = app.groups
|
||||
?.map((group) => {
|
||||
const groupNameMdText =
|
||||
`- ${group.name}` +
|
||||
(group.desc ? ` - ${group.desc}` : ``).trimEnd();
|
||||
|
||||
const exampleUrls: string[] = [];
|
||||
exampleUrls.push(...iArrayToArray(group.exampleUrls));
|
||||
iArrayToArray(group.rules)
|
||||
.map((r) =>
|
||||
typeof r == 'string' ? [] : iArrayToArray(r.exampleUrls),
|
||||
)
|
||||
.flat()
|
||||
.forEach((u) => {
|
||||
if (u) {
|
||||
exampleUrls.push(u);
|
||||
}
|
||||
});
|
||||
const exampleMdText = exampleUrls
|
||||
.map((u, i) => {
|
||||
if (u) {
|
||||
return ` - [示例-${i}](${u})`;
|
||||
}
|
||||
})
|
||||
.join(`\n`)
|
||||
.trimEnd();
|
||||
|
||||
const snapshotUrls: string[] = [];
|
||||
snapshotUrls.push(...iArrayToArray(group.snapshotUrls));
|
||||
iArrayToArray(group.rules)
|
||||
.map((r) =>
|
||||
typeof r == 'string' ? [] : iArrayToArray(r.snapshotUrls),
|
||||
)
|
||||
.flat()
|
||||
.forEach((u) => {
|
||||
if (u) {
|
||||
snapshotUrls.push(u);
|
||||
}
|
||||
});
|
||||
const snapshotMdText = snapshotUrls
|
||||
.map((u, i) => {
|
||||
if (u) {
|
||||
return ` - [快照-${i}](${u})`;
|
||||
}
|
||||
})
|
||||
.join(`\n`);
|
||||
return [groupNameMdText, exampleMdText, snapshotMdText]
|
||||
.filter((s) => s)
|
||||
.join(`\n`)
|
||||
.trimEnd();
|
||||
})
|
||||
.join(`\n`)
|
||||
.trimEnd();
|
||||
|
||||
return [appMdText, groupMdText].join(`\n`).trimEnd();
|
||||
})
|
||||
.join(`\n\n`)
|
||||
.trimEnd();
|
||||
const readMeMdText = mdTemplate
|
||||
.replace(`--APP_SIZE--`, newConfig.apps.length.toString())
|
||||
.replace(
|
||||
`--GROUP_SIZE--`,
|
||||
newConfig.apps
|
||||
.reduce((p, c) => p + (c.groups?.length || 0), 0)
|
||||
.toString(),
|
||||
)
|
||||
.replace(`--APP_LIST--`, appListText);
|
||||
|
||||
await fs.writeFile(relativePath(`../README.md`), readMeMdText);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user