Skip to content

Commit

Permalink
fix(init): Rework init end message
Browse files Browse the repository at this point in the history
  • Loading branch information
xrutayisire committed Oct 23, 2023
1 parent 1444d34 commit b384770
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 129 deletions.
104 changes: 18 additions & 86 deletions packages/init/src/SliceMachineInitProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,106 +198,38 @@ export class SliceMachineInitProcess {
console.log(
`\n${chalk.bgGreen(` ${chalk.bold.white("Slice Machine")} `)} ${chalk.dim(
"→",
)} Initialization successful!`,
)} Initialization successful!
Continue with next steps in Slice Machine.
`,
);

try {
const runSmCommand = this.context.projectInitialization?.patchedScript
? await getRunScriptCommand({
agent: this.context.packageManager || "npm",
script: "slicemachine",
})
: await getExecuteCommand({
agent: this.context.packageManager || "npm",
script: "start-slicemachine",
});

const runProjectCommand = await getRunScriptCommand({
agent: this.context.packageManager || "npm",
script: "dev",
});

const apiEndpoints = this.manager.getAPIEndpoints();
const wroomHost = new URL(apiEndpoints.PrismicWroom).host;

const dashboardURL = new URL(
`https://${this.context.repository.domain}.${wroomHost}`,
)
.toString()
.replace(/\/$/, "");
const apiURL = new URL(
"./api/v2",
`https://${this.context.repository.domain}.cdn.${wroomHost}`,
).toString();

// We prefer to manually allow console logs despite the app being a CLI to catch wild/unwanted console logs better
// eslint-disable-next-line no-console
console.log(`
YOUR REPOSITORY
Page Builder ${chalk.cyan(dashboardURL)}
API ${chalk.cyan(apiURL)}
RESOURCES
Documentation ${chalk.cyan(
this.context.framework.prismicDocumentation,
)}
Getting help ${chalk.cyan("https://community.prismic.io")}
GETTING STARTED
Run Slice Machine ${chalk.cyan(runSmCommand)}
Run your project ${chalk.cyan(runProjectCommand)}
`);

if (this.options.starter && this.options.repository) {
const { openDashboard } = await prompt<boolean, "openDashboard">({
type: "confirm",
name: "openDashboard",
message: "Would you like to open your repository?",
initial: true,
});
if (this.options.startSlicemachine) {
const runSmCommand = this.context.projectInitialization?.patchedScript
? await getRunScriptCommand({
agent: this.context.packageManager || "npm",
script: "slicemachine",
})
: await getExecuteCommand({
agent: this.context.packageManager || "npm",
script: "start-slicemachine",
});

if (openDashboard) {
open(dashboardURL);
}
} else if (this.options.startSlicemachine) {
const pkgJSONPath = path.join(this.manager.cwd, "package.json");
const pkg = JSON.parse(await fs.readFile(pkgJSONPath, "utf-8"));
const scripts = pkg.scripts || {};

const finalSmCommand = (() => {
if (
scripts["dev"] &&
typeof scripts["dev"] === "string" &&
scripts["dev"].includes(":slicemachine")
) {
return {
command: runProjectCommand,
message: `Would you like to run your project and Slice Machine concurrently (${runProjectCommand})?`,
};
}

return {
command: runSmCommand,
message: `Would you like to run Slice Machine (${runSmCommand})?`,
};
})();
const { startSlicemachine } = await prompt<
boolean,
"startSlicemachine"
>({
type: "confirm",
name: "startSlicemachine",
message: finalSmCommand.message,
message: `Run Slice Machine (${chalk.cyan(runSmCommand)})?`,
initial: true,
});

if (startSlicemachine) {
const commandReturnValue = await execaCommand(
finalSmCommand.command,
{
env: { FORCE_COLOR: "true" },
},
).pipeStdout?.(process.stdout);
const commandReturnValue = await execaCommand(runSmCommand, {
env: { FORCE_COLOR: "true" },
}).pipeStdout?.(process.stdout);
if (commandReturnValue !== undefined) {
// eslint-disable-next-line no-console
console.log(commandReturnValue.stdout);
Expand Down
44 changes: 1 addition & 43 deletions packages/init/test/SliceMachineInitProcess-run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,49 +259,7 @@ it("outputs get started final message", async (ctx) => {
});

expect(stdout).toMatch(/Initialization successful/);
expect(stdout.pop()).toMatchInlineSnapshot(`
"
YOUR REPOSITORY
Page Builder https://repo-admin.prismic.io
API https://repo-admin.cdn.prismic.io/api/v2
RESOURCES
Documentation https://prismic.dev/init/universal
Getting help https://community.prismic.io
GETTING STARTED
Run Slice Machine npm run slicemachine
Run your project npm run dev
"
`);
});

it("adapts get started final message depending on context", async (ctx) => {
const repositoryName = "repo-admin";
const initProcess = createSliceMachineInitProcess({
repository: repositoryName,
cwd: "/",
startSlicemachine: false,
});
await prepareEnvironment(ctx, initProcess, repositoryName);
await vol.promises.writeFile(
"/package.json",
JSON.stringify({
name: "package-base",
version: "0.0.0",
scripts: {
slicemachine: "another script",
},
}),
);

const { stdout } = await watchStd(() => {
return initProcess.run();
});

// Pretty final message
expect(stdout.pop()).toMatch(/npx start-slicemachine/);
expect(stdout).toMatch(/Continue with next steps in Slice Machine./);
});

it("tracks thrown errors", async (ctx) => {
Expand Down

0 comments on commit b384770

Please sign in to comment.