wp-typia 0.21.0 → 0.22.0

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.
@@ -2,5 +2,7 @@
2
2
  // Do not edit directly.
3
3
 
4
4
  export declare const fullRuntimeCommands: readonly string[];
5
+ export declare const interactiveRuntimeCommands: readonly string[];
5
6
  export declare const longValueOptions: readonly string[];
7
+ export declare const reservedCommands: readonly string[];
6
8
  export declare const shortValueOptions: readonly string[];
@@ -7,6 +7,11 @@ export const fullRuntimeCommands = Object.freeze([
7
7
  'completions',
8
8
  'complete',
9
9
  ]);
10
+ export const interactiveRuntimeCommands = Object.freeze([
11
+ 'create',
12
+ 'add',
13
+ 'migrate',
14
+ ]);
10
15
  export const longValueOptions = Object.freeze([
11
16
  '--alternate-render-targets',
12
17
  '--anchor',
@@ -41,4 +46,19 @@ export const longValueOptions = Object.freeze([
41
46
  '--to-migration-version',
42
47
  '--variant',
43
48
  ]);
49
+ export const reservedCommands = Object.freeze([
50
+ 'create',
51
+ 'init',
52
+ 'sync',
53
+ 'add',
54
+ 'migrate',
55
+ 'templates',
56
+ 'doctor',
57
+ 'mcp',
58
+ 'help',
59
+ 'version',
60
+ 'skills',
61
+ 'completions',
62
+ 'complete',
63
+ ]);
44
64
  export const shortValueOptions = Object.freeze(['-c', '-p', '-t']);
@@ -0,0 +1,34 @@
1
+ type RuntimeRoutingValues = readonly string[] | ReadonlySet<string>;
2
+
3
+ type RuntimeRoutingMetadata = {
4
+ longValueOptions: RuntimeRoutingValues;
5
+ reservedCommands: RuntimeRoutingValues;
6
+ shortValueOptions: RuntimeRoutingValues;
7
+ };
8
+
9
+ type RuntimeRoutingStream = {
10
+ isTTY?: boolean;
11
+ };
12
+
13
+ export declare function getRuntimeRoutingInvocation(
14
+ argv: readonly string[],
15
+ metadata: RuntimeRoutingMetadata,
16
+ ): {
17
+ command: string | undefined;
18
+ isSinglePositionalAlias: boolean;
19
+ positionals: string[];
20
+ };
21
+
22
+ export declare function shouldRouteToFullRuntime(options: {
23
+ argv: readonly string[];
24
+ fullRuntimeCommands: RuntimeRoutingValues;
25
+ hasBuiltRuntime: boolean;
26
+ hasWorkingBun: boolean;
27
+ interactiveRuntimeCommands: RuntimeRoutingValues;
28
+ longValueOptions: RuntimeRoutingValues;
29
+ reservedCommands: RuntimeRoutingValues;
30
+ shortValueOptions: RuntimeRoutingValues;
31
+ stdin?: RuntimeRoutingStream;
32
+ stdout?: RuntimeRoutingStream;
33
+ term?: string;
34
+ }): boolean;
@@ -0,0 +1,124 @@
1
+ import { collectPositionalIndexes } from './argv-walker.js';
2
+
3
+ function normalizeSet(values) {
4
+ return values instanceof Set ? values : new Set(values);
5
+ }
6
+
7
+ function hasBooleanFlagBeforeTerminator(argv, longFlag, shortFlag) {
8
+ let matched = false;
9
+ for (const arg of argv) {
10
+ if (arg === '--') {
11
+ break;
12
+ }
13
+ if (arg === longFlag || (shortFlag && arg === shortFlag)) {
14
+ matched = true;
15
+ continue;
16
+ }
17
+ if (arg.startsWith(`${longFlag}=`)) {
18
+ const value = arg.slice(longFlag.length + 1).toLowerCase();
19
+ matched = value !== 'false' && value !== '0' && value !== 'no';
20
+ }
21
+ }
22
+
23
+ return matched;
24
+ }
25
+
26
+ function hasLongOptionBeforeTerminator(argv, optionName) {
27
+ for (let index = 0; index < argv.length; index += 1) {
28
+ const arg = argv[index];
29
+ if (arg === '--') {
30
+ return false;
31
+ }
32
+ if (arg === optionName || arg.startsWith(`${optionName}=`)) {
33
+ return true;
34
+ }
35
+ }
36
+
37
+ return false;
38
+ }
39
+
40
+ function isInteractiveTerminal({ stdin, stdout, term }) {
41
+ return Boolean(stdin?.isTTY) && Boolean(stdout?.isTTY) && term !== 'dumb';
42
+ }
43
+
44
+ export function getRuntimeRoutingInvocation(argv, metadata) {
45
+ const positionalIndexes = collectPositionalIndexes(argv, metadata);
46
+ const positionals = positionalIndexes
47
+ .map((index) => argv[index])
48
+ .filter((value) => typeof value === 'string' && value.length > 0);
49
+ const command = positionals[0];
50
+ const reservedCommandSet = normalizeSet(metadata.reservedCommands);
51
+ const isSinglePositionalAlias =
52
+ Boolean(command) &&
53
+ positionals.length === 1 &&
54
+ !reservedCommandSet.has(command);
55
+
56
+ return {
57
+ command,
58
+ isSinglePositionalAlias,
59
+ positionals,
60
+ };
61
+ }
62
+
63
+ export function shouldRouteToFullRuntime({
64
+ argv,
65
+ fullRuntimeCommands,
66
+ hasBuiltRuntime,
67
+ hasWorkingBun,
68
+ interactiveRuntimeCommands,
69
+ longValueOptions,
70
+ reservedCommands,
71
+ shortValueOptions,
72
+ stdin = process.stdin,
73
+ stdout = process.stdout,
74
+ term = process.env.TERM,
75
+ }) {
76
+ if (!hasWorkingBun || !hasBuiltRuntime) {
77
+ return false;
78
+ }
79
+
80
+ const fullRuntimeCommandSet = normalizeSet(fullRuntimeCommands);
81
+ const interactiveRuntimeCommandSet = normalizeSet(interactiveRuntimeCommands);
82
+ const invocation = getRuntimeRoutingInvocation(argv, {
83
+ longValueOptions,
84
+ reservedCommands,
85
+ shortValueOptions,
86
+ });
87
+
88
+ if (invocation.command && fullRuntimeCommandSet.has(invocation.command)) {
89
+ return true;
90
+ }
91
+
92
+ if (
93
+ !isInteractiveTerminal({
94
+ stdin,
95
+ stdout,
96
+ term,
97
+ })
98
+ ) {
99
+ return false;
100
+ }
101
+
102
+ if (
103
+ invocation.command === 'help' ||
104
+ invocation.command === 'version' ||
105
+ hasBooleanFlagBeforeTerminator(argv, '--help', '-h') ||
106
+ hasBooleanFlagBeforeTerminator(argv, '--version', '-v') ||
107
+ hasLongOptionBeforeTerminator(argv, '--format') ||
108
+ hasBooleanFlagBeforeTerminator(argv, '--yes', '-y')
109
+ ) {
110
+ return false;
111
+ }
112
+
113
+ if (
114
+ invocation.command &&
115
+ interactiveRuntimeCommandSet.has(invocation.command)
116
+ ) {
117
+ return true;
118
+ }
119
+
120
+ return (
121
+ invocation.isSinglePositionalAlias &&
122
+ interactiveRuntimeCommandSet.has('create')
123
+ );
124
+ }
package/bin/wp-typia.js CHANGED
@@ -7,10 +7,15 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
7
7
 
8
8
  import {
9
9
  fullRuntimeCommands,
10
+ interactiveRuntimeCommands,
10
11
  longValueOptions,
12
+ reservedCommands,
11
13
  shortValueOptions,
12
14
  } from './routing-metadata.generated.js';
13
- import { findFirstPositional } from './argv-walker.js';
15
+ import {
16
+ getRuntimeRoutingInvocation,
17
+ shouldRouteToFullRuntime,
18
+ } from './runtime-routing.js';
14
19
 
15
20
  const packageRoot = path.resolve(
16
21
  path.dirname(fileURLToPath(import.meta.url)),
@@ -20,7 +25,9 @@ const cliEntrypoint = path.join(packageRoot, 'dist-bunli', 'cli.js');
20
25
  const nodeCliEntrypoint = path.join(packageRoot, 'dist-bunli', 'node-cli.js');
21
26
  const bunBinary = process.env.BUN_BIN || 'bun';
22
27
  const fullRuntimeCommandSet = new Set(fullRuntimeCommands);
28
+ const interactiveRuntimeCommandSet = new Set(interactiveRuntimeCommands);
23
29
  const longValueOptionSet = new Set(longValueOptions);
30
+ const reservedCommandSet = new Set(reservedCommands);
24
31
  const shortValueOptionSet = new Set(shortValueOptions);
25
32
  const buildScriptEntrypoint = path.join(
26
33
  packageRoot,
@@ -69,17 +76,29 @@ function ensureBuiltRuntime() {
69
76
  }
70
77
 
71
78
  const argv = process.argv.slice(2);
72
- const command = findFirstPositional(argv, {
79
+ const { command } = getRuntimeRoutingInvocation(argv, {
73
80
  longValueOptions: longValueOptionSet,
81
+ reservedCommands: reservedCommandSet,
74
82
  shortValueOptions: shortValueOptionSet,
75
83
  });
76
- const shouldUseFullRuntime = command
84
+ const commandRequiresFullRuntime = command
77
85
  ? fullRuntimeCommandSet.has(command)
78
86
  : false;
79
87
  const hasBuiltRuntime = ensureBuiltRuntime();
80
88
  const hasWorkingBun = isWorkingBunBinary();
89
+ const shouldUseFullRuntime = shouldRouteToFullRuntime({
90
+ argv,
91
+ fullRuntimeCommands: fullRuntimeCommandSet,
92
+ hasBuiltRuntime,
93
+ hasWorkingBun,
94
+ interactiveRuntimeCommands: interactiveRuntimeCommandSet,
95
+ longValueOptions: longValueOptionSet,
96
+ reservedCommands: reservedCommandSet,
97
+ shortValueOptions: shortValueOptionSet,
98
+ });
81
99
 
82
- // Keep common help on the human-readable Node fallback even when Bun is present.
100
+ // Keep common help and explicit non-TUI calls on the human-readable Node fallback
101
+ // even when Bun is present.
83
102
  if (hasWorkingBun && hasBuiltRuntime && shouldUseFullRuntime) {
84
103
  const result = spawnSync(bunBinary, [cliEntrypoint, ...argv], {
85
104
  cwd: process.cwd(),
@@ -89,7 +108,7 @@ if (hasWorkingBun && hasBuiltRuntime && shouldUseFullRuntime) {
89
108
  process.exit(result.status ?? 1);
90
109
  }
91
110
 
92
- if (shouldUseFullRuntime) {
111
+ if (commandRequiresFullRuntime) {
93
112
  if (!hasBuiltRuntime) {
94
113
  console.error(
95
114
  'Error: wp-typia could not locate its built CLI runtime. Reinstall the published package, or run `bun run build` when using a source checkout.',