secrez 2.2.0-beta.3 → 2.2.0-beta.5
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/package.json +1 -1
- package/src/Command.js +45 -30
- package/src/PreCommand.js +3 -1
- package/src/commands/Edit.js +70 -14
- package/src/editor/MemoryEditor.js +46 -10
package/package.json
CHANGED
package/src/Command.js
CHANGED
|
@@ -141,46 +141,61 @@ class Command extends PreCommand {
|
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
/**
|
|
144
|
-
*
|
|
145
|
-
* Returns true if the operation should proceed, false if it should be cancelled
|
|
144
|
+
* Fetch git status without prompting. `checkFailed` means the caller may proceed.
|
|
146
145
|
*/
|
|
147
|
-
async
|
|
146
|
+
async readGitConflictStatus() {
|
|
148
147
|
try {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
// If bypass is not allowed (e.g., external changes detected), block the operation
|
|
156
|
-
if (!conflictCheck.allowBypass) {
|
|
157
|
-
this.Logger.grey(
|
|
158
|
-
"Operation blocked for data integrity. Please quit and re-enter Secrez."
|
|
159
|
-
);
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
148
|
+
return await this.internalFs.checkGitSyncStatus();
|
|
149
|
+
} catch (e) {
|
|
150
|
+
return { hasRisk: false, checkFailed: true };
|
|
151
|
+
}
|
|
152
|
+
}
|
|
162
153
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
154
|
+
logGitConflictBlocked(conflictCheck) {
|
|
155
|
+
this.Logger.yellow(conflictCheck.message);
|
|
156
|
+
this.Logger.grey(
|
|
157
|
+
"Operation blocked for data integrity. Please quit and re-enter Secrez."
|
|
158
|
+
);
|
|
159
|
+
}
|
|
169
160
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
161
|
+
async askToBypassGitConflict(conflictCheck) {
|
|
162
|
+
this.Logger.yellow(conflictCheck.message);
|
|
163
|
+
const shouldContinue = await this.useInput({
|
|
164
|
+
type: "confirm",
|
|
165
|
+
message: "Do you want to continue anyway?",
|
|
166
|
+
default: false,
|
|
167
|
+
});
|
|
168
|
+
if (!shouldContinue) {
|
|
169
|
+
this.Logger.grey("Operation cancelled.");
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
175
174
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
175
|
+
/**
|
|
176
|
+
* Check for git conflicts before performing data-changing operations
|
|
177
|
+
* Returns true if the operation should proceed, false if it should be cancelled
|
|
178
|
+
*/
|
|
179
|
+
async checkGitConflictsBeforeOperation() {
|
|
180
|
+
const conflictCheck = await this.readGitConflictStatus();
|
|
181
|
+
|
|
182
|
+
if (conflictCheck.checkFailed) {
|
|
179
183
|
this.Logger.yellow(
|
|
180
184
|
"Warning: Could not check git status. Proceeding anyway."
|
|
181
185
|
);
|
|
182
186
|
return true;
|
|
183
187
|
}
|
|
188
|
+
|
|
189
|
+
if (!conflictCheck.hasRisk) {
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (!conflictCheck.allowBypass) {
|
|
194
|
+
this.logGitConflictBlocked(conflictCheck);
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return this.askToBypassGitConflict(conflictCheck);
|
|
184
199
|
}
|
|
185
200
|
}
|
|
186
201
|
|
package/src/PreCommand.js
CHANGED
|
@@ -17,7 +17,9 @@ class PreCommand {
|
|
|
17
17
|
const wasRaw = typeof stdin.isRaw === "boolean" ? stdin.isRaw : false;
|
|
18
18
|
|
|
19
19
|
try {
|
|
20
|
-
return await editInMemory(options.content || ""
|
|
20
|
+
return await editInMemory(options.content || "", {
|
|
21
|
+
onReady: options.onEditorReady,
|
|
22
|
+
});
|
|
21
23
|
} finally {
|
|
22
24
|
if (stdin.isTTY && typeof stdin.setRawMode === "function") {
|
|
23
25
|
try {
|
package/src/commands/Edit.js
CHANGED
|
@@ -61,9 +61,32 @@ class Edit extends require("../Command") {
|
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
async edit(options) {
|
|
65
|
-
let
|
|
66
|
-
let
|
|
64
|
+
async edit(options, checkPromise) {
|
|
65
|
+
let editorAbort = null;
|
|
66
|
+
let hardBlocked = false;
|
|
67
|
+
let userLeftEditor = false;
|
|
68
|
+
|
|
69
|
+
const reportHardBlock = (result) => {
|
|
70
|
+
if (hardBlocked) return;
|
|
71
|
+
hardBlocked = true;
|
|
72
|
+
if (editorAbort) editorAbort();
|
|
73
|
+
this.logGitConflictBlocked(result);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (checkPromise) {
|
|
77
|
+
checkPromise
|
|
78
|
+
.then((result) => {
|
|
79
|
+
if (userLeftEditor) return;
|
|
80
|
+
if (result && result.hasRisk && result.allowBypass === false) {
|
|
81
|
+
reportHardBlock(result);
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
.catch(() => {});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
let data = await this.internalFs.getTreeIndexAndPath(options.path);
|
|
89
|
+
let file = data.path;
|
|
67
90
|
let tree = data.tree;
|
|
68
91
|
|
|
69
92
|
let sanitizedPath = Entry.sanitizePath(file);
|
|
@@ -96,7 +119,9 @@ class Edit extends require("../Command") {
|
|
|
96
119
|
options.message = "Select the field to edit";
|
|
97
120
|
options.field = await this.useSelect(options);
|
|
98
121
|
if (!options.field) {
|
|
99
|
-
|
|
122
|
+
if (!hardBlocked) {
|
|
123
|
+
this.Logger.reset("Changes aborted or file not changed");
|
|
124
|
+
}
|
|
100
125
|
return;
|
|
101
126
|
}
|
|
102
127
|
}
|
|
@@ -106,12 +131,47 @@ class Edit extends require("../Command") {
|
|
|
106
131
|
} else if (!isYaml(p)) {
|
|
107
132
|
delete options.field;
|
|
108
133
|
}
|
|
134
|
+
if (hardBlocked) return;
|
|
135
|
+
|
|
109
136
|
let content = options.field
|
|
110
137
|
? fields[options.field] || ""
|
|
111
138
|
: fileData[0].content;
|
|
112
|
-
let newContent = await this.useEditor(
|
|
139
|
+
let newContent = await this.useEditor(
|
|
140
|
+
Object.assign(options, {
|
|
141
|
+
content,
|
|
142
|
+
onEditorReady: checkPromise
|
|
143
|
+
? (handle) => {
|
|
144
|
+
editorAbort = handle.abort;
|
|
145
|
+
if (hardBlocked) handle.abort();
|
|
146
|
+
}
|
|
147
|
+
: undefined,
|
|
148
|
+
})
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
if (hardBlocked) return;
|
|
152
|
+
|
|
153
|
+
if (!(newContent && newContent !== content)) {
|
|
154
|
+
userLeftEditor = true;
|
|
155
|
+
this.Logger.reset("Changes aborted or file not changed");
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (checkPromise) {
|
|
160
|
+
const result = await checkPromise;
|
|
161
|
+
if (hardBlocked) return;
|
|
162
|
+
if (result.checkFailed) {
|
|
163
|
+
this.Logger.yellow(
|
|
164
|
+
"Warning: Could not check git status. Proceeding anyway."
|
|
165
|
+
);
|
|
166
|
+
} else if (result.hasRisk && result.allowBypass === false) {
|
|
167
|
+
reportHardBlock(result);
|
|
168
|
+
return;
|
|
169
|
+
} else if (result.hasRisk) {
|
|
170
|
+
const shouldContinue = await this.askToBypassGitConflict(result);
|
|
171
|
+
if (!shouldContinue) return;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
113
174
|
|
|
114
|
-
if (newContent && newContent !== content) {
|
|
115
175
|
if (exists) {
|
|
116
176
|
let node = tree.root.getChildFromPath(p);
|
|
117
177
|
let entry = node.getEntry();
|
|
@@ -129,8 +189,8 @@ class Edit extends require("../Command") {
|
|
|
129
189
|
});
|
|
130
190
|
}
|
|
131
191
|
this.Logger.reset("File saved.");
|
|
132
|
-
}
|
|
133
|
-
|
|
192
|
+
} finally {
|
|
193
|
+
userLeftEditor = true;
|
|
134
194
|
}
|
|
135
195
|
}
|
|
136
196
|
|
|
@@ -143,12 +203,8 @@ class Edit extends require("../Command") {
|
|
|
143
203
|
path: true,
|
|
144
204
|
});
|
|
145
205
|
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
await this.edit(options);
|
|
206
|
+
const checkPromise = this.readGitConflictStatus();
|
|
207
|
+
await this.edit(options, checkPromise);
|
|
152
208
|
} catch (e) {
|
|
153
209
|
this.Logger.red(e.message);
|
|
154
210
|
}
|
|
@@ -27,6 +27,22 @@ function terminalRows() {
|
|
|
27
27
|
return process.stdout.rows || 24;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
function terminalColumns() {
|
|
31
|
+
return Math.max(2, process.stdout.columns || 80);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The footer is drawn on its own rows, one column short of the edge.
|
|
35
|
+
// A character in the last column wraps, and a wrap on the last row scrolls
|
|
36
|
+
// the viewport, which moves the text above the footer.
|
|
37
|
+
function statusChunks(status, columns) {
|
|
38
|
+
const width = Math.max(1, columns - 1);
|
|
39
|
+
const chunks = [];
|
|
40
|
+
for (let i = 0; i < status.length; i += width) {
|
|
41
|
+
chunks.push(status.slice(i, i + width));
|
|
42
|
+
}
|
|
43
|
+
return chunks.length ? chunks : [""];
|
|
44
|
+
}
|
|
45
|
+
|
|
30
46
|
function keyAction(key) {
|
|
31
47
|
if (key.ctrl) {
|
|
32
48
|
if (key.name === "a") return "first";
|
|
@@ -185,17 +201,23 @@ const Editor = {
|
|
|
185
201
|
|
|
186
202
|
render() {
|
|
187
203
|
const rows = terminalRows();
|
|
188
|
-
const
|
|
204
|
+
const columns = terminalColumns();
|
|
205
|
+
const chunks = statusChunks(EDITOR_STATUS, columns);
|
|
206
|
+
const statusTop = Math.max(0, rows - chunks.length);
|
|
189
207
|
const { lines, cursorX, cursorY } = this;
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
esc.cursorTo(0,
|
|
208
|
+
let out = clearViewport() + lines.join("\n");
|
|
209
|
+
chunks.forEach((chunk, i) => {
|
|
210
|
+
out +=
|
|
211
|
+
esc.cursorTo(0, statusTop + i) +
|
|
212
|
+
esc.eraseLine +
|
|
194
213
|
"\x1b[2m" +
|
|
195
|
-
|
|
196
|
-
"\x1b[0m"
|
|
197
|
-
|
|
198
|
-
);
|
|
214
|
+
chunk +
|
|
215
|
+
"\x1b[0m";
|
|
216
|
+
});
|
|
217
|
+
const cursorRow = Math.min(cursorY, Math.max(0, statusTop - 1));
|
|
218
|
+
const cursorCol = Math.min(cursorX, columns - 1);
|
|
219
|
+
out += esc.cursorTo(cursorCol, cursorRow);
|
|
220
|
+
this.out.write(out);
|
|
199
221
|
},
|
|
200
222
|
};
|
|
201
223
|
|
|
@@ -272,6 +294,10 @@ function createEditor(text) {
|
|
|
272
294
|
p.update();
|
|
273
295
|
p.render();
|
|
274
296
|
|
|
297
|
+
emitter.abort = () => {
|
|
298
|
+
if (isClosed) return;
|
|
299
|
+
p.abort({});
|
|
300
|
+
};
|
|
275
301
|
emitter.pause = pause;
|
|
276
302
|
emitter.resume = resume;
|
|
277
303
|
emitter.setText = (nextText) => {
|
|
@@ -287,15 +313,25 @@ function createEditor(text) {
|
|
|
287
313
|
return emitter;
|
|
288
314
|
}
|
|
289
315
|
|
|
290
|
-
function editInMemory(text) {
|
|
316
|
+
function editInMemory(text, hooks = {}) {
|
|
291
317
|
return new Promise((resolve) => {
|
|
292
318
|
const editor = createEditor(text);
|
|
319
|
+
let settled = false;
|
|
293
320
|
const finish = (value) => {
|
|
321
|
+
if (settled) return;
|
|
322
|
+
settled = true;
|
|
294
323
|
editor.pause();
|
|
295
324
|
resolve(value);
|
|
296
325
|
};
|
|
297
326
|
editor.on("submit", finish);
|
|
298
327
|
editor.on("abort", () => finish(undefined));
|
|
328
|
+
if (typeof hooks.onReady === "function") {
|
|
329
|
+
hooks.onReady({
|
|
330
|
+
abort() {
|
|
331
|
+
editor.abort();
|
|
332
|
+
},
|
|
333
|
+
});
|
|
334
|
+
}
|
|
299
335
|
});
|
|
300
336
|
}
|
|
301
337
|
|