UNPKG

5.58 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/sink
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10var __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}));
21var __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});
26var __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};
33Object.defineProperty(exports, "__esModule", { value: true });
34exports.TemplatesManager = void 0;
35const path_1 = require("path");
36const sink = __importStar(require("../../../index"));
37const Mustache_1 = require("../../Files/Formats/Mustache");
38const TemplateLiteral_1 = require("../../Files/Formats/TemplateLiteral");
39/**
40 * Templates manager to copy one or more templates to the user project.
41 */
42class 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 * Normalizes the template node to its object version
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 * Returns directory for the template key. It is defined inside the adonisrc file.
73 */
74 getDirectoryFor(templateFor) {
75 if (templateFor === './' || templateFor === '/') {
76 return './';
77 }
78 /**
79 * Ensure the object key inside package.json file is a known directory
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 * Copy templates for a given pre-defined directory within the rcfile.
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 * Copy one or more templates for a given pre-defined directory within the rc file.
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 * Define a custom logger to use
125 */
126 useLogger(logger) {
127 this.logger = logger;
128 return this;
129 }
130 /**
131 * Copy multiple templates to the destination. It takes the input of templates
132 * defined inside the package.json file.
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}
143exports.TemplatesManager = TemplatesManager;