Skip to content

Instantly share code, notes, and snippets.

@Mirv
Last active March 25, 2020 17:55
Show Gist options
  • Save Mirv/dd3bbdbd19ab682063d718a85469d3df to your computer and use it in GitHub Desktop.
Save Mirv/dd3bbdbd19ab682063d718a85469d3df to your computer and use it in GitHub Desktop.
// Allow commandline parameters to be passed in NodeJS
module.exports = function(alternate_port){
var arguments = getCommandLine();
var arguments = arguments ? arguments : alternate_port;
if(!arguments){
throw "Arguments fed into inputHandler.js in both the commandline and parameter appear to be empty or null!";
}
return arguments;
}
// In nodejs the process.argv holds the commandline input
// process.argv[0] is 'node'
// process_argv[1] is the file to run if provided
// process_argv[2] can be our port (or anything we desire)
function getCommandLine(arg_array_position = 2){
return process.argv[arg_array_position]
}
rem Makes loop with output in command line
rem Then we feed the port in to test module
@echo off
:1
"C:\Program Files\CentralOffice\node.exe" test_inputHandler.js 3000
pause
GOTO 1
// Run module with function being passed a value
const ph = require('./inputHandler.js');
console.log(ph(6));
//
// Todo -- use rest of gist in this
//
// Based on https://stackoverflow.com/a/60615051
// Module call holds export object, which now has FileLister obj - TODO - still need to wire up in other code
// allow for parameter passing via nodejs kicking off
// defaults to current directory
function getTargetDir(target_path){
var the_path;
// TODO might need process to detect if the '/' is on there already
if(!target_path){
the_path = process.argv[2] ? __dirname + '/' + process.argv[2] : __dirname + '/';
}
else {
the_path = target_path + '/';
}
return the_path;
}
var conForm = function(target){
console.log('----');
console.log({target});
console.log(target);
console.log('----');
}
module.exports = {
// allow for parameter passing via nodejs kicking off
// defaults to current directory
//getTargetDir: function (){
// // TODO might need process to detect if the '/' is on there already
// return process.argv[2] ? __dirname + '/' + process.argv[2] : (target_path ? target_path + '/' : __dirname + '/')
//},
listDir: function(){
//requiring path and fs modules
const path = require('path');
const fs = require('fs');
//joining path of directory
const directoryPath = path.join(getTargetDir(), '/');
console.log(directoryPath);
var file_list;
//passsing directoryPath and callback function
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
file_list = files; // fails to stackoverflow
console.log('files - ' + files);
console.log(files);
// conForm(file_list);
});
console.log('Exiting the list retrieval post return');
conForm(file_list);
return file_list;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment