Skip to content

Commit

Permalink
fix: change option name to 'edit-dir'
Browse files Browse the repository at this point in the history
  • Loading branch information
roseline124 committed Sep 4, 2022
1 parent 42ff9f3 commit 2ea4677
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
24 changes: 12 additions & 12 deletions packages/plugin-commands-patching/src/patch.ts
Expand Up @@ -19,11 +19,11 @@ export function rcOptionsTypes () {
}

export function cliOptionsTypes () {
return { ...rcOptionsTypes(), 'user-dir': String }
return { ...rcOptionsTypes(), 'edit-dir': String }
}

export const shorthands = {
d: '--user-dir',
d: '--edit-dir',
}

export const commandNames = ['patch']
Expand All @@ -36,13 +36,13 @@ export function help () {
list: [
{
description: 'Directory path that patch files will be stored. If not set, the files is stored in temporary directory.',
name: '--user-dir',
name: '--edit-dir',
shortAlias: '-d',
},
],
}],
url: docsUrl('patch'),
usages: ['pnpm patch [--user-dir <user directory path for patch>]'],
usages: ['pnpm patch [--edit-dir <user directory path for patch>]'],
})
}

Expand All @@ -51,7 +51,7 @@ Config,
'dir' | 'registries' | 'tag' | 'storeDir'
> &
CreateStoreControllerOptions & {
userDir?: string
editDir?: string
reporter?: (logObj: LogBase) => void
}

Expand All @@ -73,7 +73,7 @@ export async function handler (opts: PatchCommandOptions, params: string[]) {
const filesResponse = await pkgResponse.files!()

const tempDir = tempy.directory()
const patchPackageDir = createPackageDirectory(opts.userDir) ?? tempDir
const patchPackageDir = createPackageDirectory(opts.editDir) ?? tempDir
const userChangesDir = path.join(patchPackageDir, 'user')
await Promise.all([
store.ctrl.importPackage(path.join(tempDir, 'source'), {
Expand All @@ -88,15 +88,15 @@ export async function handler (opts: PatchCommandOptions, params: string[]) {
return `You can now edit the following folder: ${userChangesDir}`
}

function createPackageDirectory (userDir?: string) {
if (!userDir) {
function createPackageDirectory (editDir?: string) {
if (!editDir) {
return
}

if (fs.existsSync(userDir)) {
throw new PnpmError('DIR_ALREADY_EXISTS', `The package directory already exists: '${userDir}'`)
if (fs.existsSync(editDir)) {
throw new PnpmError('DIR_ALREADY_EXISTS', `The package directory already exists: '${editDir}'`)
}

fs.mkdirSync(userDir, { recursive: true })
return userDir
fs.mkdirSync(editDir, { recursive: true })
return editDir
}
14 changes: 7 additions & 7 deletions packages/plugin-commands-patching/test/patch.test.ts
Expand Up @@ -40,7 +40,7 @@ describe('patch and commit', () => {
const userPatchDir = output.substring(output.indexOf(':') + 1).trim()
const tempDir = os.tmpdir() // temp dir depends on the operating system (@see tempy)

// store patch files(user, source) in temporary directory when not given userDir option
// store patch files(user, source) in temporary directory when not given editDir option
expect(userPatchDir).toContain(tempDir)
expect(fs.existsSync(userPatchDir)).toBe(true)
expect(fs.existsSync(userPatchDir.replace('/user', '/source'))).toBe(true)
Expand Down Expand Up @@ -69,18 +69,18 @@ describe('patch and commit', () => {
expect(fs.existsSync('node_modules/is-positive/license')).toBe(false)
})

test('store source files in temporary directory and user files in user directory, when given userDir option', async () => {
const userDir = 'test/user/is-positive'
test('store source files in temporary directory and user files in user directory, when given editDir option', async () => {
const editDir = 'test/user/is-positive'

const patchFn = async () => patch.handler({ ...defaultPatchOption, userDir }, ['is-positive@1.0.0'])
const patchFn = async () => patch.handler({ ...defaultPatchOption, editDir }, ['is-positive@1.0.0'])
const output = await patchFn()
const userPatchDir = output.substring(output.indexOf(':') + 1).trim()

expect(userPatchDir).toBe(path.join(userDir, '/user'))
expect(userPatchDir).toBe(path.join(editDir, '/user'))
expect(fs.existsSync(userPatchDir)).toBe(true)
expect(fs.existsSync(path.join(tempySpy.mock.results[0].value, '/source'))).toBe(true)

// If userDir already exists, it should throw an error
await expect(patchFn()).rejects.toThrow(`The package directory already exists: '${userDir}'`)
// If editDir already exists, it should throw an error
await expect(patchFn()).rejects.toThrow(`The package directory already exists: '${editDir}'`)
})
})

0 comments on commit 2ea4677

Please sign in to comment.