UNPKG

2.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * @license
5 * Copyright Google Inc. All Rights Reserved.
6 *
7 * Use of this source code is governed by an MIT-style license that can be
8 * found in the LICENSE file at https://angular.io/license
9 */
10const core_1 = require("@angular-devkit/core");
11const schematics_1 = require("@angular-devkit/schematics");
12function validateName(name) {
13 if (name && /^\d/.test(name)) {
14 throw new schematics_1.SchematicsException(core_1.tags.oneLine `name (${name})
15 can not start with a digit.`);
16 }
17}
18exports.validateName = validateName;
19// Must start with a letter, and must contain only alphanumeric characters or dashes.
20// When adding a dash the segment after the dash must also start with a letter.
21exports.htmlSelectorRe = /^[a-zA-Z][.0-9a-zA-Z]*(:?-[a-zA-Z][.0-9a-zA-Z]*)*$/;
22function validateHtmlSelector(selector) {
23 if (selector && !exports.htmlSelectorRe.test(selector)) {
24 throw new schematics_1.SchematicsException(core_1.tags.oneLine `Selector (${selector})
25 is invalid.`);
26 }
27}
28exports.validateHtmlSelector = validateHtmlSelector;
29function validateProjectName(projectName) {
30 const errorIndex = getRegExpFailPosition(projectName);
31 const unsupportedProjectNames = [];
32 const packageNameRegex = /^(?:@[a-zA-Z0-9_-]+\/)?[a-zA-Z0-9_-]+$/;
33 if (errorIndex !== null) {
34 const firstMessage = core_1.tags.oneLine `
35 Project name "${projectName}" is not valid. New project names must
36 start with a letter, and must contain only alphanumeric characters or dashes.
37 When adding a dash the segment after the dash must also start with a letter.
38 `;
39 const msg = core_1.tags.stripIndent `
40 ${firstMessage}
41 ${projectName}
42 ${Array(errorIndex + 1).join(' ') + '^'}
43 `;
44 throw new schematics_1.SchematicsException(msg);
45 }
46 else if (unsupportedProjectNames.indexOf(projectName) !== -1) {
47 throw new schematics_1.SchematicsException(`Project name ${JSON.stringify(projectName)} is not a supported name.`);
48 }
49 else if (!packageNameRegex.test(projectName)) {
50 throw new schematics_1.SchematicsException(`Project name ${JSON.stringify(projectName)} is invalid.`);
51 }
52}
53exports.validateProjectName = validateProjectName;
54function getRegExpFailPosition(str) {
55 const isScope = /^@.*\/.*/.test(str);
56 if (isScope) {
57 // Remove starting @
58 str = str.replace(/^@/, '');
59 // Change / to - for validation
60 str = str.replace(/\//g, '-');
61 }
62 const parts = str.indexOf('-') >= 0 ? str.split('-') : [str];
63 const matched = [];
64 const projectNameRegexp = /^[a-zA-Z][.0-9a-zA-Z]*(-[.0-9a-zA-Z]*)*$/;
65 parts.forEach(part => {
66 if (part.match(projectNameRegexp)) {
67 matched.push(part);
68 }
69 });
70 const compare = matched.join('-');
71 return (str !== compare) ? compare.length : null;
72}