1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.NewLineFile = void 0;
|
12 | const mrm_core_1 = require("mrm-core");
|
13 | const File_1 = require("../Base/File");
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | class 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 |
|
28 |
|
29 | add(line) {
|
30 | this.addAction('add', { line });
|
31 | return this;
|
32 | }
|
33 | |
34 |
|
35 |
|
36 | update(oldText, newText) {
|
37 | this.addAction('update', { oldText, newText });
|
38 | return this;
|
39 | }
|
40 | |
41 |
|
42 |
|
43 | remove(line) {
|
44 | this.addAction('remove', { line });
|
45 | return this;
|
46 | }
|
47 | |
48 |
|
49 |
|
50 | delete() {
|
51 | this.addAction('delete');
|
52 | return this;
|
53 | }
|
54 | |
55 |
|
56 |
|
57 | get() {
|
58 | return this.filePointer.get();
|
59 | }
|
60 | |
61 |
|
62 |
|
63 | exists() {
|
64 | return this.filePointer.exists();
|
65 | }
|
66 | |
67 |
|
68 |
|
69 | commit() {
|
70 | this.cdIn();
|
71 | const actions = this.getCommitActions();
|
72 | const deleteFile = actions.find(({ action }) => action === 'delete');
|
73 | |
74 |
|
75 |
|
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 |
|
87 |
|
88 | if (handled) {
|
89 | return;
|
90 | }
|
91 | }
|
92 | if (action === 'add') {
|
93 | this.filePointer.add(body.line);
|
94 | return;
|
95 | }
|
96 | |
97 |
|
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 |
|
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 |
|
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 | }
|
144 | exports.NewLineFile = NewLineFile;
|