Merge pull request #1241 from bbassie/chore/move-root-scripts

Move clean and publish scripts into scripts folder
This commit is contained in:
Bastiaan authored and GitHub committed 2026-11-21 02:23:48 +00:00
1 parent a49841f3fc
commit 8692ea3941
8 files changed
+11 -11

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
/**
* Clean the repository of build and execution artifacts.
*
* clean: A quick clean that removes all build artifacts making the output safe to publish
* clean-tests: Removes all build and test artifacts should not be required if tests are setup correctly.
* clean-all: Removes all build, test, and execution artifacts. Including cluster config and logs!
*/
import path from "node:path";
import fs from "node:fs/promises";
import yargs from "yargs";
import npmPackage from "../package.json" with { type: "json" };
const workspaces = npmPackage.workspaces
.map(p => p.slice(0, -2))
.filter(p => !p.includes("external_plugins"));
let DRY = false;
/**
* Attempt to remove a file / directory if it exists.
*
* @param {string} filePath The path to remove.
*/
async function tryRemove(filePath) {
try {
await fs.access(filePath);
console.log(filePath);
} catch {
return;
}
if (!DRY) {
await fs.rm(filePath, { force: true, recursive: true, maxRetries: 10 });
}
}
/**
* Removes all build artifacts
*/
async function removeBuildArtifacts() {
const tasks = [];
for (const workspace of workspaces) {
const basePath = path.resolve(workspace);
const packages = await fs.readdir(basePath);
for (const packageName of packages) {
tasks.push(
tryRemove(path.join(basePath, packageName, "dist"))
);
tasks.push(
tryRemove(path.join(basePath, packageName, "node_modules", ".vite"))
);
}
}
await Promise.all(tasks);
}
/**
* Removes all test artifacts
*/
async function removeTestArtifacts() {
const artifacts = [
".nyc_output", "coverage", "temp", "static",
];
await Promise.all(artifacts.map(artifact => tryRemove(path.resolve(artifact))));
}
/**
* Removes all execution artifacts
*/
async function removeExecutionArtifacts() {
const artifacts = [
"database", "instances", "logs", "mods",
"config-ctl.json", "config-controller.json", "config-host.json",
"FactorioAdminToken.txt", "plugin-list.json",
];
await Promise.all(artifacts.map(artifact => tryRemove(path.resolve(artifact))));
}
/**
* Main function for this script
*/
async function main() {
await yargs(process.argv.slice(2))
.scriptName("clean")
.option("dry-run", {
alias: "dry",
nargs: 0,
describe: "Will not delete any files",
default: false,
type: "boolean",
})
.command("all", "Remove all artifacts", () => {}, argv => {
DRY = argv.dry;
removeBuildArtifacts();
removeTestArtifacts();
removeExecutionArtifacts();
})
.command("tests", "Remove all build and test artifacts", () => {}, argv => {
DRY = argv.dry;
removeBuildArtifacts();
removeTestArtifacts();
})
.command("$0", "Remove all build artifacts", () => {}, argv => {
DRY = argv.dry;
removeBuildArtifacts();
})
.demandCommand(1, "A command must be specified")
.strict()
.parse();
}
// Run main if started from command line
if (import.meta.main) {
main();
}
+133
View File
@@ -0,0 +1,133 @@
/**
* Publish any mods that need to be published
*/
import { openAsBlob } from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import yargs from "yargs";
let DRY = false;
const modReleaseCache = new Map();
/**
* Gets a list of all releases for a mod
*/
async function requestModReleases(mod) {
const cache = modReleaseCache.get(mod);
if (cache) {
return cache;
}
// eslint-disable-next-line no-console
console.log(`Requesting current releases of ${mod}`);
const response = await fetch(`https://mods.factorio.com/api/mods/${mod}`);
if (!response.ok) {
throw new Error(`Failed request to mod portal api: ${response.statusText}\n${await response.text()}`);
}
const json = await response.json();
modReleaseCache.set(mod, json.releases);
return json.releases;
}
/**
* Upload a mod to the mod portal
*/
export async function uploadModRelease(file, name) {
// eslint-disable-next-line no-console
console.log(`Uploading new release ${file}`);
if (DRY) {
return;
}
const headers = new Headers({
Authorization: `Bearer ${process.env.FACTORIO_TOKEN}`,
});
// Start the upload process by requesting an upload url
const initFormData = new FormData();
initFormData.append("mod", name);
const initResponse = await fetch("https://mods.factorio.com/api/v2/mods/releases/init_upload", {
method: "POST",
headers: headers,
body: initFormData,
});
if (!initResponse.ok) {
throw new Error(`Failed init mod upload: ${initResponse.statusText}\n${await initResponse.text()}`);
}
// Upload the new version of the mod
const uploadUrl = (await initResponse.json()).upload_url;
const uploadFormData = new FormData();
uploadFormData.append("file", await openAsBlob(file), path.basename(file));
const uploadResponse = await fetch(uploadUrl, {
method: "POST",
headers: headers,
body: uploadFormData,
});
if (!uploadResponse.ok) {
throw new Error(`Failed mod upload: ${uploadResponse.statusText}\n${await uploadResponse.text()}`);
}
}
const regexModZip = /(\w+)_\d+.\d+.\d+\.zip/;
/**
* Publishes all mods that require publishing
*/
export async function publishMods(dir) {
const files = await fs.readdir(dir);
const mods = files
.sort((a, b) => a.localeCompare(b, "en", { numeric: true }))
.map(file => regexModZip.exec(file))
.filter(file => Boolean(file));
for (const mod of mods) {
const [file, name] = mod;
const releases = await requestModReleases(name);
if (!releases.some(release => release.file_name === file)) {
// Upload in version order so the newest variant is published last.
await uploadModRelease(path.join(dir, file), name);
} else {
// eslint-disable-next-line no-console
console.log(`Skipped ${file}`);
}
}
}
/**
* Main function for this script
*/
async function main() {
await yargs(process.argv.slice(2))
.scriptName("publish")
.option("dry-run", {
alias: "dry",
nargs: 0,
describe: "Will not publish anything",
default: false,
type: "boolean",
})
.command(["factorio <mods-dir>", "$0 <mods-dir>"], "Publish factorio mods", yargs => {
yargs
.positional("mods-dir", {
describe: "Path to packed mods to publish",
normalize: true,
type: "string",
})
.demandOption("mods-dir", "A path to the mods dir must be specified");
}, async argv => {
DRY = argv.dry;
await publishMods(argv.modsDir);
})
.strict()
.parse();
}
// Run main if started from command line
if (import.meta.main) {
main();
}