CLI: Always uses "process.cwd" in the "init" command

This commit is contained in:
Artem Zakharchenko
2021-02-24 10:44:22 +01:00
parent 6779b5c14a
commit e964486e8d
3 changed files with 19 additions and 15 deletions
+2 -2
View File
@@ -5,12 +5,12 @@ const inquirer = require('inquirer')
const invariant = require('./invariant')
const { SERVICE_WORKER_BUILD_PATH } = require('../config/constants')
const CWD = process.env.INIT_CWD || process.cwd()
const CWD = process.cwd()
module.exports = function init(args) {
const { publicDir, save } = args
// When running as a part of "postinstall" script, CWD equals the library's directory.
// When running as a part of "postinstall" script, "cwd" equals the library's directory.
// The "postinstall" script resolves the right absolute public directory path.
const absolutePublicDir = path.isAbsolute(publicDir)
? publicDir
+13 -5
View File
@@ -1,10 +1,15 @@
const fs = require('fs')
const path = require('path')
const cwd = process.env.INIT_CWD
const { execSync } = require('child_process')
// When executing the "postinstall" script, the "process.cwd" equals
// the package directory, not the parent project where the package is installed.
// NPM stores the parent project directory in the "INIT_CWD" env variable.
const parentPackageCwd = process.env.INIT_CWD
// 1. Check if "package.json" has "msw.workerDirectory" property set.
const packageJson = JSON.parse(
fs.readFileSync(path.resolve(cwd, 'package.json'), 'utf8'),
fs.readFileSync(path.resolve(parentPackageCwd, 'package.json'), 'utf8'),
)
if (!packageJson.msw || !packageJson.msw.workerDirectory) {
@@ -13,7 +18,7 @@ if (!packageJson.msw || !packageJson.msw.workerDirectory) {
// 2. Check if the worker directory is an existing path.
const { workerDirectory } = packageJson.msw
const absoluteWorkerDirectory = path.resolve(cwd, workerDirectory)
const absoluteWorkerDirectory = path.resolve(parentPackageCwd, workerDirectory)
if (!fs.existsSync(absoluteWorkerDirectory)) {
return console.error(
@@ -23,10 +28,13 @@ if (!fs.existsSync(absoluteWorkerDirectory)) {
}
// 3. Update the worker script.
const init = require('../../cli/init')
const cliExecutable = path.resolve(process.cwd(), '../../cli/index.js')
const relativeCliExecutable = path.relative(parentPackageCwd, cliExecutable)
try {
init({ publicDir: absoluteWorkerDirectory })
execSync(`node ${relativeCliExecutable} init ${absoluteWorkerDirectory}`, {
cwd: parentPackageCwd,
})
} catch (error) {
console.error(
`[MSW] Failed to automatically update the worker script:\n${error}`,
+4 -8
View File
@@ -74,10 +74,8 @@ test('saves the worker directory in package.json when none are present', async (
await prepare()
const { stderr, stdout } = await promisifyChildProcess(
exec(`node cli/index.js init ${getPath('public')} --save`, {
env: {
INIT_CWD: getPath('.'),
},
exec(`node ../../../../cli/index.js init ${getPath('public')} --save`, {
cwd: getPath('.'),
}),
)
@@ -113,10 +111,8 @@ test('warns when current public directory does not match the saved directory fro
await prepare()
const { stderr, stdout } = await promisifyChildProcess(
exec(`node cli/index.js init ${getPath('public')} --save`, {
env: {
INIT_CWD: getPath('.'),
},
exec(`node ../../../../cli/index.js init ${getPath('public')} --save`, {
cwd: getPath('.'),
}),
)