Skip to content

feat: expose runsAfter to add-ons #554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/loose-colts-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

feat: expose `runsAfter` to add-ons
4 changes: 4 additions & 0 deletions packages/addons/storybook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export default defineAddon({
shortDescription: 'frontend workshop',
homepage: 'https://storybook.js.org',
options: {},
setup: ({ runsAfter }) => {
runsAfter('vitest');
runsAfter('eslint');
},
run: async ({ sv }) => {
await sv.execute(['storybook@latest', 'init', '--skip-install', '--no-dev'], 'inherit');
sv.devDependency(`@types/node`, getNodeTypesVersion());
Expand Down
17 changes: 9 additions & 8 deletions packages/cli/lib/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ export function setupAddons(
const addonSetupResults: Record<string, AddonSetupResult> = {};

for (const addon of addons) {
const setupResult: AddonSetupResult = { unsupported: [], dependsOn: [] };
const setupResult: AddonSetupResult = { unsupported: [], dependsOn: [], runsAfter: [] };
addon.setup?.({
...workspace,
dependsOn: (name) => setupResult.dependsOn.push(name),
unsupported: (reason) => setupResult.unsupported.push(reason)
dependsOn: (name) => {
setupResult.dependsOn.push(name);
setupResult.runsAfter.push(name);
},
unsupported: (reason) => setupResult.unsupported.push(reason),
runsAfter: (name) => setupResult.runsAfter.push(name)
});
addonSetupResults[addon.id] = setupResult;
}
Expand Down Expand Up @@ -181,13 +185,10 @@ async function runAddon({ addon, multiple, workspace }: RunAddon) {
}

// orders addons by putting addons that don't require any other addon in the front.
// This is a drastic simplification, as this could still cause some inconvenient cituations,
// This is a drastic simplification, as this could still cause some inconvenient circumstances,
// but works for now in contrary to the previous implementation
function orderAddons(addons: Array<Addon<any>>, setupResults: Record<string, AddonSetupResult>) {
return addons.sort((a, b) => {
// Adding storybook last means it will correctly detect and integrate with other addons like vitest and eslint
if (a.id === 'storybook') return 1;
if (b.id === 'storybook') return -1;
return setupResults[a.id]?.dependsOn?.length - setupResults[b.id]?.dependsOn?.length;
return setupResults[a.id]?.runsAfter?.length - setupResults[b.id]?.runsAfter?.length;
});
}
3 changes: 2 additions & 1 deletion packages/core/addon/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type Addon<Args extends OptionDefinition> = {
workspace: Workspace<Args> & {
dependsOn: (name: string) => void;
unsupported: (reason: string) => void;
runsAfter: (addonName: string) => void;
}
) => MaybePromise<void>;
run: (workspace: Workspace<Args> & { sv: SvApi }) => MaybePromise<void>;
Expand All @@ -59,7 +60,7 @@ export function defineAddon<Args extends OptionDefinition>(config: Addon<Args>):
return config;
}

export type AddonSetupResult = { dependsOn: string[]; unsupported: string[] };
export type AddonSetupResult = { dependsOn: string[]; unsupported: string[]; runsAfter: string[] };

export type AddonWithoutExplicitArgs = Addon<Record<string, Question>>;
export type AddonConfigWithoutExplicitArgs = Addon<Record<string, Question>>;
Expand Down