source: trip-planner-front/node_modules/@angular-devkit/schematics/src/rules/template.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.7 KB
Line 
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.applyTemplates = exports.template = exports.renameTemplateFiles = exports.pathTemplate = exports.applyPathTemplate = exports.contentTemplate = exports.applyContentTemplate = exports.InvalidPipeException = exports.UnknownPipeException = exports.OptionIsNotDefinedException = exports.TEMPLATE_FILENAME_RE = void 0;
11const core_1 = require("@angular-devkit/core");
12const util_1 = require("util");
13const base_1 = require("./base");
14const rename_1 = require("./rename");
15exports.TEMPLATE_FILENAME_RE = /\.template$/;
16class OptionIsNotDefinedException extends core_1.BaseException {
17 constructor(name) {
18 super(`Option "${name}" is not defined.`);
19 }
20}
21exports.OptionIsNotDefinedException = OptionIsNotDefinedException;
22class UnknownPipeException extends core_1.BaseException {
23 constructor(name) {
24 super(`Pipe "${name}" is not defined.`);
25 }
26}
27exports.UnknownPipeException = UnknownPipeException;
28class InvalidPipeException extends core_1.BaseException {
29 constructor(name) {
30 super(`Pipe "${name}" is invalid.`);
31 }
32}
33exports.InvalidPipeException = InvalidPipeException;
34const decoder = new util_1.TextDecoder('utf-8', { fatal: true });
35function applyContentTemplate(options) {
36 return (entry) => {
37 const { path, content } = entry;
38 try {
39 const decodedContent = decoder.decode(content);
40 return {
41 path,
42 content: Buffer.from(core_1.template(decodedContent, {})(options)),
43 };
44 }
45 catch (e) {
46 if (e.code === 'ERR_ENCODING_INVALID_ENCODED_DATA') {
47 return entry;
48 }
49 throw e;
50 }
51 };
52}
53exports.applyContentTemplate = applyContentTemplate;
54function contentTemplate(options) {
55 return base_1.forEach(applyContentTemplate(options));
56}
57exports.contentTemplate = contentTemplate;
58function applyPathTemplate(data, options = {
59 interpolationStart: '__',
60 interpolationEnd: '__',
61 pipeSeparator: '@',
62}) {
63 const is = options.interpolationStart;
64 const ie = options.interpolationEnd;
65 const isL = is.length;
66 const ieL = ie.length;
67 return (entry) => {
68 let path = entry.path;
69 const content = entry.content;
70 const original = path;
71 let start = path.indexOf(is);
72 // + 1 to have at least a length 1 name. `____` is not valid.
73 let end = path.indexOf(ie, start + isL + 1);
74 while (start != -1 && end != -1) {
75 const match = path.substring(start + isL, end);
76 let replacement = data[match];
77 if (!options.pipeSeparator) {
78 if (typeof replacement == 'function') {
79 replacement = replacement.call(data, original);
80 }
81 if (replacement === undefined) {
82 throw new OptionIsNotDefinedException(match);
83 }
84 }
85 else {
86 const [name, ...pipes] = match.split(options.pipeSeparator);
87 replacement = data[name];
88 if (typeof replacement == 'function') {
89 replacement = replacement.call(data, original);
90 }
91 if (replacement === undefined) {
92 throw new OptionIsNotDefinedException(name);
93 }
94 replacement = pipes.reduce((acc, pipe) => {
95 if (!pipe) {
96 return acc;
97 }
98 if (!(pipe in data)) {
99 throw new UnknownPipeException(pipe);
100 }
101 if (typeof data[pipe] != 'function') {
102 throw new InvalidPipeException(pipe);
103 }
104 // Coerce to string.
105 return '' + data[pipe](acc);
106 }, '' + replacement);
107 }
108 path = path.substring(0, start) + replacement + path.substring(end + ieL);
109 start = path.indexOf(options.interpolationStart);
110 // See above.
111 end = path.indexOf(options.interpolationEnd, start + isL + 1);
112 }
113 return { path: core_1.normalize(path), content };
114 };
115}
116exports.applyPathTemplate = applyPathTemplate;
117function pathTemplate(options) {
118 return base_1.forEach(applyPathTemplate(options));
119}
120exports.pathTemplate = pathTemplate;
121/**
122 * Remove every `.template` suffix from file names.
123 */
124function renameTemplateFiles() {
125 return rename_1.rename((path) => !!path.match(exports.TEMPLATE_FILENAME_RE), (path) => path.replace(exports.TEMPLATE_FILENAME_RE, ''));
126}
127exports.renameTemplateFiles = renameTemplateFiles;
128function template(options) {
129 return base_1.chain([
130 contentTemplate(options),
131 // Force cast to PathTemplateData. We need the type for the actual pathTemplate() call,
132 // but in this case we cannot do anything as contentTemplate are more permissive.
133 // Since values are coerced to strings in PathTemplates it will be fine in the end.
134 pathTemplate(options),
135 ]);
136}
137exports.template = template;
138function applyTemplates(options) {
139 return base_1.forEach(base_1.when((path) => path.endsWith('.template'), base_1.composeFileOperators([
140 applyContentTemplate(options),
141 // See above for this weird cast.
142 applyPathTemplate(options),
143 (entry) => {
144 return {
145 content: entry.content,
146 path: entry.path.replace(exports.TEMPLATE_FILENAME_RE, ''),
147 };
148 },
149 ])));
150}
151exports.applyTemplates = applyTemplates;
Note: See TracBrowser for help on using the repository browser.