Skip to content

Instantly share code, notes, and snippets.

@peppertech
Last active September 12, 2020 00:13
Show Gist options
  • Save peppertech/8fb663c07100421d4ea821bfdf29e350 to your computer and use it in GitHub Desktop.
Save peppertech/8fb663c07100421d4ea821bfdf29e350 to your computer and use it in GitHub Desktop.
Short nodejs script for defining all JET modules used in an application. Run from the root of the project to get the report.
const path = require("path");
const fs = require("fs");
const dir = "./src/js";
const filter = "ojs/oj*";
const ext = ".js";
let allModules = new Set();
function searchFilesInDirectory(dir, filter, ext) {
if (!fs.existsSync(dir)) {
console.log(`Specified directory: ${dir} does not exist`);
return;
}
const files = getFilesInDirectory(dir, ext);
files.forEach((file) => {
const fileContent = fs.readFileSync(file, "utf8");
const regex = new RegExp(filter);
if (regex.test(fileContent)) {
let start = fileContent.indexOf("define([");
let offset = 8;
if (start === -1) {
start = fileContent.indexOf("require([");
offset = 9;
}
if (start !== -1) {
start = start + offset;
let end = fileContent.indexOf("]", start);
let str = fileContent.substr(start, end - start);
str = str.replace(/(\r\n|\n|\r|\s)/gm, "");
console.log(`****\nJET Modules where found in file: ${file}`);
console.log("Modules used are: \n ****");
let tempArray = str.split(",");
let tempSet1 = new Set(tempArray);
for (let elem of tempSet1) {
console.log("item: " + elem);
if (elem.startsWith("'ojs/")) {
allModules.add(elem);
}
}
console.log("\n");
}
}
});
console.log("All Modules: \n");
for (let elem of allModules) {
console.log(elem);
}
}
// Using recursion, we find every file with the desired extention, even if its deeply nested in subfolders.
function getFilesInDirectory(dir, ext) {
if (!fs.existsSync(dir)) {
console.log(`Specified directory: ${dir} does not exist`);
return;
}
let files = [];
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.lstatSync(filePath);
// If we hit a directory, apply our function to that dir. If we hit a file, add it to the array of files.
if (stat.isDirectory()) {
const nestedFiles = getFilesInDirectory(filePath, ext);
files = files.concat(nestedFiles);
} else {
if (path.extname(file) === ext) {
files.push(filePath);
}
}
});
return files;
}
searchFilesInDirectory(dir, filter, ext);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment