@evjs/cli 0.2.14 → 0.2.15

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.
@@ -0,0 +1,9 @@
1
+ import type { CliFlags } from "@evjs/ev/plugin";
2
+ /**
3
+ * Parses extra command arguments into plugin-readable flag values.
4
+ *
5
+ * Supports boolean flags (`--mock`), equals values (`--target=local`),
6
+ * space-separated values (`--target local`), camelCase keys for dashed names,
7
+ * and repeated flags as arrays.
8
+ */
9
+ export declare function parseCliFlags(args: readonly string[]): CliFlags;
@@ -0,0 +1,60 @@
1
+ /** Converts dash-separated CLI flag names to camelCase context keys. */
2
+ function toFlagName(rawName) {
3
+ return rawName.replace(/-([a-zA-Z0-9])/g, (_, char) => char.toUpperCase());
4
+ }
5
+ /** Converts boolean-like flag values while preserving other strings. */
6
+ function coerceFlagValue(value) {
7
+ if (value === "true")
8
+ return true;
9
+ if (value === "false")
10
+ return false;
11
+ return value;
12
+ }
13
+ /** Stores a flag value, preserving repeated flags as an ordered array. */
14
+ function setFlag(flags, name, value) {
15
+ const current = flags[name];
16
+ if (current === undefined) {
17
+ flags[name] = value;
18
+ }
19
+ else if (Array.isArray(current)) {
20
+ current.push(value);
21
+ }
22
+ else {
23
+ flags[name] = [current, value];
24
+ }
25
+ }
26
+ /**
27
+ * Parses extra command arguments into plugin-readable flag values.
28
+ *
29
+ * Supports boolean flags (`--mock`), equals values (`--target=local`),
30
+ * space-separated values (`--target local`), camelCase keys for dashed names,
31
+ * and repeated flags as arrays.
32
+ */
33
+ export function parseCliFlags(args) {
34
+ const flags = {};
35
+ for (let index = 0; index < args.length; index++) {
36
+ const arg = args[index];
37
+ if (arg === "--")
38
+ break;
39
+ if (!arg?.startsWith("--") || arg === "--")
40
+ continue;
41
+ const rawFlag = arg.slice(2);
42
+ if (!rawFlag)
43
+ continue;
44
+ const equalsIndex = rawFlag.indexOf("=");
45
+ if (equalsIndex >= 0) {
46
+ const name = toFlagName(rawFlag.slice(0, equalsIndex));
47
+ setFlag(flags, name, coerceFlagValue(rawFlag.slice(equalsIndex + 1)));
48
+ continue;
49
+ }
50
+ const nextArg = args[index + 1];
51
+ if (nextArg && !nextArg.startsWith("-")) {
52
+ setFlag(flags, toFlagName(rawFlag), coerceFlagValue(nextArg));
53
+ index++;
54
+ }
55
+ else {
56
+ setFlag(flags, toFlagName(rawFlag), true);
57
+ }
58
+ }
59
+ return flags;
60
+ }
package/dist/cli.js CHANGED
@@ -4,6 +4,7 @@ import path from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { configure, getConsoleSink, getLogger } from "@logtape/logtape";
6
6
  import { Command } from "commander";
7
+ import { parseCliFlags } from "./cli-options.js";
7
8
  import { build, dev, prepare } from "./index.js";
8
9
  import { formatInspectCommandErrorJson, runInspectCommand, } from "./inspect-command.js";
9
10
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -45,12 +46,14 @@ const logger = getLogger(["evjs", "cli"]);
45
46
  program
46
47
  .command("dev")
47
48
  .description("Start development server")
48
- .action(async () => {
49
+ .allowUnknownOption(true)
50
+ .action(async (_options, command) => {
49
51
  const cwd = process.cwd();
52
+ const flags = parseCliFlags(command.args);
50
53
  const { loadConfig } = await import("./load-config.js");
51
54
  const config = await loadConfig(cwd);
52
55
  try {
53
- await dev(config ?? undefined, { cwd });
56
+ await dev(config ?? undefined, { cwd, flags });
54
57
  }
55
58
  catch (err) {
56
59
  logger.error `Failed to start dev server: ${err}`;
@@ -60,12 +63,14 @@ program
60
63
  program
61
64
  .command("build")
62
65
  .description("Build project for production")
63
- .action(async () => {
66
+ .allowUnknownOption(true)
67
+ .action(async (_options, command) => {
64
68
  const cwd = process.cwd();
69
+ const flags = parseCliFlags(command.args);
65
70
  const { loadConfig } = await import("./load-config.js");
66
71
  const config = await loadConfig(cwd);
67
72
  try {
68
- await build(config ?? undefined, { cwd });
73
+ await build(config ?? undefined, { cwd, flags });
69
74
  }
70
75
  catch (err) {
71
76
  logger.error `Build failed: ${err}`;
@@ -75,12 +80,14 @@ program
75
80
  program
76
81
  .command("prepare")
77
82
  .description("Generate .ev framework IR without running a bundler")
78
- .action(async () => {
83
+ .allowUnknownOption(true)
84
+ .action(async (_options, command) => {
79
85
  const cwd = process.cwd();
86
+ const flags = parseCliFlags(command.args);
80
87
  const { loadConfig } = await import("./load-config.js");
81
88
  const config = await loadConfig(cwd);
82
89
  try {
83
- await prepare(config ?? undefined, { cwd });
90
+ await prepare(config ?? undefined, { cwd, flags });
84
91
  }
85
92
  catch (err) {
86
93
  logger.error `Prepare failed: ${err}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evjs/cli",
3
- "version": "0.2.14",
3
+ "version": "0.2.15",
4
4
  "description": "Command-line wrapper for the evjs framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -28,8 +28,8 @@
28
28
  "check-types": "tsc --noEmit"
29
29
  },
30
30
  "dependencies": {
31
- "@evjs/bundler-utoopack": "0.2.14",
32
- "@evjs/ev": "0.2.14",
31
+ "@evjs/bundler-utoopack": "0.2.15",
32
+ "@evjs/ev": "0.2.15",
33
33
  "@logtape/logtape": "^2.0.4",
34
34
  "commander": "^12.1.0"
35
35
  },