Skip to content

Commit 03c5f9c

Browse files
committed
Address PR feedback: remove any casts, use core rm helper, clarify comments
1 parent 1ec9c8e commit 03c5f9c

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

packages/plugins/apps/src/index.test.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as identifier from '@dd/apps-plugin/identifier';
88
import * as uploader from '@dd/apps-plugin/upload';
99
import { getPlugins } from '@dd/apps-plugin';
1010
import * as fsHelpers from '@dd/core/helpers/fs';
11+
import type { PluginOptions } from '@dd/core/types';
1112
import {
1213
getGetPluginsArg,
1314
getMockBundler,
@@ -20,6 +21,13 @@ import path from 'path';
2021

2122
import { APPS_API_PATH } from './constants';
2223

24+
/** Extract and assert closeBundle from the first plugin's vite hooks. */
25+
function extractCloseBundle(plugins: PluginOptions[]) {
26+
const plugin = plugins[0];
27+
expect(typeof plugin?.vite?.closeBundle).toBe('function');
28+
return plugin.vite!.closeBundle as () => Promise<void>;
29+
}
30+
2331
describe('Apps Plugin - getPlugins', () => {
2432
const buildRoot = '/project';
2533
const outDir = '/project/dist';
@@ -58,10 +66,8 @@ describe('Apps Plugin - getPlugins', () => {
5866
});
5967
jest.spyOn(identifier, 'resolveIdentifier').mockReturnValue({});
6068

61-
const plugin = getPlugins(getArgs())[0];
62-
await expect((plugin as any).vite.closeBundle()).rejects.toThrow(
63-
'Missing apps identification',
64-
);
69+
const closeBundle = extractCloseBundle(getPlugins(getArgs()));
70+
await expect(closeBundle()).rejects.toThrow('Missing apps identification');
6571

6672
expect(uploadSpy).not.toHaveBeenCalled();
6773
expect(collectSpy).not.toHaveBeenCalled();
@@ -86,16 +92,18 @@ describe('Apps Plugin - getPlugins', () => {
8692
errors: [],
8793
warnings: [],
8894
});
89-
const rmSpy = jest.spyOn(fsHelpers, 'rm').mockResolvedValue(undefined as any);
90-
91-
const plugin = getPlugins(
92-
getGetPluginsArg(
93-
{ apps: { include: ['public/**/*'] } },
94-
{ bundler: { ...getMockBundler({ name: 'vite' }), outDir }, buildRoot },
95+
const rmSpy = jest.spyOn(fsHelpers, 'rm').mockResolvedValue(undefined);
96+
97+
const closeBundle = extractCloseBundle(
98+
getPlugins(
99+
getGetPluginsArg(
100+
{ apps: { include: ['public/**/*'] } },
101+
{ bundler: { ...getMockBundler({ name: 'vite' }), outDir }, buildRoot },
102+
),
95103
),
96-
)[0];
104+
);
97105

98-
await (plugin as any).vite.closeBundle();
106+
await closeBundle();
99107

100108
expect(assets.collectAssets).toHaveBeenCalledWith(['public/**/*', 'dist/**/*'], buildRoot);
101109
expect(archive.createArchive).not.toHaveBeenCalled();
@@ -116,7 +124,7 @@ describe('Apps Plugin - getPlugins', () => {
116124
{ absolutePath: '/project/dist/index.js', relativePath: 'dist/index.js' },
117125
];
118126
jest.spyOn(assets, 'collectAssets').mockResolvedValue(mockedAssets);
119-
jest.spyOn(fsHelpers, 'rm').mockResolvedValue(undefined as any);
127+
jest.spyOn(fsHelpers, 'rm').mockResolvedValue(undefined);
120128
jest.spyOn(archive, 'createArchive').mockResolvedValue({
121129
archivePath: '/tmp/dd-apps-123/datadog-apps-assets.zip',
122130
assets: mockedAssets,
@@ -127,8 +135,8 @@ describe('Apps Plugin - getPlugins', () => {
127135
warnings: ['first warning'],
128136
});
129137

130-
const plugin = getPlugins(getArgs())[0];
131-
await (plugin as any).vite.closeBundle();
138+
const closeBundle = extractCloseBundle(getPlugins(getArgs()));
139+
await closeBundle();
132140

133141
expect(assets.collectAssets).toHaveBeenCalledWith(['dist/**/*'], buildRoot);
134142
expect(archive.createArchive).toHaveBeenCalledWith([
@@ -167,7 +175,7 @@ describe('Apps Plugin - getPlugins', () => {
167175
{ absolutePath: '/project/dist/app.js', relativePath: 'dist/app.js' },
168176
];
169177
jest.spyOn(assets, 'collectAssets').mockResolvedValue(mockedAssets);
170-
jest.spyOn(fsHelpers, 'rm').mockResolvedValue(undefined as any);
178+
jest.spyOn(fsHelpers, 'rm').mockResolvedValue(undefined);
171179
jest.spyOn(archive, 'createArchive').mockResolvedValue({
172180
archivePath: '/tmp/dd-apps-456/datadog-apps-assets.zip',
173181
assets: mockedAssets,
@@ -178,8 +186,8 @@ describe('Apps Plugin - getPlugins', () => {
178186
warnings: [],
179187
});
180188

181-
const plugin = getPlugins(getArgs())[0];
182-
await expect((plugin as any).vite.closeBundle()).rejects.toThrow('upload failed');
189+
const closeBundle = extractCloseBundle(getPlugins(getArgs()));
190+
await expect(closeBundle()).rejects.toThrow('upload failed');
183191

184192
expect(mockLogFn).toHaveBeenCalledWith(expect.stringContaining('upload failed'), 'error');
185193
expect(fsHelpers.rm).toHaveBeenCalledWith(path.resolve('/tmp/dd-apps-456'));

packages/plugins/apps/src/vite/build-backend-functions.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ export async function buildBackendFunctions(
5656
output: { format: 'es', exports: 'named', entryFileNames: '[name].js' },
5757
preserveEntrySignatures: 'exports-only',
5858
treeshake: false,
59-
// Silence "use client" directive warnings from third-party deps.
60-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
61-
onwarn(warning: any, defaultHandler: any) {
59+
onwarn(warning, defaultHandler) {
6260
if (warning.code === 'MODULE_LEVEL_DIRECTIVE') {
6361
return;
6462
}
@@ -91,8 +89,8 @@ export async function buildBackendFunctions(
9189

9290
const output = Array.isArray(result) ? result[0] : result;
9391

94-
// viteBuild always returns RolldownOutput here since we don't set build.watch.
95-
// RolldownWatcher would only be returned if watch mode were enabled.
92+
// vite.build() returns RolldownOutput | RolldownWatcher.
93+
// Since we don't enable watch mode, we always get RolldownOutput.
9694
if ('output' in output) {
9795
for (const chunk of output.output) {
9896
if (chunk.type !== 'chunk' || !chunk.isEntry) {

packages/plugins/apps/src/vite/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// This product includes software developed at Datadog (https://www.datadoghq.com/).
33
// Copyright 2019-Present Datadog, Inc.
44

5+
import { rm } from '@dd/core/helpers/fs';
56
import type { Logger, PluginOptions } from '@dd/core/types';
6-
import { rm } from 'fs/promises';
77
import type { build } from 'vite';
88

99
import type { BackendFunction } from '../backend/discovery';
@@ -49,7 +49,7 @@ export const getVitePlugin = ({
4949
await handleUpload();
5050
} finally {
5151
if (backendOutDir) {
52-
await rm(backendOutDir, { recursive: true, force: true });
52+
await rm(backendOutDir);
5353
}
5454
}
5555
},

0 commit comments

Comments
 (0)