Skip to content

Commit 4cddb8a

Browse files
authored
fix: no delete exist types if fetch new types failed (#2634)
1 parent 1e93c5e commit 4cddb8a

File tree

5 files changed

+49
-27
lines changed

5 files changed

+49
-27
lines changed

.changeset/mighty-goats-hang.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
fix: no delete exist types if fetch new types failed

packages/dts-plugin/src/core/lib/DTSManager.spec.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,8 @@ describe('DTSManager', () => {
185185
});
186186
});
187187

188-
it('correct consumeTypes', async () => {
189-
const distFolder = join(projectRoot, 'dist', typesFolder);
190-
const zip = new AdmZip();
191-
await zip.addLocalFolderPromise(distFolder, {});
192-
axios.get = vi.fn().mockResolvedValueOnce({ data: zip.toBuffer() });
193-
194-
await dtsManager.consumeTypes();
195-
196-
const targetFolder = join(projectRoot, hostOptions.typesFolder);
197-
expect(
198-
dirTree(targetFolder, {
199-
exclude: [/node_modules/, /dev-worker/, /plugins/, /server/],
200-
}),
201-
).toMatchObject({
188+
describe('consumeTypes', async () => {
189+
const expectedStructure = {
202190
name: '@mf-types-dts-test-consume-types',
203191
children: [
204192
{
@@ -318,6 +306,31 @@ describe('DTSManager', () => {
318306
name: 'remotes',
319307
},
320308
],
309+
};
310+
const targetFolder = join(projectRoot, hostOptions.typesFolder);
311+
it('correct consumeTypes', async () => {
312+
const distFolder = join(projectRoot, 'dist', typesFolder);
313+
const zip = new AdmZip();
314+
await zip.addLocalFolderPromise(distFolder, {});
315+
axios.get = vi.fn().mockResolvedValueOnce({ data: zip.toBuffer() });
316+
317+
await dtsManager.consumeTypes();
318+
319+
expect(
320+
dirTree(targetFolder, {
321+
exclude: [/node_modules/, /dev-worker/, /plugins/, /server/],
322+
}),
323+
).toMatchObject(expectedStructure);
324+
});
325+
326+
it('no delete exist remote types if fetch new remote types failed', async () => {
327+
axios.get = vi.fn().mockRejectedValue(new Error('error'));
328+
await dtsManager.consumeTypes();
329+
expect(
330+
dirTree(targetFolder, {
331+
exclude: [/node_modules/, /dev-worker/, /plugins/, /server/],
332+
}),
333+
).toMatchObject(expectedStructure);
321334
});
322335
});
323336

packages/dts-plugin/src/core/lib/DTSManager.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,6 @@ class DTSManager {
341341

342342
async consumeArchiveTypes(options: HostOptions) {
343343
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(options);
344-
if (hostOptions.deleteTypesFolder) {
345-
await rm(hostOptions.typesFolder, {
346-
recursive: true,
347-
force: true,
348-
}).catch((error) =>
349-
fileLog(
350-
`Unable to remove types folder, ${error}`,
351-
'consumeArchiveTypes',
352-
'error',
353-
),
354-
);
355-
}
356344

357345
const downloadPromises = Object.entries(mapRemotesToDownload).map(
358346
async (item) => {

packages/dts-plugin/src/core/lib/archiveHandler.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import AdmZip from 'adm-zip';
22
import { resolve, join } from 'path';
3+
import { rm } from 'fs/promises';
34
import typescript from 'typescript';
45

56
import { HostOptions } from '../interfaces/HostOptions';
@@ -63,6 +64,21 @@ export const downloadTypesArchive = (hostOptions: Required<HostOptions>) => {
6364
responseType: 'arraybuffer',
6465
}).catch(downloadErrorLogger(destinationFolder, url));
6566

67+
try {
68+
if (hostOptions.deleteTypesFolder) {
69+
await rm(destinationPath, {
70+
recursive: true,
71+
force: true,
72+
});
73+
}
74+
} catch (error) {
75+
fileLog(
76+
`Unable to remove types folder, ${error}`,
77+
'downloadTypesArchive',
78+
'error',
79+
);
80+
}
81+
6682
const zip = new AdmZip(Buffer.from(response.data));
6783
zip.extractAllTo(destinationPath, true);
6884
return [destinationFolder, destinationPath];

packages/dts-plugin/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { defineConfig } from 'vite';
44
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
55

66
export default defineConfig({
7-
cacheDir: '../../node_modules/.vite/native-federation-tests',
7+
cacheDir: '../../node_modules/.vite/dts-plugin',
88

99
plugins: [nxViteTsPaths()],
1010

0 commit comments

Comments
 (0)