-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(new): standardize project name validation (#4206)
- Loading branch information
1 parent
73d5628
commit 54541a1
Showing
4 changed files
with
45 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
packages/@angular/cli/ember-cli/lib/utilities/valid-project-name.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); | ||
} | ||
|
||
} |