From b6ad539379cc73cb52eaa6b34ee2e8fe339bc348 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 18 Jun 2024 19:44:32 -0700 Subject: [PATCH] refac --- package-lock.json | 28 +++---------------- scripts/prepare-pyodide.js | 56 ++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/package-lock.json b/package-lock.json index 852f40d2a..9c4e4ebd5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3997,37 +3997,17 @@ } }, "node_modules/engine.io-client": { - "version": "6.5.3", - "resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.5.3.tgz", - "integrity": "sha512-9Z0qLB0NIisTRt1DZ/8U2k12RJn8yls/nXMZLn+/N8hANT3TcYjKFKcwbw5zFQiN4NTde3TSY9zb79e1ij6j9Q==", + "version": "6.5.4", + "resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.5.4.tgz", + "integrity": "sha512-GeZeeRjpD2qf49cZQ0Wvh/8NJNfeXkXXcoGh+F77oEAgo9gUHwT1fCRxSNU+YEEaysOJTnsFHmM5oAcPy4ntvQ==", "dependencies": { "@socket.io/component-emitter": "~3.1.0", "debug": "~4.3.1", "engine.io-parser": "~5.2.1", - "ws": "~8.11.0", + "ws": "~8.17.1", "xmlhttprequest-ssl": "~2.0.0" } }, - "node_modules/engine.io-client/node_modules/ws": { - "version": "8.11.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.11.0.tgz", - "integrity": "sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==", - "engines": { - "node": ">=10.0.0" - }, - "peerDependencies": { - "bufferutil": "^4.0.1", - "utf-8-validate": "^5.0.2" - }, - "peerDependenciesMeta": { - "bufferutil": { - "optional": true - }, - "utf-8-validate": { - "optional": true - } - } - }, "node_modules/engine.io-parser": { "version": "5.2.2", "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.2.2.tgz", diff --git a/scripts/prepare-pyodide.js b/scripts/prepare-pyodide.js index e746c09ed..5aaac5927 100644 --- a/scripts/prepare-pyodide.js +++ b/scripts/prepare-pyodide.js @@ -17,32 +17,60 @@ import { writeFile, readFile, copyFile, readdir, rmdir } from 'fs/promises'; async function downloadPackages() { console.log('Setting up pyodide + micropip'); - const pyodide = await loadPyodide({ - packageCacheDir: 'static/pyodide' - }); + + let pyodide; + try { + pyodide = await loadPyodide({ + packageCacheDir: 'static/pyodide' + }); + } catch (err) { + console.error('Failed to load Pyodide:', err); + return; + } const packageJson = JSON.parse(await readFile('package.json')); - const pyodideVersion = packageJson.dependencies.pyodide; + const pyodideVersion = packageJson.dependencies.pyodide.replace('^', ''); try { const pyodidePackageJson = JSON.parse(await readFile('static/pyodide/package.json')); - const pyodidePackageVersion = pyodidePackageJson.version; + const pyodidePackageVersion = pyodidePackageJson.version.replace('^', ''); - if (pyodideVersion.replace('^', '') !== pyodidePackageVersion) { + if (pyodideVersion !== pyodidePackageVersion) { console.log('Pyodide version mismatch, removing static/pyodide directory'); await rmdir('static/pyodide', { recursive: true }); } } catch (e) { - console.log('Pyodide package not found, downloading packages'); + console.log('Pyodide package not found, proceeding with download.'); } - await pyodide.loadPackage('micropip'); - const micropip = pyodide.pyimport('micropip'); - console.log('Downloading Pyodide packages:', packages); - await micropip.install(packages); - console.log('Pyodide packages downloaded, freezing into lock file'); - const lockFile = await micropip.freeze(); - await writeFile('static/pyodide/pyodide-lock.json', lockFile); + try { + console.log('Loading micropip package'); + await pyodide.loadPackage('micropip'); + + const micropip = pyodide.pyimport('micropip'); + console.log('Downloading Pyodide packages:', packages); + + try { + for (const pkg of packages) { + console.log(`Installing package: ${pkg}`); + await micropip.install(pkg); + } + } catch (err) { + console.error('Package installation failed:', err); + return; + } + + console.log('Pyodide packages downloaded, freezing into lock file'); + + try { + const lockFile = await micropip.freeze(); + await writeFile('static/pyodide/pyodide-lock.json', lockFile); + } catch (err) { + console.error('Failed to write lock file:', err); + } + } catch (err) { + console.error('Failed to load or install micropip:', err); + } } async function copyPyodide() {