diff --git a/src/index.ts b/src/index.ts index fede8b3..b999d84 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,24 +28,25 @@ promptForParameters().then( async (parameters) => { await setupMSW(parameters) - outro(`Now add the following script into your ${ - { - browser: "entry", - nodejs: "setting-up test", - }[parameters.integrationType] - } script then you're all set! + if (parameters.integrationType === "browser") { + outro(`Now add the following script into your entry script then you're all set! - ${ - emphasize.highlight( - "javascript", - { - browser: startWorkerScript, - nodejs: setupTestsScript, - }[parameters.integrationType], - ).value - } - - Happy mocking!`) + ${emphasize.highlight("javascript", startWorkerScript).value}`) + } else if (parameters.integrationType === "nodejs") { + outro(`Now add the following script into your setting-up test script then you're all set! + + ${emphasize.highlight("javascript", setupTestsScript).value}`) + } else if (parameters.integrationType === "both") { + outro(`Now add the following script into your entry script, + + ${emphasize.highlight("javascript", startWorkerScript).value} + + and add the following script into your setting-up test script then you're all set! + + ${emphasize.highlight("javascript", setupTestsScript).value}`) + } + console.log(` Happy mocking!`) + console.log() }, // User aborts () => { diff --git a/src/promptForParameters.ts b/src/promptForParameters.ts index 76b16d2..a36e1a6 100644 --- a/src/promptForParameters.ts +++ b/src/promptForParameters.ts @@ -37,6 +37,10 @@ export async function promptForParameters() { value: "nodejs", label: "Node", }, + { + value: "both", + label: "Both browser and Node.js", + }, ], }) diff --git a/src/setupMSW.ts b/src/setupMSW.ts index 2a3ab1c..cef003c 100644 --- a/src/setupMSW.ts +++ b/src/setupMSW.ts @@ -6,7 +6,7 @@ import { copyTemplate, runAsyncWithSpinner } from "./utils" type SetupMSWOptions = | { apiType: "rest" | "graphql" | "both" - integrationType: "browser" + integrationType: "browser" | "both" publicDirectory: string } | { @@ -44,20 +44,46 @@ export async function setupMSW(options: SetupMSWOptions) { ) } - // Cerate mocks/browser.js or mocks/server.js + // Cerate mocks/browser.js or/and mocks/server.js - const integrationFileName = { - browser: "browser.js", - nodejs: "server.js", - }[options.integrationType] + if (options.integrationType === "browser") { + await runAsyncWithSpinner( + `Generating ${pc.green("mocks/browser.js")}...`, + async () => { + await copyTemplate( + "browser.js", + path.join(mocksDirectoryPath, "browser.js"), + ) + }, + ) + } - await runAsyncWithSpinner( - `Generating ${pc.green("mocks/" + integrationFileName)}...`, - async () => { - await copyTemplate( - integrationFileName, - path.join(mocksDirectoryPath, integrationFileName), - ) - }, - ) + if (options.integrationType === "nodejs") { + await runAsyncWithSpinner( + `Generating ${pc.green("mocks/server.js")}...`, + async () => { + await copyTemplate( + "server.js", + path.join(mocksDirectoryPath, "server.js"), + ) + }, + ) + } + + if (options.integrationType === "both") { + await runAsyncWithSpinner( + `Generating ${pc.green("mocks/browser.js")} and ${pc.green( + "mocks/server.js", + )}...`, + async () => { + await Promise.all([ + copyTemplate( + "browser.js", + path.join(mocksDirectoryPath, "browser.js"), + ), + copyTemplate("server.js", path.join(mocksDirectoryPath, "server.js")), + ]) + }, + ) + } }