@convert-to-md/cli 0.2.1 → 0.2.2
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 +41 -24
package/package.json
CHANGED
package/scripts/install.js
CHANGED
|
@@ -3,7 +3,8 @@ import { createWriteStream, existsSync, mkdirSync } from 'node:fs';
|
|
|
3
3
|
import { chmod, rename, unlink } from 'node:fs/promises';
|
|
4
4
|
import { dirname, join } from 'node:path';
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
|
-
import
|
|
6
|
+
import https from 'node:https';
|
|
7
|
+
import http from 'node:http';
|
|
7
8
|
|
|
8
9
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
9
10
|
const packageRoot = join(__dirname, '..');
|
|
@@ -45,34 +46,50 @@ if (existsSync(destFile)) {
|
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
const tempFile = `${destFile}.tmp`;
|
|
48
|
-
const out = createWriteStream(tempFile, { mode: 0o755 });
|
|
49
49
|
|
|
50
50
|
console.log(`Downloading convert-to-md binary from ${downloadURL}`);
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
console.error(
|
|
52
|
+
function fetchWithRedirects(url, redirectCount = 0) {
|
|
53
|
+
if (redirectCount > 10) {
|
|
54
|
+
console.error('Too many redirects');
|
|
55
55
|
process.exit(1);
|
|
56
56
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
console.log('convert-to-md installed');
|
|
67
|
-
} catch (error) {
|
|
68
|
-
console.error(error.message);
|
|
57
|
+
const mod = url.startsWith('https') ? https : http;
|
|
58
|
+
mod.get(url, response => {
|
|
59
|
+
if (response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) {
|
|
60
|
+
response.resume();
|
|
61
|
+
fetchWithRedirects(response.headers.location, redirectCount + 1);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (response.statusCode !== 200) {
|
|
65
|
+
console.error(`Failed to download binary (status ${response.statusCode})`);
|
|
69
66
|
process.exit(1);
|
|
70
67
|
}
|
|
68
|
+
const out = createWriteStream(tempFile, { mode: 0o755 });
|
|
69
|
+
response.pipe(out);
|
|
70
|
+
out.on('finish', async () => {
|
|
71
|
+
out.close();
|
|
72
|
+
try {
|
|
73
|
+
if (process.platform !== 'win32') {
|
|
74
|
+
await chmod(tempFile, 0o755);
|
|
75
|
+
}
|
|
76
|
+
await rename(tempFile, destFile);
|
|
77
|
+
console.log('convert-to-md installed');
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error(error.message);
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
out.on('error', async error => {
|
|
84
|
+
console.error(`Write failed: ${error.message}`);
|
|
85
|
+
try { await unlink(tempFile); } catch {}
|
|
86
|
+
process.exit(1);
|
|
87
|
+
});
|
|
88
|
+
}).on('error', async error => {
|
|
89
|
+
console.error(`Download failed: ${error.message}`);
|
|
90
|
+
try { await unlink(tempFile); } catch {}
|
|
91
|
+
process.exit(1);
|
|
71
92
|
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
await unlink(tempFile);
|
|
76
|
-
} catch {}
|
|
77
|
-
process.exit(1);
|
|
78
|
-
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
fetchWithRedirects(downloadURL);
|