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