@inquirer/external-editor 2.0.0 → 2.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.
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@inquirer/external-editor",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
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
- ".": "./src/index.ts"
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ }
9
12
  },
10
13
  "repository": {
11
14
  "type": "git",
@@ -56,14 +59,7 @@
56
59
  "visual"
57
60
  ],
58
61
  "publishConfig": {
59
- "access": "public",
60
- "exports": {
61
- "./package.json": "./package.json",
62
- ".": {
63
- "types": "./dist/index.d.ts",
64
- "default": "./dist/index.js"
65
- }
66
- }
62
+ "access": "public"
67
63
  },
68
64
  "files": [
69
65
  "dist"
@@ -80,7 +76,6 @@
80
76
  "iconv-lite": "^0.7.0"
81
77
  },
82
78
  "devDependencies": {
83
- "@repo/tsconfig": "0.0.0",
84
79
  "@types/chardet": "^1.0.0",
85
80
  "typescript": "^5.9.3"
86
81
  },
@@ -95,5 +90,5 @@
95
90
  "scripts": {
96
91
  "tsc": "tsc"
97
92
  },
98
- "gitHead": "676685d33374a30340c1b9f0831c7eae2b2357dd"
93
+ "gitHead": "cce79ce3b9bbdfb4dbb798078cf3b94b9adc7d1b"
99
94
  }
@@ -1,10 +0,0 @@
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
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- /***
3
- * Node External Editor
4
- *
5
- * Kevin Gravier <kevin@mrkmg.com>
6
- * MIT 2018
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.CreateFileError = void 0;
10
- class CreateFileError extends Error {
11
- originalError;
12
- constructor(originalError) {
13
- super(`Failed to create temporary file. ${originalError.message}`);
14
- this.originalError = originalError;
15
- }
16
- }
17
- exports.CreateFileError = CreateFileError;
@@ -1,10 +0,0 @@
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
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- /***
3
- * Node External Editor
4
- *
5
- * Kevin Gravier <kevin@mrkmg.com>
6
- * MIT 2018
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.LaunchEditorError = void 0;
10
- class LaunchEditorError extends Error {
11
- originalError;
12
- constructor(originalError) {
13
- super(`Failed to launch editor. ${originalError.message}`);
14
- this.originalError = originalError;
15
- }
16
- }
17
- exports.LaunchEditorError = LaunchEditorError;
@@ -1,10 +0,0 @@
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
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- /***
3
- * Node External Editor
4
- *
5
- * Kevin Gravier <kevin@mrkmg.com>
6
- * MIT 2018
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.ReadFileError = void 0;
10
- class ReadFileError extends Error {
11
- originalError;
12
- constructor(originalError) {
13
- super(`Failed to read temporary file. ${originalError.message}`);
14
- this.originalError = originalError;
15
- }
16
- }
17
- exports.ReadFileError = ReadFileError;
@@ -1,10 +0,0 @@
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
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- /***
3
- * Node External Editor
4
- *
5
- * Kevin Gravier <kevin@mrkmg.com>
6
- * MIT 2018
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.RemoveFileError = void 0;
10
- class RemoveFileError extends Error {
11
- originalError;
12
- constructor(originalError) {
13
- super(`Failed to remove temporary file. ${originalError.message}`);
14
- this.originalError = originalError;
15
- }
16
- }
17
- exports.RemoveFileError = RemoveFileError;
@@ -1,39 +0,0 @@
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
- }
@@ -1,203 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ExternalEditor = exports.RemoveFileError = exports.ReadFileError = exports.LaunchEditorError = exports.CreateFileError = void 0;
7
- exports.edit = edit;
8
- exports.editAsync = editAsync;
9
- const chardet_1 = require("chardet");
10
- const child_process_1 = require("child_process");
11
- const fs_1 = require("fs");
12
- const node_path_1 = __importDefault(require("node:path"));
13
- const node_os_1 = __importDefault(require("node:os"));
14
- const node_crypto_1 = require("node:crypto");
15
- const iconv_lite_1 = __importDefault(require("iconv-lite"));
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
- function edit(text = '', fileOptions) {
25
- const editor = new ExternalEditor(text, fileOptions);
26
- editor.run();
27
- editor.cleanup();
28
- return editor.text;
29
- }
30
- function editAsync(text = '', callback, fileOptions) {
31
- const editor = new ExternalEditor(text, fileOptions);
32
- editor.runAsync((err, result) => {
33
- if (err) {
34
- setImmediate(callback, err, undefined);
35
- }
36
- else {
37
- try {
38
- editor.cleanup();
39
- setImmediate(callback, undefined, result);
40
- }
41
- catch (cleanupError) {
42
- setImmediate(callback, cleanupError, undefined);
43
- }
44
- }
45
- });
46
- }
47
- function sanitizeAffix(affix) {
48
- if (!affix)
49
- return '';
50
- return affix.replace(/[^a-zA-Z0-9_.-]/g, '_');
51
- }
52
- function splitStringBySpace(str) {
53
- const pieces = [];
54
- let currentString = '';
55
- for (let strIndex = 0; strIndex < str.length; strIndex++) {
56
- const currentLetter = str.charAt(strIndex);
57
- if (strIndex > 0 &&
58
- currentLetter === ' ' &&
59
- str[strIndex - 1] !== '\\' &&
60
- currentString.length > 0) {
61
- pieces.push(currentString);
62
- currentString = '';
63
- }
64
- else {
65
- currentString = `${currentString}${currentLetter}`;
66
- }
67
- }
68
- if (currentString.length > 0) {
69
- pieces.push(currentString);
70
- }
71
- return pieces;
72
- }
73
- class ExternalEditor {
74
- text = '';
75
- tempFile;
76
- editor;
77
- lastExitStatus = 0;
78
- fileOptions = {};
79
- get temp_file() {
80
- console.log('DEPRECATED: temp_file. Use tempFile moving forward.');
81
- return this.tempFile;
82
- }
83
- get last_exit_status() {
84
- console.log('DEPRECATED: last_exit_status. Use lastExitStatus moving forward.');
85
- return this.lastExitStatus;
86
- }
87
- constructor(text = '', fileOptions) {
88
- this.text = text;
89
- if (fileOptions) {
90
- this.fileOptions = fileOptions;
91
- }
92
- this.determineEditor();
93
- this.createTemporaryFile();
94
- }
95
- run() {
96
- this.launchEditor();
97
- this.readTemporaryFile();
98
- return this.text;
99
- }
100
- runAsync(callback) {
101
- try {
102
- this.launchEditorAsync(() => {
103
- try {
104
- this.readTemporaryFile();
105
- setImmediate(callback, undefined, this.text);
106
- }
107
- catch (readError) {
108
- setImmediate(callback, readError, undefined);
109
- }
110
- });
111
- }
112
- catch (launchError) {
113
- setImmediate(callback, launchError, undefined);
114
- }
115
- }
116
- cleanup() {
117
- this.removeTemporaryFile();
118
- }
119
- determineEditor() {
120
- const editor = process.env['VISUAL']
121
- ? process.env['VISUAL']
122
- : process.env['EDITOR']
123
- ? process.env['EDITOR']
124
- : process.platform.startsWith('win')
125
- ? 'notepad'
126
- : 'vim';
127
- const editorOpts = splitStringBySpace(editor).map((piece) => piece.replace('\\ ', ' '));
128
- const bin = editorOpts.shift();
129
- this.editor = { args: editorOpts, bin };
130
- }
131
- createTemporaryFile() {
132
- try {
133
- const baseDir = this.fileOptions.dir ?? node_os_1.default.tmpdir();
134
- const id = (0, node_crypto_1.randomUUID)();
135
- const prefix = sanitizeAffix(this.fileOptions.prefix);
136
- const postfix = sanitizeAffix(this.fileOptions.postfix);
137
- const filename = `${prefix}${id}${postfix}`;
138
- const candidate = node_path_1.default.resolve(baseDir, filename);
139
- const baseResolved = node_path_1.default.resolve(baseDir) + node_path_1.default.sep;
140
- if (!candidate.startsWith(baseResolved)) {
141
- throw new Error('Resolved temporary file escaped the base directory');
142
- }
143
- this.tempFile = candidate;
144
- const opt = { encoding: 'utf8', flag: 'wx' };
145
- if (Object.prototype.hasOwnProperty.call(this.fileOptions, 'mode')) {
146
- opt.mode = this.fileOptions.mode;
147
- }
148
- (0, fs_1.writeFileSync)(this.tempFile, this.text, opt);
149
- }
150
- catch (createFileError) {
151
- throw new CreateFileError_ts_1.CreateFileError(createFileError);
152
- }
153
- }
154
- readTemporaryFile() {
155
- try {
156
- const tempFileBuffer = (0, fs_1.readFileSync)(this.tempFile);
157
- if (tempFileBuffer.length === 0) {
158
- this.text = '';
159
- }
160
- else {
161
- let encoding = (0, chardet_1.detect)(tempFileBuffer) ?? 'utf8';
162
- if (!iconv_lite_1.default.encodingExists(encoding)) {
163
- // Probably a bad idea, but will at least prevent crashing
164
- encoding = 'utf8';
165
- }
166
- this.text = iconv_lite_1.default.decode(tempFileBuffer, encoding);
167
- }
168
- }
169
- catch (readFileError) {
170
- throw new ReadFileError_ts_1.ReadFileError(readFileError);
171
- }
172
- }
173
- removeTemporaryFile() {
174
- try {
175
- (0, fs_1.unlinkSync)(this.tempFile);
176
- }
177
- catch (removeFileError) {
178
- throw new RemoveFileError_ts_1.RemoveFileError(removeFileError);
179
- }
180
- }
181
- launchEditor() {
182
- try {
183
- const editorProcess = (0, child_process_1.spawnSync)(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: 'inherit' });
184
- this.lastExitStatus = editorProcess.status ?? 0;
185
- }
186
- catch (launchError) {
187
- throw new LaunchEditorError_ts_1.LaunchEditorError(launchError);
188
- }
189
- }
190
- launchEditorAsync(callback) {
191
- try {
192
- const editorProcess = (0, child_process_1.spawn)(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: 'inherit' });
193
- editorProcess.on('exit', (code) => {
194
- this.lastExitStatus = code;
195
- setImmediate(callback);
196
- });
197
- }
198
- catch (launchError) {
199
- throw new LaunchEditorError_ts_1.LaunchEditorError(launchError);
200
- }
201
- }
202
- }
203
- exports.ExternalEditor = ExternalEditor;
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,13 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,13 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,13 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,13 +0,0 @@
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,39 +0,0 @@
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/esm/index.js DELETED
@@ -1,191 +0,0 @@
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
- }
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }