From 54541a1975ce1052c4e42933d67d918360f452e6 Mon Sep 17 00:00:00 2001 From: clydin Date: Mon, 6 Feb 2017 10:22:14 -0500 Subject: [PATCH] refactor(new): standardize project name validation (#4206) --- packages/@angular/cli/commands/init.run.ts | 7 +-- packages/@angular/cli/commands/new.ts | 45 ++----------------- .../lib/utilities/valid-project-name.js | 11 ----- .../cli/utilities/validate-project-name.ts | 40 +++++++++++++++++ 4 files changed, 45 insertions(+), 58 deletions(-) delete mode 100644 packages/@angular/cli/ember-cli/lib/utilities/valid-project-name.js create mode 100644 packages/@angular/cli/utilities/validate-project-name.ts diff --git a/packages/@angular/cli/commands/init.run.ts b/packages/@angular/cli/commands/init.run.ts index 40a7eedb9778..c243ab4b76c4 100644 --- a/packages/@angular/cli/commands/init.run.ts +++ b/packages/@angular/cli/commands/init.run.ts @@ -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'); @@ -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); diff --git a/packages/@angular/cli/commands/new.ts b/packages/@angular/cli/commands/new.ts index 54abaf5dcb8c..2f0f6d4cfa77 100644 --- a/packages/@angular/cli/commands/new.ts +++ b/packages/@angular/cli/commands/new.ts @@ -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', @@ -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; } diff --git a/packages/@angular/cli/ember-cli/lib/utilities/valid-project-name.js b/packages/@angular/cli/ember-cli/lib/utilities/valid-project-name.js deleted file mode 100644 index 8d6833177bfb..000000000000 --- a/packages/@angular/cli/ember-cli/lib/utilities/valid-project-name.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict'; - -module.exports = function(name) { - name = name.toLowerCase(); - - if (['test', 'ember', 'ember-cli', 'vendor', 'app'].indexOf(name) > -1) { return false; } - if (name.indexOf('.') > -1) { return false; } - if (!isNaN(parseInt(name.charAt(0), 10))) { return false; } - - return true; -}; diff --git a/packages/@angular/cli/utilities/validate-project-name.ts b/packages/@angular/cli/utilities/validate-project-name.ts new file mode 100644 index 000000000000..6c56fe2640d5 --- /dev/null +++ b/packages/@angular/cli/utilities/validate-project-name.ts @@ -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.`); + } + +}