MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / handler

Method handler

packages/cli/src/commands/CompileCommand.ts:34–88  ·  view source on GitHub ↗

* @inheritDoc

(args: ArgumentsCamelCase<CompileArgs>)

Source from the content-addressed store, hash-verified

32 * @inheritDoc
33 */
34 async handler(args: ArgumentsCamelCase<CompileArgs>) {
35 const config = await CLIHelper.getConfiguration(args.contextName, args.config);
36 const settings = CLIHelper.getSettings();
37 config.set('debug', !!settings.verbose);
38 const metadata = await new MetadataDiscovery(
39 new MetadataStorage(),
40 config.getDriver().getPlatform(),
41 config,
42 ).discover(false);
43
44 // Default output path to next to the ORM config file
45 if (!args.out) {
46 const configPaths = args.config ?? (await CLIHelper.getConfigPaths());
47
48 for (const configPath of configPaths) {
49 const absPath = fs.absolutePath(configPath);
50
51 if (fs.pathExists(absPath)) {
52 args.out = resolve(dirname(absPath), 'compiled-functions.js');
53 break;
54 }
55 }
56 }
57
58 const captured = CompileCommand.capture(metadata, config);
59
60 const entries = captured.map(({ key, contextKeys, code }) => {
61 const params = contextKeys.join(', ');
62 const indentedCode = code.replace(/\n/g, '\n ');
63 return ` '${key}': function(${params}) {\n ${indentedCode}\n }`;
64 });
65
66 const esm = CLIHelper.isESM();
67 const version = Utils.getORMVersion();
68 const output = esm
69 ? `export default {\n __version: '${version}',\n${entries.join(',\n')}\n};\n`
70 : `'use strict';\nmodule.exports = {\n __version: '${version}',\n${entries.join(',\n')}\n};\n`;
71 const outPath = args.out ?? resolve(process.cwd(), 'compiled-functions.js');
72 const dtsPath = outPath.replace(/\.js$/, '.d.ts');
73 const dts = esm
74 ? `import type { CompiledFunctions } from '@mikro-orm/core';\ndeclare const compiledFunctions: CompiledFunctions;\nexport default compiledFunctions;\n`
75 : `import type { CompiledFunctions } from '@mikro-orm/core';\ndeclare const compiledFunctions: CompiledFunctions;\nexport = compiledFunctions;\n`;
76 mkdirSync(dirname(outPath), { recursive: true });
77 writeFileSync(outPath, output);
78 writeFileSync(dtsPath, dts);
79
80 CLIHelper.dump(colors.green(`Compiled functions generated to ${outPath} (${captured.length} functions)`));
81 CLIHelper.dump(`\nExample usage in your ORM config:\n`);
82 const importPath = esm ? './compiled-functions.js' : './compiled-functions';
83 CLIHelper.dump(
84 ` ${esm ? 'import' : 'const'} compiledFunctions ${esm ? 'from ' : '= require('}${colors.cyan(`'${importPath}'`)}${esm ? '' : ')'};`,
85 );
86 CLIHelper.dump('');
87 CLIHelper.dump(` export default defineConfig({ compiledFunctions });\n`);
88 }
89
90 static capture(metadata: MetadataStorage, config: Configuration) {
91 const captured: { key: string; contextKeys: string[]; code: string }[] = [];

Callers

nothing calls this directly

Calls 15

getSettingsMethod · 0.80
discoverMethod · 0.80
getConfigPathsMethod · 0.80
absolutePathMethod · 0.80
pathExistsMethod · 0.80
captureMethod · 0.80
isESMMethod · 0.80
getORMVersionMethod · 0.80
dumpMethod · 0.80
setMethod · 0.65
getPlatformMethod · 0.65
joinMethod · 0.65

Tested by

no test coverage detected