Use foxts more & Improve writing

This commit is contained in:
SukkaW
2025-06-21 15:39:39 +08:00
parent 53380ecd10
commit afac59f09c
7 changed files with 87 additions and 129 deletions

View File

@@ -2,6 +2,7 @@ import { dirname } from 'node:path';
import fs from 'node:fs';
import type { PathLike } from 'node:fs';
import fsp from 'node:fs/promises';
import { appendArrayInPlace } from 'foxts/append-array-in-place';
export type MaybePromise<T> = T | Promise<T>;
@@ -49,19 +50,23 @@ export const writeFile: Write = async (destination: string, input, dir = dirname
return fsp.writeFile(destination, input, { encoding: 'utf-8' });
};
export const removeFiles = async (files: string[]) => Promise.all(files.map((file) => fsp.rm(file, { force: true })));
export function withBannerArray(title: string, description: string[] | readonly string[], date: Date, content: string[]) {
return [
const result: string[] = [
'#########################################',
`# ${title}`,
`# Last Updated: ${date.toISOString()}`,
`# Size: ${content.length}`,
...description.map(line => (line ? `# ${line}` : '#')),
'#########################################',
...content,
'################## EOF ##################'
`# Size: ${content.length}`
];
appendArrayInPlace(result, description.map(line => (line ? `# ${line}` : '#')));
result.push('#########################################');
appendArrayInPlace(result, content);
result.push('################## EOF ##################', '');
return result;
};
export function notSupported(name: string) {
@@ -84,7 +89,3 @@ export function isDirectoryEmptySync(path: PathLike) {
directoryHandle.closeSync();
}
}
export function fastIpVersion(ip: string) {
return ip.includes(':') ? 6 : (ip.includes('.') ? 4 : 0);
}

View File

@@ -1,7 +1,7 @@
import type { Span } from '../../trace';
import { HostnameSmolTrie } from '../trie';
import { not, nullthrow } from 'foxts/guard';
import { fastIpVersion } from '../misc';
import { fastIpVersion } from 'foxts/fast-ip-version';
import type { MaybePromise } from '../misc';
import type { BaseWriteStrategy } from '../writing-strategy/base';
import { merge as mergeCidr } from 'fast-cidr-tools';

View File

@@ -1,7 +1,8 @@
import { appendSetElementsToArray } from 'foxts/append-set-elements-to-array';
import { BaseWriteStrategy } from './base';
import { noop } from 'foxts/noop';
import { fastIpVersion, notSupported, withBannerArray } from '../misc';
import { notSupported, withBannerArray } from '../misc';
import { fastIpVersion } from 'foxts/fast-ip-version';
import { OUTPUT_CLASH_DIR } from '../../constants/dir';
import { appendArrayInPlace } from 'foxts/append-array-in-place';
import { MARKER_DOMAIN } from '../../constants/description';

View File

@@ -1,7 +1,8 @@
import { BaseWriteStrategy } from './base';
import { appendArrayInPlace } from 'foxts/append-array-in-place';
import { noop } from 'foxts/noop';
import { fastIpVersion, withIdentityContent } from '../misc';
import { withIdentityContent } from '../misc';
import { fastIpVersion } from 'foxts/fast-ip-version';
import stringify from 'json-stringify-pretty-compact';
import { OUTPUT_SINGBOX_DIR } from '../../constants/dir';
import { MARKER_DOMAIN } from '../../constants/description';