-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathbinary.js
147 lines (125 loc) · 3.42 KB
/
binary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Based off https://github.com/cloudflare/binary-install
// Licensed under MIT
const os = require("os");
const axios = require("axios");
const unzip = require("unzipper");
const rimraf = require("rimraf");
const { ProxyAgent } = require('proxy-agent');
const { join } = require("path");
const { existsSync, mkdirSync, createWriteStream } = require("fs");
const { spawnSync } = require("child_process");
const { version: VERSION, repository: REPOSITORY } = require("./package.json");
const SUPPORTED_PLATFORMS = [
{
platform: "win32",
arch: "x64",
name: "stylua-windows-x86_64",
},
{
platform: "darwin",
arch: "x64",
name: "stylua-macos-x86_64",
},
{
platform: "darwin",
arch: "arm64",
name: "stylua-macos-aarch64",
},
{
platform: "linux",
arch: "x64",
name: "stylua-linux-x86_64",
},
{
platform: "linux",
arch: "arm64",
name: "stylua-linux-aarch64",
},
];
const error = (msg) => {
console.error(msg);
process.exit(1);
};
const downloadArtifact = (url, location) => {
const agent = new ProxyAgent();
return new Promise((resolve, reject) => {
axios
// proxy: false is needed, otherwise axios is trying to overwrite the agent
// See https://github.com/axios/axios/issues/4531
.create({ proxy: false, httpAgent: agent, httpsAgent: agent })
.get(url, { responseType: "stream" })
.then((res) => res.data.pipe(unzip.Parse()))
.then((stream) => {
stream.on("entry", (entry) => {
// if (entry.path !== outputFilename) {
// entry.autodrain();
// return;
// }
entry.pipe(location).on("finish", resolve).on("error", reject);
});
})
.catch(reject);
});
};
const getDownloadUrl = () => {
const platform = os.platform();
const arch = os.arch();
const supportedInfo = SUPPORTED_PLATFORMS.find(
(info) => info.platform === platform && info.arch === arch
);
if (!supportedInfo) {
error(`Your platform [${platform} ${arch}] is currently unsupported.`);
}
return `${REPOSITORY.url}/releases/download/v${VERSION}/${supportedInfo.name}.zip`;
};
const getInstallDirectory = () => {
const path = join(__dirname, "bin");
if (!existsSync(path)) {
mkdirSync(path, { recursive: true });
}
return path;
};
const getBinaryPath = () => {
const dir = getInstallDirectory();
if (os.platform() === "win32") {
return join(dir, "stylua.exe");
} else {
return join(dir, "stylua");
}
};
const install = () => {
const url = getDownloadUrl();
console.log(`Downloading release from ${url}`);
if (existsSync(getBinaryPath())) {
rimraf.sync(getBinaryPath());
}
const location = createWriteStream(getBinaryPath(), {
mode: 0o755,
});
return downloadArtifact(url, location)
.then(() => console.log("StyLua has been installed!"))
.catch((e) => {
error(`Error fetching release: ${e.message}`);
});
};
const uninstall = () => {
if (existsSync(getInstallDirectory())) {
rimraf.sync(getInstallDirectory());
}
console.log("StyLua has been uninstalled");
};
const run = () => {
const binaryPath = getBinaryPath();
const [, , ...args] = process.argv;
const options = { cwd: process.cwd(), stdio: "inherit" };
const result = spawnSync(binaryPath, args, options);
if (result.error) {
error(result.error);
}
process.exit(result.status);
};
module.exports = {
install,
uninstall,
run,
};