Skip to content

Commit dfdf090

Browse files
committed
fix(cli): exit gracefully if the project name fails validation
1 parent 6c94752 commit dfdf090

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

packages/cli/lib/project-generator.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,14 @@ module.exports = class ProjectGenerator extends BaseGenerator {
6060
description: 'Mark the project private (excluded from npm publish)',
6161
});
6262

63-
// argument validation
64-
if (this.args.length) {
65-
const isValid = utils.validate(this.args[0]);
66-
if (typeof isValid === 'string') throw new Error(isValid);
67-
}
68-
69-
this.setupRenameTransformer();
63+
this._setupRenameTransformer();
7064
}
7165

7266
/**
7367
* Registers a Transform Stream with Yeoman. Removes `.ejs` extension
7468
* from files that have it during project generation.
7569
*/
76-
setupRenameTransformer() {
70+
_setupRenameTransformer() {
7771
this.registerTransformStream(
7872
rename(function(file) {
7973
// extname already contains a leading '.'
@@ -88,6 +82,14 @@ module.exports = class ProjectGenerator extends BaseGenerator {
8882
}
8983

9084
setOptions() {
85+
if (this.options.name) {
86+
const msg = utils.validate(this.options.name);
87+
if (typeof msg === 'string') {
88+
this.exit(msg);
89+
return false;
90+
}
91+
}
92+
9193
this.projectInfo = {
9294
projectType: this.projectType,
9395
dependencies: utils.getDependencies(),
@@ -137,8 +139,8 @@ module.exports = class ProjectGenerator extends BaseGenerator {
137139
when:
138140
this.projectInfo.outdir == null ||
139141
// prompts if option was set to a directory that already exists
140-
utils.validateyNotExisting(this.projectInfo.outdir) !== true,
141-
validate: utils.validateyNotExisting,
142+
utils.validateNotExisting(this.projectInfo.outdir) !== true,
143+
validate: utils.validateNotExisting,
142144
default: utils.kebabCase(this.projectInfo.name),
143145
},
144146
];

packages/cli/lib/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ exports.validateClassName = function(name) {
9191
/**
9292
* Validate project directory to not exist
9393
*/
94-
exports.validateyNotExisting = function(path) {
94+
exports.validateNotExisting = function(path) {
9595
if (fs.existsSync(path)) {
9696
return util.format('Directory %s already exists.', path);
9797
}
@@ -114,7 +114,7 @@ exports.camelCase = camelCase;
114114

115115
exports.validate = function(name) {
116116
const isValid = validate(name).validForNewPackages;
117-
if (!isValid) return 'Not a valid npm package name';
117+
if (!isValid) return 'Invalid npm package name: ' + name;
118118
return isValid;
119119
};
120120

packages/cli/test/project.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const testUtils = require('./test-utils');
1111
const sinon = require('sinon');
1212
const path = require('path');
1313
const deps = require('../lib/utils').getDependencies();
14+
const expect = require('@loopback/testlab').expect;
1415

1516
module.exports = function(projGenerator, props, projectType) {
1617
return function() {
@@ -27,12 +28,24 @@ module.exports = function(projGenerator, props, projectType) {
2728
assert(!helpText.match(/loopback4:/));
2829
});
2930
});
31+
3032
describe('_setupGenerator', () => {
3133
describe('args validation', () => {
3234
it('errors out if validation fails', () => {
33-
assert.throws(() => {
34-
testUtils.testSetUpGen(projGenerator, {args: 'fooBar'});
35-
}, Error);
35+
const result = testUtils.executeGenerator(projGenerator)
36+
.withArguments(['fooBar']);
37+
return expect(result).to.be.rejectedWith(
38+
/Invalid npm package name\: fooBar/
39+
);
40+
});
41+
42+
it('errors out if validation fails', () => {
43+
const result = testUtils.executeGenerator(projGenerator)
44+
.withOptions({name: 'fooBar'})
45+
.toPromise();
46+
return expect(result).to.be.rejectedWith(
47+
/Invalid npm package name\: fooBar/
48+
);
3649
});
3750

3851
it('succeeds if no arg is provided', () => {

packages/cli/test/test-utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ exports.executeGenerator = function(GeneratorOrNamespace, settings) {
3636
runner.toPromise = function() {
3737
return new Promise((resolve, reject) => {
3838
this.on('end', () => {
39-
if (this.generator.exitGeneration) {
39+
if (this.generator.exitGeneration instanceof Error) {
4040
reject(this.generator.exitGeneration);
41+
} else if (this.generator.exitGeneration) {
42+
reject(new Error(this.generator.exitGeneration));
4143
} else {
4244
resolve(this.targetDirectory);
4345
}

0 commit comments

Comments
 (0)