|
3 | 3 | /** |
4 | 4 | * WPKernel CLI |
5 | 5 | * |
6 | | - * Main executable entry point |
| 6 | + * Main executable entry point. |
7 | 7 | */ |
8 | 8 |
|
9 | 9 | const distInitModule = new URL('../dist/commands/init.js', import.meta.url); |
10 | 10 | const distRunModule = new URL('../dist/cli/run.js', import.meta.url); |
11 | | -const sourceInitModule = new URL('../src/commands/init.ts', import.meta.url); |
12 | | -const sourceRunModule = new URL('../src/cli/run.ts', import.meta.url); |
13 | | -const forceSource = process.env.WPK_CLI_FORCE_SOURCE === '1'; |
14 | 11 |
|
15 | | -let initModuleUrl = distInitModule; |
16 | | -const runModule = await loadCliModule(); |
17 | | - |
18 | | -globalThis.__WPK_CLI_MODULE_URL__ = initModuleUrl.href; |
| 12 | +globalThis.__WPK_CLI_MODULE_URL__ = distInitModule.href; |
19 | 13 |
|
20 | 14 | try { |
21 | | - const { runCli } = runModule; |
| 15 | + const { runCli } = await import(distRunModule.href); |
22 | 16 | await runCli(process.argv.slice(2)); |
23 | 17 | } catch (error) { |
24 | | - console.error('[wpk] fatal', error); |
| 18 | + handleLoadFailure(error); |
25 | 19 | process.exitCode = 1; |
26 | 20 | } |
27 | 21 |
|
28 | | -async function loadCliModule() { |
29 | | - if (!forceSource) { |
30 | | - globalThis.__WPK_CLI_MODULE_URL__ = distInitModule.href; |
31 | | - |
32 | | - try { |
33 | | - return await import(distRunModule.href); |
34 | | - } catch (error) { |
35 | | - if (!isMissingModuleError(error, distRunModule)) { |
36 | | - throw error; |
37 | | - } |
| 22 | +function handleLoadFailure(error) { |
| 23 | + if (isMissingModuleError(error)) { |
| 24 | + console.error( |
| 25 | + '[wpk] missing compiled CLI artifacts. ' + |
| 26 | + 'Build the CLI with "pnpm --filter @wpkernel/cli build" before invoking wpk.' |
| 27 | + ); |
| 28 | + if (error) { |
| 29 | + console.error(error); |
38 | 30 | } |
| 31 | + return; |
39 | 32 | } |
40 | 33 |
|
41 | | - initModuleUrl = sourceInitModule; |
42 | | - globalThis.__WPK_CLI_MODULE_URL__ = sourceInitModule.href; |
43 | | - |
44 | | - try { |
45 | | - const { tsImport } = await import('tsx/esm/api'); |
46 | | - return await tsImport(sourceRunModule.href, import.meta.url); |
47 | | - } catch (innerError) { |
48 | | - if (isMissingSpecifier(innerError, 'tsx/esm/api')) { |
49 | | - throw new Error( |
50 | | - 'The "tsx" peer dependency is required to run the WPKernel CLI from source. ' + |
51 | | - 'Install it (e.g. "pnpm add -D tsx") or build the CLI with ' + |
52 | | - '"pnpm --filter @wpkernel/cli build" before running wpk.' |
53 | | - ); |
54 | | - } |
55 | | - |
56 | | - throw innerError; |
57 | | - } |
58 | | -} |
59 | | - |
60 | | -function isMissingModuleError(error, expectedUrl) { |
61 | | - if (!error || typeof error !== 'object') { |
62 | | - return false; |
63 | | - } |
64 | | - |
65 | | - if ('code' in error && error.code === 'ERR_MODULE_NOT_FOUND') { |
66 | | - if ('url' in error && error.url === expectedUrl.href) { |
67 | | - return true; |
68 | | - } |
69 | | - |
70 | | - if (typeof error.message === 'string') { |
71 | | - return ( |
72 | | - error.message.includes(expectedUrl.href) || |
73 | | - error.message.includes(expectedUrl.pathname) |
74 | | - ); |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - return false; |
| 34 | + console.error('[wpk] fatal', error); |
79 | 35 | } |
80 | 36 |
|
81 | | -function isMissingSpecifier(error, specifier) { |
82 | | - if (!error || typeof error !== 'object') { |
83 | | - return false; |
84 | | - } |
85 | | - |
86 | | - if ( |
87 | | - 'code' in error && |
88 | | - error.code === 'ERR_MODULE_NOT_FOUND' && |
89 | | - typeof error.message === 'string' |
90 | | - ) { |
91 | | - return error.message.includes(specifier); |
92 | | - } |
93 | | - |
94 | | - return false; |
| 37 | +function isMissingModuleError(error) { |
| 38 | + return Boolean( |
| 39 | + error && |
| 40 | + typeof error === 'object' && |
| 41 | + 'code' in error && |
| 42 | + error.code === 'ERR_MODULE_NOT_FOUND' |
| 43 | + ); |
95 | 44 | } |
0 commit comments