UNPKG

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