1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.validateProjectName = exports.validateHtmlSelector = exports.htmlSelectorRe = exports.validateName = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const core_1 = require("@angular-devkit/core");
|
12 | const schematics_1 = require("@angular-devkit/schematics");
|
13 | function 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 | }
|
19 | exports.validateName = validateName;
|
20 |
|
21 |
|
22 | exports.htmlSelectorRe = /^[a-zA-Z][.0-9a-zA-Z]*(:?-[a-zA-Z][.0-9a-zA-Z]*)*$/;
|
23 | function 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 | }
|
29 | exports.validateHtmlSelector = validateHtmlSelector;
|
30 | function 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 | }
|
54 | exports.validateProjectName = validateProjectName;
|
55 | function getRegExpFailPosition(str) {
|
56 | const isScope = /^@.*\/.*/.test(str);
|
57 | if (isScope) {
|
58 |
|
59 | str = str.replace(/^@/, '');
|
60 |
|
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 | }
|