Skip to content

Commit b0c19b6

Browse files
authored
Merge branch 'main' into docs/seo
2 parents a48f161 + 197ed86 commit b0c19b6

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

.changeset/tiny-cobras-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
fix: resolve Yarn PnP compatibility issues with client bundle generation

packages/openapi-ts/src/generate/client.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'node:fs';
2+
import { createRequire } from 'node:module';
23
import path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
44

55
import ts from 'typescript';
66

@@ -11,8 +11,11 @@ import type { ImportExportItemObject } from '../tsc/utils';
1111
import type { Config } from '../types/config';
1212
import { ensureDirSync, relativeModulePath } from './utils';
1313

14-
const __filename = fileURLToPath(import.meta.url);
15-
const __dirname = path.dirname(__filename);
14+
// Use require.resolve to find the package root, then construct the path
15+
// This approach works with Yarn PnP and doesn't rely on specific file exports
16+
const packageRoot = path.dirname(
17+
createRequire(import.meta.url).resolve('@hey-api/openapi-ts/package.json'),
18+
);
1619

1720
const getClientSrcPath = (name: string) => {
1821
const pluginFilePathComponents = name.split(path.sep);
@@ -63,6 +66,29 @@ export const clientApi = {
6366
},
6467
} satisfies Record<string, ImportExportItemObject>;
6568

69+
/**
70+
* Recursively copies files and directories.
71+
* This is a PnP-compatible alternative to fs.cpSync that works with Yarn PnP's
72+
* virtualized filesystem.
73+
*/
74+
const copyRecursivePnP = (src: string, dest: string) => {
75+
const stat = fs.statSync(src);
76+
77+
if (stat.isDirectory()) {
78+
if (!fs.existsSync(dest)) {
79+
fs.mkdirSync(dest, { recursive: true });
80+
}
81+
82+
const files = fs.readdirSync(src);
83+
for (const file of files) {
84+
copyRecursivePnP(path.join(src, file), path.join(dest, file));
85+
}
86+
} else {
87+
const content = fs.readFileSync(src);
88+
fs.writeFileSync(dest, content);
89+
}
90+
};
91+
6692
const replaceRelativeImports = (filePath: string) => {
6793
let content = fs.readFileSync(filePath, 'utf8');
6894

@@ -107,8 +133,8 @@ export const generateClientBundle = ({
107133
// copy client core
108134
const coreOutputPath = path.resolve(outputPath, 'core');
109135
ensureDirSync(coreOutputPath);
110-
const coreDistPath = path.resolve(__dirname, 'clients', 'core');
111-
fs.cpSync(coreDistPath, coreOutputPath, { recursive: true });
136+
const coreDistPath = path.resolve(packageRoot, 'dist', 'clients', 'core');
137+
copyRecursivePnP(coreDistPath, coreOutputPath);
112138
if (shouldAppendJs) {
113139
const coreFiles = fs.readdirSync(coreOutputPath);
114140
for (const file of coreFiles) {
@@ -120,11 +146,12 @@ export const generateClientBundle = ({
120146
ensureDirSync(clientOutputPath);
121147
const clientDistFolderName = plugin.name.slice('@hey-api/client-'.length);
122148
const clientDistPath = path.resolve(
123-
__dirname,
149+
packageRoot,
150+
'dist',
124151
'clients',
125152
clientDistFolderName,
126153
);
127-
fs.cpSync(clientDistPath, clientOutputPath, { recursive: true });
154+
copyRecursivePnP(clientDistPath, clientOutputPath);
128155
if (shouldAppendJs) {
129156
const clientFiles = fs.readdirSync(clientOutputPath);
130157
for (const file of clientFiles) {
@@ -143,9 +170,7 @@ export const generateClientBundle = ({
143170
if (clientSrcPath) {
144171
const dirPath = path.resolve(outputPath, 'client');
145172
ensureDirSync(dirPath);
146-
fs.cpSync(clientSrcPath, dirPath, {
147-
recursive: true,
148-
});
173+
copyRecursivePnP(clientSrcPath, dirPath);
149174
return;
150175
}
151176

0 commit comments

Comments
 (0)