From ca89339329403297f85f302a49e40262ea671d91 Mon Sep 17 00:00:00 2001 From: Welkson Ramos Date: Fri, 6 May 2016 16:03:25 -0300 Subject: [PATCH] resolves #2 #4 :smiley: --- lib/awesome-br.js | 42 ++++++++++++++++++++++++------------------ lib/questions.js | 47 ++++++++++++++++++++++++++++++----------------- lib/template.js | 23 ++++++++++++----------- 3 files changed, 66 insertions(+), 46 deletions(-) diff --git a/lib/awesome-br.js b/lib/awesome-br.js index c9942e0..51debed 100644 --- a/lib/awesome-br.js +++ b/lib/awesome-br.js @@ -2,27 +2,33 @@ 'use strict'; -var fs = require('fs'); -var path = require('path'); -var inquirer = require('inquirer'); -var questions = require('../lib/questions'); -var template = require('../lib/template'); -var args = process.argv.slice(2); - +const fs = require('fs'); +const path = require('path'); +const pathExists = require('path-exists'); +const inquirer = require('inquirer'); +const questions = require('./questions'); +const sectionTemplate = require('./template'); +const args = process.argv.slice(2); if(!args.length || args.length > 1){ - process.stdout.write('Use: awesome-br
' + - '\n\nEx: awesome-br nodejs\n'); + console.log(` + Use: awesome-br
+ Ex: awesome-br nodejs + `); process.exit(1); } -function - -inquirer.prompt(questions, function(answers){ +inquirer.prompt(questions).then((answers) => { + let str = [ ]; + let currentDir = process.cwd(); + let fileExists = pathExists.sync(`${currentDir}/sections/${args}.json`); -fs.writeFile('sections/' + args + '.json', JSON.stringify(template(answers), null, ' '), 'utf8', [{flags: 'w+'}], function(err){ - if(!err) { - console.log('Arquivo criado com sucesso!'); - }; - }); -}); \ No newline at end of file + if(!fileExists){ + fs.writeFile(`${currentDir}/sections/${args}.json`, `[${sectionTemplate(answers)}]`); +} else { + fs.appendFile(`${currentDir}/sections/${args}.json`, `${sectionTemplate(answers)}`, 'utf8', (err) => { + if(err) throw err; +}); +} +console.log('Arquivo gerado com sucesso! ✔' ); +}); diff --git a/lib/questions.js b/lib/questions.js index 7b99a47..47b6ed7 100644 --- a/lib/questions.js +++ b/lib/questions.js @@ -5,38 +5,51 @@ module.exports = [ name: "title", type: "input", message: "Titulo:" -}, -{ +}, { name: "url", type: "input", message: "URL:" -}, -{ +}, { name: "type", type: "list", message: "Tipo:", - choices : [ "artigo","apostila","curso","dica","documentação","guia","hangout","livro","pacote","palestra","screencast","site","slide","slideshare","styleguide","tutorial","url","vídeo"], filter: function (str){ - return str.toLowerCase(); - } -}, -{ + choices : [ + "artigo", + "apostila", + "curso", + "dica", + "documentação", + "guia", + "hangout", + "livro", + "pacote", + "palestra", + "screencast", + "site", + "slide", + "slideshare", + "styleguide", + "tutorial", + "url", + "vídeo" + ], filter: function (str){ + return str.toLowerCase(); + } +}, { name:"tags", type:"input", - message:"Tags (separadas por vírgulas):" -}, -{ + message: "Tags (separadas por vírgulas):" +}, { name:"authorName", type: "input", message: "Nome do Autor:" -}, -{ +}, { name: "authorUrl", type: "input", message: "URL do site do Autor:" -}, -{ +}, { name:"isOk", type:"confirm", message:"Todos os dados estão corretos?" } -] \ No newline at end of file +]; diff --git a/lib/template.js b/lib/template.js index c0a1f44..124f793 100644 --- a/lib/template.js +++ b/lib/template.js @@ -1,16 +1,17 @@ 'use strict'; -module.exports = function sectionTemplate(answers){ -return [ +const sectionTemplate = (answers) => { +return ` { - "name": answers.title, - "url": answers.url, - "type": answers.type, - "tags": [ answers.tags ], + "name": "${answers.title}", + "url": "${answers.url}", + "type": "${answers.type}", + "tags": [ "${answers.tags}"], "author": { - "name": answers.authorName, - "url": answers.authorUrl + "name": "${answers.authorName}", + "url": "${answers.authorUrl}" } - } - ]; -} \ No newline at end of file + }`; +} + +module.exports = sectionTemplate;