Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 267cb5d

Browse files
mkucmuspatzick
andauthored
fix(nuxt-module): clean up the dump bundle file after the build (#1924)
* fix(nuxt-module): clean up the dump bundle file after the build * fix(helpers): correct types * chore: add missing test for nuxt-module * chore: fix name of the test * chore: types Co-authored-by: Patryk Tomczyk <13100280+patzick@users.noreply.github.com>
1 parent a1db409 commit 267cb5d

File tree

8 files changed

+112
-17
lines changed

8 files changed

+112
-17
lines changed

api/helpers.api.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,19 @@ export function getCmsEntityByType(page?: CmsPageResponse | null): Product | Cat
2929
export function getCmsLayoutConfiguration(content: CmsBlock | CmsSection): LayoutConfiguration;
3030

3131
// @public
32-
export function getCmsLink(content?: CmsSlot): String;
32+
export function getCmsLink(content?: Omit<CmsSlot, "data"> & {
33+
data?: {
34+
url?: string;
35+
};
36+
}): String;
3337

3438
// @public
35-
export function getCmsLinkTarget(content?: CmsSlot): String;
39+
export function getCmsLinkTarget(content?: Omit<CmsSlot, "data"> & {
40+
data?: {
41+
url?: string;
42+
newTab?: boolean;
43+
};
44+
}): String;
3645

3746
// @public (undocumented)
3847
export function getCmsTechnicalPath(page: CmsPageResponse): string | undefined;

docs/landing/resources/api/helpers.getcmslink.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ Gets the link of a cms slot
99
<b>Signature:</b>
1010

1111
```typescript
12-
export declare function getCmsLink(content?: CmsSlot): String;
12+
export declare function getCmsLink(content?: Omit<CmsSlot, "data"> & {
13+
data?: {
14+
url?: string;
15+
};
16+
}): String;
1317
```
1418

1519
## Parameters
1620

1721
| Parameter | Type | Description |
1822
| --- | --- | --- |
19-
| content | CmsSlot | |
23+
| content | Omit&lt;CmsSlot, "data"&gt; &amp; { data?: { url?: string; }; } | |
2024

2125
<b>Returns:</b>
2226

docs/landing/resources/api/helpers.getcmslinktarget.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ Return the target attribute for the cms link
99
<b>Signature:</b>
1010

1111
```typescript
12-
export declare function getCmsLinkTarget(content?: CmsSlot): String;
12+
export declare function getCmsLinkTarget(content?: Omit<CmsSlot, "data"> & {
13+
data?: {
14+
url?: string;
15+
newTab?: boolean;
16+
};
17+
}): String;
1318
```
1419

1520
## Parameters
1621

1722
| Parameter | Type | Description |
1823
| --- | --- | --- |
19-
| content | CmsSlot | |
24+
| content | Omit&lt;CmsSlot, "data"&gt; &amp; { data?: { url?: string; newTab?: boolean; }; } | |
2025

2126
<b>Returns:</b>
2227

packages/cli/src/extensions/plugins-extensions.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import { join } from "path";
55
module.exports = (toolbox: GluegunToolbox) => {
66
toolbox.plugins = {};
77

8+
type DumpBundlesResponse = {
9+
success: boolean;
10+
buildArtifact: { [key: string]: string };
11+
bundleConfig: any;
12+
};
13+
814
let runningRefreshPlugins: boolean = false;
915
toolbox.plugins.invokeRefreshPlugins = async (devMode: boolean = false) => {
1016
if (runningRefreshPlugins) {
@@ -38,7 +44,7 @@ module.exports = (toolbox: GluegunToolbox) => {
3844
}: {
3945
shopwareEndpoint: string;
4046
authToken: string;
41-
}) => {
47+
}): Promise<DumpBundlesResponse> => {
4248
const pluginsConfigRsponse = await axios.post(
4349
`${toolbox.normalizeBaseUrl(
4450
shopwareEndpoint
@@ -50,7 +56,7 @@ module.exports = (toolbox: GluegunToolbox) => {
5056
},
5157
}
5258
);
53-
return pluginsConfigRsponse.data.buildArtifact;
59+
return pluginsConfigRsponse.data;
5460
};
5561

5662
toolbox.fetchPluginsConfig = async ({ config }: { config: string }) => {
@@ -310,12 +316,11 @@ module.exports = (toolbox: GluegunToolbox) => {
310316
return;
311317
}
312318

313-
const buildArtifact = await toolbox.fetchPluginsBuildArtifact({
314-
...toolbox.inputParameters,
315-
authToken,
316-
});
317-
318-
const pluginsConfig = await toolbox.fetchPluginsConfig(buildArtifact);
319+
const { buildArtifact, bundleConfig }: DumpBundlesResponse =
320+
await toolbox.fetchPluginsBuildArtifact({
321+
...toolbox.inputParameters,
322+
authToken,
323+
});
319324

320325
await toolbox.filesystem.removeAsync(`.shopware-pwa/pwa-bundles.json`);
321326
await toolbox.filesystem.removeAsync(`.shopware-pwa/pwa-bundles-assets`);
@@ -324,7 +329,7 @@ module.exports = (toolbox: GluegunToolbox) => {
324329
);
325330
await toolbox.filesystem.writeAsync(
326331
".shopware-pwa/pwa-bundles.json",
327-
pluginsConfig
332+
bundleConfig
328333
);
329334

330335
await toolbox.loadPluginsAssetFile(buildArtifact);

packages/helpers/src/cms/getCmsLink.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { CmsSlot } from "@shopware-pwa/commons";
55
*
66
* @public
77
*/
8-
export function getCmsLink(content?: CmsSlot): String {
8+
export function getCmsLink(
9+
content?: Omit<CmsSlot, "data"> & { data?: { url?: string } }
10+
): String {
911
return content?.data?.url || "";
1012
}

packages/helpers/src/cms/getCmsLinkTarget.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { CmsSlot } from "@shopware-pwa/commons";
55
*
66
* @public
77
*/
8-
export function getCmsLinkTarget(content?: CmsSlot): String {
8+
export function getCmsLinkTarget(
9+
content?: Omit<CmsSlot, "data"> & {
10+
data?: { url?: string; newTab?: boolean };
11+
}
12+
): String {
913
const inNewTab = content?.data?.newTab;
1014

1115
return inNewTab ? "_blank" : "_self";

packages/nuxt-module/__tests__/module.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,58 @@ describe("nuxt-module - ShopwarePWAModule runModule", () => {
442442
);
443443
});
444444

445+
it("should remove a temporary json file after the build is done", async () => {
446+
moduleObject.options.dev = false;
447+
const afterBuildMethods: any[] = [];
448+
mockedFse.existsSync.mockReturnValue(true);
449+
moduleObject.nuxt.hook.mockImplementation(
450+
(hookName: string, method: Function) => {
451+
hookName === "build:done" && afterBuildMethods.push(method);
452+
}
453+
);
454+
await runModule(moduleObject, {});
455+
expect(moduleObject.nuxt.hook).toBeCalledWith(
456+
"build:done",
457+
expect.any(Function)
458+
);
459+
460+
expect(afterBuildMethods.length).toEqual(1);
461+
await afterBuildMethods[0](moduleObject);
462+
const pathToDelete = path.join(
463+
moduleObject.options.rootDir,
464+
".shopware-pwa",
465+
"pwa-bundles.json"
466+
);
467+
expect(mockedFse.existsSync).toBeCalledWith(pathToDelete);
468+
expect(mockedFse.removeSync).toBeCalledWith(pathToDelete);
469+
});
470+
471+
it("should catch the error while a temporary json cannot be deleted", async () => {
472+
moduleObject.options.dev = false;
473+
const afterBuildMethods: any[] = [];
474+
mockedFse.existsSync.mockReturnValue(true);
475+
mockedFse.removeSync.mockImplementationOnce(() => {
476+
throw new Error("test");
477+
});
478+
479+
moduleObject.nuxt.hook.mockImplementation(
480+
(hookName: string, method: Function) => {
481+
hookName === "build:done" && afterBuildMethods.push(method);
482+
}
483+
);
484+
await runModule(moduleObject, {});
485+
expect(moduleObject.nuxt.hook).toBeCalledWith(
486+
"build:done",
487+
expect.any(Function)
488+
);
489+
490+
await afterBuildMethods[0](moduleObject);
491+
expect(consoleErrorSpy).toHaveBeenLastCalledWith(
492+
"error while trying to remove a dump file: ",
493+
expect.any(Error)
494+
);
495+
});
496+
445497
it("should add plugins registered in theme - js|ts files only including extracted mode", async () => {
446498
mockedFiles.getAllFiles.mockReturnValueOnce([
447499
"/file/path/plugins/notifications.js",

packages/nuxt-module/src/module.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,20 @@ export async function runModule(
299299
const sourceDir = path.join(TARGET_SOURCE, "static");
300300
const destinationDir = path.join(builder.options.rootDir, "static");
301301
await fse.copy(sourceDir, destinationDir);
302+
303+
const temporaryShopwarePwaDumpFile = path.join(
304+
moduleObject.options.rootDir,
305+
".shopware-pwa",
306+
"pwa-bundles.json"
307+
);
308+
309+
if (fse.existsSync(temporaryShopwarePwaDumpFile)) {
310+
try {
311+
fse.removeSync(temporaryShopwarePwaDumpFile);
312+
} catch (error) {
313+
console.error("error while trying to remove a dump file: ", error);
314+
}
315+
}
302316
console.info(
303317
"Moved static files to root directory static folder. Make sure your static files are placed inside `src/static` directory."
304318
);

0 commit comments

Comments
 (0)