Skip to content

Commit

Permalink
fix(postinstall): use the "init" cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 3, 2023
1 parent e6d4493 commit c57cea4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 48 deletions.
47 changes: 13 additions & 34 deletions config/scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const { execSync } = require('child_process')
// NPM stores the parent project directory in the "INIT_CWD" env variable.
const parentPackageCwd = process.env.INIT_CWD

function postinstall() {
// 1. Check if "package.json" has "msw.workerDirectory" property set.
function postInstall() {
const packageJson = JSON.parse(
fs.readFileSync(path.resolve(parentPackageCwd, 'package.json'), 'utf8'),
)
Expand All @@ -19,39 +18,19 @@ function postinstall() {

const cliExecutable = path.resolve(process.cwd(), 'cli/index.js')

// 2. Check if the worker directory is an existing path.
const workerDirectories = Array.prototype.concat(
packageJson.msw.workerDirectory,
)

for (const workerDirectory of workerDirectories) {
const absoluteWorkerDirectory = path.resolve(
parentPackageCwd,
workerDirectory,
try {
/**
* @note Call the "init" command directly. It will now copy the worker script
* to all saved paths in "msw.workerDirectory"
*/
execSync(`node ${cliExecutable} init`, {
cwd: parentPackageCwd,
})
} catch (error) {
console.error(
`[MSW] Failed to automatically update the worker script.\n\n${error}`,
)

console.log({ absoluteWorkerDirectory })

if (!fs.existsSync(absoluteWorkerDirectory)) {
console.warn(
`[MSW] Failed to automatically update the worker script at "%s": given path does not exist.`,
workerDirectory,
)
continue
}

// 3. Update the worker script.
try {
execSync(`node ${cliExecutable} init ${absoluteWorkerDirectory}`, {
cwd: parentPackageCwd,
})
} catch (error) {
console.error(
`[MSW] Failed to automatically update the worker script at "${absoluteWorkerDirectory}":\n${error}`,
)
continue
}
}
}

postinstall()
postInstall()
30 changes: 16 additions & 14 deletions test/node/msw-api/auto-update-worker.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ const fsMock = createTeardown({
rootDir: fromTemp('auto-update-worker'),
})

beforeAll(async () => {
await fsMock.prepare()
})
describe.sequential(
'worker script auto-update',
() => {
beforeAll(async () => {
await fsMock.prepare()
})

afterEach(async () => {
// await fsMock.reset()
})
afterEach(async () => {
await fsMock.reset()
})

afterAll(async () => {
await fsMock.cleanup()
})
afterAll(async () => {
await fsMock.cleanup()
})

describe.sequential(
'worker script auto-updates',
() => {
test('updates the worker script on the postinstall hook', async () => {
await fsMock.create({
'package.json': JSON.stringify({
Expand Down Expand Up @@ -71,7 +71,6 @@ describe.sequential(
`npm install msw-${packageJson.version}.tgz`,
)
expect(installCommand.stderr).toBe('')
console.log(installCommand.stdout)

expect(
fs.existsSync(fsMock.resolve('packages/one/mockServiceWorker.js')),
Expand All @@ -81,5 +80,8 @@ describe.sequential(
).toEqual(true)
})
},
{ timeout: 60_000 },
{
// These tests actually build, pack, and install MSW so they may take time.
timeout: 60_000,
},
)

0 comments on commit c57cea4

Please sign in to comment.