diff --git a/internals/scripts/InstallPyodide.js b/internals/scripts/InstallPyodide.js index 6f1bd01..68a03b8 100644 --- a/internals/scripts/InstallPyodide.js +++ b/internals/scripts/InstallPyodide.js @@ -1,65 +1,72 @@ -//mkdir app/utils/pyodide/src +// mkdir app/utils/pyodide/src // && cd app/utils/pyodide/src // curl -LJO https://github.com/iodide-project/pyodide/releases/download/0.12.0/pyodide-build-0.12.0.tar.bz2 // tar xjf pyodide-build-0.12.0.tar.bz2 // rm pyodide-build-0.12.0.tar.bz2", -import chalk from "chalk"; -import os from "os"; -import fs from "fs"; -import https from "https"; -import mkdirp from "mkdirp"; -import tar from "tar-fs"; -import url from "url"; -import gunzip from "gunzip-maybe"; +import chalk from 'chalk'; +import fs from 'fs'; +import https from 'https'; +import mkdirp from 'mkdirp'; +import tar from 'tar-fs'; +import url from 'url'; +import bz2 from 'unbzip2-stream'; -const PYODIDE_VERSION = "0.12.0"; +const PYODIDE_VERSION = '0.12.0'; const TAR_NAME = `pyodide-build-${PYODIDE_VERSION}.tar.bz2`; -const PYODIDE_DIR = "app/utils/pyodide/src/"; +const TAR_URL = `https://github.com/iodide-project/pyodide/releases/download/${PYODIDE_VERSION}/pyodide-build-${PYODIDE_VERSION}.tar.bz2`; +const PYODIDE_DIR = 'app/utils/pyodide/src/'; const writeAndUnzipFile = response => { const filePath = `${PYODIDE_DIR}${TAR_NAME}`; const writeStream = fs.createWriteStream(filePath); response.pipe(writeStream); - writeStream.on("finish", () => { + writeStream.on('finish', () => { console.log(`${chalk.green.bold(`Unzipping pyodide`)}`); const readStream = fs.createReadStream(filePath); - readStream.pipe(gunzip()).pipe(tar.extract(PYODIDE_DIR)); + try { + readStream.pipe(bz2()).pipe(tar.extract(PYODIDE_DIR)); + } catch (e) { + throw new Error('Error in unzip:', e); + } - readStream.on("end", () => { + readStream.on('end', () => { console.log(`${chalk.green.bold(`Unzip successful`)}`); }); }); }; +const downloadFile = response => { + if ( + response.statusCode > 300 && + response.statusCode < 400 && + response.headers.location + ) { + if (url.parse(response.headers.location).hostname) { + https.get(response.headers.location, writeAndUnzipFile); + } else { + https.get( + url.resolve(url.parse(TAR_URL).hostname, response.headers.location), + writeAndUnzipFile + ); + } + } else { + writeAndUnzipFile(response); + } +}; + (() => { + if (fs.existsSync(`${PYODIDE_DIR}${TAR_NAME}`)) { + console.log( + `${chalk.green.bold(`Pyodide is already present: ${PYODIDE_VERSION}...`)}` + ); + return; + } console.log( `${chalk.green.bold(`Downloading pyodide ${PYODIDE_VERSION}...`)}` ); - mkdirp.sync(`app/utils/pyodide/src`); - - https.get( - `https://github.com/iodide-project/pyodide/releases/download/${PYODIDE_VERSION}/pyodide-build-${PYODIDE_VERSION}.tar.bz2`, - response => { - if ( - response.statusCode > 300 && - response.statusCode < 400 && - response.headers.location - ) { - if (url.parse(response.headers.location).hostname) { - https.get(response.headers.location, writeAndUnzipFile); - } else { - https.get( - url.resolve(url.parse(TAR_URL).hostname, response.headers.location), - writeToFile - ); - } - } else { - writeAndUnzipFile(response); - } - } - ); + https.get(TAR_URL, downloadFile); })(); diff --git a/package.json b/package.json index 43a5aed..e658d77 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,9 @@ "test-watch": "yarn test --watch" }, "lint-staged": { - "*.{js,jsx,ts,tsx}": ["cross-env NODE_ENV=development eslint --cache"], + "*.{js,jsx,ts,tsx}": [ + "cross-env NODE_ENV=development eslint --cache" + ], "{*.json,.{babelrc,eslintrc,prettierrc,stylelintrc}}": [ "prettier --ignore-path .eslintignore --parser json --write" ], @@ -45,7 +47,9 @@ "stylelint --ignore-path .eslintignore --syntax scss --fix", "prettier --ignore-path .eslintignore --single-quote --write" ], - "*.{html,md,yml}": ["prettier --ignore-path .eslintignore --single-quote --write"] + "*.{html,md,yml}": [ + "prettier --ignore-path .eslintignore --single-quote --write" + ] }, "build": { "productName": "BrainWaves", @@ -53,6 +57,9 @@ "files": [ "dist/", "node_modules/", + "assets/", + "utils/pyodide/utils.py", + "utils/pyodide/pyimport.py", "app.html", "main.prod.js", "main.prod.js.map",