Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,12 @@ export async function handleCli(args: string[]): Promise<CliResult> {
}
}

// Execute CLI if this module is run directly
// Note: This uses eval to prevent Jest/CommonJS from parsing import.meta
// istanbul ignore next - CLI entry point, difficult to test in Jest
// @ts-ignore
try {
const importMeta = eval('import.meta');
if (importMeta && importMeta.url === `file://${process.argv[1]}`) {
handleCli(process.argv.slice(2)).then((result) => {
process.exit(result.exitCode);
}).catch((err) => {
console.error('Unexpected error:', err);
process.exit(1);
});
}
} catch {
// Intentionally ignoring exception: import.meta not available in CommonJS/Jest environments.
// This is expected when the module is imported rather than executed directly.
if (process.env.DEBUG) {
console.debug('Module loaded in CommonJS/Jest environment (import.meta not available)');
}
if (process.argv[1]?.endsWith('generate-docs-cli.ts')) {
handleCli(process.argv.slice(2)).then((result) => {
process.exit(result.exitCode);
}).catch((err) => {
console.error('Unexpected error:', err);
process.exit(1);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* 2. Render README.md from the index
*/

import { realpathSync } from "fs";
import { dirname, resolve } from "path";
import { handleCli as generateIndexCli } from "./generate-readme-index-cli.ts";
import { handleCli as renderReadmeCli } from "./render-readme-cli.ts";

Expand Down Expand Up @@ -59,30 +61,20 @@ export async function handleCli(
}
}

// Execute CLI if this module is run directly
// Note: This uses eval to prevent Jest/CommonJS from parsing import.meta
// Execute CLI if this module is run directly.
// istanbul ignore next - CLI entry point, difficult to test in Jest
// @ts-ignore
try {
const importMeta = eval('import.meta');
if (importMeta && importMeta.url === `file://${process.argv[1]}`) {
// Get the root directory (3 levels up from this file: tools/generator/readme-generator)
const rootDir = new URL("../../../", importMeta.url).pathname;
const args = process.argv.slice(2);
if (process.argv[1]?.endsWith("update-readme-cli.ts")) {
// Derive rootDir from process.argv[1] (3 levels up: tools/generator/readme-generator)
const mainPath = realpathSync(process.argv[1]);
const rootDir = resolve(dirname(mainPath), "../../..");
const args = process.argv.slice(2);

handleCli(args, rootDir)
.then((result) => {
process.exit(result.exitCode);
})
.catch((err) => {
console.error("Unexpected error:", err);
process.exit(1);
});
}
} catch {
// Intentionally ignoring exception: import.meta not available in CommonJS/Jest environments.
// This is expected when the module is imported rather than executed directly.
if (process.env.DEBUG) {
console.debug("Module loaded in CommonJS/Jest environment (import.meta not available)");
}
handleCli(args, rootDir)
.then((result) => {
process.exit(result.exitCode);
})
.catch((err) => {
console.error("Unexpected error:", err);
process.exit(1);
});
}
Loading