mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Migrate the usage of CommonJS specific code and imports to ESM. Roughly summarised this: - Changes all require() calls to import statement or expressions. - Changes usage of module.exports to export statements. - Replaces require.resolve with import.meta.resolve. - Replaces conditional imports with conditional subpath imports. - Replaces module === require.main with import.meta.main. - Adds the full filename with extention to all relative imports. - Changes all usage of __dirname and __filename to their import.meta equivalents. - Sets "type": "module" in package.json files. - Removes usage of "use strict". - Adds .ts an an alias of .js for the webpack build. Due to differences in how require and import resolves files the plugin entrypoints will have to include the filename and extension after this migration in order to work.
121 lines
2.9 KiB
JavaScript
121 lines
2.9 KiB
JavaScript
/**
|
|
* 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", ".cache"))
|
|
);
|
|
}
|
|
}
|
|
|
|
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-control.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();
|
|
}
|