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

refactor: standardize project name validation #4206

Merged
merged 1 commit into from Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.`);
}

}