secrez 2.2.0-beta.4 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "secrez",
3
- "version": "2.2.0-beta.4",
3
+ "version": "2.2.0-beta.5",
4
4
  "license": "MIT",
5
5
  "nyc": {
6
6
  "include": "src",
package/src/Command.js CHANGED
@@ -141,46 +141,61 @@ class Command extends PreCommand {
141
141
  }
142
142
 
143
143
  /**
144
- * Check for git conflicts before performing data-changing operations
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 checkGitConflictsBeforeOperation() {
146
+ async readGitConflictStatus() {
148
147
  try {
149
- const conflictCheck = await this.internalFs.checkGitSyncStatus();
150
-
151
- if (conflictCheck.hasRisk) {
152
- // Show the warning message
153
- this.Logger.yellow(conflictCheck.message);
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
- // Otherwise, ask user if they want to continue
164
- const shouldContinue = await this.useInput({
165
- type: "confirm",
166
- message: "Do you want to continue anyway?",
167
- default: false,
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
- if (!shouldContinue) {
171
- this.Logger.grey("Operation cancelled.");
172
- return false;
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
- return true;
177
- } catch (e) {
178
- // If git check fails, log a warning but continue
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 {
@@ -61,9 +61,32 @@ class Edit extends require("../Command") {
61
61
  };
62
62
  }
63
63
 
64
- async edit(options) {
65
- let data = await this.internalFs.getTreeIndexAndPath(options.path);
66
- let file = data.path;
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
- this.Logger.reset("Changes aborted or file not changed");
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(Object.assign(options, { content }));
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
- } else {
133
- this.Logger.reset("Changes aborted or file not changed");
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 shouldProceed = await this.checkGitConflictsBeforeOperation();
147
- if (!shouldProceed) {
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
  }
@@ -294,6 +294,10 @@ function createEditor(text) {
294
294
  p.update();
295
295
  p.render();
296
296
 
297
+ emitter.abort = () => {
298
+ if (isClosed) return;
299
+ p.abort({});
300
+ };
297
301
  emitter.pause = pause;
298
302
  emitter.resume = resume;
299
303
  emitter.setText = (nextText) => {
@@ -309,15 +313,25 @@ function createEditor(text) {
309
313
  return emitter;
310
314
  }
311
315
 
312
- function editInMemory(text) {
316
+ function editInMemory(text, hooks = {}) {
313
317
  return new Promise((resolve) => {
314
318
  const editor = createEditor(text);
319
+ let settled = false;
315
320
  const finish = (value) => {
321
+ if (settled) return;
322
+ settled = true;
316
323
  editor.pause();
317
324
  resolve(value);
318
325
  };
319
326
  editor.on("submit", finish);
320
327
  editor.on("abort", () => finish(undefined));
328
+ if (typeof hooks.onReady === "function") {
329
+ hooks.onReady({
330
+ abort() {
331
+ editor.abort();
332
+ },
333
+ });
334
+ }
321
335
  });
322
336
  }
323
337