@inquirer/external-editor 2.0.4 → 3.0.1

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.
@@ -0,0 +1,20 @@
1
+ export declare class CreateFileError extends Error {
2
+ name: string;
3
+ originalError: unknown;
4
+ constructor(originalError: unknown);
5
+ }
6
+ export declare class LaunchEditorError extends Error {
7
+ name: string;
8
+ originalError: unknown;
9
+ constructor(originalError: unknown);
10
+ }
11
+ export declare class ReadFileError extends Error {
12
+ name: string;
13
+ originalError: unknown;
14
+ constructor(originalError: unknown);
15
+ }
16
+ export declare class RemoveFileError extends Error {
17
+ name: string;
18
+ originalError: unknown;
19
+ constructor(originalError: unknown);
20
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,32 @@
1
+ export class CreateFileError extends Error {
2
+ name = 'CreateFileError';
3
+ originalError;
4
+ constructor(originalError) {
5
+ super(`Failed to create temporary file.${originalError instanceof Error ? ` ${originalError.message}` : ''}`, { cause: originalError });
6
+ this.originalError = originalError;
7
+ }
8
+ }
9
+ export class LaunchEditorError extends Error {
10
+ name = 'LaunchEditorError';
11
+ originalError;
12
+ constructor(originalError) {
13
+ super(`Failed to launch editor.${originalError instanceof Error ? ` ${originalError.message}` : ''}`, { cause: originalError });
14
+ this.originalError = originalError;
15
+ }
16
+ }
17
+ export class ReadFileError extends Error {
18
+ name = 'ReadFileError';
19
+ originalError;
20
+ constructor(originalError) {
21
+ super(`Failed to read temporary file.${originalError instanceof Error ? ` ${originalError.message}` : ''}`, { cause: originalError });
22
+ this.originalError = originalError;
23
+ }
24
+ }
25
+ export class RemoveFileError extends Error {
26
+ name = 'RemoveFileError';
27
+ originalError;
28
+ constructor(originalError) {
29
+ super(`Failed to remove temporary file.${originalError instanceof Error ? ` ${originalError.message}` : ''}`, { cause: originalError });
30
+ this.originalError = originalError;
31
+ }
32
+ }
package/dist/index.d.ts CHANGED
@@ -1,39 +1,33 @@
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 {
1
+ import { CreateFileError, LaunchEditorError, ReadFileError, RemoveFileError } from './errors.ts';
2
+ import { type EditorParams } from './parse-editor-command.ts';
3
+ type StringCallback = (err: Error | undefined, result: string | undefined) => void;
4
+ export type FileOptions = {
10
5
  prefix?: string;
11
6
  postfix?: string;
12
7
  mode?: number;
13
8
  template?: string;
14
9
  dir?: string;
15
- }
16
- export type StringCallback = (err: Error | undefined, result: string | undefined) => void;
17
- export type VoidCallback = () => void;
10
+ };
11
+ /** @deprecated Use FileOptions */
12
+ export type IFileOptions = FileOptions;
18
13
  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;
14
+ export declare function edit(text?: string, fileOptions?: FileOptions): string;
15
+ type EditAsync = {
16
+ /** @deprecated Use editAsync(text, options) returning a Promise instead */
17
+ (text: string, callback: StringCallback, fileOptions?: FileOptions): Promise<string>;
18
+ (text?: string, fileOptions?: FileOptions): Promise<string>;
19
+ };
20
+ export declare const editAsync: EditAsync;
21
21
  export declare class ExternalEditor {
22
- text: string;
23
- tempFile: string;
24
- editor: IEditorParams;
22
+ editor: EditorParams;
25
23
  lastExitStatus: number;
24
+ private text;
25
+ private tempFile;
26
26
  private fileOptions;
27
- get temp_file(): string;
28
- get last_exit_status(): number;
29
- constructor(text?: string, fileOptions?: IFileOptions);
27
+ constructor(text?: string, fileOptions?: FileOptions);
30
28
  run(): string;
31
- runAsync(callback: StringCallback): void;
32
- cleanup(): void;
33
- private determineEditor;
34
- private createTemporaryFile;
29
+ runAsync(callback?: StringCallback): Promise<string>;
30
+ private cleanup;
31
+ private createTempFile;
35
32
  private readTemporaryFile;
36
- private removeTemporaryFile;
37
- private launchEditor;
38
- private launchEditorAsync;
39
33
  }
package/dist/index.js CHANGED
@@ -1,123 +1,94 @@
1
1
  import { detect } from 'chardet';
2
- import { spawn, spawnSync } from 'child_process';
3
- import { readFileSync, unlinkSync, writeFileSync } from 'fs';
2
+ import { spawn, spawnSync } from 'node:child_process';
3
+ import { readFileSync, unlinkSync, 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';
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, LaunchEditorError, ReadFileError, RemoveFileError, } from "./errors.js";
9
+ import { parseEditorCommand } from "./parse-editor-command.js";
12
10
  export { CreateFileError, LaunchEditorError, ReadFileError, RemoveFileError };
13
11
  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
- });
12
+ return new ExternalEditor(text, fileOptions).run();
35
13
  }
14
+ export const editAsync = (text, callbackOrOptions, fileOptions) => {
15
+ const callback = typeof callbackOrOptions === 'function' ? callbackOrOptions : undefined;
16
+ const options = typeof callbackOrOptions === 'function' ? fileOptions : callbackOrOptions;
17
+ return new ExternalEditor(text, options).runAsync(callback);
18
+ };
36
19
  function sanitizeAffix(affix) {
37
20
  if (!affix)
38
21
  return '';
39
22
  return affix.replace(/[^a-zA-Z0-9_.-]/g, '_');
40
23
  }
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
24
  export class ExternalEditor {
63
- text = '';
64
- tempFile;
65
25
  editor;
66
26
  lastExitStatus = 0;
27
+ text = '';
28
+ tempFile = '';
67
29
  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) {
30
+ constructor(text = '', fileOptions = {}) {
77
31
  this.text = text;
78
- if (fileOptions) {
79
- this.fileOptions = fileOptions;
80
- }
81
- this.determineEditor();
82
- this.createTemporaryFile();
32
+ this.fileOptions = fileOptions;
33
+ this.editor = parseEditorCommand(process.env['VISUAL'] ??
34
+ process.env['EDITOR'] ??
35
+ (process.platform.startsWith('win') ? 'notepad' : 'vim'));
83
36
  }
84
37
  run() {
85
- this.launchEditor();
86
- this.readTemporaryFile();
87
- return this.text;
88
- }
89
- runAsync(callback) {
38
+ this.createTempFile();
90
39
  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
- });
40
+ try {
41
+ const editorProcess = spawnSync(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: 'inherit' });
42
+ this.lastExitStatus = editorProcess.status ?? 0;
43
+ }
44
+ catch (launchError) {
45
+ throw new LaunchEditorError(launchError);
46
+ }
47
+ this.readTemporaryFile();
48
+ return this.text;
100
49
  }
101
- catch (launchError) {
102
- setImmediate(callback, launchError, undefined);
50
+ finally {
51
+ this.cleanup();
103
52
  }
104
53
  }
105
- cleanup() {
106
- this.removeTemporaryFile();
54
+ runAsync(callback) {
55
+ this.createTempFile();
56
+ const promise = new Promise((resolve, reject) => {
57
+ 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;
61
+ resolve();
62
+ });
63
+ }
64
+ catch (launchError) {
65
+ reject(new LaunchEditorError(launchError));
66
+ }
67
+ })
68
+ .then(() => {
69
+ this.readTemporaryFile();
70
+ return this.text;
71
+ })
72
+ .finally(() => {
73
+ this.cleanup();
74
+ });
75
+ if (callback) {
76
+ promise.then((text) => callback(undefined, text), (err) => callback(err instanceof Error ? err : new Error(String(err)), undefined));
77
+ }
78
+ return promise;
107
79
  }
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 };
80
+ cleanup() {
81
+ if (!this.tempFile)
82
+ return;
83
+ try {
84
+ unlinkSync(this.tempFile);
85
+ this.tempFile = '';
86
+ }
87
+ catch (removeFileError) {
88
+ throw new RemoveFileError(removeFileError);
89
+ }
119
90
  }
120
- createTemporaryFile() {
91
+ createTempFile() {
121
92
  try {
122
93
  const baseDir = this.fileOptions.dir ?? os.tmpdir();
123
94
  const id = randomUUID();
@@ -159,33 +130,4 @@ export class ExternalEditor {
159
130
  throw new ReadFileError(readFileError);
160
131
  }
161
132
  }
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
133
  }
@@ -0,0 +1,5 @@
1
+ export type EditorParams = {
2
+ args: string[];
3
+ bin: string;
4
+ };
5
+ export declare function parseEditorCommand(editor: string): EditorParams;
@@ -0,0 +1,28 @@
1
+ export function parseEditorCommand(editor) {
2
+ let bin;
3
+ let rest;
4
+ if (editor.startsWith('"')) {
5
+ const closeQuote = editor.indexOf('"', 1);
6
+ if (closeQuote === -1) {
7
+ // Unmatched quote — treat the whole string as the binary
8
+ bin = editor.slice(1);
9
+ rest = '';
10
+ }
11
+ else {
12
+ bin = editor.substring(1, closeQuote);
13
+ rest = editor.substring(closeQuote + 1).trim();
14
+ }
15
+ }
16
+ else {
17
+ const firstSpace = editor.indexOf(' ');
18
+ if (firstSpace === -1) {
19
+ bin = editor;
20
+ rest = '';
21
+ }
22
+ else {
23
+ bin = editor.substring(0, firstSpace);
24
+ rest = editor.substring(firstSpace + 1).trim();
25
+ }
26
+ }
27
+ return { bin, args: rest ? rest.split(/\s+/) : [] };
28
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/external-editor",
3
- "version": "2.0.4",
3
+ "version": "3.0.1",
4
4
  "description": "Edit a string with the users preferred text editor using $VISUAL or $ENVIRONMENT",
5
5
  "keywords": [
6
6
  "answer",
@@ -77,7 +77,8 @@
77
77
  },
78
78
  "devDependencies": {
79
79
  "@types/chardet": "^1.0.0",
80
- "typescript": "^5.9.3"
80
+ "@types/node": "^25.5.2",
81
+ "typescript": "^6.0.2"
81
82
  },
82
83
  "peerDependencies": {
83
84
  "@types/node": ">=18"
@@ -92,5 +93,5 @@
92
93
  },
93
94
  "main": "./dist/index.js",
94
95
  "types": "./dist/index.d.ts",
95
- "gitHead": "b218fcc4afe888a58957aa78c9a032f9bd2d60cb"
96
+ "gitHead": "5ca6d1101d5d3f8fb066cd5b389bccfdafbbe0c0"
96
97
  }