Skip to content

Commit

Permalink
refactor(new): standardize project name validation (#4206)
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin authored and filipesilva committed Feb 6, 2017
1 parent 73d5628 commit 54541a1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 58 deletions.
7 changes: 2 additions & 5 deletions packages/@angular/cli/commands/init.run.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as chalk from 'chalk';
import LinkCli from '../tasks/link-cli';
import NpmInstall from '../tasks/npm-install';
import { validateProjectName } from '../utilities/validate-project-name';

const Promise = require('../ember-cli/lib/ext/promise');
const SilentError = require('silent-error');
const validProjectName = require('../ember-cli/lib/utilities/valid-project-name');
const normalizeBlueprint = require('../ember-cli/lib/utilities/normalize-blueprint-option');
const GitInit = require('../tasks/git-init');

Expand Down Expand Up @@ -73,10 +73,7 @@ export default function initRun(commandOptions: any, rawArgs: string[]) {
skipTests: commandOptions.skipTests
};

if (!validProjectName(packageName)) {
return Promise.reject(
new SilentError('We currently do not support a name of `' + packageName + '`.'));
}
validateProjectName(packageName);

blueprintOpts.blueprint = normalizeBlueprint(blueprintOpts.blueprint);

Expand Down
45 changes: 3 additions & 42 deletions packages/@angular/cli/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import * as chalk from 'chalk';
import InitCommand from './init';
import {oneLine, stripIndent} from 'common-tags';
import { validateProjectName } from '../utilities/validate-project-name';

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 @@ -53,36 +36,14 @@ 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(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));
}

validateProjectName(packageName);

commandOptions.name = packageName;
if (commandOptions.dryRun) {
commandOptions.skipGit = true;
}

if (packageName === '.') {
return Promise.reject(new SilentError(
`Trying to generate an application structure in this directory? Use "ng init" ` +
`instead.`));
}

if (!validProjectName(packageName)) {
return Promise.reject(
new SilentError(`We currently do not support a name of "${packageName}".`));
}

if (!commandOptions.directory) {
commandOptions.directory = packageName;
}
Expand Down

This file was deleted.

40 changes: 40 additions & 0 deletions packages/@angular/cli/utilities/validate-project-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {oneLine, stripIndent} from 'common-tags';

const SilentError = require('silent-error');

const projectNameRegexp = /^[a-zA-Z][.0-9a-zA-Z]*(-[a-zA-Z][.0-9a-zA-Z]*)*$/;
const unsupportedProjectNames = ['test', 'ember', 'ember-cli', 'vendor', 'app'];

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

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

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

export function validateProjectName(projectName: string) {
const errorIndex = getRegExpFailPosition(projectName);
if (errorIndex !== null) {
const firstMessage = oneLine`
Project name "${projectName}" 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 also start with a letter.
`;
const msg = stripIndent`
${firstMessage}
${projectName}
${Array(errorIndex + 1).join(' ') + '^'}
`;
throw new SilentError(msg);
} else if (unsupportedProjectNames.indexOf(projectName) !== -1) {
throw new SilentError(`Project name "${projectName}" is not a supported name.`);
}

}

0 comments on commit 54541a1

Please sign in to comment.