From 0fc2fb3184d942842a6efa84affde89610bc1628 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 18 Jan 2019 22:18:52 -0600 Subject: [PATCH 1/4] Erlangga Ramzy --- index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..aa022c2 --- /dev/null +++ b/index.js @@ -0,0 +1,21 @@ +var readline = require('readline'); +var fs = require('fs'); + +if(process.argv[2] === "init") { + console.log('init'); +} else { + process.exit(); +} + + +// const rl = readline.createInterface({ +// input: process.stdin, +// output: process.stdout +// }); + +// rl.question('What do you think of Node.js? ', (answer) => { +// // TODO: Log the answer in a database +// console.log(`Thank you for your valuable feedback: ${answer}`); + +// rl.close(); +// }); \ No newline at end of file From bc8b4cdbc10facb3cbda6773b09b0c7d0467710b Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 18 Jan 2019 23:31:39 -0600 Subject: [PATCH 2/4] Erlangga Ramzy --- index.js | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index aa022c2..d0b8050 100644 --- a/index.js +++ b/index.js @@ -5,17 +5,35 @@ if(process.argv[2] === "init") { console.log('init'); } else { process.exit(); -} +}; +fs.stat('./package.json', function(err) { + if (err) { + console.log('file does not exist') + } else { + process.exit(); + } +}); -// const rl = readline.createInterface({ -// input: process.stdin, -// output: process.stdout -// }); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); -// rl.question('What do you think of Node.js? ', (answer) => { -// // TODO: Log the answer in a database -// console.log(`Thank you for your valuable feedback: ${answer}`); +rl.question('package name: ', (answer1) => { + rl.question('version: ', (answer2) => { + rl.question('description: ', (answer3) => { + rl.question('entry point: ', (answer4) => { + rl.question('author: ', (answer5) => { + + }) + }) + }) + }) + // rl.close(); +}); -// rl.close(); -// }); \ No newline at end of file +// fs.writeFile('package.json', data, (err) => { +// if (err) throw err; +// console.log('The file has been saved!'); +// }); \ No newline at end of file From 5af27301276add1ff41f2b2ecabe3fe3f3d86aca Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 18 Jan 2019 23:51:23 -0600 Subject: [PATCH 3/4] Erlangga Ramzy --- index.js | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index d0b8050..64160be 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,13 @@ var readline = require('readline'); var fs = require('fs'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +const jsonObj = {}; + if(process.argv[2] === "init") { console.log('init'); } else { @@ -9,31 +16,36 @@ if(process.argv[2] === "init") { fs.stat('./package.json', function(err) { if (err) { - console.log('file does not exist') + // console.log('file does not exist') } else { process.exit(); } }); -const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout -}); - -rl.question('package name: ', (answer1) => { - rl.question('version: ', (answer2) => { - rl.question('description: ', (answer3) => { - rl.question('entry point: ', (answer4) => { - rl.question('author: ', (answer5) => { - +rl.question('package name: ', (answer) => { + jsonObj['packageName'] = answer; + rl.question('version: ', (answer) => { + jsonObj['version'] = answer; + rl.question('description: ', (answer) => { + jsonObj['description'] = answer; + rl.question('entry point: ', (answer) => { + jsonObj['entryPoint'] = answer; + rl.question('author: ', (answer) => { + jsonObj['author'] = answer; + console.log(jsonObj); + saveFile(); + rl.close(); }) }) }) }) - // rl.close(); }); -// fs.writeFile('package.json', data, (err) => { -// if (err) throw err; -// console.log('The file has been saved!'); -// }); \ No newline at end of file +function saveFile() { + const string = JSON.stringify(jsonObj); + fs.writeFile('package.json', string, (err) => { + if (err) throw err; + console.log('The file has been saved!'); + }); +} + From eb7352e7b1440f5afed71630c78734778e92e50a Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 19 Jan 2019 00:10:14 -0600 Subject: [PATCH 4/4] Erlangga Ramzy --- index.js | 56 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index 64160be..8806cc4 100644 --- a/index.js +++ b/index.js @@ -8,40 +8,40 @@ const rl = readline.createInterface({ const jsonObj = {}; +//checks if init is first arg if(process.argv[2] === "init") { - console.log('init'); + //fs.stat fetches info on a file but if it doesnt exist i can use the error to continue + fs.stat('./package.json', function(err) { + if (err) { + // console.log('file does not exist') + rl.question('package name: ', (answer) => { + jsonObj['packageName'] = answer; + rl.question('version: ', (answer) => { + jsonObj['version'] = answer; + rl.question('description: ', (answer) => { + jsonObj['description'] = answer; + rl.question('entry point: ', (answer) => { + jsonObj['entryPoint'] = answer; + rl.question('author: ', (answer) => { + jsonObj['author'] = answer; + console.log(jsonObj); + saveFile(); + rl.close(); + }) + }) + }) + }) + }); + } else { + process.exit(); + } + }); } else { process.exit(); }; -fs.stat('./package.json', function(err) { - if (err) { - // console.log('file does not exist') - } else { - process.exit(); - } -}); - -rl.question('package name: ', (answer) => { - jsonObj['packageName'] = answer; - rl.question('version: ', (answer) => { - jsonObj['version'] = answer; - rl.question('description: ', (answer) => { - jsonObj['description'] = answer; - rl.question('entry point: ', (answer) => { - jsonObj['entryPoint'] = answer; - rl.question('author: ', (answer) => { - jsonObj['author'] = answer; - console.log(jsonObj); - saveFile(); - rl.close(); - }) - }) - }) - }) -}); - function saveFile() { + //fs.writefile requires data as a string const string = JSON.stringify(jsonObj); fs.writeFile('package.json', string, (err) => { if (err) throw err;