mirror of
https://github.com/SukkaW/Surge.git
synced 2026-02-02 20:11:54 +08:00
Use stream in build tools
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// @ts-check
|
||||
const { promises: fsPromises } = require('fs');
|
||||
const fse = require('fs-extra');
|
||||
const { readFileByLine } = require('./fetch-remote-text-by-line');
|
||||
|
||||
/**
|
||||
* @param {string[]} linesA
|
||||
@@ -8,9 +9,26 @@ const fse = require('fs-extra');
|
||||
*/
|
||||
async function compareAndWriteFile(linesA, filePath) {
|
||||
await fse.ensureFile(filePath);
|
||||
const linesB = (await fsPromises.readFile(filePath, { encoding: 'utf-8' })).split('\n');
|
||||
|
||||
if (!stringArrayCompare(linesA, linesB)) {
|
||||
const rl = readFileByLine(filePath);
|
||||
|
||||
let isEqual = true;
|
||||
let index = 0;
|
||||
|
||||
for await (const lineB of rl) {
|
||||
const lineA = linesA[index];
|
||||
index++;
|
||||
|
||||
if (lineA[0] === '#' && lineB[0] === '#') {
|
||||
continue;
|
||||
}
|
||||
if (lineA !== lineB) {
|
||||
isEqual = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isEqual) {
|
||||
await fsPromises.writeFile(
|
||||
filePath,
|
||||
linesA.join('\n'),
|
||||
@@ -21,26 +39,4 @@ async function compareAndWriteFile(linesA, filePath) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string[]} linesA
|
||||
* @param {string[]} linesB
|
||||
*/
|
||||
function stringArrayCompare(linesA, linesB) {
|
||||
if (linesA.length !== linesB.length) return false;
|
||||
|
||||
for (let i = 0; i < linesA.length; i++) {
|
||||
const lineA = linesA[i];
|
||||
const lineB = linesB[i];
|
||||
if (lineA.startsWith('#') && lineB.startsWith('#')) {
|
||||
continue;
|
||||
}
|
||||
if (lineA !== lineB) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports.stringArrayCompare = stringArrayCompare;
|
||||
module.exports.compareAndWriteFile = compareAndWriteFile;
|
||||
|
||||
@@ -7,14 +7,15 @@
|
||||
* @param {string[]} content
|
||||
* @returns {string}
|
||||
*/
|
||||
const withBanner = (title, description, date, content) => {
|
||||
return `########################################
|
||||
# ${title}
|
||||
# Last Updated: ${date.toISOString()}
|
||||
# Size: ${content.length}
|
||||
${description.map(line => (line ? `# ${line}` : '#')).join('\n')}
|
||||
########################################\n${content.join('\n')}\n################# END ###################\n`;
|
||||
};
|
||||
// const withBanner = (title, description, date, content) => {
|
||||
// return `########################################
|
||||
// # ${title}
|
||||
// # Last Updated: ${date.toISOString()}
|
||||
// # Size: ${content.length}
|
||||
// ${description.map(line => (line ? `# ${line}` : '#')).join('\n')}
|
||||
// ########################################\n${content.join('\n')}\n################# END ###################\n`;
|
||||
// };
|
||||
|
||||
/**
|
||||
* @param {string} title
|
||||
* @param {string[]} description
|
||||
@@ -36,5 +37,5 @@ const withBannerArray = (title, description, date, content) => {
|
||||
];
|
||||
};
|
||||
|
||||
module.exports.withBanner = withBanner;
|
||||
// module.exports.withBanner = withBanner;
|
||||
module.exports.withBannerArray = withBannerArray;
|
||||
|
||||
Reference in New Issue
Block a user