From c202d1d57525224e85a312c36d7f029c214263c0 Mon Sep 17 00:00:00 2001 From: Alan Houston Date: Thu, 17 Jan 2019 20:32:15 -0600 Subject: [PATCH 1/7] npm inital --- index.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..6bdf8be --- /dev/null +++ b/index.js @@ -0,0 +1,35 @@ +"use strict"; + +const readline = require('readline'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +rl.question('',(answer)=>{ + let splitAnswer = answer.split(' '); + if(checkForInit(splitAnswer)){ + runInit(); + }else rl.close(); +}); + +// var fs = require('fs'); +// fs.writeFile("mynewfile.txt", "Important Info", (err) => { +// if (err) throw err; +// console.log("The file was succesfully saved!"); +// }); + + +const checkForInit=(firstWord)=>{ + return firstWord[0] === 'init'; +}; + +const runInit = ()=>{ + for(let i=0;i<6;i++){ + rl.question('Give input',(answer[i+1])=>{ + //${answer} + //node step by step slide 29 + }); + } +}; From 76995e8cac51b556908f0e02802fd1b9c31be1f5 Mon Sep 17 00:00:00 2001 From: Alan Houston Date: Fri, 18 Jan 2019 17:23:38 -0600 Subject: [PATCH 2/7] working, but no file check, code not pretty --- index.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 6bdf8be..f67dd63 100644 --- a/index.js +++ b/index.js @@ -7,29 +7,89 @@ const rl = readline.createInterface({ output: process.stdout }); -rl.question('',(answer)=>{ +const jsonPackage = { + fileInfo: [] +} + +rl.question('Type "init" ',(answer)=>{ let splitAnswer = answer.split(' '); - if(checkForInit(splitAnswer)){ - runInit(); + if(checkForInit(splitAnswer) && fileDoesNOTExist()){ + saveInputs(); }else rl.close(); }); -// var fs = require('fs'); -// fs.writeFile("mynewfile.txt", "Important Info", (err) => { -// if (err) throw err; -// console.log("The file was succesfully saved!"); -// }); - - -const checkForInit=(firstWord)=>{ +function checkForInit(firstWord){ return firstWord[0] === 'init'; }; -const runInit = ()=>{ - for(let i=0;i<6;i++){ - rl.question('Give input',(answer[i+1])=>{ - //${answer} - //node step by step slide 29 +function fileDoesNOTExist(){ + return true; //for now +}; + +// function saveInputs(){ +// rl.question('Give input: ',(answer)=>{ +// jsonPackage.fileInfo.push(answer); +// }); +// } + +function saveInputs(){ + rl.question('Give input: ',(a1)=>{ + rl.question('Give input: ',(a2)=>{ + rl.question('Give input: ',(a3)=>{ + rl.question('Give input: ',(a4)=>{ + rl.question('Give input: ',(a5)=>{ + jsonPackage.fileInfo.push(a1); + jsonPackage.fileInfo.push(a2); + jsonPackage.fileInfo.push(a3); + jsonPackage.fileInfo.push(a4); + jsonPackage.fileInfo.push(a5); + createJSONfile(); + }); + }); + }); }); - } + }); }; + +function createJSONfile(){ + const stringedInfo = JSON.stringify(jsonPackage.fileInfo) + var fs = require('fs'); + fs.appendFile("package.json", stringedInfo, (err) => { + if (err) throw err; + }); +} + + + + + + + + +// function runInitialInit(){ +// rl.question('Give input',(answer)=>{ +// sendToCreateFile(answer); +// }); +// }; + +// function runOtherInits(){ +// rl.question('Give input',(answer)=>{ +// sendToAppendFile(answer); +// }); +// }; + +// function sendToCreateFile(answer){ +// var fs = require('fs'); +// fs.writeFile("package.json", answer, (err) => { +// if (err) throw err; +// }); +// }; + +// function sendToAppendFile(answer){ +// let createFile = []; +// createFile.push(answer); +// var fs = require('fs'); +// fs.appendFile("package.json", createFile, (err) => { +// if (err) throw err; +// }); +// } \ No newline at end of file From e3f4825244e98e0c4ed429bca64ef67404366687 Mon Sep 17 00:00:00 2001 From: Alan Houston Date: Fri, 18 Jan 2019 17:59:27 -0600 Subject: [PATCH 3/7] json package to object --- index.js | 39 +++------------------------------------ 1 file changed, 3 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index f67dd63..8d443c8 100644 --- a/index.js +++ b/index.js @@ -43,7 +43,9 @@ function saveInputs(){ jsonPackage.fileInfo.push(a3); jsonPackage.fileInfo.push(a4); jsonPackage.fileInfo.push(a5); + createJSONfile(); + rl.close(); }); }); }); @@ -52,44 +54,9 @@ function saveInputs(){ }; function createJSONfile(){ - const stringedInfo = JSON.stringify(jsonPackage.fileInfo) + const stringedInfo = JSON.stringify(jsonPackage) var fs = require('fs'); fs.appendFile("package.json", stringedInfo, (err) => { if (err) throw err; }); } - - - - - - - - -// function runInitialInit(){ -// rl.question('Give input',(answer)=>{ -// sendToCreateFile(answer); -// }); -// }; - -// function runOtherInits(){ -// rl.question('Give input',(answer)=>{ -// sendToAppendFile(answer); -// }); -// }; - -// function sendToCreateFile(answer){ -// var fs = require('fs'); -// fs.writeFile("package.json", answer, (err) => { -// if (err) throw err; -// }); -// }; - -// function sendToAppendFile(answer){ -// let createFile = []; -// createFile.push(answer); -// var fs = require('fs'); -// fs.appendFile("package.json", createFile, (err) => { -// if (err) throw err; -// }); -// } \ No newline at end of file From 182a4332ec096e3adb81e1b3d643d453450e87dd Mon Sep 17 00:00:00 2001 From: Alan Houston Date: Fri, 18 Jan 2019 18:04:59 -0600 Subject: [PATCH 4/7] little more clean, check for json file still does not exist --- index.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 8d443c8..699766e 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ const jsonPackage = { rl.question('Type "init" ',(answer)=>{ let splitAnswer = answer.split(' '); if(checkForInit(splitAnswer) && fileDoesNOTExist()){ - saveInputs(); + sendToJSON(); }else rl.close(); }); @@ -26,13 +26,7 @@ function fileDoesNOTExist(){ return true; //for now }; -// function saveInputs(){ -// rl.question('Give input: ',(answer)=>{ -// jsonPackage.fileInfo.push(answer); -// }); -// } - -function saveInputs(){ +function sendToJSON(){ rl.question('Give input: ',(a1)=>{ rl.question('Give input: ',(a2)=>{ rl.question('Give input: ',(a3)=>{ @@ -43,7 +37,6 @@ function saveInputs(){ jsonPackage.fileInfo.push(a3); jsonPackage.fileInfo.push(a4); jsonPackage.fileInfo.push(a5); - createJSONfile(); rl.close(); }); From 562e1f98cd5ef6434a8df18283c88758a1f66d15 Mon Sep 17 00:00:00 2001 From: Alan Houston Date: Fri, 18 Jan 2019 18:08:59 -0600 Subject: [PATCH 5/7] tightened up main conditional --- index.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/index.js b/index.js index 699766e..e939c79 100644 --- a/index.js +++ b/index.js @@ -12,16 +12,11 @@ const jsonPackage = { } rl.question('Type "init" ',(answer)=>{ - let splitAnswer = answer.split(' '); - if(checkForInit(splitAnswer) && fileDoesNOTExist()){ + if(answer === 'init' && fileDoesNOTExist()){ sendToJSON(); }else rl.close(); }); -function checkForInit(firstWord){ - return firstWord[0] === 'init'; -}; - function fileDoesNOTExist(){ return true; //for now }; From 27cf5079a1fc5c07f34d51245067ea15c75a27f2 Mon Sep 17 00:00:00 2001 From: Alan Houston Date: Fri, 18 Jan 2019 18:15:00 -0600 Subject: [PATCH 6/7] read directions, fixed object notation --- index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index e939c79..2a79db9 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,11 @@ const rl = readline.createInterface({ }); const jsonPackage = { - fileInfo: [] + input1: '', + input2: '', + input3: '', + input4: '', + input5: '', } rl.question('Type "init" ',(answer)=>{ @@ -27,11 +31,11 @@ function sendToJSON(){ rl.question('Give input: ',(a3)=>{ rl.question('Give input: ',(a4)=>{ rl.question('Give input: ',(a5)=>{ - jsonPackage.fileInfo.push(a1); - jsonPackage.fileInfo.push(a2); - jsonPackage.fileInfo.push(a3); - jsonPackage.fileInfo.push(a4); - jsonPackage.fileInfo.push(a5); + jsonPackage.input1 = a1; + jsonPackage.input2 = a2; + jsonPackage.input3 = a3; + jsonPackage.input4 = a4; + jsonPackage.input5 = a5; createJSONfile(); rl.close(); }); From 5053957e6d45395870c264c87c5e66dbfa1e12d6 Mon Sep 17 00:00:00 2001 From: Alan Houston Date: Sun, 20 Jan 2019 15:44:28 -0600 Subject: [PATCH 7/7] acapm complete --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2a79db9..c041f39 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ "use strict"; const readline = require('readline'); +const fs = require('fs'); const rl = readline.createInterface({ input: process.stdin, @@ -16,13 +17,15 @@ const jsonPackage = { } rl.question('Type "init" ',(answer)=>{ + //first input must be init and package.json should not exist ... yet if(answer === 'init' && fileDoesNOTExist()){ sendToJSON(); }else rl.close(); }); function fileDoesNOTExist(){ - return true; //for now + //check to see if package.json exists - if it does, return false to stop rl, else run program + return !(fs.existsSync('package.json')) }; function sendToJSON(){ @@ -46,8 +49,8 @@ function sendToJSON(){ }; function createJSONfile(){ + //stringify the json object then create the file const stringedInfo = JSON.stringify(jsonPackage) - var fs = require('fs'); fs.appendFile("package.json", stringedInfo, (err) => { if (err) throw err; });