UNPKG

2.02 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 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.TemplateLiteralFile = void 0;
12const mrm_core_1 = require("mrm-core");
13const File_1 = require("../Base/File");
14/**
15 * Exposes the API to generate source files from template files.
16 */
17class TemplateLiteralFile extends File_1.File {
18 constructor(basePath, filename, templatePath) {
19 super(basePath);
20 this.actions = [];
21 this.removeOnRollback = true;
22 this.overwrite = false;
23 this.cdIn();
24 this.filePointer = (0, mrm_core_1.template)(filename, templatePath);
25 this.cdOut();
26 }
27 /**
28 * Returns existing contents for a template file
29 */
30 get() {
31 return this.filePointer.get();
32 }
33 /**
34 * A boolean telling if the file already exists
35 */
36 exists() {
37 return this.filePointer.exists();
38 }
39 /**
40 * Apply contents to the template to evaluate it's output
41 */
42 apply(contents) {
43 this.filePointer.apply(contents);
44 return this;
45 }
46 /**
47 * Commit changes
48 */
49 commit() {
50 this.cdIn();
51 /**
52 * Do not overwrite contents when file already exists and
53 * `overwrite = false`
54 */
55 if (this.filePointer.exists() && !this.overwrite) {
56 this.cdOut();
57 return;
58 }
59 this.filePointer.save();
60 this.cdOut();
61 }
62 /**
63 * Rollback changes
64 */
65 rollback() {
66 this.cdIn();
67 /**
68 * Remove the file on rollback (only when instructed) or this method results
69 * is a noop
70 */
71 if (this.filePointer.exists() && this.removeOnRollback) {
72 this.filePointer.delete();
73 }
74 this.cdOut();
75 }
76}
77exports.TemplateLiteralFile = TemplateLiteralFile;