UNPKG

1.41 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require("fs-extra");
4const AbstractReadWriter_1 = require("./AbstractReadWriter");
5function trim(s) {
6 return s.trim();
7}
8class LineReadWriter extends AbstractReadWriter_1.AbstractReadWriter {
9 constructor(contents, file) {
10 super(file);
11 this.lines = contents && (contents = contents.trim()) ? contents.split(/\n/g).map(trim) : [];
12 }
13 static createFromContents(contents) {
14 return new LineReadWriter(contents);
15 }
16 static createFromFile(filepath) {
17 try {
18 return new LineReadWriter(fs.readFileSync(filepath, 'utf8'), filepath);
19 }
20 catch (_a) {
21 return new LineReadWriter(undefined, filepath);
22 }
23 }
24 ensure(...lines) {
25 const toAdd = new Set();
26 for (const line of lines) {
27 if (!this.lines.includes(line)) {
28 toAdd.add(line);
29 }
30 }
31 if (toAdd.size) {
32 this.lines.push(...toAdd);
33 }
34 return this;
35 }
36 ensureRegex(test, line) {
37 for (const line$ of this.lines) {
38 if (test.test(line$)) {
39 return this;
40 }
41 }
42 this.lines.push(line);
43 return this;
44 }
45 toString() {
46 return this.lines.join('\n') + '\n';
47 }
48}
49exports.LineReadWriter = LineReadWriter;