2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.execTool = exports.commandExists = exports.runGoTool = exports.goBinPath = exports.installGoBin = void 0;
4 const tslib_1 = require("tslib");
5 const path_1 = tslib_1.__importDefault(require("path"));
6 const fs_1 = tslib_1.__importDefault(require("fs"));
7 const util_1 = tslib_1.__importDefault(require("util"));
8 const child_process_1 = require("child_process");
9 const coc_nvim_1 = require("coc.nvim");
10 const which_1 = tslib_1.__importDefault(require("which"));
11 const config_1 = require("./config");
12 const runExec = util_1.default.promisify(child_process_1.exec);
13 const isWin = process.platform === 'win32';
14 ////////////////////////////////////////////////////////////////////////////////
15 async function installGoBin(source, force = false) {
16 const name = goBinName(source);
17 if (!force && await goBinExists(name)) {
20 const statusItem = coc_nvim_1.workspace.createStatusBarItem(90, { progress: true });
21 statusItem.text = `Installing '${name}'`;
23 const success = await goRun(`get ${source}@latest`) && await goBinExists(name);
25 coc_nvim_1.workspace.showMessage(`Installed '${name}'`);
28 coc_nvim_1.workspace.showMessage(`Failed to install '${name}'`, 'error');
33 exports.installGoBin = installGoBin;
34 async function goBinPath(source) {
35 const name = goBinName(source);
36 return path_1.default.join(await config_1.configDir('bin'), name + (isWin ? ".exe" : ""));
38 exports.goBinPath = goBinPath;
39 async function runGoTool(name, args = []) {
40 const bin = await goBinPath(name);
41 return new Promise((resolve) => {
42 const p = child_process_1.spawn(bin, args);
44 p.stdout.on('data', (data) => out += data);
45 p.on("close", code => resolve([code, out]));
48 exports.runGoTool = runGoTool;
49 async function commandExists(command) {
50 if (path_1.default.isAbsolute(command)) {
51 return fileExists(command);
53 return new Promise((resolve) => { which_1.default(command, (err) => resolve(err == null)); });
55 exports.commandExists = commandExists;
56 ////////////////////////////////////////////////////////////////////////////////
57 async function goBinExists(source) {
58 const name = goBinName(source);
59 const bin = await goBinPath(name);
60 return fileExists(bin);
62 async function fileExists(path) {
63 return new Promise((resolve) => fs_1.default.open(path, 'r', (err) => resolve(err === null)));
65 async function goRun(args) {
66 const gopath = await config_1.configDir('tools');
67 const gobin = await config_1.configDir('bin');
77 : `env GOBIN=${gobin} go ${args}`;
79 env: Object.assign({}, process.env, env),
81 shell: isWin ? undefined : process.env.SHELL,
85 await runExec(cmd, opts);
88 coc_nvim_1.workspace.showMessage(ex, 'error');
93 async function execTool(source, args, input) {
94 const [bin, name] = await Promise.all([
98 if (!await commandExists(bin)) {
99 await installGoBin(source);
101 return new Promise((resolve, reject) => {
102 const p = child_process_1.execFile(bin, args, { cwd: coc_nvim_1.workspace.cwd }, async (err, stdout, stderr) => {
103 if (err && err.code === "ENOENT") {
104 return reject(`Error: Command ${name} not found! Run "CocCommand go.install.${name}" to install it and try again.`);
107 return reject(stderr.toString());
109 return resolve(stdout.toString());
116 exports.execTool = execTool;
117 ////////////////////////////////////////////////////////////////////////////////
118 function goBinName(source) {
119 return source.replace(/\/\.\.\.$/, '').split('/').pop();
121 //# sourceMappingURL=tools.js.map