-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (110 loc) · 3.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const fs = require("fs/promises");
const { bootstrap } = require("@kaholo/plugin-library");
const { dirname } = require("path");
const {
tryCreateRegexFromString,
parsePath,
pathExists,
endingWithNewline,
} = require("./helpers");
async function createFile({
PATH: path,
CONTENT: content,
overwrite,
return: returnContent,
}) {
const parsedPath = parsePath(path);
const fileAlreadyExists = await pathExists(parsedPath);
if (!overwrite && fileAlreadyExists) {
throw new Error(`File at ${path} already exists!`);
}
const directoryPath = dirname(path);
if (!await pathExists(directoryPath)) {
await fs.mkdir(directoryPath, { recursive: true });
}
try {
await fs.writeFile(parsedPath, endingWithNewline(content));
} catch (error) {
throw new Error(`Failed to write to a file at ${path}: ${error.message || JSON.stringify(error)}`);
}
const fileOperationResult = fileAlreadyExists ? `File ${path} overwritten.` : `Created file ${path}.`;
return returnContent ? getFileContent({ PATH: path }) : fileOperationResult;
}
async function appendToFile({
PATH: path,
CONTENT: content,
return: returnContent,
dontCreate,
}) {
let fileOperationResult;
const doCreateFile = !dontCreate;
const parsedPath = parsePath(path);
const passedPathExists = await pathExists(parsedPath);
if (!passedPathExists && !doCreateFile) {
throw new Error(`File ${path} doesn't exist!`);
}
if (!passedPathExists && doCreateFile) {
fileOperationResult = await createFile({
PATH: path,
CONTENT: endingWithNewline(content),
overwrite: true,
});
} else {
try {
await fs.appendFile(parsedPath, endingWithNewline(content));
} catch (error) {
throw new Error(`Failed to update a file at ${path}: ${error.message || JSON.stringify(error)}`);
}
fileOperationResult = `File ${path} updated.`;
}
return returnContent ? getFileContent({ PATH: path }) : fileOperationResult;
}
async function searchInFile({
PATH: path,
REGEX: searchRegex,
return: returnContent,
}) {
const parsedPath = parsePath(path);
const parsedRegex = tryCreateRegexFromString(searchRegex);
const fileContent = await getFileContent({ PATH: parsedPath });
return (
returnContent
? { matches: [...fileContent.match(parsedRegex)] }
: { found: parsedRegex.test(fileContent) }
);
}
async function replaceText({
PATH: path,
REGEX: replaceByRegex,
REPLACE: replaceValue = "", // undefined = delete the matches and replace with nothing
return: returnContent,
}) {
const parsedRegex = tryCreateRegexFromString(replaceByRegex);
const fileContent = await getFileContent({ PATH: path });
const newContent = fileContent.replace(parsedRegex, replaceValue);
await createFile({
PATH: path,
CONTENT: endingWithNewline(newContent),
overwrite: true,
});
return returnContent ? newContent : `File ${path} updated.`;
}
async function getFileContent({ PATH: path }) {
const parsedPath = parsePath(path);
if (!await pathExists(parsedPath)) {
throw new Error(`File ${path} wasn't found`);
}
try {
return await fs.readFile(parsedPath, "utf8");
} catch (error) {
throw new Error(`Failed to read content of a file at ${path}: ${error.message || JSON.stringify(error)}`);
}
}
module.exports = bootstrap({
createFile,
appendToFile,
searchInFile,
replaceText,
getFileContent,
createVaultFile: createFile,
});