mirror of
https://github.com/hanzoai/msw.git
synced 2026-09-24 17:00:34 +00:00
feat: create directory in cli init if it doesn't exist
Try to create the directory when running npx msw init public and the directory does not exist. Throw the invariant error in case the creation of the directory fails
This commit is contained in:
committed by
Artem Zakharchenko
parent
6fad600664
commit
ac447f6080
+14
-6
@@ -1,13 +1,14 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const chalk = require('chalk')
|
||||
const { until } = require('@open-draft/until')
|
||||
const inquirer = require('inquirer')
|
||||
const invariant = require('./invariant')
|
||||
const { SERVICE_WORKER_BUILD_PATH } = require('../config/constants')
|
||||
|
||||
const CWD = process.cwd()
|
||||
|
||||
module.exports = function init(args) {
|
||||
module.exports = async function init(args) {
|
||||
const { publicDir, save } = args
|
||||
|
||||
// When running as a part of "postinstall" script, "cwd" equals the library's directory.
|
||||
@@ -18,11 +19,18 @@ module.exports = function init(args) {
|
||||
const relativePublicDir = path.relative(CWD, absolutePublicDir)
|
||||
const dirExists = fs.existsSync(absolutePublicDir)
|
||||
|
||||
invariant(
|
||||
dirExists,
|
||||
'Failed to create a Service Worker at "%s": directory does not exist.\nMake sure to include a relative path to the root directory of your server.',
|
||||
absolutePublicDir,
|
||||
)
|
||||
if (!dirExists) {
|
||||
// Try to create the directory if it doesn't exist
|
||||
const [createDirectoryError] = await until(() =>
|
||||
fs.promises.mkdir(absolutePublicDir, { recursive: true }),
|
||||
)
|
||||
invariant(
|
||||
createDirectoryError == null,
|
||||
'Failed to create a Service Worker at "%s": directory does not exist and could not be created.\nMake sure to include a relative path to the root directory of your server.\n\nSee the original error below:\n%s',
|
||||
absolutePublicDir,
|
||||
createDirectoryError,
|
||||
)
|
||||
}
|
||||
|
||||
console.log(
|
||||
'Initializing the Mock Service Worker at "%s"...',
|
||||
|
||||
@@ -44,18 +44,45 @@ test('copies the worker script into an existing directory without errors', async
|
||||
await cleanup()
|
||||
})
|
||||
|
||||
test('throws an exception and does not copy the worker script given a non-existing directory', async () => {
|
||||
test('creates the directory if it does not exist', async () => {
|
||||
const { prepare, getPath, cleanup } = createTeardown(
|
||||
'tmp/cli/init/non-existing',
|
||||
)
|
||||
await prepare()
|
||||
|
||||
const { stderr } = await promisifyChildProcess(
|
||||
exec(`node cli/index.js init ${getPath('public/nested/path')} --no-save`),
|
||||
)
|
||||
|
||||
// Does not produce any errors.
|
||||
expect(stderr).toBe('')
|
||||
expect(
|
||||
fs.existsSync(getPath('public/nested/path/mockServiceWorker.js')),
|
||||
).toBe(true)
|
||||
|
||||
await cleanup()
|
||||
})
|
||||
|
||||
test('throws an exception and does not copy the worker script if creating the non-existing directory fails', async () => {
|
||||
const { prepare, getPath, cleanup } = createTeardown(
|
||||
'tmp/cli/init/non-existing',
|
||||
addFile('.tmp'), // The folder isn't actually created before we add a file
|
||||
)
|
||||
await prepare()
|
||||
|
||||
// Ensure the path exists so we can make it readonly
|
||||
expect(fs.existsSync('tmp/cli/init')).toBe(true)
|
||||
fs.chmodSync(getPath(), '0444')
|
||||
|
||||
const { stderr } = await promisifyChildProcess(
|
||||
exec(`node cli/index.js init ${getPath('non-existing-public')} --no-save`),
|
||||
)
|
||||
|
||||
// Reset the permissions
|
||||
fs.chmodSync(getPath(), '777')
|
||||
|
||||
expect(stderr).not.toBe('')
|
||||
expect(stderr).toContain('directory does not exist')
|
||||
expect(stderr).toContain('directory does not exist and could not be created')
|
||||
expect(
|
||||
fs.existsSync(getPath('non-existing-public/mockServiceWorker.js')),
|
||||
).toBe(false)
|
||||
|
||||
Reference in New Issue
Block a user