/
patch.ts
57 lines (50 loc) · 1.67 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
import fs from 'fs'
import { docsUrl } from '@pnpm/cli-utils'
import { Config, types as allTypes } from '@pnpm/config'
import { LogBase } from '@pnpm/logger'
import {
CreateStoreControllerOptions,
} from '@pnpm/store-connection-manager'
import pick from 'ramda/src/pick'
import renderHelp from 'render-help'
import tempy from 'tempy'
import PnpmError from '@pnpm/error'
import { writePackage } from './writePackage'
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: 'The package that needs to be modified will be extracted to this directory',
name: '--edit-dir',
},
],
}],
url: docsUrl('patch'),
usages: ['pnpm patch <pkg name>@<version>'],
})
}
export type PatchCommandOptions = Pick<Config, 'dir' | 'registries' | 'tag' | 'storeDir'> & CreateStoreControllerOptions & {
editDir?: string
reporter?: (logObj: LogBase) => void
}
export async function handler (opts: PatchCommandOptions, params: string[]) {
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}'`)
}
const editDir = opts.editDir ?? tempy.directory()
await writePackage(params[0], editDir, opts)
return `You can now edit the following folder: ${editDir}`
}