@eggplanty/mycli 0.1.20 → 0.1.23
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 +1 -1
- package/scripts/install.js +88 -23
package/package.json
CHANGED
package/scripts/install.js
CHANGED
|
@@ -32,45 +32,110 @@ if (!platform || !arch) {
|
|
|
32
32
|
const isWindows = process.platform === "win32";
|
|
33
33
|
const ext = isWindows ? ".zip" : ".tar.gz";
|
|
34
34
|
const archiveName = `${NAME}-${VERSION}-${platform}-${arch}${ext}`;
|
|
35
|
-
const
|
|
35
|
+
const token = process.env.MYCLI_TOKEN || "";
|
|
36
36
|
const binDir = path.join(__dirname, "..", "bin");
|
|
37
37
|
const dest = path.join(binDir, NAME + (isWindows ? ".exe" : ""));
|
|
38
38
|
|
|
39
39
|
fs.mkdirSync(binDir, { recursive: true });
|
|
40
40
|
|
|
41
|
-
function
|
|
41
|
+
function httpGet(url, headers = {}) {
|
|
42
42
|
return new Promise((resolve, reject) => {
|
|
43
|
-
const
|
|
43
|
+
const parsed = new URL(url);
|
|
44
|
+
const client = parsed.protocol === "https:" ? https : require("http");
|
|
44
45
|
client
|
|
45
|
-
.get(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return reject(
|
|
54
|
-
new Error(`Download failed with status ${res.statusCode}: ${url}`)
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
const file = fs.createWriteStream(destPath);
|
|
58
|
-
res.pipe(file);
|
|
59
|
-
file.on("finish", () => {
|
|
60
|
-
file.close();
|
|
61
|
-
resolve();
|
|
62
|
-
});
|
|
63
|
-
})
|
|
46
|
+
.get(
|
|
47
|
+
{
|
|
48
|
+
hostname: parsed.hostname,
|
|
49
|
+
path: parsed.pathname + parsed.search,
|
|
50
|
+
headers: { "User-Agent": "mycli-installer", ...headers },
|
|
51
|
+
},
|
|
52
|
+
(res) => resolve(res)
|
|
53
|
+
)
|
|
64
54
|
.on("error", reject);
|
|
65
55
|
});
|
|
66
56
|
}
|
|
67
57
|
|
|
58
|
+
function downloadToFile(url, destPath, headers = {}) {
|
|
59
|
+
return new Promise(async (resolve, reject) => {
|
|
60
|
+
try {
|
|
61
|
+
const res = await httpGet(url, headers);
|
|
62
|
+
if (res.statusCode === 302 || res.statusCode === 301) {
|
|
63
|
+
// Do not send auth token to redirected hosts (e.g. S3)
|
|
64
|
+
return downloadToFile(res.headers.location, destPath).then(
|
|
65
|
+
resolve,
|
|
66
|
+
reject
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
if (res.statusCode !== 200) {
|
|
70
|
+
return reject(
|
|
71
|
+
new Error(`Download failed with status ${res.statusCode}: ${url}`)
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
const file = fs.createWriteStream(destPath);
|
|
75
|
+
res.pipe(file);
|
|
76
|
+
file.on("finish", () => {
|
|
77
|
+
file.close();
|
|
78
|
+
resolve();
|
|
79
|
+
});
|
|
80
|
+
} catch (err) {
|
|
81
|
+
reject(err);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function getAssetDownloadUrl() {
|
|
87
|
+
if (!token) {
|
|
88
|
+
// No token, use public download URL directly
|
|
89
|
+
return `https://github.com/${REPO}/releases/download/v${VERSION}/${archiveName}`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// For private repos, use GitHub API to find the asset and get its download URL
|
|
93
|
+
const apiUrl = `https://api.github.com/repos/${REPO}/releases/tags/v${VERSION}`;
|
|
94
|
+
const res = await httpGet(apiUrl, {
|
|
95
|
+
Authorization: `token ${token}`,
|
|
96
|
+
Accept: "application/vnd.github.v3+json",
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (res.statusCode !== 200) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
`Failed to fetch release info (status ${res.statusCode}). Check that the token is valid and the release v${VERSION} exists.`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const body = await new Promise((resolve, reject) => {
|
|
106
|
+
let data = "";
|
|
107
|
+
res.on("data", (chunk) => (data += chunk));
|
|
108
|
+
res.on("end", () => resolve(data));
|
|
109
|
+
res.on("error", reject);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const release = JSON.parse(body);
|
|
113
|
+
const asset = release.assets.find((a) => a.name === archiveName);
|
|
114
|
+
if (!asset) {
|
|
115
|
+
const available = release.assets.map((a) => a.name).join(", ");
|
|
116
|
+
throw new Error(
|
|
117
|
+
`Asset "${archiveName}" not found in release v${VERSION}. Available assets: ${available || "(none)"}`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Return the API URL for downloading the asset
|
|
122
|
+
return asset.url;
|
|
123
|
+
}
|
|
124
|
+
|
|
68
125
|
async function install() {
|
|
69
126
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "mycli-"));
|
|
70
127
|
const archivePath = path.join(tmpDir, archiveName);
|
|
71
128
|
|
|
72
129
|
try {
|
|
73
|
-
await
|
|
130
|
+
const downloadUrl = await getAssetDownloadUrl();
|
|
131
|
+
|
|
132
|
+
const headers = {};
|
|
133
|
+
if (token && downloadUrl.includes("api.github.com")) {
|
|
134
|
+
headers["Authorization"] = `token ${token}`;
|
|
135
|
+
headers["Accept"] = "application/octet-stream";
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
await downloadToFile(downloadUrl, archivePath, headers);
|
|
74
139
|
|
|
75
140
|
if (isWindows) {
|
|
76
141
|
execSync(
|