| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- // @ts-check
- import { readFileByLine } from './fetch-remote-text-by-line';
- import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } from './clash';
- export async function compareAndWriteFile(linesA: string[], filePath: string) {
- let isEqual = true;
- const file = Bun.file(filePath);
- const linesALen = linesA.length;
- if (!(await file.exists())) {
- console.log(`${filePath} does not exists, writing...`);
- isEqual = false;
- } else if (linesALen === 0) {
- console.log(`Nothing to write to ${filePath}...`);
- isEqual = false;
- } else {
- let index = 0;
- for await (const lineB of readFileByLine(file)) {
- const lineA = linesA[index];
- index++;
- if (lineA === undefined) {
- // The file becomes smaller
- isEqual = false;
- break;
- }
- if (lineA[0] === '#' && lineB[0] === '#') {
- continue;
- }
- if (lineA !== lineB) {
- isEqual = false;
- break;
- }
- }
- if (index !== linesALen) {
- // The file becomes larger
- isEqual = false;
- }
- }
- if (isEqual) {
- console.log(`Same Content, bail out writing: ${filePath}`);
- return;
- }
- const writer = file.writer();
- for (let i = 0; i < linesALen; i++) {
- writer.write(`${linesA[i]}\n`);
- }
- return writer.end();
- }
- export const withBannerArray = (title: string, description: string[], date: Date, content: string[]) => {
- return [
- '########################################',
- `# ${title}`,
- `# Last Updated: ${date.toISOString()}`,
- `# Size: ${content.length}`,
- ...description.map(line => (line ? `# ${line}` : '#')),
- '########################################',
- ...content,
- '################# END ###################'
- ];
- };
- export const createRuleset = (
- title: string, description: string[], date: Date, content: string[],
- type: 'ruleset' | 'domainset', surgePath: string, clashPath: string
- ) => {
- const surgeContent = withBannerArray(title, description, date, content);
- let _clashContent;
- switch (type) {
- case 'domainset':
- _clashContent = surgeDomainsetToClashDomainset(content);
- break;
- case 'ruleset':
- _clashContent = surgeRulesetToClashClassicalTextRuleset(content);
- break;
- default:
- throw new TypeError(`Unknown type: ${type as any}`);
- }
- const clashContent = withBannerArray(title, description, date, _clashContent);
- return [
- compareAndWriteFile(surgeContent, surgePath),
- compareAndWriteFile(clashContent, clashPath)
- ];
- };
|