|
1 | 1 | import * as chalk from 'chalk'; |
2 | 2 | import InitCommand from './init'; |
3 | | -import {oneLine} from 'common-tags'; |
| 3 | +import {oneLine, stripIndent} from 'common-tags'; |
4 | 4 |
|
5 | 5 | const Command = require('../ember-cli/lib/models/command'); |
6 | 6 | const Project = require('../ember-cli/lib/models/project'); |
7 | 7 | const SilentError = require('silent-error'); |
8 | 8 | const validProjectName = require('../ember-cli/lib/utilities/valid-project-name'); |
9 | 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 | +} |
10 | 25 |
|
11 | 26 | const NewCommand = Command.extend({ |
12 | 27 | name: 'new', |
@@ -39,11 +54,18 @@ const NewCommand = Command.extend({ |
39 | 54 | `The "ng ${this.name}" command requires a name argument to be specified. ` + |
40 | 55 | `For more details, use "ng help".`)); |
41 | 56 | } |
42 | | - if (!packageName.match(/^[a-zA-Z][.0-9a-zA-Z]*(-[a-zA-Z][.0-9a-zA-Z]*)*$/)) { |
43 | | - return Promise.reject(new SilentError(oneLine` |
| 57 | + if (!packageName.match(packageNameRegexp)) { |
| 58 | + const firstMessage = oneLine` |
44 | 59 | Project name "${packageName}" is not valid. New project names must |
45 | 60 | start with a letter, and must contain only alphanumeric characters or dashes. |
46 | | - `)); |
| 61 | + When adding a dash the segment after the dash must start with a letter too. |
| 62 | + `; |
| 63 | + const msg = stripIndent` |
| 64 | + ${firstMessage} |
| 65 | + ${packageName} |
| 66 | + ${Array(getRegExpFailPosition(packageName) + 1).join(' ') + '^'} |
| 67 | + `; |
| 68 | + return Promise.reject(new SilentError(msg)); |
47 | 69 | } |
48 | 70 |
|
49 | 71 | commandOptions.name = packageName; |
|
0 commit comments