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

Function copyWorkerScript

cli/init.js:110–139  ·  view source on GitHub ↗

* @param {string} destination * @param {string} cwd * @returns {Promise }

(destination, cwd)

Source from the content-addressed store, hash-verified

108 * @returns {Promise<string>}
109 */
110async function copyWorkerScript(destination, cwd) {
111 // When running as a part of "postinstall" script, "cwd" equals the library's directory.
112 // The "postinstall" script resolves the right absolute public directory path.
113 const absolutePublicDir = toAbsolutePath(destination, cwd)
114
115 if (!fs.existsSync(absolutePublicDir)) {
116 await fs.promises
117 .mkdir(absolutePublicDir, { recursive: true })
118 .catch((error) => {
119 throw new Error(
120 invariant(
121 false,
122 'Failed to copy the worker script at "%s": directory does not exist and could not be created.\nMake sure to include a relative path to the public directory of your application.\n\nSee the original error below:\n\n%s',
123 absolutePublicDir,
124 error,
125 ),
126 )
127 })
128 }
129
130 // eslint-disable-next-line no-console
131 console.log('Copying the worker script at "%s"...', absolutePublicDir)
132
133 const workerFilename = path.basename(SERVICE_WORKER_BUILD_PATH)
134 const workerDestinationPath = path.resolve(absolutePublicDir, workerFilename)
135
136 fs.copyFileSync(SERVICE_WORKER_BUILD_PATH, workerDestinationPath)
137
138 return workerDestinationPath
139}
140
141/**
142 * @param {Array<string>} paths

Callers 1

initFunction · 0.85

Calls 3

invariantFunction · 0.90
toAbsolutePathFunction · 0.85
logMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…