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);
}