MCPcopy Create free account
hub / github.com/hey-api/hey-api / replaceImports

Function replaceImports

packages/openapi-python/src/generate/client.ts:100–140  ·  view source on GitHub ↗
({
  filePath,
  header,
  renamed,
}: {
  filePath: string;
  header?: string;
  renamed: Map<string, string>;
})

Source from the content-addressed store, hash-verified

98}
99
100function replaceImports({
101 filePath,
102 header,
103 renamed,
104}: {
105 filePath: string;
106 header?: string;
107 renamed: Map<string, string>;
108}): void {
109 let content = fs.readFileSync(filePath, 'utf8');
110
111 // Dev mode: rewrite source bundle imports to match output structure
112 if (isEnvironment('development')) {
113 // ...client_core.bundle.foo -> ..core.foo
114 content = content.replace(
115 /from\s+(\.{3,})\.?client_core\.bundle\./g,
116 (match, dots) => `from ${dots === '...' ? '..' : dots.slice(0, -1)}core.`,
117 );
118 // ...client_core.bundle (index import) -> ..core
119 content = content.replace(
120 /from\s+(\.{3,})\.?client_core\.bundle['"]?/g,
121 (match, dots) => `from ${dots === '...' ? '..' : dots.slice(0, -1)}core`,
122 );
123 }
124
125 content = content.replace(/from\s+(.+?)\s+import/g, (match, importPath) => {
126 const cleanPath = importPath.replace(/^['"]|['"]$/g, '');
127 if (!cleanPath.startsWith('.')) return match;
128 const leadingDots = cleanPath.match(/^\.+/)?.[0] || '';
129 const moduleName = cleanPath.replace(/^\.+/, '');
130 if (!moduleName) return match;
131 const replacedName = renamed.get(moduleName) ?? moduleName;
132 return `from ${leadingDots}${replacedName} import`;
133 });
134
135 const fileHeader = header ?? '';
136
137 content = `${fileHeader}${content}`;
138
139 fs.writeFileSync(filePath, content, 'utf8');
140}
141
142/**
143 * Creates a `client` folder containing the same modules as the client package.

Callers 1

generateClientBundleFunction · 0.70

Calls 3

isEnvironmentFunction · 0.90
sliceMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected