@synaptic-simulations/mach
Version:
The last MSFS instrument bundler you'll ever need.
168 lines • 8.23 kB
JavaScript
;
/*
* SPDX-FileCopyrightText: 2022 Synaptic Simulations and its contributors
* SPDX-License-Identifier: MIT
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.machWatch = exports.machBuild = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const chalk_1 = __importDefault(require("chalk"));
const commander_1 = require("commander");
const dotenv_1 = __importDefault(require("dotenv"));
// @ts-ignore FIXME: remove the ignore when/if https://github.com/antitoxic/import-single-ts/pull/7 is merged
const import_single_ts_1 = require("import-single-ts");
const signale_1 = __importDefault(require("signale"));
const zod_validation_error_1 = require("zod-validation-error");
const package_json_1 = require("./package.json");
const mach_1 = require("./src/mach");
const types_1 = require("./src/types");
const CONFIG_FILENAMES = ["mach.config.js", "mach.config.ts"];
try {
dotenv_1.default.config();
}
catch (_a) {
// .env is optional, but dotenv throws an error if it cannot load it
}
const cli = new commander_1.Command();
const logger = new signale_1.default.Signale({
types: {
error: { badge: "×", label: "error", color: "red", stream: process.stderr },
},
});
const commandWithOptions = (name) => cli
.command(name)
.option("-c, --config <filename>", "specify path to configuration file")
.option("-d, --work-in-config-dir", "use config directory as working directory")
.option("-b, --bundles <dirname>", "bundles output directory", "./bundles")
.option("-e, --werror", "makes all warnings into errors")
.option("-f, --filter <regex>", "regex filter of included instrument names")
.option("-m, --minify", "minify bundle code")
.option("-s, --skip-simulator-package", "skips writing simulator package templates")
.option("-t, --output-metafile", "output `build_meta.json` file to bundles directory")
.addOption(new commander_1.Option("-p, --sourcemaps [type]", "generate sourcemaps")
.choices(["linked", "external", "inline", "both"])
.preset("inline"))
.option("-v, --verbose", "output additional build information")
.hook("preAction", async (_thisCommand, actionCommand) => {
var _a;
signale_1.default.info(`Welcome to ${chalk_1.default.cyanBright("Mach")}, v${package_json_1.version}`);
const filter = actionCommand.getOptionValue("filter");
if (filter) {
actionCommand.setOptionValue("filter", new RegExp(filter));
}
// Load config
let config = (_a = actionCommand.getOptionValue("config")) !== null && _a !== void 0 ? _a : CONFIG_FILENAMES.find((file) => node_fs_1.default.existsSync(file));
if (!config) {
signale_1.default.error("Configuration file not found, consider using `-c` or `--config` to specify the path");
process.exit(1);
}
config = node_path_1.default.resolve(config).replace(/\\/g, "/");
// FIXME: remove the type cast when/if https://github.com/antitoxic/import-single-ts/pull/7 is merged
await (config.endsWith(".ts") ? (0, import_single_ts_1.importSingleTs)(config) : Promise.resolve(`${config}`).then(s => __importStar(require(s))))
.then((module) => {
// Check config integrity
const result = types_1.MachConfigSchema.safeParse(module.default);
if (result.success) {
actionCommand.setOptionValue("config", result.data);
}
else {
logger.error("Invalid config file", chalk_1.default.redBright(config));
logger.error(chalk_1.default.white((0, zod_validation_error_1.fromError)(result.error)));
process.exit(1);
}
logger.info("Loaded config file", chalk_1.default.cyanBright(config), "\n");
if (actionCommand.getOptionValue("workInConfigDir")) {
process.chdir(node_path_1.default.dirname(config));
}
})
.catch((error) => {
logger.error("Unable to load config file", chalk_1.default.redBright(config));
logger.error(error);
process.exit(1);
});
});
cli.name("mach").version(package_json_1.version).description(package_json_1.description);
commandWithOptions("build")
.description("compile instruments specified in configuration file")
.action((args) => {
const parsedArgs = types_1.MachArgsSchema.parse(args);
const instruments = parsedArgs.filter
? parsedArgs.config.instruments.filter((instrument) => parsedArgs.filter.test(instrument.name))
: parsedArgs.config.instruments;
signale_1.default.start(`Building ${instruments.length} instrument${instruments.length !== 1 ? "s" : ""}\n`);
const startTime = performance.now();
(0, mach_1.machBuild)(instruments, parsedArgs).then((results) => {
const stopTime = performance.now();
const numSuccess = results.filter(({ status }) => status === "fulfilled").length;
const numFailed = instruments.length - numSuccess;
if (numSuccess > 0) {
signale_1.default.success(`Built ${numSuccess} instrument${instruments.length !== 1 ? "s" : ""} in`, chalk_1.default.greenBright(`${(stopTime - startTime).toFixed()} ms`), "\n");
}
if (numFailed > 0) {
signale_1.default.error(`${instruments.length} instrument${instruments.length !== 1 ? "s" : ""} failed to build`);
process.exit(1);
}
});
});
commandWithOptions("watch")
.description("watch instruments for changes and re-compile bundles when updated")
.action((args) => {
const parsedArgs = types_1.MachArgsSchema.parse(args);
const instruments = parsedArgs.filter
? parsedArgs.config.instruments.filter((instrument) => parsedArgs.filter.test(instrument.name))
: args.config.instruments;
(0, mach_1.machWatch)(instruments, parsedArgs).then((results) => {
if (results.some(({ status }) => status === "rejected")) {
signale_1.default.error("Watch mode requires a successful build to initialize");
process.exit(1);
}
signale_1.default.watch("Watching for changes\n");
});
});
cli.parse();
var mach_2 = require("./src/mach");
Object.defineProperty(exports, "machBuild", { enumerable: true, get: function () { return mach_2.machBuild; } });
Object.defineProperty(exports, "machWatch", { enumerable: true, get: function () { return mach_2.machWatch; } });
__exportStar(require("./src/types"), exports);
//# sourceMappingURL=index.js.map