@inquirer/external-editor 1.0.2 → 2.0.0

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
@@ -5,16 +5,6 @@ A Node.js module to edit a string with the user's preferred text editor using $V
5
5
  > [!NOTE]
6
6
  > This package is a replacement for the unmaintained `external-editor`. It includes security fixes.
7
7
 
8
- # Special Thanks
9
-
10
- <div align="center" markdown="1">
11
-
12
- [![Graphite](https://github.com/user-attachments/assets/53db40ca-2254-481a-a094-6597f8716e29)](https://graphite.dev/?utm_source=npmjs&utm_medium=repo&utm_campaign=inquirerjs)<br>
13
-
14
- ### [Graphite is the AI developer productivity platform helping teams on GitHub ship higher quality software, faster](https://graphite.dev/?utm_source=npmjs&utm_medium=repo&utm_campaign=inquirerjs)
15
-
16
- </div>
17
-
18
8
  # Installation
19
9
 
20
10
  <table>
@@ -1,7 +1,7 @@
1
- import { CreateFileError } from './errors/CreateFileError.js';
2
- import { LaunchEditorError } from './errors/LaunchEditorError.js';
3
- import { ReadFileError } from './errors/ReadFileError.js';
4
- import { RemoveFileError } from './errors/RemoveFileError.js';
1
+ import { CreateFileError } from './errors/CreateFileError.ts';
2
+ import { LaunchEditorError } from './errors/LaunchEditorError.ts';
3
+ import { ReadFileError } from './errors/ReadFileError.ts';
4
+ import { RemoveFileError } from './errors/RemoveFileError.ts';
5
5
  export interface IEditorParams {
6
6
  args: string[];
7
7
  bin: string;
@@ -13,14 +13,14 @@ const node_path_1 = __importDefault(require("node:path"));
13
13
  const node_os_1 = __importDefault(require("node:os"));
14
14
  const node_crypto_1 = require("node:crypto");
15
15
  const iconv_lite_1 = __importDefault(require("iconv-lite"));
16
- const CreateFileError_js_1 = require("./errors/CreateFileError.js");
17
- Object.defineProperty(exports, "CreateFileError", { enumerable: true, get: function () { return CreateFileError_js_1.CreateFileError; } });
18
- const LaunchEditorError_js_1 = require("./errors/LaunchEditorError.js");
19
- Object.defineProperty(exports, "LaunchEditorError", { enumerable: true, get: function () { return LaunchEditorError_js_1.LaunchEditorError; } });
20
- const ReadFileError_js_1 = require("./errors/ReadFileError.js");
21
- Object.defineProperty(exports, "ReadFileError", { enumerable: true, get: function () { return ReadFileError_js_1.ReadFileError; } });
22
- const RemoveFileError_js_1 = require("./errors/RemoveFileError.js");
23
- Object.defineProperty(exports, "RemoveFileError", { enumerable: true, get: function () { return RemoveFileError_js_1.RemoveFileError; } });
16
+ const CreateFileError_ts_1 = require("./errors/CreateFileError.js");
17
+ Object.defineProperty(exports, "CreateFileError", { enumerable: true, get: function () { return CreateFileError_ts_1.CreateFileError; } });
18
+ const LaunchEditorError_ts_1 = require("./errors/LaunchEditorError.js");
19
+ Object.defineProperty(exports, "LaunchEditorError", { enumerable: true, get: function () { return LaunchEditorError_ts_1.LaunchEditorError; } });
20
+ const ReadFileError_ts_1 = require("./errors/ReadFileError.js");
21
+ Object.defineProperty(exports, "ReadFileError", { enumerable: true, get: function () { return ReadFileError_ts_1.ReadFileError; } });
22
+ const RemoveFileError_ts_1 = require("./errors/RemoveFileError.js");
23
+ Object.defineProperty(exports, "RemoveFileError", { enumerable: true, get: function () { return RemoveFileError_ts_1.RemoveFileError; } });
24
24
  function edit(text = '', fileOptions) {
25
25
  const editor = new ExternalEditor(text, fileOptions);
26
26
  editor.run();
@@ -148,7 +148,7 @@ class ExternalEditor {
148
148
  (0, fs_1.writeFileSync)(this.tempFile, this.text, opt);
149
149
  }
150
150
  catch (createFileError) {
151
- throw new CreateFileError_js_1.CreateFileError(createFileError);
151
+ throw new CreateFileError_ts_1.CreateFileError(createFileError);
152
152
  }
153
153
  }
154
154
  readTemporaryFile() {
@@ -167,7 +167,7 @@ class ExternalEditor {
167
167
  }
168
168
  }
169
169
  catch (readFileError) {
170
- throw new ReadFileError_js_1.ReadFileError(readFileError);
170
+ throw new ReadFileError_ts_1.ReadFileError(readFileError);
171
171
  }
172
172
  }
173
173
  removeTemporaryFile() {
@@ -175,7 +175,7 @@ class ExternalEditor {
175
175
  (0, fs_1.unlinkSync)(this.tempFile);
176
176
  }
177
177
  catch (removeFileError) {
178
- throw new RemoveFileError_js_1.RemoveFileError(removeFileError);
178
+ throw new RemoveFileError_ts_1.RemoveFileError(removeFileError);
179
179
  }
180
180
  }
181
181
  launchEditor() {
@@ -184,7 +184,7 @@ class ExternalEditor {
184
184
  this.lastExitStatus = editorProcess.status ?? 0;
185
185
  }
186
186
  catch (launchError) {
187
- throw new LaunchEditorError_js_1.LaunchEditorError(launchError);
187
+ throw new LaunchEditorError_ts_1.LaunchEditorError(launchError);
188
188
  }
189
189
  }
190
190
  launchEditorAsync(callback) {
@@ -196,7 +196,7 @@ class ExternalEditor {
196
196
  });
197
197
  }
198
198
  catch (launchError) {
199
- throw new LaunchEditorError_js_1.LaunchEditorError(launchError);
199
+ throw new LaunchEditorError_ts_1.LaunchEditorError(launchError);
200
200
  }
201
201
  }
202
202
  }
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,13 @@
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
+ }
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,13 @@
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
+ }
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,13 @@
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
+ }
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,13 @@
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
+ }
@@ -1,7 +1,7 @@
1
- import { CreateFileError } from './errors/CreateFileError.js';
2
- import { LaunchEditorError } from './errors/LaunchEditorError.js';
3
- import { ReadFileError } from './errors/ReadFileError.js';
4
- import { RemoveFileError } from './errors/RemoveFileError.js';
1
+ import { CreateFileError } from './errors/CreateFileError.ts';
2
+ import { LaunchEditorError } from './errors/LaunchEditorError.ts';
3
+ import { ReadFileError } from './errors/ReadFileError.ts';
4
+ import { RemoveFileError } from './errors/RemoveFileError.ts';
5
5
  export interface IEditorParams {
6
6
  args: string[];
7
7
  bin: string;
package/dist/esm/index.js CHANGED
@@ -5,10 +5,10 @@ import path from 'node:path';
5
5
  import os from 'node:os';
6
6
  import { randomUUID } from 'node:crypto';
7
7
  import iconv from 'iconv-lite';
8
- import { CreateFileError } from './errors/CreateFileError.js';
9
- import { LaunchEditorError } from './errors/LaunchEditorError.js';
10
- import { ReadFileError } from './errors/ReadFileError.js';
11
- import { RemoveFileError } from './errors/RemoveFileError.js';
8
+ import { CreateFileError } from "./errors/CreateFileError.js";
9
+ import { LaunchEditorError } from "./errors/LaunchEditorError.js";
10
+ import { ReadFileError } from "./errors/ReadFileError.js";
11
+ import { RemoveFileError } from "./errors/RemoveFileError.js";
12
12
  export { CreateFileError, LaunchEditorError, ReadFileError, RemoveFileError };
13
13
  export function edit(text = '', fileOptions) {
14
14
  const editor = new ExternalEditor(text, fileOptions);
@@ -0,0 +1,39 @@
1
+ import { CreateFileError } from './errors/CreateFileError.ts';
2
+ import { LaunchEditorError } from './errors/LaunchEditorError.ts';
3
+ import { ReadFileError } from './errors/ReadFileError.ts';
4
+ import { RemoveFileError } from './errors/RemoveFileError.ts';
5
+ export interface IEditorParams {
6
+ args: string[];
7
+ bin: string;
8
+ }
9
+ export interface IFileOptions {
10
+ prefix?: string;
11
+ postfix?: string;
12
+ mode?: number;
13
+ template?: string;
14
+ dir?: string;
15
+ }
16
+ export type StringCallback = (err: Error | undefined, result: string | undefined) => void;
17
+ export type VoidCallback = () => void;
18
+ export { CreateFileError, LaunchEditorError, ReadFileError, RemoveFileError };
19
+ export declare function edit(text?: string, fileOptions?: IFileOptions): string;
20
+ export declare function editAsync(text: string | undefined, callback: StringCallback, fileOptions?: IFileOptions): void;
21
+ export declare class ExternalEditor {
22
+ text: string;
23
+ tempFile: string;
24
+ editor: IEditorParams;
25
+ lastExitStatus: number;
26
+ private fileOptions;
27
+ get temp_file(): string;
28
+ get last_exit_status(): number;
29
+ constructor(text?: string, fileOptions?: IFileOptions);
30
+ run(): string;
31
+ runAsync(callback: StringCallback): void;
32
+ cleanup(): void;
33
+ private determineEditor;
34
+ private createTemporaryFile;
35
+ private readTemporaryFile;
36
+ private removeTemporaryFile;
37
+ private launchEditor;
38
+ private launchEditorAsync;
39
+ }
package/dist/index.js ADDED
@@ -0,0 +1,191 @@
1
+ import { detect } from 'chardet';
2
+ import { spawn, spawnSync } from 'child_process';
3
+ import { readFileSync, unlinkSync, writeFileSync } from 'fs';
4
+ import path from 'node:path';
5
+ import os from 'node:os';
6
+ import { randomUUID } from 'node:crypto';
7
+ import iconv from 'iconv-lite';
8
+ import { CreateFileError } from "./errors/CreateFileError.js";
9
+ import { LaunchEditorError } from "./errors/LaunchEditorError.js";
10
+ import { ReadFileError } from "./errors/ReadFileError.js";
11
+ import { RemoveFileError } from "./errors/RemoveFileError.js";
12
+ export { CreateFileError, LaunchEditorError, ReadFileError, RemoveFileError };
13
+ export function edit(text = '', fileOptions) {
14
+ const editor = new ExternalEditor(text, fileOptions);
15
+ editor.run();
16
+ editor.cleanup();
17
+ return editor.text;
18
+ }
19
+ export function editAsync(text = '', callback, fileOptions) {
20
+ const editor = new ExternalEditor(text, fileOptions);
21
+ editor.runAsync((err, result) => {
22
+ if (err) {
23
+ setImmediate(callback, err, undefined);
24
+ }
25
+ else {
26
+ try {
27
+ editor.cleanup();
28
+ setImmediate(callback, undefined, result);
29
+ }
30
+ catch (cleanupError) {
31
+ setImmediate(callback, cleanupError, undefined);
32
+ }
33
+ }
34
+ });
35
+ }
36
+ function sanitizeAffix(affix) {
37
+ if (!affix)
38
+ return '';
39
+ return affix.replace(/[^a-zA-Z0-9_.-]/g, '_');
40
+ }
41
+ function splitStringBySpace(str) {
42
+ const pieces = [];
43
+ let currentString = '';
44
+ for (let strIndex = 0; strIndex < str.length; strIndex++) {
45
+ const currentLetter = str.charAt(strIndex);
46
+ if (strIndex > 0 &&
47
+ currentLetter === ' ' &&
48
+ str[strIndex - 1] !== '\\' &&
49
+ currentString.length > 0) {
50
+ pieces.push(currentString);
51
+ currentString = '';
52
+ }
53
+ else {
54
+ currentString = `${currentString}${currentLetter}`;
55
+ }
56
+ }
57
+ if (currentString.length > 0) {
58
+ pieces.push(currentString);
59
+ }
60
+ return pieces;
61
+ }
62
+ export class ExternalEditor {
63
+ text = '';
64
+ tempFile;
65
+ editor;
66
+ lastExitStatus = 0;
67
+ fileOptions = {};
68
+ get temp_file() {
69
+ console.log('DEPRECATED: temp_file. Use tempFile moving forward.');
70
+ return this.tempFile;
71
+ }
72
+ get last_exit_status() {
73
+ console.log('DEPRECATED: last_exit_status. Use lastExitStatus moving forward.');
74
+ return this.lastExitStatus;
75
+ }
76
+ constructor(text = '', fileOptions) {
77
+ this.text = text;
78
+ if (fileOptions) {
79
+ this.fileOptions = fileOptions;
80
+ }
81
+ this.determineEditor();
82
+ this.createTemporaryFile();
83
+ }
84
+ run() {
85
+ this.launchEditor();
86
+ this.readTemporaryFile();
87
+ return this.text;
88
+ }
89
+ runAsync(callback) {
90
+ try {
91
+ this.launchEditorAsync(() => {
92
+ try {
93
+ this.readTemporaryFile();
94
+ setImmediate(callback, undefined, this.text);
95
+ }
96
+ catch (readError) {
97
+ setImmediate(callback, readError, undefined);
98
+ }
99
+ });
100
+ }
101
+ catch (launchError) {
102
+ setImmediate(callback, launchError, undefined);
103
+ }
104
+ }
105
+ cleanup() {
106
+ this.removeTemporaryFile();
107
+ }
108
+ determineEditor() {
109
+ const editor = process.env['VISUAL']
110
+ ? process.env['VISUAL']
111
+ : process.env['EDITOR']
112
+ ? process.env['EDITOR']
113
+ : process.platform.startsWith('win')
114
+ ? 'notepad'
115
+ : 'vim';
116
+ const editorOpts = splitStringBySpace(editor).map((piece) => piece.replace('\\ ', ' '));
117
+ const bin = editorOpts.shift();
118
+ this.editor = { args: editorOpts, bin };
119
+ }
120
+ createTemporaryFile() {
121
+ try {
122
+ const baseDir = this.fileOptions.dir ?? os.tmpdir();
123
+ const id = randomUUID();
124
+ const prefix = sanitizeAffix(this.fileOptions.prefix);
125
+ const postfix = sanitizeAffix(this.fileOptions.postfix);
126
+ const filename = `${prefix}${id}${postfix}`;
127
+ const candidate = path.resolve(baseDir, filename);
128
+ const baseResolved = path.resolve(baseDir) + path.sep;
129
+ if (!candidate.startsWith(baseResolved)) {
130
+ throw new Error('Resolved temporary file escaped the base directory');
131
+ }
132
+ this.tempFile = candidate;
133
+ const opt = { encoding: 'utf8', flag: 'wx' };
134
+ if (Object.prototype.hasOwnProperty.call(this.fileOptions, 'mode')) {
135
+ opt.mode = this.fileOptions.mode;
136
+ }
137
+ writeFileSync(this.tempFile, this.text, opt);
138
+ }
139
+ catch (createFileError) {
140
+ throw new CreateFileError(createFileError);
141
+ }
142
+ }
143
+ readTemporaryFile() {
144
+ try {
145
+ const tempFileBuffer = readFileSync(this.tempFile);
146
+ if (tempFileBuffer.length === 0) {
147
+ this.text = '';
148
+ }
149
+ else {
150
+ let encoding = detect(tempFileBuffer) ?? 'utf8';
151
+ if (!iconv.encodingExists(encoding)) {
152
+ // Probably a bad idea, but will at least prevent crashing
153
+ encoding = 'utf8';
154
+ }
155
+ this.text = iconv.decode(tempFileBuffer, encoding);
156
+ }
157
+ }
158
+ catch (readFileError) {
159
+ throw new ReadFileError(readFileError);
160
+ }
161
+ }
162
+ removeTemporaryFile() {
163
+ try {
164
+ unlinkSync(this.tempFile);
165
+ }
166
+ catch (removeFileError) {
167
+ throw new RemoveFileError(removeFileError);
168
+ }
169
+ }
170
+ launchEditor() {
171
+ try {
172
+ const editorProcess = spawnSync(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: 'inherit' });
173
+ this.lastExitStatus = editorProcess.status ?? 0;
174
+ }
175
+ catch (launchError) {
176
+ throw new LaunchEditorError(launchError);
177
+ }
178
+ }
179
+ launchEditorAsync(callback) {
180
+ try {
181
+ const editorProcess = spawn(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: 'inherit' });
182
+ editorProcess.on('exit', (code) => {
183
+ this.lastExitStatus = code;
184
+ setImmediate(callback);
185
+ });
186
+ }
187
+ catch (launchError) {
188
+ throw new LaunchEditorError(launchError);
189
+ }
190
+ }
191
+ }
package/package.json CHANGED
@@ -1,24 +1,12 @@
1
1
  {
2
2
  "name": "@inquirer/external-editor",
3
- "version": "1.0.2",
3
+ "version": "2.0.0",
4
4
  "description": "Edit a string with the users preferred text editor using $VISUAL or $ENVIRONMENT",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
8
- ".": {
9
- "import": {
10
- "types": "./dist/esm/index.d.ts",
11
- "default": "./dist/esm/index.js"
12
- },
13
- "require": {
14
- "types": "./dist/commonjs/index.d.ts",
15
- "default": "./dist/commonjs/index.js"
16
- }
17
- }
8
+ ".": "./src/index.ts"
18
9
  },
19
- "module": "./dist/esm/index.js",
20
- "main": "./dist/commonjs/index.js",
21
- "types": "./dist/commonjs/index.d.ts",
22
10
  "repository": {
23
11
  "type": "git",
24
12
  "url": "https://github.com/SBoudrias/Inquirer.js.git"
@@ -68,26 +56,33 @@
68
56
  "visual"
69
57
  ],
70
58
  "publishConfig": {
71
- "access": "public"
59
+ "access": "public",
60
+ "exports": {
61
+ "./package.json": "./package.json",
62
+ ".": {
63
+ "types": "./dist/index.d.ts",
64
+ "default": "./dist/index.js"
65
+ }
66
+ }
72
67
  },
73
68
  "files": [
74
69
  "dist"
75
70
  ],
76
71
  "engines": {
77
- "node": ">=18"
72
+ "node": ">=23.5.0 || ^22.13.0 || ^21.7.0 || ^20.12.0"
78
73
  },
79
74
  "author": "Simon Boudrias <admin@simonboudrias.com>",
80
75
  "license": "MIT",
81
76
  "sideEffects": false,
82
77
  "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/main/packages/external-editor/README.md",
83
78
  "dependencies": {
84
- "chardet": "^2.1.0",
79
+ "chardet": "^2.1.1",
85
80
  "iconv-lite": "^0.7.0"
86
81
  },
87
82
  "devDependencies": {
88
- "@arethetypeswrong/cli": "^0.18.2",
83
+ "@repo/tsconfig": "0.0.0",
89
84
  "@types/chardet": "^1.0.0",
90
- "tshy": "^3.0.2"
85
+ "typescript": "^5.9.3"
91
86
  },
92
87
  "peerDependencies": {
93
88
  "@types/node": ">=18"
@@ -97,18 +92,8 @@
97
92
  "optional": true
98
93
  }
99
94
  },
100
- "tshy": {
101
- "exclude": [
102
- "src/**/*.test.ts"
103
- ],
104
- "exports": {
105
- "./package.json": "./package.json",
106
- ".": "./src/index.ts"
107
- }
108
- },
109
95
  "scripts": {
110
- "tsc": "tshy",
111
- "attw": "attw --pack"
96
+ "tsc": "tsc"
112
97
  },
113
- "gitHead": "62045dc4a4cd6f31ba0f77b20874be2409570957"
98
+ "gitHead": "676685d33374a30340c1b9f0831c7eae2b2357dd"
114
99
  }