mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-21 05:40:29 +08:00
114 lines
4.4 KiB
JavaScript
114 lines
4.4 KiB
JavaScript
'use strict';Object.defineProperty(exports,Symbol.toStringTag,{value:'Module'});const createFile=require('../../_virtual/create-file.cjs'),require$$0=require('foxts/async-write-to-stream'),require$$3=require('foxts/fast-string-array-join'),require$$1=require('node:fs'),require$$0$1=require('picocolors'),fetchTextByLine=require('./fetch-text-by-line.cjs'),misc=require('./misc.cjs');var hasRequiredCreateFile;
|
|
|
|
function requireCreateFile () {
|
|
if (hasRequiredCreateFile) return createFile.__exports;
|
|
hasRequiredCreateFile = 1;
|
|
(function (exports) {
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
function _export(target, all) {
|
|
for(var name in all)Object.defineProperty(target, name, {
|
|
enumerable: true,
|
|
get: all[name]
|
|
});
|
|
}
|
|
_export(exports, {
|
|
compareAndWriteFile: function() {
|
|
return compareAndWriteFile;
|
|
},
|
|
fileEqual: function() {
|
|
return fileEqual;
|
|
}
|
|
});
|
|
const _asyncwritetostream = require$$0;
|
|
const _faststringarrayjoin = require$$3;
|
|
const _nodefs = /*#__PURE__*/ _interop_require_default(require$$1);
|
|
const _picocolors = /*#__PURE__*/ _interop_require_default(require$$0$1);
|
|
const _fetchtextbyline = /*@__PURE__*/ fetchTextByLine.__require();
|
|
const _misc = /*@__PURE__*/ misc.__require();
|
|
function _interop_require_default(obj) {
|
|
return obj && obj.__esModule ? obj : {
|
|
default: obj
|
|
};
|
|
}
|
|
async function fileEqual(linesA, source) {
|
|
if (linesA.length === 0) {
|
|
return false;
|
|
}
|
|
const linesABound = linesA.length - 1;
|
|
let index = -1;
|
|
for await (const lineB of source){
|
|
index++;
|
|
if (index > linesABound) {
|
|
return index === linesA.length && lineB.length === 0;
|
|
}
|
|
const lineA = linesA[index];
|
|
if (lineA.length === 0) {
|
|
if (lineB.length === 0) {
|
|
continue;
|
|
}
|
|
// lineA is empty but lineB is not
|
|
return false;
|
|
}
|
|
// now lineA can not be empty
|
|
if (lineB.length === 0) {
|
|
// lineB is empty but lineA is not
|
|
return false;
|
|
}
|
|
// now both lines can not be empty
|
|
const firstCharA = lineA.charCodeAt(0);
|
|
const firstCharB = lineB.charCodeAt(0);
|
|
if (firstCharA !== firstCharB) {
|
|
return false;
|
|
}
|
|
// now firstCharA is equal to firstCharB, we only need to check the first char
|
|
if (firstCharA === 35 /* # */ ) {
|
|
continue;
|
|
}
|
|
// adguard conf
|
|
if (firstCharA === 33 /* ! */ ) {
|
|
continue;
|
|
}
|
|
if (firstCharA === 47 /* / */ && lineA[1] === '/' && lineB[1] === '/' && lineA[3] === '#' && lineB[3] === '#') {
|
|
continue;
|
|
}
|
|
if (lineA !== lineB) {
|
|
return false;
|
|
}
|
|
}
|
|
// The file becomes larger
|
|
return !(index < linesABound);
|
|
}
|
|
async function compareAndWriteFile(span, linesA, filePath) {
|
|
const linesALen = linesA.length;
|
|
const isEqual = await span.traceChildAsync(`compare ${filePath}`, async ()=>{
|
|
if (_nodefs.default.existsSync(filePath)) {
|
|
return fileEqual(linesA, (0, _fetchtextbyline.readFileByLine)(filePath));
|
|
}
|
|
console.log(`${filePath} does not exists, writing...`);
|
|
return false;
|
|
});
|
|
if (isEqual) {
|
|
console.log(_picocolors.default.gray(_picocolors.default.dim(`same content, bail out writing: ${filePath}`)));
|
|
return;
|
|
}
|
|
await span.traceChildAsync(`writing ${filePath}`, async ()=>{
|
|
// The default highwater mark is normally 16384,
|
|
// So we make sure direct write to file if the content is
|
|
// most likely less than 500 lines
|
|
if (linesALen < 500) {
|
|
return (0, _misc.writeFile)(filePath, (0, _faststringarrayjoin.fastStringArrayJoin)(linesA, '\n') + '\n');
|
|
}
|
|
const writeStream = _nodefs.default.createWriteStream(filePath);
|
|
for(let i = 0; i < linesALen; i++){
|
|
const p = (0, _asyncwritetostream.asyncWriteToStream)(writeStream, linesA[i] + '\n');
|
|
// eslint-disable-next-line no-await-in-loop -- stream high water mark
|
|
if (p) await p;
|
|
}
|
|
writeStream.end();
|
|
});
|
|
}
|
|
} (createFile.__exports));
|
|
return createFile.__exports;
|
|
}exports.__require=requireCreateFile; |