Skip to content

Commit

Permalink
feat: display next steps after running the adders (#409)
Browse files Browse the repository at this point in the history
* initial implementation

* changeset

* apply suggestions

* add more context for next steps execution

* fix imports

* fix type

* add next step message to `drizzle`

* fix wording

* changeset
  • Loading branch information
manuel3108 committed Jun 22, 2024
1 parent 1802786 commit 0a20a35
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-crabs-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@svelte-add/drizzle": minor
---

feat: display next steps after running the drizzle
5 changes: 5 additions & 0 deletions .changeset/moody-pumas-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@svelte-add/core": minor
---

feat: display next steps after running the adders
5 changes: 5 additions & 0 deletions adders/drizzle/config/adder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ export const adder = defineAdderConfig({
},
},
],
nextSteps: () => {
const steps = ["You will need to set DATABASE_URL in your production environment"];

return steps;
},
});

function addEnvVar(content: string, key: string, value: string) {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/adder/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { OptionDefinition, OptionValues, Question } from "./options.js";
import type { FileTypes } from "../files/processors.js";
import type { Workspace } from "../utils/workspace.js";
import type { Postcondition } from "./postconditions.js";
import type { Colors } from "picocolors/types.js";

export type { CssAstEditor, HtmlAstEditor, JsAstEditor, SvelteAstEditor };

Expand Down Expand Up @@ -51,6 +52,7 @@ export type InlineAdderConfig<Args extends OptionDefinition> = BaseAdderConfig<A
integrationType: "inline";
packages: PackageDefinition<Args>[];
files: FileTypes<Args>[];
nextSteps?: (data: { options: OptionValues<Args>; cwd: string; colors: Colors; docs: string | undefined }) => string[];
installHook?: (workspace: Workspace<Args>) => Promise<void>;
uninstallHook?: (workspace: Workspace<Args>) => Promise<void>;
};
Expand Down
15 changes: 10 additions & 5 deletions packages/core/adder/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { validatePreconditions } from "./preconditions.js";
import { endPrompts, startPrompts, groupedMultiSelectPrompt } from "../utils/prompts.js";
import { type CategoryKeys, categories } from "./categories.js";
import { checkPostconditions, printUnmetPostconditions } from "./postconditions.js";
import { displayNextSteps } from "./nextSteps.js";

export type AdderDetails<Args extends OptionDefinition> = {
config: AdderConfig<Args>;
Expand Down Expand Up @@ -99,7 +100,7 @@ async function executePlan<Args extends OptionDefinition>(
) {
const remoteControlled = remoteControlOptions !== undefined;
const isTesting = remoteControlled && remoteControlOptions.isTesting;
const isExecutingMultipleAdders = adderDetails.length > 1;
const isRunningCli = adderDetails.length > 1;

if (!isTesting) {
console.log(pc.gray(`${executingAdder.name} version ${executingAdder.version}\n`));
Expand All @@ -122,14 +123,15 @@ async function executePlan<Args extends OptionDefinition>(

// select appropriate adders
let userSelectedAdders = executionPlan.commonCliOptions.adders ?? [];
if (userSelectedAdders.length == 0 && isExecutingMultipleAdders) {
if (userSelectedAdders.length == 0 && isRunningCli) {
// if the user has not selected any adders via the cli and we are currently executing for more than one adder
// the user should have the possibility to select the adders he want's to add.
userSelectedAdders = await askForAddersToApply(adderDetails, projectType);
} else if (userSelectedAdders.length == 0 && !isExecutingMultipleAdders) {
} else if (userSelectedAdders.length == 0 && !isRunningCli) {
// if we are executing only one adder, then we can safely assume that this adder should be added
userSelectedAdders = [adderDetails[0].config.metadata.id];
}
const isApplyingMultipleAdders = userSelectedAdders.length > 1;

// remove unselected adder data
const addersToRemove = adderDetails.filter((x) => !userSelectedAdders.includes(x.config.metadata.id));
Expand Down Expand Up @@ -180,7 +182,7 @@ async function executePlan<Args extends OptionDefinition>(
throw new Error(`Unknown integration type`);
}

const unmetAdderPostconditions = await checkPostconditions(config, checks, adderWorkspace, isExecutingMultipleAdders);
const unmetAdderPostconditions = await checkPostconditions(config, checks, adderWorkspace, isApplyingMultipleAdders);
unmetPostconditions.push(...unmetAdderPostconditions);
}

Expand All @@ -193,7 +195,10 @@ async function executePlan<Args extends OptionDefinition>(
if (!remoteControlled && !executionPlan.commonCliOptions.skipInstall)
await suggestInstallingDependencies(executionPlan.workingDirectory);

if (!isTesting) endPrompts("You're all set!");
if (!isTesting) {
displayNextSteps(adderDetails, isApplyingMultipleAdders, executionPlan);
endPrompts("You're all set!");
}
}
type AdderOption = { value: string; label: string; hint: string };
async function askForAddersToApply<Args extends OptionDefinition>(
Expand Down
37 changes: 37 additions & 0 deletions packages/core/adder/nextSteps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { messagePrompt } from "../utils/prompts";
import type { InlineAdderConfig } from "./config";
import type { AdderDetails, AddersExecutionPlan } from "./execute";
import type { OptionDefinition, OptionValues } from "./options";
import pc from "picocolors";

export function displayNextSteps<Args extends OptionDefinition>(
adderDetails: AdderDetails<Args>[],
multipleAdders: boolean,
executionPlan: AddersExecutionPlan,
) {
const allAddersMessage = adderDetails
.filter((x) => x.config.integrationType == "inline" && x.config.nextSteps)
.map((x) => x.config as InlineAdderConfig<Args>)
.map((x) => {
// only doing this to narrow the type, `nextSteps` should already exist here
if (!x.nextSteps) return "";
const metadata = x.metadata;
let adderMessage = "";
if (multipleAdders) {
adderMessage = `${pc.green(metadata.name)}:\n`;
}

const options = executionPlan.cliOptionsByAdderId[x.metadata.id] as OptionValues<Args>;

const adderNextSteps = x.nextSteps({
options,
cwd: executionPlan.workingDirectory,
colors: pc,
docs: x.metadata.website?.documentation,
});
adderMessage += `- ${adderNextSteps.join("\n- ")}`;
return adderMessage;
})
.join("\n\n");
messagePrompt("Next steps", allAddersMessage);
}

0 comments on commit 0a20a35

Please sign in to comment.