You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.4 KiB
71 lines
2.4 KiB
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const childProcess = require("child_process");
|
|
const path = require("path");
|
|
const enum_1 = require("./enum");
|
|
const error_1 = require("./error");
|
|
const utils_1 = require("./utils");
|
|
const getArgs_1 = require("./getArgs");
|
|
// function isTerminalEditor (editor) {
|
|
// switch (editor) {
|
|
// case 'vim':
|
|
// case 'emacs':
|
|
// case 'nano':
|
|
// return true
|
|
// }
|
|
// return false
|
|
// }
|
|
const openEditor = async ({ name, commands, args = [], fileName, lineNumber, colNumber, }) => {
|
|
return new Promise((resolve, reject) => {
|
|
if (!commands.length) {
|
|
const error = new error_1.default({
|
|
editor: name,
|
|
code: enum_1.ERROR_CODE.UNKNOWN,
|
|
}, 'no editor avalibe');
|
|
reject(error);
|
|
}
|
|
if (utils_1.isWSL(fileName)) {
|
|
fileName = path.relative('', fileName);
|
|
}
|
|
if (lineNumber) {
|
|
const extraArgs = getArgs_1.default(name, fileName, lineNumber, colNumber);
|
|
args = args.concat(extraArgs);
|
|
}
|
|
else {
|
|
args.push(fileName);
|
|
}
|
|
utils_1.log('openEditor:commands', commands);
|
|
/* eslint-disable no-restricted-syntax */
|
|
for (const command of commands) {
|
|
try {
|
|
let _childProcess = null;
|
|
if (utils_1.getOS() === 'windows') {
|
|
// On Windows, launch the editor in a shell because spawn can only
|
|
// launch .exe files.
|
|
_childProcess = childProcess.spawnSync('cmd.exe', ['/C', command].concat(args), { stdio: 'inherit' });
|
|
}
|
|
else {
|
|
_childProcess = childProcess.spawnSync(command, args, { stdio: 'inherit' });
|
|
}
|
|
if (_childProcess
|
|
&& _childProcess.status !== null) {
|
|
resolve({
|
|
success: true,
|
|
editorBin: command,
|
|
message: '成功打开编辑器',
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
catch (e) { }
|
|
}
|
|
/* eslint-enable */
|
|
const error = new error_1.default({
|
|
success: false,
|
|
editor: name,
|
|
}, '不能打开编辑器');
|
|
reject(error);
|
|
});
|
|
};
|
|
exports.default = openEditor;
|