74 lines
2.0 KiB
JavaScript
74 lines
2.0 KiB
JavaScript
import { execSync } from "child_process";
|
|
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 = [];
|
|
|
|
// 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`, "");
|
|
|
|
// Update urls
|
|
packageJsonUrls.push([`${libName}/__init__.mpy`, `__init__.mpy`]);
|
|
|
|
// Generate module folders/files
|
|
for (let modPy of readdirSync(".").filter((f) => f.slice(-3) == ".py")) {
|
|
const modName = modPy.slice(0, -3);
|
|
const modInitPath = `${modName}/__init__.mpy`;
|
|
const modMpyPath = `${modName}/${modName}.mpy`;
|
|
|
|
// create mod dir
|
|
mkdirSync(`${libName}/${modName}`);
|
|
|
|
// compile mpy file
|
|
execSync(`pipx run mpy-cross ${modPy} -o ${libName}/${modMpyPath}`, {
|
|
encoding: "utf8",
|
|
});
|
|
|
|
// Add module files to repo
|
|
writeFileSync(`${libName}/${modInitPath}`, "");
|
|
|
|
// Update urls
|
|
packageJsonUrls.push([`${libName}/${modInitPath}`, `${modInitPath}`]);
|
|
packageJsonUrls.push([`${libName}/${modMpyPath}`, `${modMpyPath}`]);
|
|
}
|
|
|
|
// Add package.json to repo
|
|
writeFileSync(
|
|
`${libName}/package.json`,
|
|
JSON.stringify({
|
|
urls: packageJsonUrls,
|
|
deps: [],
|
|
version: version,
|
|
}),
|
|
);
|
|
|
|
// create tar file
|
|
execSync(`tar czf ${tarName} ${libName}`, {
|
|
encoding: "utf8",
|
|
});
|
|
|
|
console.log(readdirSync(libName));
|
|
|
|
let r = await fetch(`${repoBaseUrl}/${tarName}`, {
|
|
method: "PUT",
|
|
headers: {
|
|
Authorization: `Basic ${Buffer.from(`${process.env.INPUT_REPO_USER}:${process.env.INPUT_REPO_PASS}`).toString("base64")}`,
|
|
},
|
|
body: readFileSync(tarName).buffer,
|
|
});
|
|
|
|
if (r.status > 399) {
|
|
throw `${r.status}: FAILED TO UPLOAD TO ${repoBaseUrl}`;
|
|
}
|