|
6 | 6 | * 2. Render README.md from the index |
7 | 7 | */ |
8 | 8 |
|
| 9 | +import { realpathSync } from "fs"; |
| 10 | +import { dirname, resolve } from "path"; |
9 | 11 | import { handleCli as generateIndexCli } from "./generate-readme-index-cli.ts"; |
10 | 12 | import { handleCli as renderReadmeCli } from "./render-readme-cli.ts"; |
11 | 13 |
|
@@ -59,30 +61,20 @@ export async function handleCli( |
59 | 61 | } |
60 | 62 | } |
61 | 63 |
|
62 | | -// Execute CLI if this module is run directly |
63 | | -// Note: This uses eval to prevent Jest/CommonJS from parsing import.meta |
| 64 | +// Execute CLI if this module is run directly. |
64 | 65 | // istanbul ignore next - CLI entry point, difficult to test in Jest |
65 | | -// @ts-ignore |
66 | | -try { |
67 | | - const importMeta = eval('import.meta'); |
68 | | - if (importMeta && importMeta.url === `file://${process.argv[1]}`) { |
69 | | - // Get the root directory (3 levels up from this file: tools/generator/readme-generator) |
70 | | - const rootDir = new URL("../../../", importMeta.url).pathname; |
71 | | - const args = process.argv.slice(2); |
| 66 | +if (process.argv[1]?.endsWith("update-readme-cli.ts")) { |
| 67 | + // Derive rootDir from process.argv[1] (3 levels up: tools/generator/readme-generator) |
| 68 | + const mainPath = realpathSync(process.argv[1]); |
| 69 | + const rootDir = resolve(dirname(mainPath), "../../.."); |
| 70 | + const args = process.argv.slice(2); |
72 | 71 |
|
73 | | - handleCli(args, rootDir) |
74 | | - .then((result) => { |
75 | | - process.exit(result.exitCode); |
76 | | - }) |
77 | | - .catch((err) => { |
78 | | - console.error("Unexpected error:", err); |
79 | | - process.exit(1); |
80 | | - }); |
81 | | - } |
82 | | -} catch { |
83 | | - // Intentionally ignoring exception: import.meta not available in CommonJS/Jest environments. |
84 | | - // This is expected when the module is imported rather than executed directly. |
85 | | - if (process.env.DEBUG) { |
86 | | - console.debug("Module loaded in CommonJS/Jest environment (import.meta not available)"); |
87 | | - } |
| 72 | + handleCli(args, rootDir) |
| 73 | + .then((result) => { |
| 74 | + process.exit(result.exitCode); |
| 75 | + }) |
| 76 | + .catch((err) => { |
| 77 | + console.error("Unexpected error:", err); |
| 78 | + process.exit(1); |
| 79 | + }); |
88 | 80 | } |
0 commit comments