@inquirer/external-editor 1.0.3 → 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.
@@ -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
+ }
@@ -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.3",
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,13 +56,20 @@
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",
@@ -85,10 +80,9 @@
85
80
  "iconv-lite": "^0.7.0"
86
81
  },
87
82
  "devDependencies": {
88
- "@arethetypeswrong/cli": "^0.18.2",
89
83
  "@repo/tsconfig": "0.0.0",
90
84
  "@types/chardet": "^1.0.0",
91
- "tshy": "^3.0.3"
85
+ "typescript": "^5.9.3"
92
86
  },
93
87
  "peerDependencies": {
94
88
  "@types/node": ">=18"
@@ -98,18 +92,8 @@
98
92
  "optional": true
99
93
  }
100
94
  },
101
- "tshy": {
102
- "exclude": [
103
- "src/**/*.test.ts"
104
- ],
105
- "exports": {
106
- "./package.json": "./package.json",
107
- ".": "./src/index.ts"
108
- }
109
- },
110
95
  "scripts": {
111
- "tsc": "tshy",
112
- "attw": "attw --pack"
96
+ "tsc": "tsc"
113
97
  },
114
- "gitHead": "6881993e517e76fa891b72e1f5086fd11f7676ac"
98
+ "gitHead": "676685d33374a30340c1b9f0831c7eae2b2357dd"
115
99
  }