1
1
import fs from 'node:fs' ;
2
+ import { createRequire } from 'node:module' ;
2
3
import path from 'node:path' ;
3
- import { fileURLToPath } from 'node:url' ;
4
4
5
5
import ts from 'typescript' ;
6
6
@@ -11,8 +11,11 @@ import type { ImportExportItemObject } from '../tsc/utils';
11
11
import type { Config } from '../types/config' ;
12
12
import { ensureDirSync , relativeModulePath } from './utils' ;
13
13
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
+ ) ;
16
19
17
20
const getClientSrcPath = ( name : string ) => {
18
21
const pluginFilePathComponents = name . split ( path . sep ) ;
@@ -63,6 +66,29 @@ export const clientApi = {
63
66
} ,
64
67
} satisfies Record < string , ImportExportItemObject > ;
65
68
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
+
66
92
const replaceRelativeImports = ( filePath : string ) => {
67
93
let content = fs . readFileSync ( filePath , 'utf8' ) ;
68
94
@@ -107,8 +133,8 @@ export const generateClientBundle = ({
107
133
// copy client core
108
134
const coreOutputPath = path . resolve ( outputPath , 'core' ) ;
109
135
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 ) ;
112
138
if ( shouldAppendJs ) {
113
139
const coreFiles = fs . readdirSync ( coreOutputPath ) ;
114
140
for ( const file of coreFiles ) {
@@ -120,11 +146,12 @@ export const generateClientBundle = ({
120
146
ensureDirSync ( clientOutputPath ) ;
121
147
const clientDistFolderName = plugin . name . slice ( '@hey-api/client-' . length ) ;
122
148
const clientDistPath = path . resolve (
123
- __dirname ,
149
+ packageRoot ,
150
+ 'dist' ,
124
151
'clients' ,
125
152
clientDistFolderName ,
126
153
) ;
127
- fs . cpSync ( clientDistPath , clientOutputPath , { recursive : true } ) ;
154
+ copyRecursivePnP ( clientDistPath , clientOutputPath ) ;
128
155
if ( shouldAppendJs ) {
129
156
const clientFiles = fs . readdirSync ( clientOutputPath ) ;
130
157
for ( const file of clientFiles ) {
@@ -143,9 +170,7 @@ export const generateClientBundle = ({
143
170
if ( clientSrcPath ) {
144
171
const dirPath = path . resolve ( outputPath , 'client' ) ;
145
172
ensureDirSync ( dirPath ) ;
146
- fs . cpSync ( clientSrcPath , dirPath , {
147
- recursive : true ,
148
- } ) ;
173
+ copyRecursivePnP ( clientSrcPath , dirPath ) ;
149
174
return ;
150
175
}
151
176
0 commit comments