summary refs log tree commit diff
path: root/scripts/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/utils.js')
-rw-r--r--scripts/utils.js48
1 files changed, 42 insertions, 6 deletions
diff --git a/scripts/utils.js b/scripts/utils.js
index 84aaeed6..b679392b 100644
--- a/scripts/utils.js
+++ b/scripts/utils.js
@@ -4,6 +4,8 @@ const { env } = require("process");
 const { execSync } = require("child_process");
 const { argv, stdout, exit } = require("process");
 
+const projectRoot = path.resolve(path.join(__dirname, ".."));
+
 function copyRecursiveSync(src, dest) {
 	//if (verbose) console.log(`cpsync: ${src} -> ${dest}`);
 	let exists = fs.existsSync(src);
@@ -16,10 +18,7 @@ function copyRecursiveSync(src, dest) {
 	if (isDirectory) {
 		fs.mkdirSync(dest, { recursive: true });
 		fs.readdirSync(src).forEach(function (childItemName) {
-			copyRecursiveSync(
-				path.join(src, childItemName),
-				path.join(dest, childItemName)
-			);
+			copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName));
 		});
 	} else {
 		fs.copyFileSync(src, dest);
@@ -44,8 +43,45 @@ function getLines(output) {
 	return output.split("\n").length;
 }
 
-module.exports = { 
+function getDirs(dir) {
+	return fs.readdirSync(dir).filter((x) => {
+		try {
+			fs.readdirSync(dir.join(dir, x));
+			return true;
+		} catch (e) {
+			return false;
+		}
+	});
+}
+
+function walk(dir) {
+	let results = [];
+	let list = fs.readdirSync(dir);
+	list.forEach(function (file) {
+		file = dir + "/" + file;
+		let stat = fs.statSync(file);
+		if (stat && stat.isDirectory()) {
+			/* Recurse into a subdirectory */
+			results = results.concat(walk(file));
+		} else {
+			results.push(file);
+		}
+	});
+	return results;
+}
+
+function sanitizeVarName(str) {
+	return str.replace("-", "_").replace(/[^\w\s]/gi, "");
+}
+
+module.exports = {
 	//consts
+	projectRoot,
 	//functions
-	copyRecursiveSync, execIn, getLines
+	copyRecursiveSync,
+	execIn,
+	getLines,
+	getDirs,
+	walk,
+	sanitizeVarName
 };