Add more flags and verbose logging
This commit is contained in:
parent
4e31f18045
commit
542d28cb53
13 changed files with 406 additions and 135 deletions
174
src/index.ts
174
src/index.ts
|
@ -1,34 +1,164 @@
|
|||
import { Glob } from "bun";
|
||||
import chalk from "chalk";
|
||||
import { parseArgs } from "node:util";
|
||||
import { loadBindingFile } from "./binding";
|
||||
import type { AnyResource } from "./playbill-schema";
|
||||
import {
|
||||
type AnyResource,
|
||||
ValidatedPlaybillSchema,
|
||||
getEmptyPlaybill,
|
||||
} from "./playbill-schema";
|
||||
import { loadResourceFile } from "./resource";
|
||||
import { version } from "../package.json" with { type: "json" };
|
||||
|
||||
const { values: options, positionals: args } = parseArgs({
|
||||
args: Bun.argv.slice(2),
|
||||
options: {
|
||||
write: {
|
||||
short: "w",
|
||||
type: "boolean",
|
||||
default: false,
|
||||
},
|
||||
check: {
|
||||
short: "c",
|
||||
type: "boolean",
|
||||
default: false,
|
||||
},
|
||||
outfile: {
|
||||
short: "o",
|
||||
type: "string",
|
||||
default: "playbill.json",
|
||||
},
|
||||
help: {
|
||||
short: "h",
|
||||
default: false,
|
||||
type: "boolean",
|
||||
},
|
||||
verbose: {
|
||||
short: "v",
|
||||
default: false,
|
||||
type: "boolean",
|
||||
},
|
||||
},
|
||||
strict: true,
|
||||
allowPositionals: true,
|
||||
});
|
||||
|
||||
if (args.length === 0 || options.help) {
|
||||
console.log(`${chalk.bold("muse")} - Compile and validate Playbills for Proscenium
|
||||
${chalk.dim(`v${version}`)}
|
||||
|
||||
Usage:
|
||||
muse [/path/to/binding.yaml] <options>
|
||||
|
||||
Options:
|
||||
--check Only load and check the current binding and resources, but do not compile
|
||||
--write Write the output to a file. If not specified, outputs to stdout
|
||||
--outfile Specify the output file path [default: playbill.json]
|
||||
--verbose, -v Verbose output
|
||||
--help, -h Show this help message
|
||||
`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (options.check && options.write) {
|
||||
console.error("Cannot use --check and --write together");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const VERBOSE = options.verbose;
|
||||
|
||||
function verboseLog(msg: string) {
|
||||
if (VERBOSE) {
|
||||
console.log(chalk.dim(msg));
|
||||
}
|
||||
}
|
||||
|
||||
const bindingPath = args[0] ?? "./binding.yaml";
|
||||
|
||||
verboseLog(`Using binding file: ${bindingPath}`);
|
||||
|
||||
// Check if the binding file exists
|
||||
const bindingFileCheck = Bun.file(bindingPath);
|
||||
const bindingFileExists = await bindingFileCheck.exists();
|
||||
|
||||
if (!bindingFileExists) {
|
||||
console.error(`Binding file not found: ${bindingPath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
verboseLog("↳ Binding file found");
|
||||
|
||||
const bindingPath = Bun.argv[2];
|
||||
const binding = await loadBindingFile(bindingPath);
|
||||
|
||||
const fileGlobs = binding.files;
|
||||
const bindingFileDirname = bindingPath.split("/").slice(0, -1).join("/");
|
||||
|
||||
const allFilePaths: string[] = [];
|
||||
|
||||
for (const thisGlob of fileGlobs) {
|
||||
const glob = new Glob(thisGlob);
|
||||
|
||||
const results = glob.scanSync({
|
||||
cwd: bindingFileDirname,
|
||||
absolute: true,
|
||||
followSymlinks: true,
|
||||
onlyFiles: true,
|
||||
});
|
||||
|
||||
allFilePaths.push(...results);
|
||||
}
|
||||
verboseLog(
|
||||
`↳ Binding loaded with ${binding.files.length} associated resources`,
|
||||
);
|
||||
|
||||
const loadedResources: AnyResource[] = [];
|
||||
|
||||
for (const filepath of allFilePaths) {
|
||||
verboseLog("Loading resources");
|
||||
|
||||
// Load resources listed in the binding file
|
||||
for (const filepath of binding.files) {
|
||||
verboseLog(`↳ Loading ${filepath}...`);
|
||||
const loaded = await loadResourceFile(filepath);
|
||||
verboseLog(` ↳ ${filepath} loaded with ${loaded.length} resources`);
|
||||
loadedResources.push(...loaded);
|
||||
}
|
||||
|
||||
console.log(loadedResources);
|
||||
verboseLog("Constructing playbill");
|
||||
|
||||
// Consjtruct the playbill object
|
||||
const playbill = getEmptyPlaybill();
|
||||
|
||||
playbill.id = binding.info.id;
|
||||
playbill.name = binding.info.name;
|
||||
playbill.author = binding.info.author ?? "Anonymous";
|
||||
playbill.version = binding.info.version;
|
||||
|
||||
for (const resource of loadedResources) {
|
||||
switch (resource.$define) {
|
||||
case "ability":
|
||||
playbill.abilities.push(resource);
|
||||
break;
|
||||
case "species":
|
||||
playbill.species.push(resource);
|
||||
break;
|
||||
case "entity":
|
||||
playbill.entities.push(resource);
|
||||
break;
|
||||
case "item":
|
||||
playbill.items.push(resource);
|
||||
break;
|
||||
case "tag":
|
||||
playbill.tags.push(resource);
|
||||
break;
|
||||
case "method":
|
||||
playbill.methods.push(resource);
|
||||
break;
|
||||
case "lore":
|
||||
playbill.lore.push(resource);
|
||||
break;
|
||||
case "rule":
|
||||
playbill.rules.push(resource);
|
||||
break;
|
||||
case "playbill":
|
||||
throw new Error("Cannot load playbills rn dawg");
|
||||
}
|
||||
}
|
||||
|
||||
verboseLog("Validating playbill");
|
||||
|
||||
// Validate the playbill object
|
||||
const validatedPlaybill = ValidatedPlaybillSchema.parse(playbill);
|
||||
|
||||
verboseLog("Playbill validated");
|
||||
|
||||
if (options.write) {
|
||||
await Bun.write(options.outfile, JSON.stringify(validatedPlaybill, null, 2));
|
||||
} else if (!options.check) {
|
||||
console.log(validatedPlaybill);
|
||||
}
|
||||
|
||||
if (options.check) {
|
||||
console.log(chalk.green("Playbill validated successfully"));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue