@eggplanty/mycli 0.1.22 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/scripts/install.js +85 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eggplanty/mycli",
3
- "version": "0.1.22",
3
+ "version": "0.1.23",
4
4
  "description": "A simple CLI demo built with Go",
5
5
  "bin": {
6
6
  "mycli": "scripts/run.js"
@@ -32,53 +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 url = `https://github.com/${REPO}/releases/download/v${VERSION}/${archiveName}`;
36
35
  const token = process.env.MYCLI_TOKEN || "";
37
36
  const binDir = path.join(__dirname, "..", "bin");
38
37
  const dest = path.join(binDir, NAME + (isWindows ? ".exe" : ""));
39
38
 
40
39
  fs.mkdirSync(binDir, { recursive: true });
41
40
 
42
- function download(url, destPath, useAuth = true) {
41
+ function httpGet(url, headers = {}) {
43
42
  return new Promise((resolve, reject) => {
44
43
  const parsed = new URL(url);
45
44
  const client = parsed.protocol === "https:" ? https : require("http");
46
- const headers = {};
47
- if (token && useAuth) {
48
- headers["Authorization"] = `token ${token}`;
49
- headers["Accept"] = "application/octet-stream";
50
- }
51
45
  client
52
- .get({ hostname: parsed.hostname, path: parsed.pathname + parsed.search, headers }, (res) => {
53
- if (res.statusCode === 302 || res.statusCode === 301) {
54
- // Do not send auth token to redirected hosts (e.g. S3)
55
- return download(res.headers.location, destPath, false).then(
56
- resolve,
57
- reject
58
- );
59
- }
60
- if (res.statusCode !== 200) {
61
- return reject(
62
- new Error(`Download failed with status ${res.statusCode}: ${url}`)
63
- );
64
- }
65
- const file = fs.createWriteStream(destPath);
66
- res.pipe(file);
67
- file.on("finish", () => {
68
- file.close();
69
- resolve();
70
- });
71
- })
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
+ )
72
54
  .on("error", reject);
73
55
  });
74
56
  }
75
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
+
76
125
  async function install() {
77
126
  const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "mycli-"));
78
127
  const archivePath = path.join(tmpDir, archiveName);
79
128
 
80
129
  try {
81
- await download(url, archivePath);
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);
82
139
 
83
140
  if (isWindows) {
84
141
  execSync(