Skip to content

Commit fc6dbd6

Browse files
committed
feat(new) add indicator that points out where the package name fails
1 parent d397d94 commit fc6dbd6

File tree

1 file changed

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

1 file changed

+25
-4
lines changed

packages/angular-cli/commands/new.ts

+25-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',
@@ -38,12 +53,18 @@ const NewCommand = Command.extend({
3853
`The "ng ${this.name}" command requires a name argument to be specified. ` +
3954
`For more details, use "ng help".`));
4055
}
41-
if (!packageName.match(/^[a-zA-Z][.0-9a-zA-Z]*(-[a-zA-Z][.0-9a-zA-Z]*)*$/)) {
42-
return Promise.reject(new SilentError(oneLine`
56+
if (!packageName.match(packageNameRegexp)) {
57+
const firstMessage = oneLine`
4358
Project name "${packageName}" is not valid. New project names must
4459
start with a letter, and must contain only alphanumeric characters or dashes.
4560
When adding a dash the segment after the dash must start with a letter too.
46-
`));
61+
`;
62+
const msg = stripIndent`
63+
${firstMessage}
64+
${packageName}
65+
${Array(getRegExpFailPosition(packageName) + 1).join(' ') + '^'}
66+
`;
67+
return Promise.reject(new SilentError(msg));
4768
}
4869

4970
commandOptions.name = packageName;

0 commit comments

Comments
 (0)