-
-
Notifications
You must be signed in to change notification settings - Fork 934
/
index.ts
45 lines (41 loc) · 1.39 KB
/
index.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
import path from 'path'
import { PnpmError } from '@pnpm/error'
import { safeReadPackageJsonFromDir } from '@pnpm/read-package-json'
import { PackageScripts } from '@pnpm/types'
import rimraf from '@zkochan/rimraf'
import execa from 'execa'
import preferredPM from 'preferred-pm'
const PREPARE_SCRIPTS = [
'preinstall',
'install',
'postinstall',
'prepare',
]
const PREPUBLISH_SCRIPTS = [
'prepublish',
'prepublishOnly',
'prepack',
'publish',
'postpublish',
]
export async function preparePackage (pkgDir: string) {
const manifest = await safeReadPackageJsonFromDir(pkgDir)
if (manifest?.scripts == null || !packageShouldBeBuilt(manifest.scripts)) return
const pm = (await preferredPM(pkgDir))?.name ?? 'npm'
try {
await execa(pm, ['install'], { cwd: pkgDir })
for (const scriptName of PREPUBLISH_SCRIPTS) {
if (manifest.scripts[scriptName] == null || manifest.scripts[scriptName] === '') continue
await execa(pm, ['run', scriptName], { cwd: pkgDir })
}
} catch (err: any) { // eslint-disable-line
throw new PnpmError('PREPARE_PKG_FAILURE', err.shortMessage ?? err.message)
}
await rimraf(path.join(pkgDir, 'node_modules'))
}
function packageShouldBeBuilt (packageScripts: PackageScripts): boolean {
return [
...PREPUBLISH_SCRIPTS,
...PREPARE_SCRIPTS,
].some((scriptName) => packageScripts[scriptName] != null && packageScripts[scriptName] !== '')
}