basant307/AI_Governance_Project
048
1import fs from 'node:fs'2import path from 'node:path'3import { execSync } from 'node:child_process'4 5// When executing the "postinstall" script, the "process.cwd" equals6// the package directory, not the parent project where the package is installed.7// NPM stores the parent project directory in the "INIT_CWD" env variable.8const parentPackageCwd = process.env.INIT_CWD9 10function postInstall() {11 const packageJson = JSON.parse(12 fs.readFileSync(path.resolve(parentPackageCwd, 'package.json'), 'utf8'),13 )14 15 if (!packageJson.msw || !packageJson.msw.workerDirectory) {16 return17 }18 19 const cliExecutable = path.resolve(process.cwd(), 'cli/index.js')20 21 try {22 /**23 * @note Call the "init" command directly. It will now copy the worker script24 * to all saved paths in "msw.workerDirectory"25 */26 execSync(`node ${cliExecutable} init`, {27 cwd: parentPackageCwd,28 })29 } catch (error) {30 console.error(31 `[MSW] Failed to automatically update the worker script.\n\n${error}`,32 )33 }34}35 36postInstall()37 