Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-go / lib / utils / tools.js
1 "use strict";
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)) {
18         return true;
19     }
20     const statusItem = coc_nvim_1.workspace.createStatusBarItem(90, { progress: true });
21     statusItem.text = `Installing '${name}'`;
22     statusItem.show();
23     const success = await goRun(`get ${source}@latest`) && await goBinExists(name);
24     if (success) {
25         coc_nvim_1.workspace.showMessage(`Installed '${name}'`);
26     }
27     else {
28         coc_nvim_1.workspace.showMessage(`Failed to install '${name}'`, 'error');
29     }
30     statusItem.hide();
31     return success;
32 }
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" : ""));
37 }
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);
43         let out = "";
44         p.stdout.on('data', (data) => out += data);
45         p.on("close", code => resolve([code, out]));
46     });
47 }
48 exports.runGoTool = runGoTool;
49 async function commandExists(command) {
50     if (path_1.default.isAbsolute(command)) {
51         return fileExists(command);
52     }
53     return new Promise((resolve) => { which_1.default(command, (err) => resolve(err == null)); });
54 }
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);
61 }
62 async function fileExists(path) {
63     return new Promise((resolve) => fs_1.default.open(path, 'r', (err) => resolve(err === null)));
64 }
65 async function goRun(args) {
66     const gopath = await config_1.configDir('tools');
67     const gobin = await config_1.configDir('bin');
68     const env = {
69         GO111MODULE: 'on',
70         GOBIN: gobin,
71         GOPATH: gopath,
72         GOROOT: '',
73         GOTOOLDIR: '',
74     };
75     const cmd = isWin
76         ? `go ${args}`
77         : `env GOBIN=${gobin} go ${args}`;
78     const opts = {
79         env: Object.assign({}, process.env, env),
80         cwd: gopath,
81         shell: isWin ? undefined : process.env.SHELL,
82         windowsHide: true,
83     };
84     try {
85         await runExec(cmd, opts);
86     }
87     catch (ex) {
88         coc_nvim_1.workspace.showMessage(ex, 'error');
89         return false;
90     }
91     return true;
92 }
93 async function execTool(source, args, input) {
94     const [bin, name] = await Promise.all([
95         goBinPath(source),
96         goBinName(source),
97     ]);
98     if (!await commandExists(bin)) {
99         await installGoBin(source);
100     }
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.`);
105             }
106             if (err) {
107                 return reject(stderr.toString());
108             }
109             return resolve(stdout.toString());
110         });
111         if (p.pid) {
112             p.stdin.end(input);
113         }
114     });
115 }
116 exports.execTool = execTool;
117 ////////////////////////////////////////////////////////////////////////////////
118 function goBinName(source) {
119     return source.replace(/\/\.\.\.$/, '').split('/').pop();
120 }
121 //# sourceMappingURL=tools.js.map