Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(new): improve error message when project name does not match regex #3902

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions packages/angular-cli/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import * as chalk from 'chalk';
import InitCommand from './init';
import {oneLine} from 'common-tags';
import {oneLine, stripIndent} from 'common-tags';

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

const packageNameRegexp = /^[a-zA-Z][.0-9a-zA-Z]*(-[a-zA-Z][.0-9a-zA-Z]*)*$/;

function getRegExpFailPosition(str: string) {
const parts = str.split('-');
const matched: string[] = [];

parts.forEach(part => {
if (part.match(packageNameRegexp)) {
matched.push(part);
}
});

const compare = matched.join('-');
return (str !== compare) ? compare.length : null;
}

const NewCommand = Command.extend({
name: 'new',
Expand Down Expand Up @@ -39,11 +54,18 @@ const NewCommand = Command.extend({
`The "ng ${this.name}" command requires a name argument to be specified. ` +
`For more details, use "ng help".`));
}
if (!packageName.match(/^[a-zA-Z][.0-9a-zA-Z]*(-[a-zA-Z][.0-9a-zA-Z]*)*$/)) {
return Promise.reject(new SilentError(oneLine`
if (!packageName.match(packageNameRegexp)) {
const firstMessage = oneLine`
Project name "${packageName}" is not valid. New project names must
start with a letter, and must contain only alphanumeric characters or dashes.
`));
When adding a dash the segment after the dash must start with a letter too.
`;
const msg = stripIndent`
${firstMessage}
${packageName}
${Array(getRegExpFailPosition(packageName) + 1).join(' ') + '^'}
`;
return Promise.reject(new SilentError(msg));
}

commandOptions.name = packageName;
Expand Down