1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
11 | if (k2 === undefined) k2 = k;
|
12 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
13 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
14 | desc = { enumerable: true, get: function() { return m[k]; } };
|
15 | }
|
16 | Object.defineProperty(o, k2, desc);
|
17 | }) : (function(o, m, k, k2) {
|
18 | if (k2 === undefined) k2 = k;
|
19 | o[k2] = m[k];
|
20 | }));
|
21 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
22 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
23 | }) : function(o, v) {
|
24 | o["default"] = v;
|
25 | });
|
26 | var __importStar = (this && this.__importStar) || function (mod) {
|
27 | if (mod && mod.__esModule) return mod;
|
28 | var result = {};
|
29 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
30 | __setModuleDefault(result, mod);
|
31 | return result;
|
32 | };
|
33 | Object.defineProperty(exports, "__esModule", { value: true });
|
34 | exports.TemplatesManager = void 0;
|
35 | const path_1 = require("path");
|
36 | const sink = __importStar(require("../../../index"));
|
37 | const Mustache_1 = require("../../Files/Formats/Mustache");
|
38 | const TemplateLiteral_1 = require("../../Files/Formats/TemplateLiteral");
|
39 |
|
40 |
|
41 |
|
42 | class TemplatesManager {
|
43 | constructor(projectRoot, templatesSourceDir, application) {
|
44 | this.projectRoot = projectRoot;
|
45 | this.templatesSourceDir = templatesSourceDir;
|
46 | this.application = application;
|
47 | this.logger = sink.logger;
|
48 | if (!(0, path_1.isAbsolute)(this.projectRoot)) {
|
49 | throw new Error('Templates manager needs an absolute path to the project root');
|
50 | }
|
51 | if (!(0, path_1.isAbsolute)(this.templatesSourceDir)) {
|
52 | throw new Error('Templates manager needs an absolute path to the templates source directory');
|
53 | }
|
54 | }
|
55 | |
56 |
|
57 |
|
58 | normalizeTemplateNode(template) {
|
59 | template =
|
60 | typeof template === 'string'
|
61 | ? {
|
62 | src: template,
|
63 | dest: template.replace(new RegExp(`${(0, path_1.extname)(template)}$`), ''),
|
64 | mustache: false,
|
65 | data: {},
|
66 | }
|
67 | : template;
|
68 | template.dest = (0, path_1.extname)(template.dest) === '' ? `${template.dest}.ts` : template.dest;
|
69 | return template;
|
70 | }
|
71 | |
72 |
|
73 |
|
74 | getDirectoryFor(templateFor) {
|
75 | if (templateFor === './' || templateFor === '/') {
|
76 | return './';
|
77 | }
|
78 | |
79 |
|
80 |
|
81 | const configuredDirectory = this.application.directoriesMap.get(templateFor);
|
82 | if (!configuredDirectory) {
|
83 | this.logger.warning(`Unknown directory type ${this.logger.colors.underline(templateFor)}`);
|
84 | return;
|
85 | }
|
86 | return configuredDirectory;
|
87 | }
|
88 | |
89 |
|
90 |
|
91 | copyTemplateFor(templateFor, template) {
|
92 | const configuredDirectory = this.getDirectoryFor(templateFor);
|
93 | if (!configuredDirectory) {
|
94 | return;
|
95 | }
|
96 | if (!template.src || !template.dest) {
|
97 | throw new Error('"src" and "dest" are required when copying templates');
|
98 | }
|
99 | const sourcePath = (0, path_1.join)(this.templatesSourceDir, template.src);
|
100 | const destinationPath = (0, path_1.normalize)(`${configuredDirectory}/${template.dest}`);
|
101 | const renderer = template.mustache
|
102 | ? new Mustache_1.MustacheFile(this.projectRoot, destinationPath, sourcePath)
|
103 | : new TemplateLiteral_1.TemplateLiteralFile(this.projectRoot, destinationPath, sourcePath);
|
104 | const hasFile = renderer.exists();
|
105 | renderer.apply(template.data);
|
106 | renderer.commit();
|
107 | if (hasFile) {
|
108 | this.logger.action('create').skipped(destinationPath);
|
109 | }
|
110 | else {
|
111 | this.logger.action('create').succeeded(destinationPath);
|
112 | }
|
113 | }
|
114 | |
115 |
|
116 |
|
117 | copyTemplatesFor(templateFor, templates) {
|
118 | templates = Array.isArray(templates) ? templates : [templates];
|
119 | templates
|
120 | .map((template) => this.normalizeTemplateNode(template))
|
121 | .forEach((template) => this.copyTemplateFor(templateFor, template));
|
122 | }
|
123 | |
124 |
|
125 |
|
126 | useLogger(logger) {
|
127 | this.logger = logger;
|
128 | return this;
|
129 | }
|
130 | |
131 |
|
132 |
|
133 |
|
134 | async copy(templates) {
|
135 | Object.keys(templates).forEach((templateFor) => {
|
136 | if (templateFor === 'basePath') {
|
137 | return;
|
138 | }
|
139 | this.copyTemplatesFor(templateFor, templates[templateFor]);
|
140 | });
|
141 | }
|
142 | }
|
143 | exports.TemplatesManager = TemplatesManager;
|