react-gnome
Version:
## Getting Started
128 lines (126 loc) • 3.83 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
// src/programs/base.ts
import { defineOption } from "clify.js";
import { EnvVars } from "../utils/env-vars.mjs";
import { ESBuild } from "../utils/esbuild.mjs";
import { handleProgramError } from "../utils/handle-program-error.mjs";
import { parseEnvVarConfig } from "../utils/parse-env-var-config.mjs";
import { readConfig } from "../utils/read-config.mjs";
import { validateAppName } from "../utils/validate-app-name.mjs";
import { validatePrefix } from "../utils/validate-prefix.mjs";
var WatchOpt = defineOption({
char: "w",
name: "watch",
type: "boolean"
});
var BuildModeOpt = defineOption({
char: "m",
name: "mode",
type: "string",
description: "The build mode, either 'development' or 'production'.",
default: "production",
required: true,
validate(value) {
if (value !== "development" && value !== "production") {
return {
message: "Invalid mode argument.",
received: value,
expected: "'development' or 'production'"
};
}
return "ok";
}
});
var Program = class {
constructor(init) {
__publicField(this, "type", "build");
__publicField(this, "envs", new EnvVars());
__publicField(this, "config");
__publicField(this, "cwd", process.cwd());
__publicField(this, "resources");
__publicField(this, "esbuildCtx", new ESBuild());
__publicField(this, "args");
this.args = {
watch: init.option(WatchOpt),
mode: init.option(BuildModeOpt)
};
}
get isDev() {
return this.args.mode.value === "development";
}
get watchMode() {
return this.args.watch.value || false;
}
get appName() {
return validateAppName(
this.config.applicationName.replace(/[^\w\d_-]/g, "")
);
}
get appID() {
if (this.config.applicationPrefix) {
const prefix = this.config.applicationPrefix.trim().replace(/(^\.+)|(\.+$)/g, "");
validatePrefix(prefix);
return `${prefix}.${this.config.applicationName}`;
}
return `org.gnome.${this.config.applicationName}`;
}
populateDefaultEnvVars() {
parseEnvVarConfig(this);
this.envs.define(
"friendlyAppName",
this.config.friendlyName ?? this.config.applicationName
);
this.envs.define("appName", this.appName);
this.envs.define("appVersion", this.config.applicationVersion);
this.envs.define("appId", this.appID);
this.envs.define("mode", this.isDev ? "development" : "production");
}
/** @internal */
async run() {
try {
this.config = await readConfig(this);
this.populateDefaultEnvVars();
const result = await this.main(this);
if (this.afterBuild) {
await this.afterBuild();
}
return result;
} catch (e) {
handleProgramError(e);
} finally {
if (!this.esbuildCtx.isWatching) {
await this.esbuildCtx.dispose();
}
}
}
async runWith(args, config, workingDir) {
try {
if (workingDir) {
this.cwd = workingDir;
}
this.config = config;
for (const [key, value] of Object.entries(args)) {
const arg = this.args[key];
if (arg) {
arg.setDefault(value);
}
}
Object.freeze(this);
Object.freeze(this.args);
Object.freeze(this.config);
this.populateDefaultEnvVars();
return await this.main(this);
} catch (e) {
handleProgramError(e);
} finally {
if (!this.esbuildCtx.isWatching) {
await this.esbuildCtx.dispose();
}
}
}
};
export {
Program
};