|
1 | | -const process = require('process'); |
2 | | -const { spawn } = require('node:child_process'); |
3 | | -const path = require('node:path'); |
4 | | -const fs = require('node:fs'); |
5 | | -const unzip = require('extract-zip'); |
6 | | -const chalk = require('chalk'); |
7 | | -const { execSync } = require('child_process'); |
8 | | -const { createSpinner } = require('nanospinner'); |
9 | | -const { default: axios } = require('axios'); |
10 | | -const { exit } = require('process'); |
11 | | - |
12 | | -const assetPath = path.join(__dirname, '../..', 'assets'); |
| 1 | +import process from 'process'; |
| 2 | +import { spawn } from 'node:child_process'; |
| 3 | +import path from 'node:path'; |
| 4 | +import fs from 'node:fs'; |
| 5 | +import { Readable } from 'node:stream'; |
| 6 | +import { pipeline } from 'node:stream/promises'; |
| 7 | +import unzip from 'extract-zip'; |
| 8 | +import chalk from 'chalk'; |
| 9 | +import { execSync } from 'child_process'; |
| 10 | +import { createSpinner } from 'nanospinner'; |
| 11 | +import { exit } from 'process'; |
| 12 | +import { fileURLToPath } from 'node:url'; |
| 13 | + |
| 14 | +const assetPath = path.join( |
| 15 | + path.dirname(fileURLToPath(import.meta.url)), |
| 16 | + '../..', |
| 17 | + 'assets', |
| 18 | +); |
13 | 19 |
|
14 | 20 | // YOU CAN EDIT ME |
15 | 21 | const JULIA_VERSION_PARTS = [1, 10, 10]; |
@@ -47,41 +53,21 @@ const downloadJulia = async () => { |
47 | 53 | const spinner = createSpinner( |
48 | 54 | `\tDownloading Julia ${JULIA_VERSION} for ${platform}`, |
49 | 55 | ).start(); |
50 | | - const writer = fs.createWriteStream(path.join(assetPath, ZIP_NAME)); |
51 | 56 |
|
52 | | - const response = await axios.get(JULIA_URL, { |
53 | | - responseType: 'stream', |
54 | | - onDownloadProgress: (progressEvent) => { |
55 | | - const percentage = Math.round( |
56 | | - (progressEvent.loaded * 100) / progressEvent.total, |
57 | | - ); |
58 | | - spinner |
59 | | - .update({ text: `\tDownloading Julia ${JULIA_VERSION} ${percentage}%` }) |
60 | | - .spin(); |
61 | | - }, |
62 | | - }); |
| 57 | + const response = await fetch(JULIA_URL); |
| 58 | + if (!response.ok) { |
| 59 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 60 | + } |
63 | 61 |
|
64 | | - response.data.pipe(writer); |
| 62 | + const filePath = path.join(assetPath, ZIP_NAME); |
| 63 | + const writeStream = fs.createWriteStream(filePath); |
| 64 | + const readStream = Readable.fromWeb(response.body); |
65 | 65 |
|
66 | | - return new Promise((resolve, reject) => { |
67 | | - if (response.status > 399) { |
68 | | - reject(response.statusText); |
69 | | - } |
70 | | - response.data.on('error', (e) => { |
71 | | - console.log(); |
72 | | - reject(e); |
73 | | - }); |
74 | | - writer.on('error', (e) => { |
75 | | - console.log(); |
76 | | - reject(e); |
77 | | - }); |
78 | | - writer.on('finish', (args) => { |
79 | | - spinner.success({ |
80 | | - text: `\tDownloaded Julia (for ${platform}) (size: ${(response.data.length / 1024 / 1024).toFixed(2)} MB)`, |
81 | | - mark: '✓', |
82 | | - }); |
83 | | - resolve(args); |
84 | | - }); |
| 66 | + await pipeline(readStream, writeStream); |
| 67 | + |
| 68 | + spinner.success({ |
| 69 | + text: `\tDownloaded Julia (for ${platform})`, |
| 70 | + mark: '✓', |
85 | 71 | }); |
86 | 72 | }; |
87 | 73 |
|
@@ -274,7 +260,7 @@ const extractJulia = async () => { |
274 | 260 | spinner1.success({ text: '\tExtracted!', mark: '✓' }); |
275 | 261 | }; |
276 | 262 |
|
277 | | -exports.default = async (context) => { |
| 263 | +export default async (context) => { |
278 | 264 | let files = fs.readdirSync(assetPath); |
279 | 265 |
|
280 | 266 | if (!files.includes(JULIA_DIR_NAME)) { |
|
0 commit comments