-
-
Notifications
You must be signed in to change notification settings - Fork 938
/
patch.ts
102 lines (91 loc) · 2.77 KB
/
patch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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'
export function rcOptionsTypes () {
return pick([], allTypes)
}
export function cliOptionsTypes () {
return { ...rcOptionsTypes(), 'edit-dir': String }
}
export const shorthands = {
d: '--edit-dir',
}
export const commandNames = ['patch']
export function help () {
return renderHelp({
description: 'Prepare a package for patching',
descriptionLists: [{
title: 'Options',
list: [
{
description: 'Directory path that patch files will be stored. If not set, the files is stored in temporary directory.',
name: '--edit-dir',
shortAlias: '-d',
},
],
}],
url: docsUrl('patch'),
usages: ['pnpm patch [--edit-dir <user directory path for patch>]'],
})
}
export type PatchCommandOptions = Pick<
Config,
'dir' | 'registries' | 'tag' | 'storeDir'
> &
CreateStoreControllerOptions & {
editDir?: string
reporter?: (logObj: LogBase) => void
}
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 patchPackageDir = createPackageDirectory(opts.editDir) ?? tempDir
const userChangesDir = path.join(patchPackageDir, '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 (!editDir) {
return
}
if (fs.existsSync(editDir)) {
throw new PnpmError('DIR_ALREADY_EXISTS', `The package directory already exists: '${editDir}'`)
}
fs.mkdirSync(editDir, { recursive: true })
return editDir
}