Last active
October 16, 2018 13:33
-
-
Save nulltier/cddb7b97eec1a0fb32b574e3764b17d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// how to run | |
// $ node ./detect-root-foders.js --root ../../../.. --ext jsp --until folderName | |
const glob = require('glob'); | |
const ROOT = 'root'; | |
const EXT = 'ext'; | |
const UNTIL = 'until'; | |
function getSettings (argv) { | |
return argv.reduce((params, arg, i, all) => { | |
if (arg.indexOf('--') === 0 && all[i + 1]) { | |
params[arg.slice(2)] = all[i + 1]; | |
} | |
return params; | |
}, {}); | |
} | |
function checkSettings(settings) { | |
if (!settings[ROOT]) { | |
throw 'Please provide the root path for search'; | |
} | |
if (!settings[EXT]) { | |
throw 'Please provide file extention that have to be searched'; | |
} | |
return true; | |
} | |
function getRootFolders(paths, root, until) { | |
const roots = {}; | |
let counter = 0; | |
paths.forEach(path => { | |
const rootFolder = path.replace(root, '').replace(/^\//ig, ''); | |
let rootPath; | |
if (until) { | |
const separated = rootFolder.split(until); | |
if (separated.length === 2) { | |
rootPath = separated[0] + until; | |
} | |
} else { | |
rootPath = rootFolder.split('/')[0]; | |
} | |
if (rootPath) { | |
if (typeof roots[rootPath] !== 'undefined') { | |
roots[rootPath] += 1; | |
} else { | |
roots[rootPath] = 1; | |
} | |
counter += 1; | |
} | |
}); | |
return { | |
counter, | |
folders: roots | |
} | |
} | |
const settings = getSettings(process.argv); | |
if (checkSettings(settings)) { | |
const root = settings[ROOT].replace(/\/$/ig, ''); | |
glob(`${root}/**/*.${settings[EXT]}`, null, (er, files) => { | |
const {counter, folders} = getRootFolders(files, root, settings[UNTIL]); | |
console.log('total amount:', counter); | |
console.log(''); | |
console.log('folders:', folders) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment