Skip to content

Commit 54541a1

Browse files
clydinfilipesilva
authored andcommitted
refactor(new): standardize project name validation (#4206)
1 parent 73d5628 commit 54541a1

File tree

4 files changed

+45
-58
lines changed

4 files changed

+45
-58
lines changed

packages/@angular/cli/commands/init.run.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as chalk from 'chalk';
22
import LinkCli from '../tasks/link-cli';
33
import NpmInstall from '../tasks/npm-install';
4+
import { validateProjectName } from '../utilities/validate-project-name';
45

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

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

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

8178
blueprintOpts.blueprint = normalizeBlueprint(blueprintOpts.blueprint);
8279

packages/@angular/cli/commands/new.ts

+3-42
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
11
import * as chalk from 'chalk';
22
import InitCommand from './init';
3-
import {oneLine, stripIndent} from 'common-tags';
3+
import { validateProjectName } from '../utilities/validate-project-name';
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');
8-
const validProjectName = require('../ember-cli/lib/utilities/valid-project-name');
9-
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-
}
258

269
const NewCommand = Command.extend({
2710
name: 'new',
@@ -53,36 +36,14 @@ const NewCommand = Command.extend({
5336
`The "ng ${this.name}" command requires a name argument to be specified. ` +
5437
`For more details, use "ng help".`));
5538
}
56-
if (!packageName.match(packageNameRegexp)) {
57-
const firstMessage = oneLine`
58-
Project name "${packageName}" is not valid. New project names must
59-
start with a letter, and must contain only alphanumeric characters or dashes.
60-
When adding a dash the segment after the dash must start with a letter too.
61-
`;
62-
const msg = stripIndent`
63-
${firstMessage}
64-
${packageName}
65-
${Array(getRegExpFailPosition(packageName) + 1).join(' ') + '^'}
66-
`;
67-
return Promise.reject(new SilentError(msg));
68-
}
39+
40+
validateProjectName(packageName);
6941

7042
commandOptions.name = packageName;
7143
if (commandOptions.dryRun) {
7244
commandOptions.skipGit = true;
7345
}
7446

75-
if (packageName === '.') {
76-
return Promise.reject(new SilentError(
77-
`Trying to generate an application structure in this directory? Use "ng init" ` +
78-
`instead.`));
79-
}
80-
81-
if (!validProjectName(packageName)) {
82-
return Promise.reject(
83-
new SilentError(`We currently do not support a name of "${packageName}".`));
84-
}
85-
8647
if (!commandOptions.directory) {
8748
commandOptions.directory = packageName;
8849
}

packages/@angular/cli/ember-cli/lib/utilities/valid-project-name.js

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {oneLine, stripIndent} from 'common-tags';
2+
3+
const SilentError = require('silent-error');
4+
5+
const projectNameRegexp = /^[a-zA-Z][.0-9a-zA-Z]*(-[a-zA-Z][.0-9a-zA-Z]*)*$/;
6+
const unsupportedProjectNames = ['test', 'ember', 'ember-cli', 'vendor', 'app'];
7+
8+
function getRegExpFailPosition(str: string): number | null {
9+
const parts = str.split('-');
10+
const matched: string[] = [];
11+
12+
parts.forEach(part => {
13+
if (part.match(projectNameRegexp)) {
14+
matched.push(part);
15+
}
16+
});
17+
18+
const compare = matched.join('-');
19+
return (str !== compare) ? compare.length : null;
20+
}
21+
22+
export function validateProjectName(projectName: string) {
23+
const errorIndex = getRegExpFailPosition(projectName);
24+
if (errorIndex !== null) {
25+
const firstMessage = oneLine`
26+
Project name "${projectName}" is not valid. New project names must
27+
start with a letter, and must contain only alphanumeric characters or dashes.
28+
When adding a dash the segment after the dash must also start with a letter.
29+
`;
30+
const msg = stripIndent`
31+
${firstMessage}
32+
${projectName}
33+
${Array(errorIndex + 1).join(' ') + '^'}
34+
`;
35+
throw new SilentError(msg);
36+
} else if (unsupportedProjectNames.indexOf(projectName) !== -1) {
37+
throw new SilentError(`Project name "${projectName}" is not a supported name.`);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)