mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
New build infra
This commit is contained in:
35
Build/lib/string-array-compare.js
Normal file
35
Build/lib/string-array-compare.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const { promises: fsPromises } = require('fs');
|
||||
|
||||
async function compareAndWriteFile(linesA, filePath) {
|
||||
const linesB = (await fsPromises.readFile(filePath, { encoding: 'utf-8' })).split('\n');
|
||||
|
||||
if (!stringArrayCompare(linesA, linesB)) {
|
||||
await fsPromises.writeFile(
|
||||
filePath,
|
||||
linesA.join('\n'),
|
||||
{ encoding: 'utf-8' }
|
||||
)
|
||||
} else {
|
||||
console.log(`Same Content, bail out writing: ${filePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -6,12 +6,33 @@
|
||||
* @returns {string}
|
||||
*/
|
||||
const withBanner = (title, description, date, content) => {
|
||||
return `########################################
|
||||
return `########################################
|
||||
# ${title}
|
||||
${description.map(line => `# ${line}`).join('\n')}
|
||||
# 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
|
||||
* @param {Date} date
|
||||
* @param {string[]} content
|
||||
* @returns {string[]}
|
||||
*/
|
||||
const withBannerArray = (title, description, date, content) => {
|
||||
return [
|
||||
`########################################`,
|
||||
`# ${title}`,
|
||||
`# Last Updated: ${date.toISOString()}`,
|
||||
`# Size: ${content.length}`,
|
||||
...description.map(line => line ? `# ${line}` : '#'),
|
||||
`########################################`,
|
||||
...content,
|
||||
`################# END ###################`,
|
||||
''
|
||||
];
|
||||
};
|
||||
|
||||
module.exports.withBanner = withBanner;
|
||||
module.exports.withBannerArray = withBannerArray;
|
||||
|
||||
Reference in New Issue
Block a user