|
| 1 | +#!/usr/bin/env node --stack_size=800 -r ts-node/register |
| 2 | + |
| 3 | +import { readFileSync, writeFileSync } from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { Project, type SourceFile } from 'ts-morph'; |
| 6 | + |
| 7 | +// Get target package from command line argument |
| 8 | +const targetPackage = process.argv[2]; |
| 9 | +if (!targetPackage) { |
| 10 | + console.error('Usage: ./generate/reExportEverything.ts <package-name>'); |
| 11 | + console.error('Example: ./generate/reExportEverything.ts cma-client-node'); |
| 12 | + process.exit(1); |
| 13 | +} |
| 14 | + |
| 15 | +const SRC_FILE = path.resolve( |
| 16 | + __dirname, |
| 17 | + `../packages/${targetPackage}/src/index.ts`, |
| 18 | +); |
| 19 | +const CMA_CLIENT_PATH = path.resolve( |
| 20 | + __dirname, |
| 21 | + '../packages/cma-client/dist/types/index.d.ts', |
| 22 | +); |
| 23 | + |
| 24 | +const START_MARKER = '// <AUTO-GENERATED-EXPORTS>'; |
| 25 | +const END_MARKER = '// </AUTO-GENERATED-EXPORTS>'; |
| 26 | + |
| 27 | +const blacklist = new Set([ |
| 28 | + 'buildClient', |
| 29 | + 'Client', |
| 30 | + 'GenericClient', |
| 31 | + 'Resources', |
| 32 | + 'default', |
| 33 | +]); |
| 34 | + |
| 35 | +const project = new Project({ |
| 36 | + compilerOptions: { |
| 37 | + moduleResolution: 2, // Node |
| 38 | + target: 99, // ESNext |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +// Track collected exports |
| 43 | +const collected = { |
| 44 | + value: new Set<string>(), |
| 45 | + type: new Set<string>(), |
| 46 | +}; |
| 47 | + |
| 48 | +// Avoid infinite recursion |
| 49 | +const visitedFiles = new Set<string>(); |
| 50 | + |
| 51 | +function collectExports(file: SourceFile) { |
| 52 | + if (visitedFiles.has(file.getFilePath())) return; |
| 53 | + visitedFiles.add(file.getFilePath()); |
| 54 | + |
| 55 | + for (const stmt of file.getExportDeclarations()) { |
| 56 | + const moduleSpecifier = stmt.getModuleSpecifierSourceFile(); |
| 57 | + |
| 58 | + // Handle named exports |
| 59 | + const namedExports = stmt.getNamedExports(); |
| 60 | + for (const ne of namedExports) { |
| 61 | + // Use alias if present, otherwise use original name |
| 62 | + const name = ne.getAliasNode()?.getText() || ne.getName(); |
| 63 | + if (blacklist.has(name)) continue; |
| 64 | + |
| 65 | + if (stmt.isTypeOnly()) collected.type.add(name); |
| 66 | + else collected.value.add(name); |
| 67 | + } |
| 68 | + |
| 69 | + // Handle export * (wildcards) - recursively collect from the target file |
| 70 | + // Note: export * statements have 0 named exports and no namespace export identifier |
| 71 | + const isWildcard = |
| 72 | + stmt.getNamedExports().length === 0 && !stmt.getNamespaceExport(); |
| 73 | + |
| 74 | + if (isWildcard && moduleSpecifier) { |
| 75 | + // Case 1: moduleSpecifier is resolved by ts-morph |
| 76 | + collectExports(moduleSpecifier); |
| 77 | + } else if (isWildcard && stmt.getModuleSpecifier()) { |
| 78 | + // Case 2: relative imports that ts-morph didn't resolve |
| 79 | + const moduleString = stmt.getModuleSpecifier()?.getLiteralValue(); |
| 80 | + if (moduleString) { |
| 81 | + // Try to resolve the module path relative to current file |
| 82 | + const currentDir = path.dirname(file.getFilePath()); |
| 83 | + const resolvedPath = path.resolve(currentDir, `${moduleString}.d.ts`); |
| 84 | + |
| 85 | + try { |
| 86 | + const targetFile = project.addSourceFileAtPath(resolvedPath); |
| 87 | + collectExports(targetFile); |
| 88 | + } catch (e) { |
| 89 | + console.warn( |
| 90 | + `Could not resolve wildcard export: ${moduleString} from ${file.getFilePath()}`, |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Handle "export * as NS" → collect namespace as a value export |
| 97 | + const nsExport = stmt.getNamespaceExport(); |
| 98 | + if (nsExport) { |
| 99 | + const nsName = nsExport.getName(); |
| 100 | + if (nsName && !blacklist.has(nsName)) { |
| 101 | + collected.value.add(nsName); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Collect direct exports from functions, classes, variables |
| 107 | + for (const fn of file.getFunctions()) { |
| 108 | + if (fn.isExported() && !blacklist.has(fn.getName() || '')) { |
| 109 | + collected.value.add(fn.getName() || ''); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + for (const cls of file.getClasses()) { |
| 114 | + if (cls.isExported() && !blacklist.has(cls.getName() || '')) { |
| 115 | + collected.value.add(cls.getName() || ''); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + for (const vs of file.getVariableStatements()) { |
| 120 | + if (vs.isExported()) { |
| 121 | + for (const decl of vs.getDeclarations()) { |
| 122 | + const name = decl.getName(); |
| 123 | + if (!blacklist.has(name)) { |
| 124 | + collected.value.add(name); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Also collect type-only exports directly declared in this file |
| 131 | + for (const ta of file.getTypeAliases()) { |
| 132 | + if (ta.isExported() && !blacklist.has(ta.getName())) { |
| 133 | + collected.type.add(ta.getName()); |
| 134 | + } |
| 135 | + } |
| 136 | + for (const i of file.getInterfaces()) { |
| 137 | + if (i.isExported() && !blacklist.has(i.getName())) { |
| 138 | + collected.type.add(i.getName()); |
| 139 | + } |
| 140 | + } |
| 141 | + for (const e of file.getEnums()) { |
| 142 | + if (e.isExported() && !blacklist.has(e.getName())) { |
| 143 | + collected.value.add(e.getName()); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +const cmaFile = project.addSourceFileAtPath(CMA_CLIENT_PATH); |
| 149 | +collectExports(cmaFile); |
| 150 | + |
| 151 | +const valueExports = Array.from(collected.value).sort(); |
| 152 | +const typeExports = Array.from(collected.type).sort(); |
| 153 | + |
| 154 | +const newExportBlock = ` |
| 155 | +${START_MARKER} |
| 156 | +export { |
| 157 | + ${valueExports.join(',\n ')}, |
| 158 | +} from '@datocms/cma-client'; |
| 159 | +
|
| 160 | +export type { |
| 161 | + ${typeExports.join(',\n ')}, |
| 162 | +} from '@datocms/cma-client'; |
| 163 | +${END_MARKER} |
| 164 | +`.trim(); |
| 165 | + |
| 166 | +// Read existing file |
| 167 | +let content = ''; |
| 168 | +try { |
| 169 | + content = readFileSync(SRC_FILE, 'utf-8'); |
| 170 | +} catch { |
| 171 | + console.warn(`File ${SRC_FILE} not found. Creating a new one.`); |
| 172 | +} |
| 173 | + |
| 174 | +// Replace old export block if present, otherwise prepend |
| 175 | +if (content.includes(START_MARKER) && content.includes(END_MARKER)) { |
| 176 | + const regex = new RegExp(`${START_MARKER}[\\s\\S]*?${END_MARKER}`, 'm'); |
| 177 | + content = content.replace(regex, newExportBlock); |
| 178 | +} else { |
| 179 | + content = `${newExportBlock}\n\n${content}`; |
| 180 | +} |
| 181 | + |
| 182 | +// Write updated file |
| 183 | +writeFileSync(SRC_FILE, content); |
| 184 | +console.log(`Updated exports (including types) in ${SRC_FILE}`); |
0 commit comments