Skip to content

Commit 6af9a36

Browse files
committed
fix(adapter-nuxt): init slice library index files
1 parent 4ed7b6a commit 6af9a36

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

packages/adapter-nuxt/src/hooks/project-init.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { builders, loadFile, writeFile } from "magicast";
1616
import { buildSrcPath } from "../lib/buildSrcPath";
1717
import { rejectIfNecessary } from "../lib/rejectIfNecessary";
1818
import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
19+
import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
1920

2021
import type { PluginOptions } from "../types";
2122

@@ -262,6 +263,28 @@ const modifySliceMachineConfig = async ({
262263
});
263264
};
264265

266+
const upsertSliceLibraryIndexFiles = async (
267+
context: SliceMachineContext<PluginOptions>,
268+
) => {
269+
// We must use the `getProject()` helper to get the latest version of
270+
// the project config. The config may have been modified in
271+
// `modifySliceMachineConfig()` and will not be relfected in
272+
// `context.project`.
273+
// TODO: Automatically update the plugin runner's in-memory `project`
274+
// object when `updateSliceMachineConfig()` is called.
275+
const project = await context.helpers.getProject();
276+
277+
if (!project.config.libraries) {
278+
return;
279+
}
280+
281+
await Promise.all(
282+
project.config.libraries.map(async (libraryID) => {
283+
await upsertSliceLibraryIndexFile({ libraryID, ...context });
284+
}),
285+
);
286+
};
287+
265288
export const projectInit: ProjectInitHook<PluginOptions> = async (
266289
{ installDependencies: _installDependencies },
267290
context,
@@ -275,4 +298,8 @@ export const projectInit: ProjectInitHook<PluginOptions> = async (
275298
modifySliceMachineConfig(context),
276299
]),
277300
);
301+
302+
// This must happen after `modifySliceMachineConfig()` since the
303+
// location of the default Slice library may change.
304+
await upsertSliceLibraryIndexFiles(context);
278305
};

packages/adapter-nuxt/test/plugin-project-init.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,25 @@ describe("modify slicemachine.config.json", () => {
529529
expect(contents.libraries).toStrictEqual(preHookConfig.libraries);
530530
});
531531
});
532+
533+
test("creates all Slice library index files", async (ctx) => {
534+
await fs.writeFile(
535+
path.join(ctx.project.root, "slicemachine.config.json"),
536+
JSON.stringify({
537+
...ctx.project.config,
538+
libraries: ["./foo", "./bar"],
539+
}),
540+
);
541+
542+
await ctx.pluginRunner.callHook("project:init", {
543+
log: vi.fn(),
544+
installDependencies: vi.fn(),
545+
});
546+
547+
expect(await fs.readdir(path.join(ctx.project.root, "foo"))).includes(
548+
"index.js",
549+
);
550+
expect(await fs.readdir(path.join(ctx.project.root, "bar"))).includes(
551+
"index.js",
552+
);
553+
});

0 commit comments

Comments
 (0)