MCPcopy
hub / github.com/mswjs/msw / init

Function init

cli/init.js:8–92  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

6import { SERVICE_WORKER_BUILD_PATH } from '../config/constants.js'
7
8export async function init(args) {
9 const CWD = args.cwd || process.cwd()
10 const publicDir = args._[1] ? normalizePath(args._[1]) : undefined
11
12 const packageJsonPath = path.resolve(CWD, 'package.json')
13 const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
14 const savedWorkerDirectories = Array.prototype
15 .concat((packageJson.msw && packageJson.msw.workerDirectory) || [])
16 .map(normalizePath)
17
18 if (publicDir) {
19 // If the public directory was provided, copy the worker script
20 // to that directory only. Even if there are paths stored in "msw.workerDirectory",
21 // those will not be touched.
22 await copyWorkerScript(publicDir, CWD)
23 const relativePublicDir = path.relative(CWD, publicDir)
24 printSuccessMessage([publicDir])
25
26 if (args.save) {
27 // Only save the public path if it's not already saved in "package.json".
28 if (!savedWorkerDirectories.includes(relativePublicDir)) {
29 saveWorkerDirectory(packageJsonPath, relativePublicDir)
30 }
31 }
32 // Explicitly check if "save" was not provided (was null).
33 // You can also provide the "--no-save" option, and then "args.save"
34 // will equal to false.
35 else if (args.save == null) {
36 // eslint-disable-next-line no-console
37 console.log(`\
38 ${colors.cyan(
39 'INFO',
40 )} In order to ease the future updates to the worker script,
41 we recommend saving the path to the worker directory in your package.json.`)
42
43 // If the "--save" flag was not provided, prompt to save
44 // the public path.
45 promptWorkerDirectoryUpdate(
46 `Do you wish to save "${relativePublicDir}" as the worker directory?`,
47 packageJsonPath,
48 relativePublicDir,
49 )
50 }
51
52 return
53 }
54
55 // Calling "init" without a public directory but with the "--save" flag is a no-op.
56 invariant(
57 args.save == null,
58 'Failed to copy the worker script: cannot call the "init" command without a public directory but with the "--save" flag. Either drop the "--save" flag to copy the worker script to all paths listed in "msw.workerDirectory", or add an explicit public directory to the command, like "npx msw init ./public".',
59 )
60
61 // If the public directory was not provided, check any existing
62 // paths in "msw.workerDirectory". When called without the public
63 // directory, the "init" command must copy the worker script
64 // to all the paths stored in "msw.workerDirectory".
65 if (savedWorkerDirectories.length > 0) {

Callers

nothing calls this directly

Calls 10

invariantFunction · 0.90
copyWorkerScriptFunction · 0.85
printSuccessMessageFunction · 0.85
saveWorkerDirectoryFunction · 0.85
toAbsolutePathFunction · 0.85
printFailureMessageFunction · 0.85
normalizePathFunction · 0.70
parseMethod · 0.45
logMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…