@@ -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" });
|
||||
|
||||
if (r.status > 399) {
|
||||
throw `${r.status}: FAILED TO UPLOAD TO ${repoBaseUrl}/${urlPath}`;
|
||||
}
|
||||
};
|
||||
// create base lib dir
|
||||
mkdirSync(libName);
|
||||
|
||||
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
|
||||
writeFileSync(`${libName}/__init__.mpy`, new ArrayBuffer(0));
|
||||
|
||||
// Add base lib file to repo
|
||||
await sendFile(`__init__.mpy`, new ArrayBuffer(0));
|
||||
// Update urls
|
||||
packageJsonUrls.push([`${libName}/__init__.mpy`, `__init__.mpy`]);
|
||||
|
||||
// Update urls
|
||||
packageJsonUrls.push([`${libName}/__init__.mpy`, "__init__.mpy"]);
|
||||
|
||||
// Generate module folders/files
|
||||
for (let moduleFile of readdirSync(".").filter((f) => f.slice(-2) == "py")) {
|
||||
const modName = moduleFile.slice(0, -3);
|
||||
// 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 modPyPath = `${modName}.mpy`;
|
||||
const modMpyPath = `${modName}/${modName}.mpy`;
|
||||
|
||||
// compile mpy file
|
||||
execSync(`pipx run mpy-cross ${moduleFile}`, {
|
||||
execSync(`pipx run mpy-cross ${modPy} -o ${libName}/${modMpyPath}`, {
|
||||
encoding: "utf8",
|
||||
});
|
||||
|
||||
// Add module files to repo
|
||||
await sendFile(modPyPath, readFileSync(`${modName}.mpy`).buffer);
|
||||
writeFileSync(`${libName}/${modInitPath}`, new ArrayBuffer(0));
|
||||
|
||||
// Update urls
|
||||
packageJsonUrls.push([`${libName}/${modInitPath}`, "__init__.mpy"]);
|
||||
packageJsonUrls.push([`${libName}/${modName}/${modPyPath}`, modPyPath]);
|
||||
}
|
||||
packageJsonUrls.push([`${libName}/${modInitPath}`, `${modInitPath}`]);
|
||||
packageJsonUrls.push([`${libName}/${modMpyPath}`, `${modMpyPath}`]);
|
||||
}
|
||||
|
||||
// Add package.json to repo
|
||||
await sendFile(
|
||||
`package.json`,
|
||||
// Add package.json to repo
|
||||
writeFileSync(
|
||||
`${libName}/package.json`,
|
||||
JSON.stringify({
|
||||
urls: packageJsonUrls,
|
||||
deps: [],
|
||||
version: version,
|
||||
}),
|
||||
);
|
||||
};
|
||||
);
|
||||
|
||||
doIt();
|
||||
// create tar file
|
||||
execSync(`tar czf ${tarName} ${libName}`, {
|
||||
encoding: "utf8",
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
if (r.status > 399) {
|
||||
throw `${r.status}: FAILED TO UPLOAD TO ${repoBaseUrl}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user