Skip to content

Instantly share code, notes, and snippets.

@delasy
Last active January 4, 2025 07:47
Show Gist options
  • Save delasy/e15f3ec5971a7ca33d9dac713ad83100 to your computer and use it in GitHub Desktop.
Save delasy/e15f3ec5971a7ca33d9dac713ad83100 to your computer and use it in GitHub Desktop.
Check files in current folder start with a certain content (Node.js)
const fs = require("node:fs");
const path = require("node:path");
const banner = `<text-goes-here>`;
const exclusions = ["Dockerfile", "build"];
function traverseFolder (folderPath) {
const realFolderPath = fs.realpathSync(folderPath);
const entries = fs.readdirSync(realFolderPath, { withFileTypes: true });
for (const entry of entries) {
if (entry.name.includes(".") || exclusions.includes(entry.name)) {
continue;
}
const entryPath = entry.parentPath + entry.name;
if (entry.isDirectory()) {
traverseFolder(entryPath);
continue;
}
const content = fs.readFileSync(entryPath, "utf8");
if (!content.startsWith(banner)) {
console.log(entryPath);
}
}
}
traverseFolder(process.cwd());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment