prompt for app name when one is not given. These tests are not completing in this commit and this is annoying me greatly.

This commit is contained in:
Todd Baur 2017-03-09 20:07:24 +09:00
parent eea0d71f44
commit cda54a8166
3 changed files with 65 additions and 27 deletions

View File

@ -1,12 +1,15 @@
var program = require('commander');
var colors = require('colors');
var shelljs = require('shelljs');
var promptly = require('promptly');
var path = require('path');
var Cmd = function(Embark) {
this.Embark = Embark;
program.version(Embark.version);
};
Cmd.prototype.process = function(args) {
this.newApp();
this.demo();
@ -28,17 +31,37 @@ Cmd.prototype.process = function(args) {
Cmd.prototype.newApp = function() {
var self = this;
var validateName = function (value) {
try {
if(value.match(/^[a-zA-Z\s\-]+$/)) return value;
} catch (e) {
throw new Error('Name must be only letters, spaces, or dashes', 9)
}
};
program
.command('new [name]')
.description('new application')
.action(function (name, options) {
var parentDirectory = path.dirname(__dirname).split("/").pop();
if (name === undefined) {
console.log("please specify your app Name".red);
console.log("e.g embark new MyApp".green);
console.log("e.g embark new --help for more information".green);
process.exit(9);
return promptly.prompt("Name your app: (default: " + parentDirectory + ")", {
default: parentDirectory,
validator: validateName
}, function (err, inputvalue) {
if (err) {
console.error('Invalid name:', err.message);
// Manually call retry
// The passed error has a retry method to easily prompt again.
return err.retry();
} else {
//slightly different assignment of name since it comes from child prompt
return self.Embark.generateTemplate('boilerplate', parentDirectory, inputvalue);
}
});
}
self.Embark.generateTemplate('boilerplate', './', name);
});
};

View File

@ -26,6 +26,7 @@
"fs-extra": "^2.0.0",
"globule": "^1.1.0",
"merge": "^1.2.0",
"promptly": "^2.1.0",
"serve-static": "^1.11.1",
"shelljs": "^0.5.0",
"solc": "0.4.8",
@ -49,6 +50,8 @@
"devDependencies": {
"grunt": "^1.0.1",
"grunt-cli": "^1.2.0",
"grunt-contrib-clean": "^1.0.0",
"grunt-contrib-coffee": "^1.0.0",
"grunt-contrib-jshint": "^1.0.0",
"grunt-mocha-test": "^0.13.2",
"matchdep": "^1.0.1",

View File

@ -1,30 +1,42 @@
var Embark = require('../lib/index');
var Cmd = require('../lib/cmd');
var passingLines = function () {
var lines = [];
lines.push('Initializing Embark Template....');
lines.push('Installing packages.. this can take a few seconds');
lines.push('Init complete');
return lines;
};
describe('embark.Cmd', function () {
var cmd = new Cmd(Embark);
var pl = passingLines();
var appname = 'deleteapp';
describe('#new', function () {
it('it should not create an app without a name', function (done) {
cmd.newApp(undefined, function (output) {
var lines = output.split('\n');
assert.equal(lines[0], 'please specify your app Name');
assert.equal(lines[1], 'e.g embark new MyApp');
assert.equal(lines[2], 'e.g embark new --help for more information');
});
done();
});
it('it should create an app with a name', function (done) {
var appname = 'deleteapp';
this.timeout(0);
it('it should create an app with a `name` argument set', function (done) {
cmd.newApp(appname, function (output) {
var lines = output.split('\n');
assert.equal(lines[0], 'Initializing Embark Template....');
assert.equal(lines[1], 'Installing packages.. this can take a few seconds');
assert.equal(lines[2], 'Init complete');
console.log(lines);
assert.equal(lines[0], pl[0]);
assert.equal(lines[1], pl[1]);
assert.equal(lines[2], pl[2]);
assert.equal(lines[3], 'App ready at ./' + appname);
});
done();
});
});
it('it should prompt when given an empty app name', function (done) {
cmd.newApp(undefined, function (output) {
var lines = output.split('\n');
console.log(lines);
process.stdout.write(appname);
assert.equal(lines[0], pl[0]);
done();
});
});
});
});