From dea25d9944ebcc0bc85a83639772a8bc4298a7a8 Mon Sep 17 00:00:00 2001 From: User Date: Mon, 21 Jan 2019 16:13:45 -0600 Subject: [PATCH 1/2] Reading 5 user inputs and converting them into a JSON string that gets written into a JSON file named package.json --- index.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..481246f --- /dev/null +++ b/index.js @@ -0,0 +1,52 @@ +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); +var path = './package.json'; +var fs = require('fs'); + +var userInputs = {inputs : []}; + +let val = ''; + +//process.argv.forEach(function (val, index, array) { +for (let index = 2; index < process.argv.length; index++) { + + val = process.argv[index]; + //console.log(index + ': ' + val); + + if (val != 'init') { + console.log('Terminating the program...'); + process.exit(); + } + else { + if (fs.existsSync(path)) { + console.log('Terminating the program...'); + process.exit(); + } + else { + console.log("Please type in 5 commands..."); + let commandCounter = 0; + + rl.on('line', (input) => { + userInputs.inputs.push({key: commandCounter, value: input}); + if (commandCounter == 4) { + let jsonString = JSON.stringify(userInputs); + + fs.writeFile('package.json', jsonString, 'utf8', function(err) { + if (err) { + return console.log(err); + } + }); + + rl.close(); + } + commandCounter += 1; + }); + + } + } +} + + From 49a3021093e0378940a2dfd9b0fdb3e9e28a2c6a Mon Sep 17 00:00:00 2001 From: User Date: Mon, 21 Jan 2019 16:16:32 -0600 Subject: [PATCH 2/2] removing the fs.close and some comments. --- index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/index.js b/index.js index 481246f..9b52797 100644 --- a/index.js +++ b/index.js @@ -10,11 +10,9 @@ var userInputs = {inputs : []}; let val = ''; -//process.argv.forEach(function (val, index, array) { for (let index = 2; index < process.argv.length; index++) { val = process.argv[index]; - //console.log(index + ': ' + val); if (val != 'init') { console.log('Terminating the program...'); @@ -39,7 +37,6 @@ for (let index = 2; index < process.argv.length; index++) { return console.log(err); } }); - rl.close(); } commandCounter += 1;