From 0f3921b3e65fbad4b5565e612993fcbfc808f30c Mon Sep 17 00:00:00 2001 From: lucatarik Date: Tue, 3 Oct 2017 20:58:18 +0200 Subject: [PATCH 1/2] Added support for Windows --- lib/options.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/options.js b/lib/options.js index b899b4e..ffe4711 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,7 +57,7 @@ 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 = () => { From 8535d2d03e08bb82a377ee6c993f67fbf3a4fa8f Mon Sep 17 00:00:00 2001 From: lucatarik Date: Tue, 3 Oct 2017 21:26:53 +0200 Subject: [PATCH 2/2] add subcommand to instantly execute script --- bin/app.js | 2 ++ lib/options.js | 75 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 19 deletions(-) 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 ffe4711..002a897 100644 --- a/lib/options.js +++ b/lib/options.js @@ -61,24 +61,26 @@ var saveJson = (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 }