Files
py_publish/action.js
T
bryce ec47c17f9e
Push to main / Reset-Version-Tag (push) Successful in 6s
test_2
2026-05-18 10:30:15 -06:00

62 lines
1.7 KiB
JavaScript

import { execSync } from "child_process";
import { readFileSync, mkdirSync, writeFileSync, readdirSync } from "fs";
const libName = process.env.PWD;
const version = readFileSync("version", { encoding: "utf8" });
const repoBaseUrl = `${process.env.INPUT_REPO_URL}/${libName}/${version}`;
const packageJsonUrls = [];
const sendFile = async (path, data) => {
await fetch(`${repoBaseUrl}/${path}`, {
method: "PUT",
headers: {
Authorization: `Basic ${process.env.INPUT_REPO_USER}:${process.env.INPUT_REPO_PASS}`,
},
body: data,
});
};
// 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));
// Update urls
packageJsonUrls.push([`${libName}/__init__.mpy`, "__init__.mpy"]);
// Generate module folders/files
readdirSync(".")
.filter((f) => f.slice(-2) == "py")
.forEach(async (moduleFile) => {
const modName = moduleFile.slice(0, -3);
const modInitPath = `${modName}/__init__.mpy`;
const modPyPath = `${modName}/${modName}.mpy`;
// compile mpy file
execSync(`pipx run mpy-cross ${moduleFile}`, {
encoding: "utf8",
});
execSync(`ls`, {
encoding: "utf8",
});
// Add module files to repo
await sendFile(modInitPath, new ArrayBuffer(0));
await sendFile(modPyPath, readFileSync(modPyPath).buffer);
// Update urls
packageJsonUrls.push([`${libName}/${modInitPath}`, modInitPath]);
packageJsonUrls.push([`${libName}/${modPyPath}`, modPyPath]);
});
// Add package.json to repo
await sendFile(`package.json`, {
urls: packageJsonUrls,
deps: [],
version: version,
});