Skip to content

Commit

Permalink
fix: cr issues
Browse files Browse the repository at this point in the history
  • Loading branch information
keepview committed Feb 14, 2025
1 parent 16a4e3c commit 8c5a9df
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 110 deletions.
182 changes: 77 additions & 105 deletions packages/cli/plugin-bff/src/utils/clientGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,55 +33,6 @@ const CLIENT_DIR = 'client';
const EXPORT_PREFIX = `./${API_DIR}/`;
const TYPE_PREFIX = `${API_DIR}/`;

function deepMerge<T extends Record<string, any>>(
target: T | undefined,
source: Partial<T>,
strategy?: {
array?: 'append' | 'prepend' | 'replace';
dedupe?: boolean;
},
): T {
const base = (target || {}) as T;
const merged = { ...base };

for (const [key, value] of Object.entries(source)) {
if (Array.isArray(value) && Array.isArray(base[key])) {
merged[key as keyof T] = [
...(strategy?.array === 'prepend' ? value : base[key]),
...(strategy?.array === 'append' ? value : []),
...(strategy?.array !== 'replace' && strategy?.array !== 'prepend'
? base[key]
: []),
].filter((v, i, a) =>
strategy?.dedupe
? a.findIndex(e => JSON.stringify(e) === JSON.stringify(v)) === i
: true,
) as T[keyof T];
continue;
}

if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
merged[key as keyof T] = deepMerge(base[key], value, strategy);
continue;
}

if (!(key in base)) {
merged[key as keyof T] = value;
}
}

return merged;
}

const filterObjectKeys = <T extends Record<string, any>>(
obj: T | undefined,
predicate: (key: string) => boolean,
): T => {
return Object.fromEntries(
Object.entries(obj || {}).filter(([key]) => predicate(key)),
) as T;
};

export async function readDirectoryFiles(
appDirectory: string,
directory: string,
Expand Down Expand Up @@ -140,6 +91,41 @@ export async function readDirectoryFiles(
return filesList;
}

function mergePackageJson(
packageJson: any,
files?: string[],
typesVersion?: Record<string, any>,
exports?: Record<string, any>,
) {
if (files) {
packageJson.files = [...new Set([...(packageJson.files || []), ...files])];
}

if (typesVersion) {
packageJson.typesVersions ??= {};
const starTypes = packageJson.typesVersions['*'] || {};

Object.keys(starTypes).forEach(
k => k.startsWith(TYPE_PREFIX) && delete starTypes[k],
);

packageJson.typesVersions['*'] = {
...starTypes,
...(typesVersion['*'] || {}),
};
}

if (exports) {
packageJson.exports ??= {};

Object.keys(packageJson.exports).forEach(
k => k.startsWith(EXPORT_PREFIX) && delete packageJson.exports[k],
);

Object.assign(packageJson.exports, exports);
}
}

async function writeTargetFile(absTargetDir: string, content: string) {
await fs.mkdir(path.dirname(absTargetDir), { recursive: true });
await fs.writeFile(absTargetDir, content);
Expand All @@ -159,72 +145,58 @@ async function setPackage(
const packageContent = await fs.readFile(packagePath, 'utf8');
const packageJson = JSON.parse(packageContent);

packageJson.exports = filterObjectKeys(
packageJson.exports,
key => !key.startsWith(EXPORT_PREFIX),
);
const addFiles = [
`${relativeDistPath}/${CLIENT_DIR}/**/*`,
`${relativeDistPath}/${RUNTIME_DIR}/**/*`,
`${relativeDistPath}/${PLUGIN_DIR}/**/*`,
];

const typesVersions = {
'*': files.reduce(
(acc, file) => {
const typeFilePath = `./${file.targetDir}`.replace('js', 'd.ts');
return {
...acc,
[`${TYPE_PREFIX}${file.exportKey}`]: [typeFilePath],
};
},
{
[RUNTIME_DIR]: [`./${relativeDistPath}/${RUNTIME_DIR}/index.d.ts`],
[PLUGIN_DIR]: [`./${relativeDistPath}/${PLUGIN_DIR}/index.d.ts`],
},
),
};

if (packageJson.typesVersions?.['*']) {
packageJson.typesVersions['*'] = filterObjectKeys(
packageJson.typesVersions['*'],
key => !key.startsWith(TYPE_PREFIX),
);
}
const exports = files.reduce(
(acc, file) => {
const exportKey = `${EXPORT_PREFIX}${file.exportKey}`;
const jsFilePath = `./${file.targetDir}`;

const mergedPackage = deepMerge(
packageJson,
{
exports: files.reduce(
(acc, file) => {
const exportKey = `${EXPORT_PREFIX}${file.exportKey}`;
const jsFilePath = `./${file.targetDir}`;
return deepMerge(acc, {
[exportKey]: {
import: jsFilePath,
types: jsFilePath.replace('js', 'd.ts'),
},
});
},
{
'./plugin': {
require: `./${relativeDistPath}/${PLUGIN_DIR}/index.js`,
types: `./${relativeDistPath}/${PLUGIN_DIR}/index.d.ts`,
},
'./runtime': {
import: `./${relativeDistPath}/${RUNTIME_DIR}/index.js`,
types: `./${relativeDistPath}/${RUNTIME_DIR}/index.d.ts`,
},
return {
...acc,
[exportKey]: {
import: jsFilePath,
types: jsFilePath.replace(/\.js$/, '.d.ts'),
},
),
typesVersions: {
'*': files.reduce(
(acc, file) => {
const typeFilePath = `./${file.targetDir}`.replace('js', 'd.ts');
return deepMerge(acc, {
[`${TYPE_PREFIX}${file.exportKey}`]: [typeFilePath],
});
},
{
runtime: [`./${relativeDistPath}/${RUNTIME_DIR}/index.d.ts`],
plugin: [`./${relativeDistPath}/${PLUGIN_DIR}/index.d.ts`],
},
),
},
files: [
`${relativeDistPath}/${CLIENT_DIR}/**/*`,
`${relativeDistPath}/${RUNTIME_DIR}/**/*`,
`${relativeDistPath}/${PLUGIN_DIR}/**/*`,
],
};
},
{
array: 'append',
dedupe: true,
[`./${PLUGIN_DIR}`]: {
require: `./${relativeDistPath}/${PLUGIN_DIR}/index.js`,
types: `./${relativeDistPath}/${PLUGIN_DIR}/index.d.ts`,
},
[`./${RUNTIME_DIR}`]: {
import: `./${relativeDistPath}/${RUNTIME_DIR}/index.js`,
types: `./${relativeDistPath}/${RUNTIME_DIR}/index.d.ts`,
},
},
);

mergePackageJson(packageJson, addFiles, typesVersions, exports);

await fs.promises.writeFile(
packagePath,
JSON.stringify(mergedPackage, null, 2),
JSON.stringify(packageJson, null, 2),
);
} catch (error) {
logger.error(`package.json update failed: ${error}`);
Expand Down
1 change: 0 additions & 1 deletion packages/server/create-request/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const configure = (options: IOptions) => {
requestId,
setDomain({
target: 'browser',
requestId,
}),
);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/server/create-request/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export const configure = (options: IOptions<typeof nodeFetch>) => {
domainMap.set(
requestId,
setDomain({
target: 'node',
requestId,
target: 'server',
}),
);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/server/create-request/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export type IOptions<F = typeof fetch> = {
interceptor?: (request: F) => F;
allowedHeaders?: string[];
setDomain?: (ops?: {
target: 'node' | 'browser';
requestId: string;
target: 'server' | 'browser';
}) => string;
requestId?: string;
};

0 comments on commit 8c5a9df

Please sign in to comment.