@lacrous/omnicloud 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js ADDED
@@ -0,0 +1,161 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli.ts
4
+ import { spawn } from "child_process";
5
+ import { existsSync, readFileSync, realpathSync } from "fs";
6
+ import { createRequire } from "module";
7
+ import { dirname, join, resolve } from "path";
8
+ import { fileURLToPath } from "url";
9
+ var packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
10
+ var serverDir = join(packageRoot, "server");
11
+ var schemaPath = join(serverDir, "prisma", "schema.prisma");
12
+ var pkg = JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8"));
13
+ var USAGE = `omnicloud ${pkg.version}
14
+
15
+ Usage:
16
+ omnicloud start [--migrate] [--env <file>] Run the OmniCloud server
17
+ omnicloud migrate [--env <file>] Apply database migrations
18
+ omnicloud version Print the version
19
+ omnicloud help Show this message
20
+
21
+ Configuration is read from environment variables, then from a .env file in the
22
+ current directory (or the file given with --env). Variables already set in the
23
+ environment are never overwritten. See https://github.com/Lacrous/OmniCloud#configuration.
24
+ `;
25
+ function parseArgs(argv) {
26
+ const [command = "help", ...rest] = argv;
27
+ let migrate2 = false;
28
+ let envFile = null;
29
+ for (let i = 0; i < rest.length; i++) {
30
+ const arg = rest[i];
31
+ if (arg === "--migrate") migrate2 = true;
32
+ else if (arg === "--env") {
33
+ const value = rest[++i];
34
+ if (!value) throw new Error("--env requires a file path");
35
+ envFile = value;
36
+ } else if (arg?.startsWith("--env=")) envFile = arg.slice("--env=".length);
37
+ else throw new Error(`Unknown option: ${arg}`);
38
+ }
39
+ return { command, migrate: migrate2, envFile };
40
+ }
41
+ function parseEnvFile(content) {
42
+ const values = {};
43
+ for (const rawLine of content.split(/\r?\n/)) {
44
+ const line = rawLine.trim();
45
+ if (!line || line.startsWith("#")) continue;
46
+ const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(line);
47
+ if (!match) continue;
48
+ const key = match[1];
49
+ let value = (match[2] ?? "").trim();
50
+ const quote = value[0];
51
+ if ((quote === '"' || quote === "'") && value.endsWith(quote) && value.length >= 2) {
52
+ value = value.slice(1, -1);
53
+ } else {
54
+ const hash = value.search(/\s#/);
55
+ if (hash >= 0) value = value.slice(0, hash).trim();
56
+ }
57
+ if (key) values[key] = value;
58
+ }
59
+ return values;
60
+ }
61
+ function loadEnvFile(path, env) {
62
+ if (!existsSync(path)) return;
63
+ for (const [key, value] of Object.entries(parseEnvFile(readFileSync(path, "utf8")))) {
64
+ if (env[key] === void 0) env[key] = value;
65
+ }
66
+ }
67
+ function prismaCli() {
68
+ const require2 = createRequire(join(packageRoot, "package.json"));
69
+ return require2.resolve("prisma/build/index.js");
70
+ }
71
+ function run(command, args, env) {
72
+ return new Promise((resolvePromise, reject) => {
73
+ const child = spawn(command, args, { stdio: "inherit", env });
74
+ child.on("error", reject);
75
+ child.on("exit", (code, signal) => resolvePromise(signal ? 1 : code ?? 1));
76
+ });
77
+ }
78
+ async function migrate(env) {
79
+ if (!env.DATABASE_URL) {
80
+ throw new Error("DATABASE_URL is not set. Add it to your environment or .env file.");
81
+ }
82
+ const code = await run(
83
+ process.execPath,
84
+ [prismaCli(), "migrate", "deploy", "--schema", schemaPath],
85
+ env
86
+ );
87
+ if (code !== 0) throw new Error(`Database migration failed (exit ${code}).`);
88
+ }
89
+ async function generate(env) {
90
+ const code = await run(process.execPath, [prismaCli(), "generate", "--schema", schemaPath], env);
91
+ if (code !== 0) throw new Error(`Prisma client generation failed (exit ${code}).`);
92
+ }
93
+ async function start(args, env) {
94
+ if (!existsSync(join(serverDir, "index.js"))) {
95
+ throw new Error("The server bundle is missing. Reinstall @lacrous/omnicloud.");
96
+ }
97
+ await generate(env);
98
+ if (args.migrate) await migrate(env);
99
+ env.WEB_DIST_DIR ??= join(serverDir, "web");
100
+ env.NODE_ENV ??= "production";
101
+ const code = await run(process.execPath, [join(serverDir, "index.js")], env);
102
+ if (code !== 0) throw new Error(`Server exited with code ${code}.`);
103
+ }
104
+ async function main(argv, env = process.env) {
105
+ let args;
106
+ try {
107
+ args = parseArgs(argv);
108
+ } catch (error) {
109
+ process.stderr.write(`${error.message}
110
+
111
+ ${USAGE}`);
112
+ return 2;
113
+ }
114
+ if (args.command === "help" || args.command === "--help" || args.command === "-h") {
115
+ process.stdout.write(USAGE);
116
+ return 0;
117
+ }
118
+ if (args.command === "version" || args.command === "--version" || args.command === "-v") {
119
+ process.stdout.write(`${pkg.version}
120
+ `);
121
+ return 0;
122
+ }
123
+ loadEnvFile(resolve(args.envFile ?? ".env"), env);
124
+ try {
125
+ if (args.command === "migrate") {
126
+ await migrate(env);
127
+ await generate(env);
128
+ return 0;
129
+ }
130
+ if (args.command === "start") {
131
+ await start(args, env);
132
+ return 0;
133
+ }
134
+ } catch (error) {
135
+ process.stderr.write(`omnicloud: ${error.message}
136
+ `);
137
+ return 1;
138
+ }
139
+ process.stderr.write(`Unknown command: ${args.command}
140
+
141
+ ${USAGE}`);
142
+ return 2;
143
+ }
144
+ function isEntryPoint() {
145
+ const entry = process.argv[1];
146
+ if (!entry) return false;
147
+ try {
148
+ return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url));
149
+ } catch {
150
+ return false;
151
+ }
152
+ }
153
+ if (isEntryPoint()) {
154
+ main(process.argv.slice(2)).then((code) => process.exit(code));
155
+ }
156
+ export {
157
+ loadEnvFile,
158
+ main,
159
+ parseArgs,
160
+ parseEnvFile
161
+ };