tools: Only check for the section start/end signal, not the entire line

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2023-02-28 04:19:40 +01:00
parent d1e3b6d0d1
commit 75e13996f1

View file

@ -1,6 +1,6 @@
/* AUTOGENERATED COPYRIGHT HEADER START // AUTOGENERATED COPYRIGHT HEADER START
* Copyright (C) 2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com> // Copyright (C) 2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
* AUTOGENERATED COPYRIGHT HEADER END */ // AUTOGENERATED COPYRIGHT HEADER END
const CHILD_PROCESS = require("node:child_process"); const CHILD_PROCESS = require("node:child_process");
const PROCESS = require("node:process"); const PROCESS = require("node:process");
const PATH = require("node:path"); const PATH = require("node:path");
@ -8,6 +8,9 @@ const FS = require("node:fs");
const FSPROMISES = require("node:fs/promises"); const FSPROMISES = require("node:fs/promises");
const OS = require("os"); const OS = require("os");
const SECTION_START = "AUTOGENERATED COPYRIGHT HEADER START";
const SECTION_END = "AUTOGENERATED COPYRIGHT HEADER END";
async function git_isIgnored(path) { async function git_isIgnored(path) {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
try { try {
@ -148,10 +151,10 @@ function makeHeader(file, copyright) {
".yml", ".yml",
], ],
prepend: [ prepend: [
"#\u0020AUTOGENERATED COPYRIGHT HEADER START", `# ${SECTION_START}`,
], ],
append: [ append: [
"#\u0020AUTOGENERATED COPYRIGHT HEADER END", `# ${SECTION_END}`,
], ],
prefix: "# ", prefix: "# ",
suffix: "", suffix: "",
@ -164,15 +167,15 @@ function makeHeader(file, copyright) {
".iss.in", ".iss.in",
], ],
prepend: [ prepend: [
";\u0020AUTOGENERATED COPYRIGHT HEADER START", `; ${SECTION_START}`,
], ],
append: [ append: [
";\u0020AUTOGENERATED COPYRIGHT HEADER END", `; ${SECTION_END}`,
], ],
prefix: "; ", prefix: "; ",
suffix: "", suffix: "",
}, },
"/**/": { "//": {
files: [ files: [
], exts: [ ], exts: [
".c", ".c",
@ -189,12 +192,12 @@ function makeHeader(file, copyright) {
".effect" ".effect"
], ],
prepend: [ prepend: [
"/*\u0020AUTOGENERATED COPYRIGHT HEADER START", `// ${SECTION_START}`,
], ],
append: [ append: [
" *\u0020AUTOGENERATED COPYRIGHT HEADER END */", `// ${SECTION_END}`,
], ],
prefix: " * ", prefix: "// ",
suffix: "", suffix: "",
}, },
"<!---->": { "<!---->": {
@ -212,13 +215,13 @@ function makeHeader(file, copyright) {
".pkgproj.in", ".pkgproj.in",
], ],
prepend: [ prepend: [
"<!--\u0020AUTOGENERATED COPYRIGHT HEADER START", `<!-- ${SECTION_START} -->`,
], ],
append: [ append: [
" --\u0020AUTOGENERATED COPYRIGHT HEADER END -->", `<!-- ${SECTION_END} -->`,
], ],
prefix: " --", prefix: "<!-- ",
suffix: "", suffix: " -->",
} }
}; };
@ -251,9 +254,15 @@ async function addCopyright(file) {
let header = makeHeader(file, copyright); let header = makeHeader(file, copyright);
let insert = Buffer.from(header.join(eol) + eol); let insert = Buffer.from(header.join(eol) + eol);
let startHeader = content.indexOf(header[0]); // Find the starting point.
let endHeader = content.indexOf(header[header.length - 1], startHeader + 1); let startHeader = content.indexOf(SECTION_START);
endHeader += header[header.length - 1].length + eol.length; startHeader = content.lastIndexOf(eol, startHeader);
startHeader += Buffer.from(eol).byteLength;
// Find the ending point.
let endHeader = content.indexOf(SECTION_END);
endHeader = content.indexOf(eol, endHeader);
endHeader += Buffer.from(eol).byteLength;
let fd = await FSPROMISES.open(file, "w+"); let fd = await FSPROMISES.open(file, "w+");
let fp = []; let fp = [];