diff --git a/bin/app.js b/bin/app.js index a3ecb33..52c4000 100755 --- a/bin/app.js +++ b/bin/app.js @@ -30,3 +30,5 @@ else if(flagCheck === undefined){ console.log("run") options.run(); } +else + options.runCommand(flagCheck); diff --git a/lib/options.js b/lib/options.js index b899b4e..002a897 100644 --- a/lib/options.js +++ b/lib/options.js @@ -4,7 +4,7 @@ var cmd = require('node-cmd'); var createJson = () => { try{ - fs.writeFileSync(process.env.HOME+'/.scriptr.json', '{"commandName":[], "commandCode":[]}'); + fs.writeFileSync(process.env.HOME||process.env.USERPROFILE+'/.scriptr.json', '{"commandName":[], "commandCode":[]}'); return loadJson(); } catch(e){ @@ -14,7 +14,7 @@ var createJson = () => { var loadJson = () => { try{ - var json = fs.readFileSync(process.env.HOME+'/.scriptr.json'); + var json = fs.readFileSync(process.env.HOME||process.env.USERPROFILE+'/.scriptr.json'); return JSON.parse(json); } catch(e){ @@ -57,28 +57,30 @@ var addCommand = () => { var saveJson = (json) => { console.log(json); - fs.writeFileSync(process.env.HOME+'/.scriptr.json', JSON.stringify(json)); + fs.writeFileSync(process.env.HOME||process.env.USERPROFILE+'/.scriptr.json', JSON.stringify(json)); } var run = () => { - showMenu(); - const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout - }); - rl.question('Select an entry: ', (choice) => { - commands = loadSelection(choice-1); - for(var i = 0; i < commands.length; i++){ - // options.checkCd(commands[i]); - cmd.get( - commands[i], - function(err, data, stderr){ - console.log(data); - } - ); - } - rl.close(); - }); + if(showMenu()) + { + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + rl.question('Select an entry: ', (choice) => { + commands = loadSelection(choice-1); + for(var i = 0; i < commands.length; i++){ + // options.checkCd(commands[i]); + cmd.get( + commands[i], + function(err, data, stderr){ + console.log(data); + } + ); + } + rl.close(); + }); + } } var deleteCommand = () => { @@ -116,12 +118,46 @@ var editCommand = () => { var showMenu = () => { var scripts = loadJson(); + if (scripts.commandName.length == 0) + { + addCommand(); + return false; + } console.log("----------------------------------"); for(var i = 0; i < scripts.commandName.length; i++){ console.log("| "+"("+(i+1)+".) "+scripts.commandName[i]); } console.log("----------------------------------"); + return true; +} +var runCommand = (cmdname) => { + var scripts = loadJson(); + if (scripts.commandName.length == 0) + { + console.log("command "+cmdname+" not found"); + return false; + } + console.log("----------------------------------"); + for(var i = 0; i < scripts.commandName.length; i++){ + if(scripts.commandName[i]==cmdname) + { + commands = loadSelection(i); + for(var j = 0; j < commands.length; j++) + { + cmd.get( + commands[j], + function(err, data, stderr){ + console.log(data||err||stderr); + } + ); + } + console.log("----------------------------------"); + return true; + } + } + console.log("command "+cmdname+" not found"); + return false; } module.exports = { @@ -130,5 +166,6 @@ module.exports = { loadSelection, run, deleteCommand, - editCommand + editCommand, + runCommand }