diff --git a/index.js b/app/index.js similarity index 69% rename from index.js rename to app/index.js index f65b4fb..e66ceac 100644 --- a/index.js +++ b/app/index.js @@ -4,19 +4,13 @@ const fs = require('fs'); const childProcess = require('child_process'); const slugify = require('slug'); const inquirer = require('inquirer'); +const questions = require('./questions'); const spawn = childProcess.spawn; const exec = childProcess.exec; function willReplacePath() { - const questions = [{ - type: 'confirm', - name: 'replace', - message: 'You alredy have a project with this name. Replace project?', - default: false - }]; - - return inquirer.prompt(questions); + return inquirer.prompt(questions.replace); } function createPath(name) { @@ -36,7 +30,7 @@ function newProject(name) { fs.exists(name, exists => { if (exists) { // TODO: improve tests for this. - return replace().then(res => res.replace ? resolve(createPath(name + 1)) : reject(false)); + return replace().then(res => res.replace ? resolve(createPath(name + 1)) : reject(Error(`Couldn't replace the path`))); } return resolve(createPath(name)); @@ -44,4 +38,15 @@ function newProject(name) { }); } +function init(answers) { + const content = JSON.stringify(answers, null, 2); + + fs.writeFile('frontpress.json', content, err => { + if (err) { + throw err; + } + }); +} + module.exports.new = newProject; +module.exports.init = init; diff --git a/app/questions.js b/app/questions.js new file mode 100644 index 0000000..b2b39c8 --- /dev/null +++ b/app/questions.js @@ -0,0 +1,27 @@ +module.exports.replace = [{ + type: 'confirm', + name: 'replace', + message: 'You alredy have a project with this name. Replace project?', + default: false +}]; + +module.exports.init = [{ + type: 'input', + name: 'restApiUrl', + message: 'WordPress API url:', + default: 'https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com' +}, { + type: 'list', + name: 'apiVersion', + message: 'WordPress API version:', + choices: ['v1', 'v2'] +}, { + type: 'confirm', + name: 'useDiqus', + message: 'Do you want to use Disqus on this project?', + default: true +}, { + when: response => response.useDiqus, + name: 'disqusShortname', + message: 'Nice! What\'s your Disqus shortname?' +}]; diff --git a/cli.js b/cli.js index 3eefadd..c113864 100644 --- a/cli.js +++ b/cli.js @@ -2,13 +2,22 @@ 'use strict'; const program = require('commander'); -const frontpress = require('./'); +const inquirer = require('inquirer'); +const frontpress = require('./app'); +const questions = require('./app/questions'); program .version('0.0.0') .option('-n, --new ', 'create a new project') + .option('-i, --init', 'init the project') .parse(process.argv); if (program.new) { frontpress.new(program.new); } + +if (program.init) { + inquirer + .prompt(questions.init) + .then(answers => frontpress.init(answers)); +} diff --git a/test.js b/test.js index ba0f0a7..9cc660c 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,6 @@ +import fs from 'fs'; import test from 'ava'; -import fn from './'; +import fn from './app'; test('create a new project', async t => { const name = 'test'; @@ -14,3 +15,27 @@ test('create a new project', async t => { t.true(project.mkdir.spawnargs.indexOf(name) !== -1, 'should have the project name on spawn args'); t.true(project.touch.spawnargs.indexOf(`${name}/index.html`) !== -1, 'should have an index.html inside the projec\'s path on spawn args'); }); + +test('generate the frontpress.json', async t => { + const objectTest = {name: 'frontpress'}; + + fn.init(objectTest); + + const dataRead = await pReadFileByName('frontpress.json'); + + t.is('object', typeof dataRead, '`dataRead` is an object'); + t.true(Object.prototype.hasOwnProperty.call(dataRead, 'name'), '`dataRead` has the `name` property'); + t.is(dataRead.name, objectTest.name, '`dataRead.name` and `objectTest.name` are the same'); +}); + +function pReadFileByName(fileName) { + return new Promise((resolve, reject) => { + fs.readFile(fileName, 'utf8', (err, data) => { + if (err) { + return reject(err); + } + + return resolve(JSON.parse(data)); + }); + }); +}