diff --git a/action.js b/action.js index 7985e36..80896be 100644 --- a/action.js +++ b/action.js @@ -3,63 +3,66 @@ import { readFileSync, mkdirSync, writeFileSync, readdirSync } from "fs"; const libName = process.env.PWD.split("/").slice(-1)[0]; const version = readFileSync("version", { encoding: "utf8" }); +const tarName = `${libName}_${version}.tgz`; const repoBaseUrl = `${process.env.INPUT_REPO_URL}/${libName}/${version}`; const packageJsonUrls = []; -const sendFile = async (urlPath, data) => { - let r = await fetch(`${repoBaseUrl}/${urlPath}`, { - method: "PUT", - headers: { - Authorization: `Basic ${Buffer.from(`${process.env.INPUT_REPO_USER}:${process.env.INPUT_REPO_PASS}`).toString("base64")}`, - }, - body: data, +// Get system utils +execSync("apt update", { encoding: "utf8" }); +execSync("apt install -y pipx", { encoding: "utf8" }); +execSync("pipx install mpy-cross", { encoding: "utf8" }); + +// create base lib dir +mkdirSync(libName); + +// Add base lib file to repo +writeFileSync(`${libName}/__init__.mpy`, new ArrayBuffer(0)); + +// Update urls +packageJsonUrls.push([`${libName}/__init__.mpy`, `__init__.mpy`]); + +// Generate module folders/files +for (let modPy of readdirSync(".").filter((f) => f.slice(-2) == "py")) { + const modName = modPy.slice(0, -3); + const modInitPath = `${modName}/__init__.mpy`; + const modMpyPath = `${modName}/${modName}.mpy`; + + // compile mpy file + execSync(`pipx run mpy-cross ${modPy} -o ${libName}/${modMpyPath}`, { + encoding: "utf8", }); - if (r.status > 399) { - throw `${r.status}: FAILED TO UPLOAD TO ${repoBaseUrl}/${urlPath}`; - } -}; - -const doIt = async () => { - // Get system utils - execSync("apt update", { encoding: "utf8" }); - execSync("apt install -y pipx", { encoding: "utf8" }); - execSync("pipx install mpy-cross", { encoding: "utf8" }); - - // Add base lib file to repo - await sendFile(`__init__.mpy`, new ArrayBuffer(0)); + // Add module files to repo + writeFileSync(`${libName}/${modInitPath}`, new ArrayBuffer(0)); // Update urls - packageJsonUrls.push([`${libName}/__init__.mpy`, "__init__.mpy"]); + packageJsonUrls.push([`${libName}/${modInitPath}`, `${modInitPath}`]); + packageJsonUrls.push([`${libName}/${modMpyPath}`, `${modMpyPath}`]); +} - // Generate module folders/files - for (let moduleFile of readdirSync(".").filter((f) => f.slice(-2) == "py")) { - const modName = moduleFile.slice(0, -3); - const modInitPath = `${modName}/__init__.mpy`; - const modPyPath = `${modName}.mpy`; +// Add package.json to repo +writeFileSync( + `${libName}/package.json`, + JSON.stringify({ + urls: packageJsonUrls, + deps: [], + version: version, + }), +); - // compile mpy file - execSync(`pipx run mpy-cross ${moduleFile}`, { - encoding: "utf8", - }); +// create tar file +execSync(`tar czf ${tarName} ${libName}`, { + encoding: "utf8", +}); - // Add module files to repo - await sendFile(modPyPath, readFileSync(`${modName}.mpy`).buffer); +let r = await fetch(`${repoBaseUrl}/${libName}.tgz`, { + method: "PUT", + headers: { + Authorization: `Basic ${Buffer.from(`${process.env.INPUT_REPO_USER}:${process.env.INPUT_REPO_PASS}`).toString("base64")}`, + }, + body: readFileSync(`${libName}.tgz`).buffer, +}); - // Update urls - packageJsonUrls.push([`${libName}/${modInitPath}`, "__init__.mpy"]); - packageJsonUrls.push([`${libName}/${modName}/${modPyPath}`, modPyPath]); - } - - // Add package.json to repo - await sendFile( - `package.json`, - JSON.stringify({ - urls: packageJsonUrls, - deps: [], - version: version, - }), - ); -}; - -doIt(); +if (r.status > 399) { + throw `${r.status}: FAILED TO UPLOAD TO ${repoBaseUrl}`; +}