Files
Surge_by_SukkaW/Build/lib/create-file.ts
SukkaW f65c8fa326
Some checks failed
Build / Build (push) Has been cancelled
Build / Diff output (push) Has been cancelled
Build / Deploy to Cloudflare Pages (3.114.12) (push) Has been cancelled
Build / Deploy to GitHub and GitLab (push) Has been cancelled
Build / Remove Artifacts after Deployment (push) Has been cancelled
Whitelist backblazeb2.com in phishing & Other minor changes
Seriously, why don't fucking Backblaze request Public Suffix? It is the
whole reason your main domain getting blocked.
2026-03-11 19:02:30 +08:00

57 lines
2.1 KiB
TypeScript

import { asyncWriteToStream } from 'foxts/async-write-to-stream';
import { fastStringArrayJoin } from 'foxts/fast-string-array-join';
import fs from 'node:fs';
import picocolors from 'picocolors';
import type { Span } from '../trace';
import { readFileByLine } from './fetch-text-by-line';
import { writeFile } from './misc';
import { createCompareSource, fileEqualWithCommentComparator } from 'foxts/compare-source';
import { promisify } from 'node:util';
export const fileEqual = createCompareSource(fileEqualWithCommentComparator);
export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
// readFileByLine will not include last empty line. So we always pop the linesA for comparison purpose
if (linesA.length > 0 && linesA[linesA.length - 1] === '') {
linesA.pop();
}
const isEqual = await span.traceChildAsync(`compare ${filePath}`, async () => {
if (fs.existsSync(filePath)) {
return fileEqual(linesA, readFileByLine(filePath));
}
console.log(`${filePath} does not exists, writing...`);
return false;
});
if (isEqual) {
console.log(picocolors.gray(picocolors.dim(`same content, bail out writing: ${filePath}`)));
return;
}
return span.traceChildAsync<void>(`writing ${filePath}`, async () => {
const linesALen = linesA.length;
// The default highwater mark is normally 16384,
// So we make sure direct write to file if the content is
// most likely less than 250 lines
if (linesALen < 250) {
return writeFile(filePath, fastStringArrayJoin(linesA, '\n'));
}
const writeStream = fs.createWriteStream(filePath);
let p;
for (let i = 0; i < linesALen; i++) {
p = asyncWriteToStream(writeStream, linesA[i] + '\n');
// eslint-disable-next-line no-await-in-loop -- stream high water mark
if (p) await p;
}
await new Promise<void>(resolve => {
// Since we previously poped the last empty line for comparison, we need to add it back here to ensure final EOF line
writeStream.end('\n', resolve);
});
await promisify(writeStream.close.bind(writeStream))();
});
}