alinea
Version:
Headless git-based CMS
291 lines (287 loc) • 11.4 kB
JavaScript
import {
package_default
} from "../chunks/chunk-NWJMNYVI.js";
import "../chunks/chunk-NZLE2WMY.js";
// node_modules/mri/lib/index.mjs
function toArr(any) {
return any == null ? [] : Array.isArray(any) ? any : [any];
}
function toVal(out, key, val, opts) {
var x, old = out[key], nxt = !!~opts.string.indexOf(key) ? val == null || val === true ? "" : String(val) : typeof val === "boolean" ? val : !!~opts.boolean.indexOf(key) ? val === "false" ? false : val === "true" || (out._.push((x = +val, x * 0 === 0) ? x : val), !!val) : (x = +val, x * 0 === 0) ? x : val;
out[key] = old == null ? nxt : Array.isArray(old) ? old.concat(nxt) : [old, nxt];
}
function lib_default(args, opts) {
args = args || [];
opts = opts || {};
var k, arr, arg, name, val, out = { _: [] };
var i2 = 0, j = 0, idx = 0, len = args.length;
const alibi = opts.alias !== void 0;
const strict = opts.unknown !== void 0;
const defaults = opts.default !== void 0;
opts.alias = opts.alias || {};
opts.string = toArr(opts.string);
opts.boolean = toArr(opts.boolean);
if (alibi) {
for (k in opts.alias) {
arr = opts.alias[k] = toArr(opts.alias[k]);
for (i2 = 0; i2 < arr.length; i2++) {
(opts.alias[arr[i2]] = arr.concat(k)).splice(i2, 1);
}
}
}
for (i2 = opts.boolean.length; i2-- > 0; ) {
arr = opts.alias[opts.boolean[i2]] || [];
for (j = arr.length; j-- > 0; ) opts.boolean.push(arr[j]);
}
for (i2 = opts.string.length; i2-- > 0; ) {
arr = opts.alias[opts.string[i2]] || [];
for (j = arr.length; j-- > 0; ) opts.string.push(arr[j]);
}
if (defaults) {
for (k in opts.default) {
name = typeof opts.default[k];
arr = opts.alias[k] = opts.alias[k] || [];
if (opts[name] !== void 0) {
opts[name].push(k);
for (i2 = 0; i2 < arr.length; i2++) {
opts[name].push(arr[i2]);
}
}
}
}
const keys = strict ? Object.keys(opts.alias) : [];
for (i2 = 0; i2 < len; i2++) {
arg = args[i2];
if (arg === "--") {
out._ = out._.concat(args.slice(++i2));
break;
}
for (j = 0; j < arg.length; j++) {
if (arg.charCodeAt(j) !== 45) break;
}
if (j === 0) {
out._.push(arg);
} else if (arg.substring(j, j + 3) === "no-") {
name = arg.substring(j + 3);
if (strict && !~keys.indexOf(name)) {
return opts.unknown(arg);
}
out[name] = false;
} else {
for (idx = j + 1; idx < arg.length; idx++) {
if (arg.charCodeAt(idx) === 61) break;
}
name = arg.substring(j, idx);
val = arg.substring(++idx) || (i2 + 1 === len || ("" + args[i2 + 1]).charCodeAt(0) === 45 || args[++i2]);
arr = j === 2 ? [name] : name;
for (idx = 0; idx < arr.length; idx++) {
name = arr[idx];
if (strict && !~keys.indexOf(name)) return opts.unknown("-".repeat(j) + name);
toVal(out, name, idx + 1 < arr.length || val, opts);
}
}
}
if (defaults) {
for (k in opts.default) {
if (out[k] === void 0) {
out[k] = opts.default[k];
}
}
}
if (alibi) {
for (k in out) {
arr = opts.alias[k] || [];
while (arr.length > 0) {
out[arr.shift()] = out[k];
}
}
}
return out;
}
// node_modules/sade/lib/index.mjs
var t = "__all__";
var i = "__default__";
var s = "\n";
function r(e) {
if (!e.length) return "";
let t2 = function(e2) {
let t3 = 0, i2 = 0, s2 = 0, r2 = e2.length;
if (r2) for (; r2--; ) i2 = e2[r2].length, i2 > t3 && (s2 = r2, t3 = i2);
return e2[s2].length;
}(e.map((e2) => e2[0])) + 4;
return e.map((e2) => e2[0] + " ".repeat(t2 - e2[0].length) + e2[1] + (null == e2[2] ? "" : ` (default ${e2[2]})`));
}
function n(e) {
return e;
}
function l(e, t2, i2) {
if (!t2 || !t2.length) return "";
let r2 = 0, n2 = "";
for (n2 += "\n " + e; r2 < t2.length; r2++) n2 += "\n " + i2(t2[r2]);
return n2 + s;
}
function a(e, t2, i2 = 1) {
let s2 = l("ERROR", [t2], n);
s2 += `
Run \`$ ${e} --help\` for more info.
`, console.error(s2), process.exit(i2);
}
var o = class {
constructor(e, s2) {
let [r2, ...n2] = e.split(/\s+/);
s2 = s2 || n2.length > 0, this.bin = r2, this.ver = "0.0.0", this.default = "", this.tree = {}, this.command(t), this.command([i].concat(s2 ? n2 : "<command>").join(" ")), this.single = s2, this.curr = "";
}
command(e, t2, i2 = {}) {
if (this.single) throw new Error('Disable "single" mode to add commands');
let s2 = [], r2 = [], n2 = /(\[|<)/;
if (e.split(/\s+/).forEach((e2) => {
(n2.test(e2.charAt(0)) ? r2 : s2).push(e2);
}), s2 = s2.join(" "), s2 in this.tree) throw new Error("Command already exists: " + s2);
return s2.includes("__") || r2.unshift(s2), r2 = r2.join(" "), this.curr = s2, i2.default && (this.default = s2), this.tree[s2] = { usage: r2, alibi: [], options: [], alias: {}, default: {}, examples: [] }, i2.alias && this.alias(i2.alias), t2 && this.describe(t2), this;
}
describe(e) {
return this.tree[this.curr || i].describe = Array.isArray(e) ? e : function(e2) {
return (e2 || "").replace(/([.?!])\s*(?=[A-Z])/g, "$1|").split("|");
}(e), this;
}
alias(...e) {
if (this.single) throw new Error('Cannot call `alias()` in "single" mode');
if (!this.curr) throw new Error("Cannot call `alias()` before defining a command");
return (this.tree[this.curr].alibi = this.tree[this.curr].alibi.concat(...e)).forEach((e2) => this.tree[e2] = this.curr), this;
}
option(e, i2, s2) {
let r2 = this.tree[this.curr || t], [n2, l2] = function(e2) {
return (e2 || "").split(/^-{1,2}|,|\s+-{1,2}|\s+/).filter(Boolean);
}(e);
if (l2 && l2.length > 1 && ([n2, l2] = [l2, n2]), e = "--" + n2, l2 && l2.length > 0) {
e = `-${l2}, ${e}`;
let t2 = r2.alias[l2];
r2.alias[l2] = (t2 || []).concat(n2);
}
let a2 = [e, i2 || ""];
return void 0 !== s2 ? (a2.push(s2), r2.default[n2] = s2) : l2 || (r2.default[n2] = void 0), r2.options.push(a2), this;
}
action(e) {
return this.tree[this.curr || i].handler = e, this;
}
example(e) {
return this.tree[this.curr || i].examples.push(e), this;
}
version(e) {
return this.ver = e, this;
}
parse(s2, r2 = {}) {
s2 = s2.slice();
let n2, l2, o2, h, u = 2, f = lib_default(s2.slice(u), { alias: { h: "help", v: "version" } }), c = this.single, p = this.bin, d = "";
if (c) h = this.tree[i];
else {
let e, t2 = 1, i2 = f._.length + 1;
for (; t2 < i2; t2++) if (n2 = f._.slice(0, t2).join(" "), e = this.tree[n2], "string" == typeof e) l2 = (d = e).split(" "), s2.splice(s2.indexOf(f._[0]), t2, ...l2), t2 += l2.length - t2;
else if (e) d = n2;
else if (d) break;
if (h = this.tree[d], o2 = void 0 === h, o2) {
if (this.default) d = this.default, h = this.tree[d], s2.unshift(d), u++;
else if (n2) return a(p, "Invalid command: " + n2);
}
}
if (f.help) return this.help(!c && !o2 && d);
if (f.version) return this._version();
if (!c && void 0 === h) return a(p, "No command specified.");
let g = this.tree[t];
r2.alias = Object.assign(g.alias, h.alias, r2.alias), r2.default = Object.assign(g.default, h.default, r2.default), n2 = d.split(" "), l2 = s2.indexOf(n2[0], 2), ~l2 && s2.splice(l2, n2.length);
let m = lib_default(s2.slice(u), r2);
if (!m || "string" == typeof m) return a(p, m || "Parsed unknown option flag(s)!");
let b = h.usage.split(/\s+/), _ = b.filter((e) => "<" === e.charAt(0)), v = m._.splice(0, _.length);
if (v.length < _.length) return d && (p += " " + d), a(p, "Insufficient arguments!");
b.filter((e) => "[" === e.charAt(0)).forEach((e) => {
v.push(m._.shift());
}), v.push(m);
let $ = h.handler;
return r2.lazy ? { args: v, name: d, handler: $ } : $.apply(null, v);
}
help(e) {
console.log(function(e2, a2, o2, h) {
let u = "", f = a2[o2], c = "$ " + e2, p = a2[t], d = (e3) => `${c} ${e3}`.replace(/\s+/g, " "), g = [["-h, --help", "Displays this message"]];
if (o2 === i && g.unshift(["-v, --version", "Displays current version"]), f.options = (f.options || []).concat(p.options, g), f.options.length > 0 && (f.usage += " [options]"), u += l("Description", f.describe, n), u += l("Usage", [f.usage], d), h || o2 !== i) h || o2 === i || (u += l("Aliases", f.alibi, d));
else {
let e3, t2 = /^__/, i2 = "", o3 = [];
for (e3 in a2) "string" == typeof a2[e3] || t2.test(e3) || o3.push([e3, (a2[e3].describe || [""])[0]]) < 3 && (i2 += `
${c} ${e3} --help`);
u += l("Available Commands", r(o3), n), u += "\n For more info, run any command with the `--help` flag" + i2 + s;
}
return u += l("Options", r(f.options), n), u += l("Examples", f.examples.map(d), n), u;
}(this.bin, this.tree, e || i, this.single));
}
_version() {
console.log(`${this.bin}, ${this.ver}`);
}
};
var lib_default2 = (e, t2) => new o(e, t2);
// src/cli/bin.ts
import { ensureEnv } from "./util/EnsureEnv.js";
import { ensureLibs } from "./util/EnsureLibs.js";
import { ensureNode } from "./util/EnsureNode.js";
import { forwardCommand } from "./util/ForwardCommand.js";
var prog = lib_default2("alinea");
var libs = {
react: "18.0.0",
"react-dom": "18.0.0"
};
prog.version(package_default.version).command("init").describe("Initialize a new Alinea project in the current directory").action(async (args) => {
ensureNode();
ensureLibs(libs);
const { init } = await import("./Init.js");
return init(args);
}).command("dev").alias("serve").describe("Start a development dashboard").option("-c, --config", "Config file location").option("-d, --dir", "Root directory of the project").option("-p, --port", "Port to listen on").option("-b, --base", "Base URL for previews").option("--production", "Use production backend").option("--dev", "Watch alinea sources").action(async (args) => {
ensureNode();
ensureLibs(libs);
ensureEnv(args.dir);
process.env.NODE_ENV = args.production ? "production" : "development";
const { serve } = await import("./Serve.js");
return serve({
...args,
alineaDev: args.dev,
cwd: args.dir,
base: args.base,
onAfterGenerate: forwardCommand,
configFile: args.config,
cmd: "dev"
});
}).command("build").alias("generate").describe("Generate types and content cache").option("-c, --config", "Config file location").option("-d, --dir", "Root directory of the project").option("-w, --watch", "Watch for changes to source files").option(
"--fix",
"Any missing or incorrect properties will be overwritten by their default"
).action(async (args) => {
ensureNode();
ensureLibs(libs);
ensureEnv(args.dir);
process.env.NODE_ENV = "production";
if (args.fix) {
const { generate } = await import("./Generate.js");
for await (const _ of generate({
...args,
cwd: args.dir,
base: args.base,
configFile: args.config,
onAfterGenerate() {
process.exit(0);
},
cmd: "build"
})) {
}
return;
}
const { serve } = await import("./Serve.js");
return serve({
...args,
alineaDev: args.dev,
cwd: args.dir,
production: true,
onAfterGenerate: (env) => {
const isForwarding = forwardCommand(env);
if (!isForwarding) process.exit(0);
},
configFile: args.config,
cmd: "build"
});
});
prog.parse(process.argv);