Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: init #4

Merged
merged 3 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions index.js → app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -36,12 +30,23 @@ 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));
});
});
}

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;
27 changes: 27 additions & 0 deletions app/questions.js
Original file line number Diff line number Diff line change
@@ -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?'
}];
11 changes: 10 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>', '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));
}
27 changes: 26 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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));
});
});
}