-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython.download.mjs
50 lines (40 loc) · 1.48 KB
/
python.download.mjs
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
import fs from 'fs';
import path from 'path';
import { pathToFileURL } from 'url';
import axios from 'axios';
import { Extract } from 'unzipper';
export async function downloadFile(url, outputPath) {
const writer = fs.createWriteStream(outputPath);
const response = await axios({
url,
responseType: 'stream',
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
export function unzipFile(zipPath, outputDir) {
fs.createReadStream(zipPath)
.pipe(Extract({ path: outputDir }))
.on('close', () => {
console.log('Arquivo descompactado com sucesso!');
})
.on('error', (err) => {
console.error('Erro ao descompactar o arquivo:', err);
});
}
export async function downloadPython({ pythonURL, distDir } = {}) {
distDir ||= "./dist/openai/"
pythonURL ||= 'https://www.python.org/ftp/python/3.8.10/python-3.8.10-embed-amd64.zip'
const zipFileName = pythonURL.split('/')[ pythonURL.split('/').length - 1 ]
const outputDirPath = path.resolve('./python');
const outputZipPath = path.resolve('./python', zipFileName);
if (!fs.existsSync(outputZipPath)) {
if (!fs.existsSync(outputDirPath)) { fs.mkdirSync(outputDirPath); }
await downloadFile(pythonURL, outputZipPath);
}
unzipFile(outputZipPath, path.resolve(distDir, 'lib', 'python'));
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) { downloadPython() }