Skip to content

Instantly share code, notes, and snippets.

@DNI9
Last active July 1, 2024 19:53
Show Gist options
  • Save DNI9/615cf02d4051c2ea34ce1794d9476ba1 to your computer and use it in GitHub Desktop.
Save DNI9/615cf02d4051c2ea34ce1794d9476ba1 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const path = require("path");
const DRY_RUN = false;
const EXTERNAL_LINE = "External Policy Admin Page";
const PCN_INFO_LINE = "Policy Change Notice Information";
const NEXT_PAGE_LINE = "And Goto Next Page";
/**
* @param {string} filePath
*/
function updateFeatureFile(filePath) {
fs.readFile(filePath, "utf8", (err, data) => {
console.log(`==> Processing file: ${filePath}`);
if (err) {
console.error("==> Error reading file: ", err);
return;
}
const lines = data.split("\r\n");
let pcnUpToDate = false;
let lastExternalLine = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.includes(EXTERNAL_LINE)) {
lastExternalLine = i;
if (lines[lastExternalLine + 2].includes(PCN_INFO_LINE)) {
pcnUpToDate = true;
break;
}
}
if (line.includes(PCN_INFO_LINE)) {
const deletedLines = lines.splice(i, 2);
const nextNumberedLine = lines[lastExternalLine + 1];
if (nextNumberedLine.includes("Next") && nextNumberedLine.includes("Pages")) {
deletedLines[1] = nextNumberedLine;
lines.splice(lastExternalLine + 1, 1, NEXT_PAGE_LINE, ...deletedLines);
} else {
lines.splice(lastExternalLine + 2, 0, ...deletedLines);
}
}
}
if (pcnUpToDate) {
console.log("==> Up to date, skipping: ", filePath);
return;
}
const updatedFile = lines.join("\r\n");
if (!DRY_RUN) {
fs.writeFile(filePath, updatedFile, err => {
if (err) {
console.error("==> Error writing file: ", err);
return;
}
console.log("==> Successfully updated: ", filePath);
});
} else {
console.log(updatedFile);
}
});
}
/**
* @param {string} dir
*/
function processDir(dir) {
fs.readdirSync(dir).forEach(fileName => {
const filePath = path.join(dir, fileName);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
processDir(filePath);
} else if (fileName.endsWith(".feature")) {
updateFeatureFile(filePath);
}
});
}
processDir(process.argv[2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment