fix(patch): allow to edit a package in any directory (#5331)

close #5330

ref #5293
This commit is contained in:
Zoltan Kochan authored and GitHub committed 2022-09-10 19:34:14 +03:00
1 parent 32814aa96f
commit 3f0137077e
5 files changed
+77 -60

No files matched your search

+6 -37
View File
@@ -1,18 +1,15 @@
import fs from 'fs'
import path from 'path'
import { docsUrl } from '@pnpm/cli-utils'
import { Config, types as allTypes } from '@pnpm/config'
import { LogBase } from '@pnpm/logger'
import {
createOrConnectStoreController,
CreateStoreControllerOptions,
} from '@pnpm/store-connection-manager'
import parseWantedDependency from '@pnpm/parse-wanted-dependency'
import pick from 'ramda/src/pick'
import pickRegistryForPackage from '@pnpm/pick-registry-for-package'
import renderHelp from 'render-help'
import tempy from 'tempy'
import PnpmError from '@pnpm/error'
import { writePackage } from './writePackage'
export function rcOptionsTypes () {
return pick([], allTypes)
@@ -51,38 +48,10 @@ export type PatchCommandOptions = Pick<Config, 'dir' | 'registries' | 'tag' | 's
}
export async function handler (opts: PatchCommandOptions, params: string[]) {
const store = await createOrConnectStoreController({
...opts,
packageImportMethod: 'clone-or-copy',
})
const dep = parseWantedDependency(params[0])
const pkgResponse = await store.ctrl.requestPackage(dep, {
downloadPriority: 1,
lockfileDir: opts.dir,
preferredVersions: {},
projectDir: opts.dir,
registry: (dep.alias && pickRegistryForPackage(opts.registries, dep.alias)) ?? opts.registries.default,
})
const filesResponse = await pkgResponse.files!()
const tempDir = tempy.directory()
const userChangesDir = opts.editDir ? createPackageDirectory(opts.editDir) : path.join(tempDir, 'user')
await Promise.all([
store.ctrl.importPackage(path.join(tempDir, 'source'), {
filesResponse,
force: true,
}),
store.ctrl.importPackage(userChangesDir, {
filesResponse,
force: true,
}),
])
return `You can now edit the following folder: ${userChangesDir}`
}
function createPackageDirectory (editDir: string) {
if (fs.existsSync(editDir)) {
throw new PnpmError('PATCH_EDIT_DIR_EXISTS', `The target directory already exists: '${editDir}'`)
if (opts.editDir && fs.existsSync(opts.editDir) && fs.readdirSync(opts.editDir).length > 0) {
throw new PnpmError('PATCH_EDIT_DIR_EXISTS', `The target directory already exists: '${opts.editDir}'`)
}
fs.mkdirSync(editDir, { recursive: true })
return editDir
const editDir = opts.editDir ?? tempy.directory()
await writePackage(params[0], editDir, opts)
return `You can now edit the following folder: ${editDir}`
}