-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddLicence.js
executable file
·36 lines (31 loc) · 1.33 KB
/
addLicence.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env node
const fs = require("fs-extra");
const glob = require("glob");
// Function to prepend content to each TypeScript file
async function prependContentToFiles(rootDirectory, contentFile, fileExtension) {
try {
// Read the content to be prepended
const content = await fs.readFile(contentFile, "utf8");
const comment = `/*\n${content}\n*/\n\n`;
// Find all TypeScript files in the specified root directory, excluding node_modules
const files = glob.sync(`${rootDirectory}/**/*.${fileExtension}`, {
ignore: "**/node_modules/**", // Exclude node_modules
});
// Prepend the comment to each file
for (const file of files) {
const existingContent = await fs.readFile(file, "utf8");
// Check if the license header is already present at the beginning of the file
if (!existingContent.startsWith(comment)) {
// If not, prepend the comment
await fs.writeFile(file, comment + existingContent);
console.log(`Prepended content to ${file}`);
} else {
console.log(`License header already exists in ${file}, skipping.`);
}
}
} catch (error) {
console.error("Error:", error);
}
}
// Usage example: Applies to all `.ts` files in the current directory and its subdirectories, excluding node_modules
prependContentToFiles("./", "./LICENSE", "ts");