Skip to content

Commit

Permalink
feat: allow selecting both integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed Feb 28, 2023
1 parent a22eadd commit 36e3c22
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 32 deletions.
35 changes: 18 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
() => {
Expand Down
4 changes: 4 additions & 0 deletions src/promptForParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export async function promptForParameters() {
value: "nodejs",
label: "Node",
},
{
value: "both",
label: "Both browser and Node.js",
},
],
})

Expand Down
56 changes: 41 additions & 15 deletions src/setupMSW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { copyTemplate, runAsyncWithSpinner } from "./utils"
type SetupMSWOptions =
| {
apiType: "rest" | "graphql" | "both"
integrationType: "browser"
integrationType: "browser" | "both"
publicDirectory: string
}
| {
Expand Down Expand Up @@ -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")),
])
},
)
}
}

0 comments on commit 36e3c22

Please # to comment.