Skip to content

Commit bf23b13

Browse files
beemanfilipesilva
authored andcommitted
fix(new): improve error message when project name does not match regex
Fix #3816 Close #3902
1 parent 76380a6 commit bf23b13

File tree

1 file changed

+26
-4
lines changed
  • packages/angular-cli/commands

1 file changed

+26
-4
lines changed

packages/angular-cli/commands/new.ts

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
import * as chalk from 'chalk';
22
import InitCommand from './init';
3-
import {oneLine} from 'common-tags';
3+
import {oneLine, stripIndent} from 'common-tags';
44

55
const Command = require('../ember-cli/lib/models/command');
66
const Project = require('../ember-cli/lib/models/project');
77
const SilentError = require('silent-error');
88
const validProjectName = require('../ember-cli/lib/utilities/valid-project-name');
99

10+
const packageNameRegexp = /^[a-zA-Z][.0-9a-zA-Z]*(-[a-zA-Z][.0-9a-zA-Z]*)*$/;
11+
12+
function getRegExpFailPosition(str: string) {
13+
const parts = str.split('-');
14+
const matched: string[] = [];
15+
16+
parts.forEach(part => {
17+
if (part.match(packageNameRegexp)) {
18+
matched.push(part);
19+
}
20+
});
21+
22+
const compare = matched.join('-');
23+
return (str !== compare) ? compare.length : null;
24+
}
1025

1126
const NewCommand = Command.extend({
1227
name: 'new',
@@ -39,11 +54,18 @@ const NewCommand = Command.extend({
3954
`The "ng ${this.name}" command requires a name argument to be specified. ` +
4055
`For more details, use "ng help".`));
4156
}
42-
if (!packageName.match(/^[a-zA-Z][.0-9a-zA-Z]*(-[a-zA-Z][.0-9a-zA-Z]*)*$/)) {
43-
return Promise.reject(new SilentError(oneLine`
57+
if (!packageName.match(packageNameRegexp)) {
58+
const firstMessage = oneLine`
4459
Project name "${packageName}" is not valid. New project names must
4560
start with a letter, and must contain only alphanumeric characters or dashes.
46-
`));
61+
When adding a dash the segment after the dash must start with a letter too.
62+
`;
63+
const msg = stripIndent`
64+
${firstMessage}
65+
${packageName}
66+
${Array(getRegExpFailPosition(packageName) + 1).join(' ') + '^'}
67+
`;
68+
return Promise.reject(new SilentError(msg));
4769
}
4870

4971
commandOptions.name = packageName;

0 commit comments

Comments
 (0)