{"version":3,"file":"prepare-config-artifacts.mjs","names":[],"sources":["../../../src/libs/compile/prepare-config-artifacts.ts"],"sourcesContent":["import { mkdir, readFile, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport ts from \"typescript\";\nimport { LucidError } from \"../../utils/errors/index.js\";\nimport {\n\ttype ConfigArtifactKey,\n\tconfigArtifactEntries,\n\ttype PreparedConfigArtifacts,\n} from \"./config-artifacts.js\";\n\nconst runtimeSafeImportPaths = new Map([\n\t[\"@lucidcms/runtime-node\", \"@lucidcms/runtime-node/runtime\"],\n\t[\"@lucidcms/runtime-cloudflare\", \"@lucidcms/runtime-cloudflare/runtime\"],\n]);\n\nconst artifactProperties = {\n\tconfig: \"config\",\n\tdb: \"db\",\n\truntime: \"runtime\",\n} as const;\n\n/**\n * Picks the TypeScript parser mode that matches the config file extension.\n */\nconst getScriptKind = (configPath: string) => {\n\tif (configPath.endsWith(\".tsx\")) return ts.ScriptKind.TSX;\n\tif (configPath.endsWith(\".jsx\")) return ts.ScriptKind.JSX;\n\tif (configPath.endsWith(\".js\") || configPath.endsWith(\".mjs\")) {\n\t\treturn ts.ScriptKind.JS;\n\t}\n\treturn ts.ScriptKind.TS;\n};\n\n/**\n * Adds every local name declared by a binding pattern to the supplied set.\n */\nconst collectBindingNames = (\n\tname: ts.BindingName,\n\tnames: Set<string>,\n): void => {\n\tif (ts.isIdentifier(name)) {\n\t\tnames.add(name.text);\n\t\treturn;\n\t}\n\n\tfor (const element of name.elements) {\n\t\tif (ts.isOmittedExpression(element)) continue;\n\t\tcollectBindingNames(element.name, names);\n\t}\n};\n\n/**\n * Collects names declared inside a node so later passes can ignore local declarations.\n */\nconst collectDeclaredNames = (node: ts.Node): Set<string> => {\n\tconst names = new Set<string>();\n\n\tconst visit = (child: ts.Node) => {\n\t\tif (\n\t\t\t(ts.isFunctionDeclaration(child) ||\n\t\t\t\tts.isClassDeclaration(child) ||\n\t\t\t\tts.isInterfaceDeclaration(child) ||\n\t\t\t\tts.isTypeAliasDeclaration(child) ||\n\t\t\t\tts.isEnumDeclaration(child)) &&\n\t\t\tchild.name\n\t\t) {\n\t\t\tnames.add(child.name.text);\n\t\t}\n\n\t\tif (ts.isVariableDeclaration(child)) {\n\t\t\tcollectBindingNames(child.name, names);\n\t\t}\n\n\t\tif (ts.isParameter(child)) {\n\t\t\tcollectBindingNames(child.name, names);\n\t\t}\n\n\t\tts.forEachChild(child, visit);\n\t};\n\n\tvisit(node);\n\treturn names;\n};\n\n/**\n * Detects identifiers that introduce a declaration instead of reading a value.\n */\nconst isDeclarationIdentifier = (node: ts.Identifier): boolean => {\n\tconst parent = node.parent;\n\n\treturn (\n\t\t(ts.isVariableDeclaration(parent) && parent.name === node) ||\n\t\t(ts.isFunctionDeclaration(parent) && parent.name === node) ||\n\t\t(ts.isClassDeclaration(parent) && parent.name === node) ||\n\t\t(ts.isInterfaceDeclaration(parent) && parent.name === node) ||\n\t\t(ts.isTypeAliasDeclaration(parent) && parent.name === node) ||\n\t\t(ts.isEnumDeclaration(parent) && parent.name === node) ||\n\t\t(ts.isParameter(parent) && parent.name === node) ||\n\t\t(ts.isImportSpecifier(parent) && parent.name === node) ||\n\t\t(ts.isImportClause(parent) && parent.name === node) ||\n\t\t(ts.isNamespaceImport(parent) && parent.name === node)\n\t);\n};\n\n/**\n * Detects property names where the identifier is syntax, not a referenced value.\n */\nconst isPropertyNameIdentifier = (node: ts.Identifier): boolean => {\n\tconst parent = node.parent;\n\n\tif (ts.isShorthandPropertyAssignment(parent)) {\n\t\treturn false;\n\t}\n\n\treturn (\n\t\t(ts.isPropertyAccessExpression(parent) && parent.name === node) ||\n\t\t(ts.isPropertyAssignment(parent) && parent.name === node) ||\n\t\t(ts.isMethodDeclaration(parent) && parent.name === node) ||\n\t\t(ts.isPropertyDeclaration(parent) && parent.name === node) ||\n\t\t(ts.isPropertySignature(parent) && parent.name === node) ||\n\t\t(ts.isMethodSignature(parent) && parent.name === node) ||\n\t\t(ts.isGetAccessorDeclaration(parent) && parent.name === node) ||\n\t\t(ts.isSetAccessorDeclaration(parent) && parent.name === node) ||\n\t\t(ts.isQualifiedName(parent) && parent.right === node)\n\t);\n};\n\n/**\n * Finds external identifiers read by a node after filtering local declarations.\n */\nconst collectReferencedIdentifiers = (node: ts.Node): Set<string> => {\n\tconst referenced = new Set<string>();\n\tconst declared = collectDeclaredNames(node);\n\n\tconst visit = (child: ts.Node) => {\n\t\tif (\n\t\t\tts.isIdentifier(child) &&\n\t\t\t!declared.has(child.text) &&\n\t\t\t!isDeclarationIdentifier(child) &&\n\t\t\t!isPropertyNameIdentifier(child)\n\t\t) {\n\t\t\treferenced.add(child.text);\n\t\t}\n\n\t\tts.forEachChild(child, visit);\n\t};\n\n\tvisit(node);\n\treturn referenced;\n};\n\nconst getStatementText = (sourceFile: ts.SourceFile, statement: ts.Statement) =>\n\tstatement.getFullText(sourceFile).trim();\n\nconst stripExportModifier = (statementText: string) =>\n\tstatementText\n\t\t.replace(/^export\\s+declare\\s+/, \"declare \")\n\t\t.replace(/^export\\s+/, \"\");\n\n/**\n * Finds the object passed to `export default configureLucid(...)`.\n */\nconst findDefaultConfigureCall = (sourceFile: ts.SourceFile) => {\n\tfor (const statement of sourceFile.statements) {\n\t\tif (!ts.isExportAssignment(statement)) continue;\n\t\tif (!ts.isCallExpression(statement.expression)) continue;\n\n\t\tconst [definition] = statement.expression.arguments;\n\t\tif (definition && ts.isObjectLiteralExpression(definition)) {\n\t\t\treturn definition;\n\t\t}\n\t}\n\n\tthrow new LucidError({\n\t\tmessage:\n\t\t\t\"Lucid config artifact splitting requires `export default configureLucid({ runtime, db, config })`.\",\n\t});\n};\n\n/**\n * Reads an object property name when the splitter supports it.\n */\nconst getPropertyName = (name: ts.PropertyName): string | undefined => {\n\tif (ts.isIdentifier(name) || ts.isStringLiteral(name)) {\n\t\treturn name.text;\n\t}\n};\n\n/**\n * Gets a top-level config artifact expression, including shorthand properties.\n */\nconst getObjectPropertyExpression = (\n\tobject: ts.ObjectLiteralExpression,\n\tpropertyName: string,\n): ts.Expression => {\n\tfor (const property of object.properties) {\n\t\tif (\n\t\t\tts.isShorthandPropertyAssignment(property) &&\n\t\t\tproperty.name.text === propertyName\n\t\t) {\n\t\t\treturn property.name;\n\t\t}\n\n\t\tif (\n\t\t\tts.isPropertyAssignment(property) &&\n\t\t\tgetPropertyName(property.name) === propertyName\n\t\t) {\n\t\t\treturn property.initializer;\n\t\t}\n\t}\n\n\tthrow new LucidError({\n\t\tmessage: `Lucid config is missing the top-level \\`${propertyName}\\` property.`,\n\t});\n};\n\n/**\n * Returns the exported `env` schema expression when the project defines one.\n */\nconst getExportedEnvExpression = (\n\tsourceFile: ts.SourceFile,\n): ts.Expression | undefined => {\n\tfor (const statement of sourceFile.statements) {\n\t\tif (!ts.isVariableStatement(statement)) continue;\n\t\tconst hasExport = statement.modifiers?.some(\n\t\t\t(modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword,\n\t\t);\n\t\tif (!hasExport) continue;\n\n\t\tfor (const declaration of statement.declarationList.declarations) {\n\t\t\tif (\n\t\t\t\tts.isIdentifier(declaration.name) &&\n\t\t\t\tdeclaration.name.text === \"env\" &&\n\t\t\t\tdeclaration.initializer\n\t\t\t) {\n\t\t\t\treturn declaration.initializer;\n\t\t\t}\n\t\t}\n\t}\n};\n\n/**\n * Maps top-level declarations by name so artifacts can pull in dependencies.\n */\nconst getTopLevelDeclarations = (sourceFile: ts.SourceFile) => {\n\tconst declarations = new Map<string, ts.Statement>();\n\n\tfor (const statement of sourceFile.statements) {\n\t\tif (ts.isImportDeclaration(statement) || ts.isExportAssignment(statement)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst names = collectDeclaredNames(statement);\n\t\tfor (const name of names) {\n\t\t\tdeclarations.set(name, statement);\n\t\t}\n\t}\n\n\treturn declarations;\n};\n\n/**\n * Walks from an artifact expression to the top-level statements it depends on.\n */\nconst resolveNeededStatements = (\n\tsourceFile: ts.SourceFile,\n\trootNode: ts.Node | undefined,\n) => {\n\tconst declarations = getTopLevelDeclarations(sourceFile);\n\tconst queue = rootNode\n\t\t? Array.from(collectReferencedIdentifiers(rootNode))\n\t\t: [];\n\tconst neededStatements = new Set<ts.Statement>();\n\tconst neededIdentifiers = new Set(queue);\n\n\twhile (queue.length > 0) {\n\t\tconst identifier = queue.shift();\n\t\tif (!identifier) continue;\n\n\t\tconst declaration = declarations.get(identifier);\n\t\tif (!declaration || neededStatements.has(declaration)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tneededStatements.add(declaration);\n\n\t\tfor (const referenced of collectReferencedIdentifiers(declaration)) {\n\t\t\tif (neededIdentifiers.has(referenced)) continue;\n\t\t\tneededIdentifiers.add(referenced);\n\t\t\tqueue.push(referenced);\n\t\t}\n\t}\n\n\treturn {\n\t\tneededStatements,\n\t\tneededIdentifiers,\n\t};\n};\n\n/**\n * Renders a named import specifier while preserving aliases and type-only imports.\n */\nconst renderImportSpecifier = (\n\tsourceFile: ts.SourceFile,\n\tspecifier: ts.ImportSpecifier,\n) => {\n\tconst importedName = specifier.propertyName\n\t\t? `${specifier.propertyName.getText(sourceFile)} as ${specifier.name.getText(sourceFile)}`\n\t\t: specifier.name.getText(sourceFile);\n\n\treturn specifier.isTypeOnly ? `type ${importedName}` : importedName;\n};\n\nconst ensureRelativeImportPath = (importPath: string) =>\n\timportPath.startsWith(\".\") ? importPath : `./${importPath}`;\n\nconst toImportPath = (filePath: string) => filePath.split(path.sep).join(\"/\");\n\n/**\n * Rewrites relative imports so generated artifacts can live in the output tree.\n */\nconst rewriteRelativeImportPath = (props: {\n\tmodulePath: string;\n\tsourceFile: ts.SourceFile;\n\ttarget: ConfigArtifactKey;\n\toutputPath: string;\n}) => {\n\tif (!props.modulePath.startsWith(\".\")) {\n\t\treturn props.modulePath;\n\t}\n\n\tconst sourceDir = path.dirname(props.sourceFile.fileName);\n\tconst artifactDir = path.dirname(\n\t\tpath.join(props.outputPath, `${configArtifactEntries[props.target]}.ts`),\n\t);\n\tconst resolvedModulePath = path.resolve(sourceDir, props.modulePath);\n\tconst relativeModulePath = path.relative(artifactDir, resolvedModulePath);\n\n\treturn ensureRelativeImportPath(toImportPath(relativeModulePath));\n};\n\n/**\n * Renders an import with only the specifiers required by a generated artifact.\n */\nconst renderImport = (\n\tsourceFile: ts.SourceFile,\n\tstatement: ts.ImportDeclaration,\n\tneededIdentifiers: Set<string>,\n\ttarget: ConfigArtifactKey,\n\toutputPath: string,\n) => {\n\tconst importClause = statement.importClause;\n\n\tif (!importClause) {\n\t\treturn undefined;\n\t}\n\n\tconst parts: string[] = [];\n\n\tif (importClause.name && neededIdentifiers.has(importClause.name.text)) {\n\t\tparts.push(importClause.name.text);\n\t}\n\n\tif (importClause.namedBindings) {\n\t\tif (ts.isNamespaceImport(importClause.namedBindings)) {\n\t\t\tif (neededIdentifiers.has(importClause.namedBindings.name.text)) {\n\t\t\t\tparts.push(`* as ${importClause.namedBindings.name.text}`);\n\t\t\t}\n\t\t} else {\n\t\t\tconst namedSpecifiers = importClause.namedBindings.elements.filter(\n\t\t\t\t(specifier) => neededIdentifiers.has(specifier.name.text),\n\t\t\t);\n\n\t\t\tif (namedSpecifiers.length > 0) {\n\t\t\t\tparts.push(\n\t\t\t\t\t`{ ${namedSpecifiers\n\t\t\t\t\t\t.map((specifier) => renderImportSpecifier(sourceFile, specifier))\n\t\t\t\t\t\t.join(\", \")} }`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\tif (parts.length === 0) {\n\t\treturn undefined;\n\t}\n\n\tconst modulePath = ts.isStringLiteral(statement.moduleSpecifier)\n\t\t? statement.moduleSpecifier.text\n\t\t: statement.moduleSpecifier.getText(sourceFile);\n\tconst runtimeSafeModulePath =\n\t\ttarget === \"runtime\"\n\t\t\t? (runtimeSafeImportPaths.get(modulePath) ?? modulePath)\n\t\t\t: modulePath;\n\tconst rewrittenModulePath = rewriteRelativeImportPath({\n\t\tmodulePath: runtimeSafeModulePath,\n\t\tsourceFile,\n\t\ttarget,\n\t\toutputPath,\n\t});\n\tconst typeKeyword = importClause.isTypeOnly ? \" type\" : \"\";\n\n\treturn `import${typeKeyword} ${parts.join(\", \")} from ${JSON.stringify(\n\t\trewrittenModulePath,\n\t)};`;\n};\n\n/**\n * Builds the source text for one split config artifact.\n */\nconst renderArtifactSource = (props: {\n\tsourceFile: ts.SourceFile;\n\ttarget: ConfigArtifactKey;\n\toutputPath: string;\n\texpression?: ts.Expression;\n}) => {\n\tconst { neededStatements, neededIdentifiers } = resolveNeededStatements(\n\t\tprops.sourceFile,\n\t\tprops.expression,\n\t);\n\tconst sections: string[] = [];\n\n\tfor (const statement of props.sourceFile.statements) {\n\t\tif (!ts.isImportDeclaration(statement)) continue;\n\n\t\tconst rendered = renderImport(\n\t\t\tprops.sourceFile,\n\t\t\tstatement,\n\t\t\tneededIdentifiers,\n\t\t\tprops.target,\n\t\t\tprops.outputPath,\n\t\t);\n\n\t\tif (rendered) {\n\t\t\tsections.push(rendered);\n\t\t}\n\t}\n\n\tfor (const statement of props.sourceFile.statements) {\n\t\tif (!neededStatements.has(statement)) continue;\n\n\t\tsections.push(\n\t\t\tstripExportModifier(getStatementText(props.sourceFile, statement)),\n\t\t);\n\t}\n\n\tif (props.target === \"env\") {\n\t\tif (props.expression) {\n\t\t\tsections.push(\n\t\t\t\t`export const env = ${props.expression.getText(props.sourceFile)};`,\n\t\t\t);\n\t\t} else {\n\t\t\tsections.push(\"export {};\");\n\t\t}\n\t} else {\n\t\tif (!props.expression) {\n\t\t\tthrow new LucidError({\n\t\t\t\tmessage: `Lucid config is missing the \\`${props.target}\\` artifact expression.`,\n\t\t\t});\n\t\t}\n\t\tsections.push(\n\t\t\t`export default ${props.expression.getText(props.sourceFile)};`,\n\t\t);\n\t}\n\n\treturn `${sections.join(\"\\n\\n\")}\\n`;\n};\n\n/**\n * Splits a `configureLucid` config file into runtime, db, env and config modules.\n */\nconst prepareConfigArtifacts = async (props: {\n\tconfigPath: string;\n\toutputPath: string;\n}): Promise<PreparedConfigArtifacts> => {\n\tconst source = await readFile(props.configPath, \"utf-8\");\n\tconst sourceFile = ts.createSourceFile(\n\t\tprops.configPath,\n\t\tsource,\n\t\tts.ScriptTarget.Latest,\n\t\ttrue,\n\t\tgetScriptKind(props.configPath),\n\t);\n\tconst definition = findDefaultConfigureCall(sourceFile);\n\tconst expressions = {\n\t\tconfig: getObjectPropertyExpression(definition, artifactProperties.config),\n\t\tdb: getObjectPropertyExpression(definition, artifactProperties.db),\n\t\truntime: getObjectPropertyExpression(\n\t\t\tdefinition,\n\t\t\tartifactProperties.runtime,\n\t\t),\n\t\tenv: getExportedEnvExpression(sourceFile),\n\t};\n\tconst artifacts = Object.fromEntries(\n\t\tObject.entries(configArtifactEntries).map(([key, entry]) => [\n\t\t\tkey,\n\t\t\tpath.join(props.outputPath, `${entry}.ts`),\n\t\t]),\n\t) as PreparedConfigArtifacts;\n\n\tawait mkdir(path.join(props.outputPath, \"lucid\"), { recursive: true });\n\n\tawait Promise.all(\n\t\tObject.entries(artifacts).map(async ([key, filePath]) => {\n\t\t\tconst target = key as ConfigArtifactKey;\n\t\t\tawait writeFile(\n\t\t\t\tfilePath,\n\t\t\t\trenderArtifactSource({\n\t\t\t\t\tsourceFile,\n\t\t\t\t\ttarget,\n\t\t\t\t\toutputPath: props.outputPath,\n\t\t\t\t\texpression: expressions[target],\n\t\t\t\t}),\n\t\t\t);\n\t\t}),\n\t);\n\n\treturn artifacts;\n};\n\nexport default prepareConfigArtifacts;\n"],"mappings":"0OAUA,MAAM,EAAyB,IAAI,IAAI,CACtC,CAAC,yBAA0B,gCAAgC,EAC3D,CAAC,+BAAgC,sCAAsC,CACxE,CAAC,EAEK,EAAqB,CAC1B,OAAQ,SACR,GAAI,KACJ,QAAS,SACV,EAKM,EAAiB,GAClB,EAAW,SAAS,MAAM,EAAU,EAAG,WAAW,IAClD,EAAW,SAAS,MAAM,EAAU,EAAG,WAAW,IAClD,EAAW,SAAS,KAAK,GAAK,EAAW,SAAS,MAAM,EACpD,E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