node-keyword-scrapper
Version:
NodeJS keyword scrapper in folders and directory. Search and generate report for specific word in directory.
66 lines (60 loc) • 2.42 kB
JavaScript
const fs = require('fs');
const colors = require('ansi-colors');
if (process.argv.slice(2).length === 0) {
return logError();
} else if (process.argv.slice(2).length > 0 && process.argv.slice(2).length < 2) {
return logError();
} else if (process.argv.slice(2).length >= 2) {
try {
const params = process.argv.slice(2);
let directoryPath = params[0];
let keysFilePath = params[1];
let opts = params[3] ? JSON.parse(params[3]) : null;
let extensions = params[2] ? params[2].split(',') : null;
if (fs.existsSync(directoryPath) && fs.existsSync(keysFilePath)) {
const p = require('../dist/bundle');
p.searchKeysInDirectory(directoryPath, keysFilePath, extensions, opts);
} else {
if (!fs.existsSync(directoryPath)) console.log(colors.redBright('___ Provided directory path does not exists ___'));
if (!fs.existsSync(keysFilePath)) console.log(colors.redBright('___ Provided JSON file path does not exists ___'));
logError();
}
} catch (error) {
console.log(error);
return logError();
}
}
function logError() {
console.table({
'directoryPath': {
'Description': 'Directory path to search for files',
'Example': '/User/bob/folders',
'Required': true,
'Default': '',
},
'keysFilePath': {
'Description': 'Path for JSON file containing keys',
'Example': '/User/bob/key.json',
'Required': true,
'Default': '',
},
'extensions': {
'Description': 'File extensions to search for',
'Example': ".js,.html",
'Required': false,
'Default': ['.ts', '.html'],
},
'opts': {
'Description': 'Options for key transpose',
'Example': '{"output":"FOLDER_PATH","transpose":{"char":".","charReplacement":"?."},"exclude":["/node_modules","coverage"]}',
'Required': false,
'Default': {},
}
});
console.log('\x1b[36m%s\x1b[0m', '**************************************************************************************');
console.log('\x1b[33m%s\x1b[0m', '* Please provide valid directory path, file extensions and configuration (Optional) *');
console.log('\x1b[33m%s\x1b[0m', '* Example command - [ node multi.js /User/bob/Downloads/modules ["ts"] {"char": "?"} *');
console.log('\x1b[36m%s\x1b[0m', '**************************************************************************************');
return;
}