mirror of
https://github.com/bower/bower.git
synced 2026-04-24 03:00:19 -04:00
Small improvements to the init command.
This commit is contained in:
@@ -6,14 +6,16 @@
|
||||
// http://opensource.org/licenses/MIT
|
||||
// ==========================================
|
||||
|
||||
var nopt = require('nopt');
|
||||
var promptly = require('promptly');
|
||||
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
var util = require('util');
|
||||
var util = require('util');
|
||||
|
||||
var nopt = require('nopt');
|
||||
var promptly = require('promptly');
|
||||
var _ = require('lodash');
|
||||
|
||||
var help = require('./help');
|
||||
|
||||
var Manager = require('../core/manager');
|
||||
var config = require('../core/config');
|
||||
|
||||
@@ -37,6 +39,7 @@ Init.prototype.getDependenciesJSON = function () {
|
||||
var pkg = this.dependencies[name][0];
|
||||
|
||||
pkg.on('loadJSON', function () {
|
||||
// TODO: use fetch endpoint here
|
||||
json[pkg.name] = '~' + pkg.version;
|
||||
remaining -= 1;
|
||||
if (remaining === 0) {
|
||||
@@ -76,18 +79,24 @@ Init.prototype.showPrompt = function (question, cb) {
|
||||
Init.prototype.prompts = function (dependencies) {
|
||||
var main = this.json.main || this.getMain(this.json.name) || '';
|
||||
|
||||
var questions = [
|
||||
var questions = _.compact([
|
||||
{key: 'name', prompt: 'name', value: this.json.name, yesno: false},
|
||||
{key: 'version', prompt: 'version', value: this.json.version, yesno: false},
|
||||
{key: 'main', prompt: 'main file', value: main, yesno: false},
|
||||
{key: 'dependencies', prompt: 'add current components as dependencies? (y/n)', value: dependencies, yesno: true},
|
||||
_.size(dependencies) ? {key: 'dependencies', prompt: 'add current components as dependencies? (y/n)', value: dependencies, yesno: true} : null,
|
||||
{key: 'ignore', prompt: 'add commonly ignored files to ignore list? (y/n)', value: commonIgnore, yesno: true}
|
||||
];
|
||||
]);
|
||||
var index = 0;
|
||||
var question = questions[index];
|
||||
|
||||
var cb = function (err, value) {
|
||||
this.json[question.key] = question.yesno ? (value ? question.value : []) : value;
|
||||
if (question.yesno) {
|
||||
if (value) {
|
||||
this.json[question.key] = question.value;
|
||||
}
|
||||
} else if (value) {
|
||||
this.json[question.key] = value;
|
||||
}
|
||||
index += 1;
|
||||
if (index < questions.length) {
|
||||
question = questions[index];
|
||||
|
||||
@@ -3,6 +3,5 @@
|
||||
"version": "1.2.3",
|
||||
"main": ["sample.js"],
|
||||
"ignore": ["thingToIgnore"],
|
||||
"dependencies" : {},
|
||||
"custom": "A custom field"
|
||||
}
|
||||
|
||||
34
test/init.js
34
test/init.js
@@ -33,7 +33,6 @@ describe('init', function () {
|
||||
'name: [package-new]',
|
||||
'version: [0.0.0]',
|
||||
'main file: [index.js]',
|
||||
'add current components as dependencies? (y/n): [y]',
|
||||
'add commonly ignored files to ignore list? (y/n): [y]'
|
||||
];
|
||||
|
||||
@@ -43,12 +42,11 @@ describe('init', function () {
|
||||
process.stdin.emit('data', '\n');
|
||||
})
|
||||
.on('end', function () {
|
||||
assert.strictEqual(counter, 5);
|
||||
assert.strictEqual(counter, 4);
|
||||
assert.deepEqual(savedData, {
|
||||
name: 'package-new',
|
||||
version: '0.0.0',
|
||||
main: 'index.js',
|
||||
dependencies: {},
|
||||
ignore: ['**/.*', 'node_modules', 'components']
|
||||
});
|
||||
next();
|
||||
@@ -61,25 +59,23 @@ describe('init', function () {
|
||||
after(restorecwd);
|
||||
|
||||
it('Should use your answers', function (next) {
|
||||
var index = 0;
|
||||
var answers = ['different-name', '2.3.1', 'other.js', 'n', 'n'];
|
||||
var counter = 0;
|
||||
var answers = ['different-name', '2.3.1', 'other.js', 'n'];
|
||||
|
||||
init()
|
||||
.on('prompt', function () {
|
||||
process.stdin.emit('data', answers[index++] + '\n');
|
||||
process.stdin.emit('data', answers[counter++] + '\n');
|
||||
})
|
||||
.on('end', function () {
|
||||
assert.strictEqual(counter, 4);
|
||||
assert.deepEqual(savedData, {
|
||||
name: 'different-name',
|
||||
version: '2.3.1',
|
||||
main: 'other.js',
|
||||
dependencies: {},
|
||||
ignore: []
|
||||
main: 'other.js'
|
||||
});
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
@@ -94,7 +90,6 @@ describe('init', function () {
|
||||
'name: [sample-package]',
|
||||
'version: [1.2.3]',
|
||||
'main file: [sample.js]',
|
||||
'add current components as dependencies? (y/n): [y]',
|
||||
'add commonly ignored files to ignore list? (y/n): [y]'
|
||||
];
|
||||
|
||||
@@ -104,11 +99,11 @@ describe('init', function () {
|
||||
process.stdin.emit('data', '\n');
|
||||
})
|
||||
.on('end', function () {
|
||||
assert.strictEqual(counter, 4);
|
||||
assert.deepEqual(savedData, {
|
||||
'name': 'sample-package',
|
||||
'version': '1.2.3',
|
||||
'main': ['sample.js'],
|
||||
'dependencies': {},
|
||||
'ignore': [
|
||||
'**/.*',
|
||||
'node_modules',
|
||||
@@ -126,15 +121,26 @@ describe('init', function () {
|
||||
after(restorecwd);
|
||||
|
||||
it('Should output the correct components and versions', function (next) {
|
||||
var counter = 0;
|
||||
|
||||
var questions = [
|
||||
'name: [package-existing-components]',
|
||||
'version: [0.0.0]',
|
||||
'main file: []',
|
||||
'add current components as dependencies? (y/n): [y]',
|
||||
'add commonly ignored files to ignore list? (y/n): [y]'
|
||||
];
|
||||
|
||||
init()
|
||||
.on('prompt', function () {
|
||||
.on('prompt', function (prompt) {
|
||||
assert.strictEqual(prompt, questions[counter++]);
|
||||
process.stdin.emit('data', '\n');
|
||||
})
|
||||
.on('end', function () {
|
||||
assert.strictEqual(counter, 5);
|
||||
assert.deepEqual(savedData, {
|
||||
'name': 'package-existing-components',
|
||||
'version': '0.0.0',
|
||||
'main': '',
|
||||
'dependencies': {
|
||||
'backbone': '~0.9.10',
|
||||
'jquery': '~1.9.1',
|
||||
|
||||
Reference in New Issue
Block a user