From cda54a8166d02a8135959d0718c67f0b4c77a3ee Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Thu, 9 Mar 2017 20:07:24 +0900 Subject: [PATCH 1/5] prompt for app name when one is not given. These tests are not completing in this commit and this is annoying me greatly. --- lib/cmd.js | 45 ++++++++++++++++++++++++++++++++++----------- package.json | 3 +++ test/cmd.js | 44 ++++++++++++++++++++++++++++---------------- 3 files changed, 65 insertions(+), 27 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index b60ac4ff..c642cb8a 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -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,19 +31,39 @@ Cmd.prototype.process = function(args) { Cmd.prototype.newApp = function() { var self = this; - program - .command('new [name]') - .description('new application') - .action(function(name, options) { - 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); + 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) } - self.Embark.generateTemplate('boilerplate', './', name); - }); + }; + + program + .command('new [name]') + .description('new application') + .action(function (name, options) { + var parentDirectory = path.dirname(__dirname).split("/").pop(); + if (name === undefined) { + 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); + }); }; Cmd.prototype.demo = function() { diff --git a/package.json b/package.json index 06833395..0e553fb0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/cmd.js b/test/cmd.js index 8b64a97d..eb72e15f 100644 --- a/test/cmd.js +++ b/test/cmd.js @@ -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(); }); - 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(); + }); + }); + }); }); \ No newline at end of file From 9aac650c1ea9c042c27e074d5fd556719152d2ab Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Thu, 9 Mar 2017 20:18:21 +0900 Subject: [PATCH 2/5] add missing semicolon --- lib/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cmd.js b/lib/cmd.js index c642cb8a..49a4aadc 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -36,7 +36,7 @@ Cmd.prototype.newApp = function() { try { if(value.match(/^[a-zA-Z\s\-]+$/)) return value; } catch (e) { - throw new Error('Name must be only letters, spaces, or dashes', 9) + throw new Error('Name must be only letters, spaces, or dashes'); } }; From 146cca7ef3d7a54cd83546a02c9faed0314dc510 Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Fri, 10 Mar 2017 08:06:03 +0900 Subject: [PATCH 3/5] remove unneccesary returns --- lib/cmd.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index 49a4aadc..e529ff7d 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -54,10 +54,10 @@ Cmd.prototype.newApp = function() { console.error('Invalid name:', err.message); // Manually call retry // The passed error has a retry method to easily prompt again. - return err.retry(); + 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', parentDirectory, inputvalue); } }); } From 2a7511b46e840379bf1d25ce10469bbefb463461 Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Fri, 10 Mar 2017 08:06:20 +0900 Subject: [PATCH 4/5] stdout -> stdin --- test/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cmd.js b/test/cmd.js index eb72e15f..d5853dea 100644 --- a/test/cmd.js +++ b/test/cmd.js @@ -32,7 +32,7 @@ describe('embark.Cmd', function () { cmd.newApp(undefined, function (output) { var lines = output.split('\n'); console.log(lines); - process.stdout.write(appname); + process.stdin.write(appname + '\n'); assert.equal(lines[0], pl[0]); done(); }); From b186bf966c3bfc7a9c29b38d0fd89bcb4e27fbdc Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Wed, 22 Mar 2017 14:13:58 +0900 Subject: [PATCH 5/5] fixed merge conflicts --- lib/cmd.js | 48 ++++++++++++++++++++++++++++++---------- test/cmd.js | 64 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 73 insertions(+), 39 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index 4c774b03..8c5319c5 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -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 Embark = require('../lib/index'); var Cmd = function() { program.version(Embark.version); }; + Cmd.prototype.process = function(args) { this.newApp(); this.demo(); @@ -26,20 +29,41 @@ Cmd.prototype.process = function(args) { program.parse(args); }; -Cmd.prototype.newApp = function() { - program - .command('new [name]') - .description('new application') - .action(function(name, options) { - if (name === undefined) { +Cmd.prototype.newApp = function(name) { - 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); + 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'); } - Embark.generateTemplate('boilerplate', './', name); - }); + }; + + program + .command('new [name]') + .description('new application') + .action(function (name) { + if (name === undefined) { + var parentDirectory = path.dirname(__dirname).split("/").pop(); + return promptly.prompt("Name your app (default is " + 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. + err.retry(); + } else { + //slightly different assignment of name since it comes from child prompt + Embark.generateTemplate('boilerplate', './', inputvalue); + } + }); + } else { + Embark.generateTemplate('boilerplate', './', name); + } + + }); }; Cmd.prototype.demo = function() { diff --git a/test/cmd.js b/test/cmd.js index 8565ab3d..fe386f5c 100644 --- a/test/cmd.js +++ b/test/cmd.js @@ -1,41 +1,51 @@ var Embark = require('../lib/index'); var Cmd = require('../lib/cmd'); +// Function to send a line to stdin +function sendLine(line) { + setImmediate(function () { + process.stdin.emit('data', line + '\n'); + }); +} + +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 () { this.timeout(0); - var cmd = new Cmd(Embark); - 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(); - }); + describe('#new', function () { it('it should create an app with a name', function (done) { + var cmd = new Cmd(Embark); + var pl = passingLines(); var appname = 'deleteapp'; 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(); + }); }); - }); - // describe("#help", function () { - // it('it should spit out helpful text if no arguments are supplied', function (done) { - // cmd.process([], function (output) { - // var lines = output.split('\n'); - // assert.equal(lines[0], '\n'); - // assert.equal(lines[1], 'Usage:'); - // done(); - // }); - // }) - // }) + it('it should prompt when given an empty app name', function (done) { + var cmd = new Cmd(Embark); + var pl = passingLines(); + var appname = 'deleteapp'; + + cmd.newApp(undefined, function (output) { + var lines = output.split('\n'); + console.log(lines); + sendLine(appname + '\n'); + assert.equal(lines[0], pl[0]); + done(); + }); + }); + }) }); \ No newline at end of file