UNPKG

3.99 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.NewLineFile = void 0;
12const mrm_core_1 = require("mrm-core");
13const File_1 = require("../Base/File");
14/**
15 * Base class to work with raw text new line files. For example `.env`
16 * file or `.gitignore`
17 */
18class NewLineFile extends File_1.File {
19 constructor(basePath, filename) {
20 super(basePath);
21 this.actions = [];
22 this.cdIn();
23 this.filePointer = (0, mrm_core_1.lines)(filename);
24 this.cdOut();
25 }
26 /**
27 * Add one or more new lines
28 */
29 add(line) {
30 this.addAction('add', { line });
31 return this;
32 }
33 /**
34 * Update existing text with new text
35 */
36 update(oldText, newText) {
37 this.addAction('update', { oldText, newText });
38 return this;
39 }
40 /**
41 * Remove lines matching the give text
42 */
43 remove(line) {
44 this.addAction('remove', { line });
45 return this;
46 }
47 /**
48 * Delete file
49 */
50 delete() {
51 this.addAction('delete');
52 return this;
53 }
54 /**
55 * Get contents for the file
56 */
57 get() {
58 return this.filePointer.get();
59 }
60 /**
61 * A boolean telling if the file already exists
62 */
63 exists() {
64 return this.filePointer.exists();
65 }
66 /**
67 * Commit mutations
68 */
69 commit() {
70 this.cdIn();
71 const actions = this.getCommitActions();
72 const deleteFile = actions.find(({ action }) => action === 'delete');
73 /**
74 * In case of `delete` action. There is no point running
75 * other actions and we can simply delete the file
76 */
77 if (deleteFile) {
78 this.filePointer.delete();
79 this.cdOut();
80 return;
81 }
82 actions.forEach(({ action, body }) => {
83 if (typeof this[`on${action}`] === 'function') {
84 const handled = this[`on${action}`]('commit', body);
85 /**
86 * Return early when action is handled by the hook
87 */
88 if (handled) {
89 return;
90 }
91 }
92 if (action === 'add') {
93 this.filePointer.add(body.line);
94 return;
95 }
96 /**
97 * On update we remove the old line and add the new one
98 */
99 if (action === 'update') {
100 this.filePointer.remove(body.oldText);
101 this.filePointer.add(body.newText);
102 return;
103 }
104 if (action === 'remove') {
105 this.filePointer.remove(body.line);
106 }
107 });
108 this.filePointer.save();
109 this.cdOut();
110 }
111 /**
112 * Rollback mutations
113 */
114 rollback() {
115 this.cdIn();
116 const actions = this.getRevertActions();
117 actions.forEach(({ action, body }) => {
118 if (typeof this[`on${action}`] === 'function') {
119 const handled = this[`on${action}`]('rollback', body);
120 /**
121 * Return early when action is handled by the hook
122 */
123 if (handled) {
124 return;
125 }
126 }
127 if (action === 'add') {
128 this.filePointer.remove(body.line);
129 return;
130 }
131 if (action === 'update') {
132 this.filePointer.remove(body.newText);
133 this.filePointer.add(body.oldText);
134 return;
135 }
136 if (action === 'remove') {
137 this.filePointer.add(body.line);
138 }
139 });
140 this.filePointer.save();
141 this.cdOut();
142 }
143}
144exports.NewLineFile = NewLineFile;