@calamity-inc/slave 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +17 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -28,6 +28,7 @@ if (!env.OPENAI_API_KEY) {
|
|
|
28
28
|
console.error("An API key is required.");
|
|
29
29
|
process.exit(1);
|
|
30
30
|
}
|
|
31
|
+
console.log();
|
|
31
32
|
|
|
32
33
|
fs.writeFileSync(path.join(configDir, ".env"), `OPENAI_API_KEY=${env.OPENAI_API_KEY}`, { encoding: "utf8", mode: 0o600 });
|
|
33
34
|
fs.chmodSync(path.join(configDir, ".env"), 0o600);
|
|
@@ -56,6 +57,15 @@ const isOutOfReach = (p, type) => {
|
|
|
56
57
|
return false;
|
|
57
58
|
};
|
|
58
59
|
|
|
60
|
+
const readFileForModel = (filePath) => fs.readFileSync(filePath, "utf8").replaceAll("\r\n", "\n");
|
|
61
|
+
const writeFileFromModel = (filePath, data, usesCRLF) => {
|
|
62
|
+
if (usesCRLF === undefined) {
|
|
63
|
+
usesCRLF = fs.existsSync(filePath) && fs.readFileSync(filePath, "utf8").includes("\r\n");
|
|
64
|
+
}
|
|
65
|
+
const normalized = data.replaceAll("\r\n", "\n");
|
|
66
|
+
fs.writeFileSync(filePath, usesCRLF ? normalized.replaceAll("\n", "\r\n") : normalized, "utf8");
|
|
67
|
+
};
|
|
68
|
+
|
|
59
69
|
const functions = {
|
|
60
70
|
ls: ({ path }) => {
|
|
61
71
|
path ??= ".";
|
|
@@ -93,7 +103,7 @@ const functions = {
|
|
|
93
103
|
.filter(file => !isOutOfReach(file));
|
|
94
104
|
const res = [];
|
|
95
105
|
for (const file of files) {
|
|
96
|
-
const matches =
|
|
106
|
+
const matches = readFileForModel(file).split(substring).length - 1;
|
|
97
107
|
if (matches) {
|
|
98
108
|
res.push(`${file} has ${matches} match${matches == 1 ? "" : "es"}`);
|
|
99
109
|
}
|
|
@@ -104,25 +114,27 @@ const functions = {
|
|
|
104
114
|
if (isOutOfReach(path)) {
|
|
105
115
|
return "Out of your reach";
|
|
106
116
|
}
|
|
107
|
-
return
|
|
117
|
+
return readFileForModel(path);
|
|
108
118
|
},
|
|
109
119
|
writeFile: ({ path, data }) => {
|
|
110
120
|
if (isOutOfReach(path, "write")) {
|
|
111
121
|
return "Out of your reach";
|
|
112
122
|
}
|
|
113
|
-
|
|
123
|
+
writeFileFromModel(path, data);
|
|
114
124
|
return "OK";
|
|
115
125
|
},
|
|
116
126
|
fileFindAndReplace: ({ path, substring, replacement, expectedMatches }) => {
|
|
117
127
|
if (isOutOfReach(path, "write")) {
|
|
118
128
|
return "Out of your reach";
|
|
119
129
|
}
|
|
120
|
-
const
|
|
130
|
+
const fileContent = fs.readFileSync(path, "utf8");
|
|
131
|
+
const usesCRLF = fileContent.includes("\r\n");
|
|
132
|
+
const arr = fileContent.replaceAll("\r\n", "\n").split(substring);
|
|
121
133
|
const matches = arr.length - 1;
|
|
122
134
|
if (matches == 0 || (expectedMatches && matches != expectedMatches)) {
|
|
123
135
|
return `There were ${matches} match${matches == 1 ? "" : "es"}`;
|
|
124
136
|
}
|
|
125
|
-
|
|
137
|
+
writeFileFromModel(path, arr.join(replacement), usesCRLF);
|
|
126
138
|
return "OK";
|
|
127
139
|
},
|
|
128
140
|
system: ({ command }) => {
|