这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-cobras-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: resolve Yarn PnP compatibility issues with client bundle generation
45 changes: 35 additions & 10 deletions packages/openapi-ts/src/generate/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import ts from 'typescript';

Expand All @@ -11,8 +11,11 @@
import type { Config } from '../types/config';
import { ensureDirSync, relativeModulePath } from './utils';

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

const getClientSrcPath = (name: string) => {
const pluginFilePathComponents = name.split(path.sep);
Expand Down Expand Up @@ -63,6 +66,29 @@
},
} satisfies Record<string, ImportExportItemObject>;

/**
* Recursively copies files and directories.
* This is a PnP-compatible alternative to fs.cpSync that works with Yarn PnP's
* virtualized filesystem.
*/
const copyRecursivePnP = (src: string, dest: string) => {
const stat = fs.statSync(src);

if (stat.isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}

const files = fs.readdirSync(src);
for (const file of files) {
copyRecursivePnP(path.join(src, file), path.join(dest, file));
}
} else {
const content = fs.readFileSync(src);

Check failure

Code scanning / CodeQL

Potential file system race condition High

The file may have changed since it
was checked
.
fs.writeFileSync(dest, content);
}
};

const replaceRelativeImports = (filePath: string) => {
let content = fs.readFileSync(filePath, 'utf8');

Expand Down Expand Up @@ -107,8 +133,8 @@
// copy client core
const coreOutputPath = path.resolve(outputPath, 'core');
ensureDirSync(coreOutputPath);
const coreDistPath = path.resolve(__dirname, 'clients', 'core');
fs.cpSync(coreDistPath, coreOutputPath, { recursive: true });
const coreDistPath = path.resolve(packageRoot, 'dist', 'clients', 'core');
copyRecursivePnP(coreDistPath, coreOutputPath);
if (shouldAppendJs) {
const coreFiles = fs.readdirSync(coreOutputPath);
for (const file of coreFiles) {
Expand All @@ -120,11 +146,12 @@
ensureDirSync(clientOutputPath);
const clientDistFolderName = plugin.name.slice('@hey-api/client-'.length);
const clientDistPath = path.resolve(
__dirname,
packageRoot,
'dist',
'clients',
clientDistFolderName,
);
fs.cpSync(clientDistPath, clientOutputPath, { recursive: true });
copyRecursivePnP(clientDistPath, clientOutputPath);
if (shouldAppendJs) {
const clientFiles = fs.readdirSync(clientOutputPath);
for (const file of clientFiles) {
Expand All @@ -143,9 +170,7 @@
if (clientSrcPath) {
const dirPath = path.resolve(outputPath, 'client');
ensureDirSync(dirPath);
fs.cpSync(clientSrcPath, dirPath, {
recursive: true,
});
copyRecursivePnP(clientSrcPath, dirPath);
return;
}

Expand Down
Loading