@cjser/inquirer__external-editor 3.0.0-cjser.2
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/LICENSE +22 -0
- package/README.md +159 -0
- package/dist/errors/CreateFileError.d.ts +10 -0
- package/dist/errors/CreateFileError.js +13 -0
- package/dist/errors/LaunchEditorError.d.ts +10 -0
- package/dist/errors/LaunchEditorError.js +13 -0
- package/dist/errors/ReadFileError.d.ts +10 -0
- package/dist/errors/ReadFileError.js +13 -0
- package/dist/errors/RemoveFileError.d.ts +10 -0
- package/dist/errors/RemoveFileError.js +13 -0
- package/dist/errors.d.ts +20 -0
- package/dist/errors.js +32 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +133 -0
- package/dist/parse-editor-command.d.ts +5 -0
- package/dist/parse-editor-command.js +28 -0
- package/dist-cjser/index.cjs +230 -0
- package/package.json +129 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2025 Simon Boudrias
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
|
4
|
+
obtaining a copy of this software and associated documentation
|
|
5
|
+
files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use,
|
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the
|
|
9
|
+
Software is furnished to do so, subject to the following
|
|
10
|
+
conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# `@inquirer/external-editor`
|
|
2
|
+
|
|
3
|
+
A Node.js module to edit a string with the user's preferred text editor using $VISUAL or $EDITOR.
|
|
4
|
+
|
|
5
|
+
> [!NOTE]
|
|
6
|
+
> This package is a replacement for the unmaintained `external-editor`. It includes security fixes.
|
|
7
|
+
|
|
8
|
+
# Installation
|
|
9
|
+
|
|
10
|
+
<table>
|
|
11
|
+
<tr>
|
|
12
|
+
<th>npm</th>
|
|
13
|
+
<th>yarn</th>
|
|
14
|
+
</tr>
|
|
15
|
+
<tr>
|
|
16
|
+
<td>
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install @inquirer/external-editor
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
</td>
|
|
23
|
+
<td>
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
yarn add @inquirer/external-editor
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
</td>
|
|
30
|
+
</tr>
|
|
31
|
+
</table>
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
A simple example using the `edit` function
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { edit } from '@inquirer/external-editor';
|
|
39
|
+
|
|
40
|
+
const data = edit('\n\n# Please write your text above');
|
|
41
|
+
console.log(data);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Example relying on the class construct
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import {
|
|
48
|
+
ExternalEditor,
|
|
49
|
+
CreateFileError,
|
|
50
|
+
ReadFileError,
|
|
51
|
+
RemoveFileError,
|
|
52
|
+
LaunchEditorError,
|
|
53
|
+
} from '@inquirer/external-editor';
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const editor = new ExternalEditor();
|
|
57
|
+
const text = editor.run(); // the text is also available in editor.text
|
|
58
|
+
|
|
59
|
+
if (editor.lastExitStatus !== 0) {
|
|
60
|
+
console.log('The editor exited with a non-zero code');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Do things with the text
|
|
64
|
+
editor.cleanup();
|
|
65
|
+
} catch (err) {
|
|
66
|
+
if (err instanceof CreateFileError) {
|
|
67
|
+
console.log('Failed to create the temporary file');
|
|
68
|
+
} else if (err instanceof ReadFileError) {
|
|
69
|
+
console.log('Failed to read the temporary file');
|
|
70
|
+
} else if (err instanceof LaunchEditorError) {
|
|
71
|
+
console.log('Failed to launch your editor');
|
|
72
|
+
} else if (err instanceof RemoveFileError) {
|
|
73
|
+
console.log('Failed to remove the temporary file');
|
|
74
|
+
} else {
|
|
75
|
+
throw err;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### API
|
|
81
|
+
|
|
82
|
+
**Convenience Functions**
|
|
83
|
+
|
|
84
|
+
- `edit(text, config)`
|
|
85
|
+
- `text` (string) _Optional_ Defaults to empty string
|
|
86
|
+
- `config` (Config) _Optional_ Options for temporary file creation
|
|
87
|
+
- **Returns** (string) The contents of the file
|
|
88
|
+
- Could throw `CreateFileError`, `ReadFileError`, or `LaunchEditorError`, or `RemoveFileError`
|
|
89
|
+
- `editAsync(text, callback, config)`
|
|
90
|
+
- `text` (string) _Optional_ Defaults to empty string
|
|
91
|
+
- `callback` (function (error?, text?))
|
|
92
|
+
- `error` could be of type `CreateFileError`, `ReadFileError`, `LaunchEditorError`, or `RemoveFileError`
|
|
93
|
+
- `text` (string) The contents of the file
|
|
94
|
+
- `config` (Config) _Optional_ Options for temporary file creation
|
|
95
|
+
|
|
96
|
+
**Errors**
|
|
97
|
+
|
|
98
|
+
- `CreateFileError` Error thrown if the temporary file could not be created.
|
|
99
|
+
- `ReadFileError` Error thrown if the temporary file could not be read.
|
|
100
|
+
- `RemoveFileError` Error thrown if the temporary file could not be removed during cleanup.
|
|
101
|
+
- `LaunchEditorError` Error thrown if the editor could not be launched.
|
|
102
|
+
|
|
103
|
+
**External Editor Public Methods**
|
|
104
|
+
|
|
105
|
+
- `new ExternalEditor(text, config)`
|
|
106
|
+
- `text` (string) _Optional_ Defaults to empty string
|
|
107
|
+
- `config` (Config) _Optional_ Options for temporary file creation
|
|
108
|
+
- Could throw `CreateFileError`
|
|
109
|
+
- `run()` Launches the editor.
|
|
110
|
+
- **Returns** (string) The contents of the file
|
|
111
|
+
- Could throw `LaunchEditorError` or `ReadFileError`
|
|
112
|
+
- `runAsync(callback)` Launches the editor in an async way
|
|
113
|
+
- `callback` (function (error?, text?))
|
|
114
|
+
- `error` could be of type `ReadFileError` or `LaunchEditorError`
|
|
115
|
+
- `text` (string) The contents of the file
|
|
116
|
+
- `cleanup()` Removes the temporary file.
|
|
117
|
+
- Could throw `RemoveFileError`
|
|
118
|
+
|
|
119
|
+
**External Editor Public Properties**
|
|
120
|
+
|
|
121
|
+
- `text` (string) _readonly_ The text in the temporary file.
|
|
122
|
+
- `editor.bin` (string) The editor determined from the environment.
|
|
123
|
+
- `editor.args` (array) Default arguments for the bin
|
|
124
|
+
- `tempFile` (string) Path to temporary file. Can be changed, but be careful as the temporary file probably already
|
|
125
|
+
exists and would need be removed manually.
|
|
126
|
+
- `lastExitStatus` (number) The last exit code emitted from the editor.
|
|
127
|
+
|
|
128
|
+
**Config Options**
|
|
129
|
+
|
|
130
|
+
- `prefix` (string) _Optional_ A prefix for the file name.
|
|
131
|
+
- `postfix` (string) _Optional_ A postfix for the file name. Useful if you want to provide an extension.
|
|
132
|
+
- `mode` (number) _Optional_ Which mode to create the file with. e.g. 644
|
|
133
|
+
- `dir` (string) _Optional_ Which path to store the file.
|
|
134
|
+
|
|
135
|
+
## Why Synchronous?
|
|
136
|
+
|
|
137
|
+
Everything is synchronous to make sure the editor has complete control of the stdin and stdout. Testing has shown
|
|
138
|
+
async launching of the editor can lead to issues when using readline or other packages which try to read from stdin or
|
|
139
|
+
write to stdout. Seeing as this will be used in an interactive CLI environment, I made the decision to force the package
|
|
140
|
+
to be synchronous. If you know a reliable way to force all stdin and stdout to be limited only to the child_process,
|
|
141
|
+
please submit a PR.
|
|
142
|
+
|
|
143
|
+
If async is really needed, you can use `editAsync` or `runAsync`. If you are using readline or have anything else
|
|
144
|
+
listening to the stdin or you write to stdout, you will most likely have problem, so make sure to remove any other
|
|
145
|
+
listeners on stdin, stdout, or stderr.
|
|
146
|
+
|
|
147
|
+
## Demo
|
|
148
|
+
|
|
149
|
+
[](https://asciinema.org/a/a1qh9lypbe65mj0ivfuoslz2s)
|
|
150
|
+
|
|
151
|
+
# License
|
|
152
|
+
|
|
153
|
+
Copyright (c) 2025 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart))<br/>
|
|
154
|
+
Licensed under the MIT license.
|
|
155
|
+
|
|
156
|
+
## cjser
|
|
157
|
+
|
|
158
|
+
This package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.
|
|
159
|
+
Original repository: https://github.com/SBoudrias/Inquirer.js
|
|
@@ -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,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,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,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
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -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
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
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 = {
|
|
5
|
+
prefix?: string;
|
|
6
|
+
postfix?: string;
|
|
7
|
+
mode?: number;
|
|
8
|
+
template?: string;
|
|
9
|
+
dir?: string;
|
|
10
|
+
};
|
|
11
|
+
/** @deprecated Use FileOptions */
|
|
12
|
+
export type IFileOptions = FileOptions;
|
|
13
|
+
export { CreateFileError, LaunchEditorError, ReadFileError, RemoveFileError };
|
|
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
|
+
export declare class ExternalEditor {
|
|
22
|
+
editor: EditorParams;
|
|
23
|
+
lastExitStatus: number;
|
|
24
|
+
private text;
|
|
25
|
+
private tempFile;
|
|
26
|
+
private fileOptions;
|
|
27
|
+
constructor(text?: string, fileOptions?: FileOptions);
|
|
28
|
+
run(): string;
|
|
29
|
+
runAsync(callback?: StringCallback): Promise<string>;
|
|
30
|
+
private cleanup;
|
|
31
|
+
private createTempFile;
|
|
32
|
+
private readTemporaryFile;
|
|
33
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { detect } from 'chardet';
|
|
2
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
3
|
+
import { readFileSync, unlinkSync, writeFileSync } from 'node: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, LaunchEditorError, ReadFileError, RemoveFileError, } from "./errors.js";
|
|
9
|
+
import { parseEditorCommand } from "./parse-editor-command.js";
|
|
10
|
+
export { CreateFileError, LaunchEditorError, ReadFileError, RemoveFileError };
|
|
11
|
+
export function edit(text = '', fileOptions) {
|
|
12
|
+
return new ExternalEditor(text, fileOptions).run();
|
|
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
|
+
};
|
|
19
|
+
function sanitizeAffix(affix) {
|
|
20
|
+
if (!affix)
|
|
21
|
+
return '';
|
|
22
|
+
return affix.replace(/[^a-zA-Z0-9_.-]/g, '_');
|
|
23
|
+
}
|
|
24
|
+
export class ExternalEditor {
|
|
25
|
+
editor;
|
|
26
|
+
lastExitStatus = 0;
|
|
27
|
+
text = '';
|
|
28
|
+
tempFile = '';
|
|
29
|
+
fileOptions = {};
|
|
30
|
+
constructor(text = '', fileOptions = {}) {
|
|
31
|
+
this.text = text;
|
|
32
|
+
this.fileOptions = fileOptions;
|
|
33
|
+
this.editor = parseEditorCommand(process.env['VISUAL'] ??
|
|
34
|
+
process.env['EDITOR'] ??
|
|
35
|
+
(process.platform.startsWith('win') ? 'notepad' : 'vim'));
|
|
36
|
+
}
|
|
37
|
+
run() {
|
|
38
|
+
this.createTempFile();
|
|
39
|
+
try {
|
|
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;
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
this.cleanup();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
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;
|
|
79
|
+
}
|
|
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
|
+
}
|
|
90
|
+
}
|
|
91
|
+
createTempFile() {
|
|
92
|
+
try {
|
|
93
|
+
const baseDir = this.fileOptions.dir ?? os.tmpdir();
|
|
94
|
+
const id = randomUUID();
|
|
95
|
+
const prefix = sanitizeAffix(this.fileOptions.prefix);
|
|
96
|
+
const postfix = sanitizeAffix(this.fileOptions.postfix);
|
|
97
|
+
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;
|
|
104
|
+
const opt = { encoding: 'utf8', flag: 'wx' };
|
|
105
|
+
if (Object.prototype.hasOwnProperty.call(this.fileOptions, 'mode')) {
|
|
106
|
+
opt.mode = this.fileOptions.mode;
|
|
107
|
+
}
|
|
108
|
+
writeFileSync(this.tempFile, this.text, opt);
|
|
109
|
+
}
|
|
110
|
+
catch (createFileError) {
|
|
111
|
+
throw new CreateFileError(createFileError);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
readTemporaryFile() {
|
|
115
|
+
try {
|
|
116
|
+
const tempFileBuffer = readFileSync(this.tempFile);
|
|
117
|
+
if (tempFileBuffer.length === 0) {
|
|
118
|
+
this.text = '';
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
let encoding = detect(tempFileBuffer) ?? 'utf8';
|
|
122
|
+
if (!iconv.encodingExists(encoding)) {
|
|
123
|
+
// Probably a bad idea, but will at least prevent crashing
|
|
124
|
+
encoding = 'utf8';
|
|
125
|
+
}
|
|
126
|
+
this.text = iconv.decode(tempFileBuffer, encoding);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch (readFileError) {
|
|
130
|
+
throw new ReadFileError(readFileError);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// packages/@cjser/inquirer__external-editor.tmp-26-1778156888407/dist/index.js
|
|
30
|
+
var index_exports = {};
|
|
31
|
+
__export(index_exports, {
|
|
32
|
+
CreateFileError: () => CreateFileError,
|
|
33
|
+
ExternalEditor: () => ExternalEditor,
|
|
34
|
+
LaunchEditorError: () => LaunchEditorError,
|
|
35
|
+
ReadFileError: () => ReadFileError,
|
|
36
|
+
RemoveFileError: () => RemoveFileError,
|
|
37
|
+
edit: () => edit,
|
|
38
|
+
editAsync: () => editAsync
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(index_exports);
|
|
41
|
+
var import_chardet = require("chardet");
|
|
42
|
+
var import_node_child_process = require("node:child_process");
|
|
43
|
+
var import_node_fs = require("node:fs");
|
|
44
|
+
var import_node_path = __toESM(require("node:path"), 1);
|
|
45
|
+
var import_node_os = __toESM(require("node:os"), 1);
|
|
46
|
+
var import_node_crypto = require("node:crypto");
|
|
47
|
+
var import_iconv_lite = __toESM(require("iconv-lite"), 1);
|
|
48
|
+
|
|
49
|
+
// packages/@cjser/inquirer__external-editor.tmp-26-1778156888407/dist/errors.js
|
|
50
|
+
var CreateFileError = class extends Error {
|
|
51
|
+
name = "CreateFileError";
|
|
52
|
+
originalError;
|
|
53
|
+
constructor(originalError) {
|
|
54
|
+
super(`Failed to create temporary file.${originalError instanceof Error ? ` ${originalError.message}` : ""}`, { cause: originalError });
|
|
55
|
+
this.originalError = originalError;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var LaunchEditorError = class extends Error {
|
|
59
|
+
name = "LaunchEditorError";
|
|
60
|
+
originalError;
|
|
61
|
+
constructor(originalError) {
|
|
62
|
+
super(`Failed to launch editor.${originalError instanceof Error ? ` ${originalError.message}` : ""}`, { cause: originalError });
|
|
63
|
+
this.originalError = originalError;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
var ReadFileError = class extends Error {
|
|
67
|
+
name = "ReadFileError";
|
|
68
|
+
originalError;
|
|
69
|
+
constructor(originalError) {
|
|
70
|
+
super(`Failed to read temporary file.${originalError instanceof Error ? ` ${originalError.message}` : ""}`, { cause: originalError });
|
|
71
|
+
this.originalError = originalError;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var RemoveFileError = class extends Error {
|
|
75
|
+
name = "RemoveFileError";
|
|
76
|
+
originalError;
|
|
77
|
+
constructor(originalError) {
|
|
78
|
+
super(`Failed to remove temporary file.${originalError instanceof Error ? ` ${originalError.message}` : ""}`, { cause: originalError });
|
|
79
|
+
this.originalError = originalError;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// packages/@cjser/inquirer__external-editor.tmp-26-1778156888407/dist/parse-editor-command.js
|
|
84
|
+
function parseEditorCommand(editor) {
|
|
85
|
+
let bin;
|
|
86
|
+
let rest;
|
|
87
|
+
if (editor.startsWith('"')) {
|
|
88
|
+
const closeQuote = editor.indexOf('"', 1);
|
|
89
|
+
if (closeQuote === -1) {
|
|
90
|
+
bin = editor.slice(1);
|
|
91
|
+
rest = "";
|
|
92
|
+
} else {
|
|
93
|
+
bin = editor.substring(1, closeQuote);
|
|
94
|
+
rest = editor.substring(closeQuote + 1).trim();
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
const firstSpace = editor.indexOf(" ");
|
|
98
|
+
if (firstSpace === -1) {
|
|
99
|
+
bin = editor;
|
|
100
|
+
rest = "";
|
|
101
|
+
} else {
|
|
102
|
+
bin = editor.substring(0, firstSpace);
|
|
103
|
+
rest = editor.substring(firstSpace + 1).trim();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { bin, args: rest ? rest.split(/\s+/) : [] };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// packages/@cjser/inquirer__external-editor.tmp-26-1778156888407/dist/index.js
|
|
110
|
+
function edit(text = "", fileOptions) {
|
|
111
|
+
return new ExternalEditor(text, fileOptions).run();
|
|
112
|
+
}
|
|
113
|
+
var editAsync = (text, callbackOrOptions, fileOptions) => {
|
|
114
|
+
const callback = typeof callbackOrOptions === "function" ? callbackOrOptions : void 0;
|
|
115
|
+
const options = typeof callbackOrOptions === "function" ? fileOptions : callbackOrOptions;
|
|
116
|
+
return new ExternalEditor(text, options).runAsync(callback);
|
|
117
|
+
};
|
|
118
|
+
function sanitizeAffix(affix) {
|
|
119
|
+
if (!affix)
|
|
120
|
+
return "";
|
|
121
|
+
return affix.replace(/[^a-zA-Z0-9_.-]/g, "_");
|
|
122
|
+
}
|
|
123
|
+
var ExternalEditor = class {
|
|
124
|
+
editor;
|
|
125
|
+
lastExitStatus = 0;
|
|
126
|
+
text = "";
|
|
127
|
+
tempFile = "";
|
|
128
|
+
fileOptions = {};
|
|
129
|
+
constructor(text = "", fileOptions = {}) {
|
|
130
|
+
this.text = text;
|
|
131
|
+
this.fileOptions = fileOptions;
|
|
132
|
+
this.editor = parseEditorCommand(process.env["VISUAL"] ?? process.env["EDITOR"] ?? (process.platform.startsWith("win") ? "notepad" : "vim"));
|
|
133
|
+
}
|
|
134
|
+
run() {
|
|
135
|
+
this.createTempFile();
|
|
136
|
+
try {
|
|
137
|
+
try {
|
|
138
|
+
const editorProcess = (0, import_node_child_process.spawnSync)(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: "inherit" });
|
|
139
|
+
this.lastExitStatus = editorProcess.status ?? 0;
|
|
140
|
+
} catch (launchError) {
|
|
141
|
+
throw new LaunchEditorError(launchError);
|
|
142
|
+
}
|
|
143
|
+
this.readTemporaryFile();
|
|
144
|
+
return this.text;
|
|
145
|
+
} finally {
|
|
146
|
+
this.cleanup();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
runAsync(callback) {
|
|
150
|
+
this.createTempFile();
|
|
151
|
+
const promise = new Promise((resolve, reject) => {
|
|
152
|
+
try {
|
|
153
|
+
const editorProcess = (0, import_node_child_process.spawn)(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: "inherit" });
|
|
154
|
+
editorProcess.on("exit", (code) => {
|
|
155
|
+
this.lastExitStatus = code;
|
|
156
|
+
resolve();
|
|
157
|
+
});
|
|
158
|
+
} catch (launchError) {
|
|
159
|
+
reject(new LaunchEditorError(launchError));
|
|
160
|
+
}
|
|
161
|
+
}).then(() => {
|
|
162
|
+
this.readTemporaryFile();
|
|
163
|
+
return this.text;
|
|
164
|
+
}).finally(() => {
|
|
165
|
+
this.cleanup();
|
|
166
|
+
});
|
|
167
|
+
if (callback) {
|
|
168
|
+
promise.then((text) => callback(void 0, text), (err) => callback(err instanceof Error ? err : new Error(String(err)), void 0));
|
|
169
|
+
}
|
|
170
|
+
return promise;
|
|
171
|
+
}
|
|
172
|
+
cleanup() {
|
|
173
|
+
if (!this.tempFile)
|
|
174
|
+
return;
|
|
175
|
+
try {
|
|
176
|
+
(0, import_node_fs.unlinkSync)(this.tempFile);
|
|
177
|
+
this.tempFile = "";
|
|
178
|
+
} catch (removeFileError) {
|
|
179
|
+
throw new RemoveFileError(removeFileError);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
createTempFile() {
|
|
183
|
+
try {
|
|
184
|
+
const baseDir = this.fileOptions.dir ?? import_node_os.default.tmpdir();
|
|
185
|
+
const id = (0, import_node_crypto.randomUUID)();
|
|
186
|
+
const prefix = sanitizeAffix(this.fileOptions.prefix);
|
|
187
|
+
const postfix = sanitizeAffix(this.fileOptions.postfix);
|
|
188
|
+
const filename = `${prefix}${id}${postfix}`;
|
|
189
|
+
const candidate = import_node_path.default.resolve(baseDir, filename);
|
|
190
|
+
const baseResolved = import_node_path.default.resolve(baseDir) + import_node_path.default.sep;
|
|
191
|
+
if (!candidate.startsWith(baseResolved)) {
|
|
192
|
+
throw new Error("Resolved temporary file escaped the base directory");
|
|
193
|
+
}
|
|
194
|
+
this.tempFile = candidate;
|
|
195
|
+
const opt = { encoding: "utf8", flag: "wx" };
|
|
196
|
+
if (Object.prototype.hasOwnProperty.call(this.fileOptions, "mode")) {
|
|
197
|
+
opt.mode = this.fileOptions.mode;
|
|
198
|
+
}
|
|
199
|
+
(0, import_node_fs.writeFileSync)(this.tempFile, this.text, opt);
|
|
200
|
+
} catch (createFileError) {
|
|
201
|
+
throw new CreateFileError(createFileError);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
readTemporaryFile() {
|
|
205
|
+
try {
|
|
206
|
+
const tempFileBuffer = (0, import_node_fs.readFileSync)(this.tempFile);
|
|
207
|
+
if (tempFileBuffer.length === 0) {
|
|
208
|
+
this.text = "";
|
|
209
|
+
} else {
|
|
210
|
+
let encoding = (0, import_chardet.detect)(tempFileBuffer) ?? "utf8";
|
|
211
|
+
if (!import_iconv_lite.default.encodingExists(encoding)) {
|
|
212
|
+
encoding = "utf8";
|
|
213
|
+
}
|
|
214
|
+
this.text = import_iconv_lite.default.decode(tempFileBuffer, encoding);
|
|
215
|
+
}
|
|
216
|
+
} catch (readFileError) {
|
|
217
|
+
throw new ReadFileError(readFileError);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
222
|
+
0 && (module.exports = {
|
|
223
|
+
CreateFileError,
|
|
224
|
+
ExternalEditor,
|
|
225
|
+
LaunchEditorError,
|
|
226
|
+
ReadFileError,
|
|
227
|
+
RemoveFileError,
|
|
228
|
+
edit,
|
|
229
|
+
editAsync
|
|
230
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cjser/inquirer__external-editor",
|
|
3
|
+
"version": "3.0.0-cjser.2",
|
|
4
|
+
"description": "Edit a string with the users preferred text editor using $VISUAL or $ENVIRONMENT",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"answer",
|
|
7
|
+
"answers",
|
|
8
|
+
"ask",
|
|
9
|
+
"base",
|
|
10
|
+
"cli",
|
|
11
|
+
"command",
|
|
12
|
+
"command-line",
|
|
13
|
+
"confirm",
|
|
14
|
+
"editor",
|
|
15
|
+
"enquirer",
|
|
16
|
+
"external",
|
|
17
|
+
"external-editor",
|
|
18
|
+
"generate",
|
|
19
|
+
"generator",
|
|
20
|
+
"hyper",
|
|
21
|
+
"input",
|
|
22
|
+
"inquire",
|
|
23
|
+
"inquirer",
|
|
24
|
+
"interface",
|
|
25
|
+
"iterm",
|
|
26
|
+
"javascript",
|
|
27
|
+
"menu",
|
|
28
|
+
"node",
|
|
29
|
+
"nodejs",
|
|
30
|
+
"prompt",
|
|
31
|
+
"promptly",
|
|
32
|
+
"prompts",
|
|
33
|
+
"question",
|
|
34
|
+
"readline",
|
|
35
|
+
"scaffold",
|
|
36
|
+
"scaffolder",
|
|
37
|
+
"scaffolding",
|
|
38
|
+
"stdin",
|
|
39
|
+
"stdout",
|
|
40
|
+
"terminal",
|
|
41
|
+
"tty",
|
|
42
|
+
"ui",
|
|
43
|
+
"user",
|
|
44
|
+
"visual",
|
|
45
|
+
"yeoman",
|
|
46
|
+
"yo",
|
|
47
|
+
"zsh"
|
|
48
|
+
],
|
|
49
|
+
"homepage": "https://github.com/SBoudrias/Inquirer.js/blob/main/packages/external-editor/README.md",
|
|
50
|
+
"license": "MIT",
|
|
51
|
+
"author": "Simon Boudrias <admin@simonboudrias.com>",
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "https://code.moenext.com/3rdeye/cjser.git"
|
|
55
|
+
},
|
|
56
|
+
"files": [
|
|
57
|
+
"dist",
|
|
58
|
+
"dist-cjser"
|
|
59
|
+
],
|
|
60
|
+
"type": "module",
|
|
61
|
+
"sideEffects": false,
|
|
62
|
+
"exports": {
|
|
63
|
+
".": {
|
|
64
|
+
"types": "./dist/index.d.ts",
|
|
65
|
+
"require": "./dist-cjser/index.cjs",
|
|
66
|
+
"default": "./dist/index.js"
|
|
67
|
+
},
|
|
68
|
+
"./package.json": "./package.json"
|
|
69
|
+
},
|
|
70
|
+
"publishConfig": {
|
|
71
|
+
"access": "public"
|
|
72
|
+
},
|
|
73
|
+
"scripts": {
|
|
74
|
+
"tsc": "tsc"
|
|
75
|
+
},
|
|
76
|
+
"dependencies": {
|
|
77
|
+
"chardet": "^2.1.1",
|
|
78
|
+
"iconv-lite": "^0.7.2"
|
|
79
|
+
},
|
|
80
|
+
"devDependencies": {
|
|
81
|
+
"@types/chardet": "^1.0.0",
|
|
82
|
+
"@types/node": "^25.5.2",
|
|
83
|
+
"typescript": "^6.0.2"
|
|
84
|
+
},
|
|
85
|
+
"peerDependencies": {
|
|
86
|
+
"@types/node": ">=18"
|
|
87
|
+
},
|
|
88
|
+
"peerDependenciesMeta": {
|
|
89
|
+
"@types/node": {
|
|
90
|
+
"optional": true
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
"engines": {
|
|
94
|
+
"node": ">=23.5.0 || ^22.13.0 || ^21.7.0 || ^20.12.0"
|
|
95
|
+
},
|
|
96
|
+
"main": "./dist-cjser/index.cjs",
|
|
97
|
+
"types": "./dist/index.d.ts",
|
|
98
|
+
"gitHead": "e68fe01d65359e083581c48c4a18cd8f97d88842",
|
|
99
|
+
"cjser": {
|
|
100
|
+
"sourceVersion": "3.0.0",
|
|
101
|
+
"cjserVersion": 2,
|
|
102
|
+
"original": {
|
|
103
|
+
"name": "@inquirer/external-editor",
|
|
104
|
+
"version": "3.0.0",
|
|
105
|
+
"main": "./dist/index.js",
|
|
106
|
+
"exports": {
|
|
107
|
+
".": {
|
|
108
|
+
"types": "./dist/index.d.ts",
|
|
109
|
+
"default": "./dist/index.js"
|
|
110
|
+
},
|
|
111
|
+
"./package.json": "./package.json"
|
|
112
|
+
},
|
|
113
|
+
"repository": {
|
|
114
|
+
"type": "git",
|
|
115
|
+
"url": "https://github.com/SBoudrias/Inquirer.js.git"
|
|
116
|
+
},
|
|
117
|
+
"dependencies": {
|
|
118
|
+
"chardet": "^2.1.1",
|
|
119
|
+
"iconv-lite": "^0.7.2"
|
|
120
|
+
},
|
|
121
|
+
"files": [
|
|
122
|
+
"dist"
|
|
123
|
+
],
|
|
124
|
+
"scripts": {
|
|
125
|
+
"tsc": "tsc"
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|