@inquirer/external-editor 3.0.1 → 3.0.3

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/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.1",
3
+ "version": "3.0.3",
4
4
  "description": "Edit a string with the users preferred text editor using $VISUAL or $ENVIRONMENT",
5
5
  "keywords": [
6
6
  "answer",
@@ -89,9 +89,9 @@
89
89
  }
90
90
  },
91
91
  "engines": {
92
- "node": ">=23.5.0 || ^22.13.0 || ^21.7.0 || ^20.12.0"
92
+ "node": ">=23.5.0 || ^22.13.0 || ^20.17.0"
93
93
  },
94
94
  "main": "./dist/index.js",
95
95
  "types": "./dist/index.d.ts",
96
- "gitHead": "5ca6d1101d5d3f8fb066cd5b389bccfdafbbe0c0"
96
+ "gitHead": "bfd8710229e3d3c9784bef3bbfbd84c7bd09bb9e"
97
97
  }