@inquirer/external-editor 3.0.2 → 3.0.4

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/README.md CHANGED
@@ -77,6 +77,19 @@ try {
77
77
  }
78
78
  ```
79
79
 
80
+ ### Windows editor commands
81
+
82
+ On Windows, prefer setting `$VISUAL` or `$EDITOR` to the editor executable
83
+ rather than a `.cmd` or `.bat` shim. This package launches the editor directly
84
+ instead of through a shell so editor arguments and temporary file paths are not
85
+ interpreted as shell commands.
86
+
87
+ For example, use `Code.exe` with `--wait` instead of `code.cmd`:
88
+
89
+ ```powershell
90
+ setx VISUAL '"C:\Program Files\Microsoft VS Code\Code.exe" --wait'
91
+ ```
92
+
80
93
  #### API
81
94
 
82
95
  **Convenience Functions**
package/dist/index.d.ts CHANGED
@@ -23,11 +23,13 @@ export declare class ExternalEditor {
23
23
  lastExitStatus: number;
24
24
  private text;
25
25
  private tempFile;
26
+ private tempDir;
26
27
  private fileOptions;
27
28
  constructor(text?: string, fileOptions?: FileOptions);
28
29
  run(): string;
29
30
  runAsync(callback?: StringCallback): Promise<string>;
30
31
  private cleanup;
31
32
  private createTempFile;
33
+ private editorArgs;
32
34
  private readTemporaryFile;
33
35
  }
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { detect } from 'chardet';
2
2
  import { spawn, spawnSync } from 'node:child_process';
3
- import { readFileSync, unlinkSync, writeFileSync } from 'node:fs';
3
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync, } from 'node:fs';
4
4
  import path from 'node:path';
5
5
  import os from 'node:os';
6
6
  import { randomUUID } from 'node:crypto';
@@ -26,6 +26,7 @@ export class ExternalEditor {
26
26
  lastExitStatus = 0;
27
27
  text = '';
28
28
  tempFile = '';
29
+ tempDir = '';
29
30
  fileOptions = {};
30
31
  constructor(text = '', fileOptions = {}) {
31
32
  this.text = text;
@@ -38,7 +39,13 @@ export class ExternalEditor {
38
39
  this.createTempFile();
39
40
  try {
40
41
  try {
41
- const editorProcess = spawnSync(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: 'inherit' });
42
+ const editorProcess = spawnSync(this.editor.bin, this.editorArgs(), {
43
+ shell: false,
44
+ stdio: 'inherit',
45
+ });
46
+ if (editorProcess.error) {
47
+ throw editorProcess.error;
48
+ }
42
49
  this.lastExitStatus = editorProcess.status ?? 0;
43
50
  }
44
51
  catch (launchError) {
@@ -55,9 +62,15 @@ export class ExternalEditor {
55
62
  this.createTempFile();
56
63
  const promise = new Promise((resolve, reject) => {
57
64
  try {
58
- const editorProcess = spawn(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: 'inherit' });
59
- editorProcess.on('exit', (code) => {
60
- this.lastExitStatus = code;
65
+ const editorProcess = spawn(this.editor.bin, this.editorArgs(), {
66
+ shell: false,
67
+ stdio: 'inherit',
68
+ });
69
+ editorProcess.once('error', (launchError) => {
70
+ reject(new LaunchEditorError(launchError));
71
+ });
72
+ editorProcess.once('exit', (code) => {
73
+ this.lastExitStatus = code ?? 0;
61
74
  resolve();
62
75
  });
63
76
  }
@@ -78,11 +91,12 @@ export class ExternalEditor {
78
91
  return promise;
79
92
  }
80
93
  cleanup() {
81
- if (!this.tempFile)
94
+ if (!this.tempDir)
82
95
  return;
83
96
  try {
84
- unlinkSync(this.tempFile);
97
+ rmSync(this.tempDir, { force: true, recursive: true });
85
98
  this.tempFile = '';
99
+ this.tempDir = '';
86
100
  }
87
101
  catch (removeFileError) {
88
102
  throw new RemoveFileError(removeFileError);
@@ -90,17 +104,13 @@ export class ExternalEditor {
90
104
  }
91
105
  createTempFile() {
92
106
  try {
93
- const baseDir = this.fileOptions.dir ?? os.tmpdir();
107
+ const baseDir = path.resolve(this.fileOptions.dir ?? os.tmpdir());
108
+ this.tempDir = mkdtempSync(path.join(baseDir, 'inquirer-editor-'));
94
109
  const id = randomUUID();
95
110
  const prefix = sanitizeAffix(this.fileOptions.prefix);
96
111
  const postfix = sanitizeAffix(this.fileOptions.postfix);
97
112
  const filename = `${prefix}${id}${postfix}`;
98
- const candidate = path.resolve(baseDir, filename);
99
- const baseResolved = path.resolve(baseDir) + path.sep;
100
- if (!candidate.startsWith(baseResolved)) {
101
- throw new Error('Resolved temporary file escaped the base directory');
102
- }
103
- this.tempFile = candidate;
113
+ this.tempFile = path.join(this.tempDir, filename);
104
114
  const opt = { encoding: 'utf8', flag: 'wx' };
105
115
  if (Object.prototype.hasOwnProperty.call(this.fileOptions, 'mode')) {
106
116
  opt.mode = this.fileOptions.mode;
@@ -111,6 +121,9 @@ export class ExternalEditor {
111
121
  throw new CreateFileError(createFileError);
112
122
  }
113
123
  }
124
+ editorArgs() {
125
+ return [...this.editor.args, this.tempFile];
126
+ }
114
127
  readTemporaryFile() {
115
128
  try {
116
129
  const tempFileBuffer = readFileSync(this.tempFile);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/external-editor",
3
- "version": "3.0.2",
3
+ "version": "3.0.4",
4
4
  "description": "Edit a string with the users preferred text editor using $VISUAL or $ENVIRONMENT",
5
5
  "keywords": [
6
6
  "answer",
@@ -76,8 +76,9 @@
76
76
  "iconv-lite": "^0.7.2"
77
77
  },
78
78
  "devDependencies": {
79
+ "@repo/tsconfig": "0.0.0",
79
80
  "@types/chardet": "^1.0.0",
80
- "@types/node": "^25.5.2",
81
+ "@types/node": "^26.1.1",
81
82
  "typescript": "^6.0.2"
82
83
  },
83
84
  "peerDependencies": {
@@ -93,5 +94,5 @@
93
94
  },
94
95
  "main": "./dist/index.js",
95
96
  "types": "./dist/index.d.ts",
96
- "gitHead": "b43359d803733b1a7c4638addcc270987fb894c3"
97
+ "gitHead": "999706755afbdcae271f62decbd9bcd05560905b"
97
98
  }
@@ -1,10 +0,0 @@
1
- /***
2
- * Node External Editor
3
- *
4
- * Kevin Gravier <kevin@mrkmg.com>
5
- * MIT 2018
6
- */
7
- export declare class CreateFileError extends Error {
8
- originalError: Error;
9
- constructor(originalError: Error);
10
- }
@@ -1,13 +0,0 @@
1
- /***
2
- * Node External Editor
3
- *
4
- * Kevin Gravier <kevin@mrkmg.com>
5
- * MIT 2018
6
- */
7
- export class CreateFileError extends Error {
8
- originalError;
9
- constructor(originalError) {
10
- super(`Failed to create temporary file. ${originalError.message}`);
11
- this.originalError = originalError;
12
- }
13
- }
@@ -1,10 +0,0 @@
1
- /***
2
- * Node External Editor
3
- *
4
- * Kevin Gravier <kevin@mrkmg.com>
5
- * MIT 2018
6
- */
7
- export declare class LaunchEditorError extends Error {
8
- originalError: Error;
9
- constructor(originalError: Error);
10
- }
@@ -1,13 +0,0 @@
1
- /***
2
- * Node External Editor
3
- *
4
- * Kevin Gravier <kevin@mrkmg.com>
5
- * MIT 2018
6
- */
7
- export class LaunchEditorError extends Error {
8
- originalError;
9
- constructor(originalError) {
10
- super(`Failed to launch editor. ${originalError.message}`);
11
- this.originalError = originalError;
12
- }
13
- }
@@ -1,10 +0,0 @@
1
- /***
2
- * Node External Editor
3
- *
4
- * Kevin Gravier <kevin@mrkmg.com>
5
- * MIT 2018
6
- */
7
- export declare class ReadFileError extends Error {
8
- originalError: Error;
9
- constructor(originalError: Error);
10
- }
@@ -1,13 +0,0 @@
1
- /***
2
- * Node External Editor
3
- *
4
- * Kevin Gravier <kevin@mrkmg.com>
5
- * MIT 2018
6
- */
7
- export class ReadFileError extends Error {
8
- originalError;
9
- constructor(originalError) {
10
- super(`Failed to read temporary file. ${originalError.message}`);
11
- this.originalError = originalError;
12
- }
13
- }
@@ -1,10 +0,0 @@
1
- /***
2
- * Node External Editor
3
- *
4
- * Kevin Gravier <kevin@mrkmg.com>
5
- * MIT 2018
6
- */
7
- export declare class RemoveFileError extends Error {
8
- originalError: Error;
9
- constructor(originalError: Error);
10
- }
@@ -1,13 +0,0 @@
1
- /***
2
- * Node External Editor
3
- *
4
- * Kevin Gravier <kevin@mrkmg.com>
5
- * MIT 2018
6
- */
7
- export class RemoveFileError extends Error {
8
- originalError;
9
- constructor(originalError) {
10
- super(`Failed to remove temporary file. ${originalError.message}`);
11
- this.originalError = originalError;
12
- }
13
- }