-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.ts
More file actions
78 lines (69 loc) · 2.56 KB
/
exec.ts
File metadata and controls
78 lines (69 loc) · 2.56 KB
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
/**
* @file Execute Yarn Classic (v1.x) commands with optimized flags and security
* defaults. SECURITY: Array-based arguments prevent command injection. NOTE:
* This is the canonical home of the cross-yarn execYarn. Yarn Berry (v2-v4)
* and ZPM (v6+) currently delegate to this implementation — that's
* load-bearing in the dir layout: when version-specific behavior diverges
* (e.g. Berry's `--immutable` replacing Classic's `--frozen-lockfile`), the
* override lands in the version's own eco/npm/yarn-{berry,zpm}/exec.ts file,
* and yarn-classic stays here.
*/
import { execBin } from '../../../../bin/exec'
import { isDebug } from '../../../../debug/namespace'
import {
ArrayPrototypeIndexOf,
ArrayPrototypeSlice,
} from '../../../../primordials/array'
import { isNpmLoglevelFlag, isNpmProgressFlag } from '../../npm/flags'
import { isPnpmIgnoreScriptsFlag } from '../../pnpm/flags'
import { SetCtor } from '../../../../primordials/map-set'
import type { SpawnOptions } from '../../../../process/spawn/types'
// Commands that support --ignore-scripts in yarn (similar to npm/pnpm).
const yarnInstallLikeCommands = new SetCtor([
'add',
'import',
'install',
'link',
'remove',
'unlink',
'upgrade',
])
/**
* Execute yarn commands with optimized flags and settings.
*
* @example
* ;```typescript
* await execYarn(['install'])
* await execYarn(['add', 'lodash'], { cwd: '/tmp/project' })
* ```
*/
export function execYarn(args: string[], options?: SpawnOptions | undefined) {
const useDebug = isDebug()
const terminatorPos = ArrayPrototypeIndexOf(args, '--')
const yarnArgs = (
terminatorPos === -1 ? args : ArrayPrototypeSlice(args, 0, terminatorPos)
).filter((a: string) => !isNpmProgressFlag(a))
const otherArgs =
terminatorPos === -1 ? [] : ArrayPrototypeSlice(args, terminatorPos)
const firstArg = yarnArgs[0]
// execYarn is exercised via integration tests passing `['--version']`;
// most install-like-flag branches don't fire there.
/* c8 ignore start */
const supportsIgnoreScripts = firstArg
? yarnInstallLikeCommands.has(firstArg)
: false
const logLevelArgs =
useDebug || yarnArgs.some(isNpmLoglevelFlag) ? [] : ['--silent']
const hasIgnoreScriptsFlag = yarnArgs.some(isPnpmIgnoreScriptsFlag)
const ignoreScriptsArgs =
!supportsIgnoreScripts || hasIgnoreScriptsFlag ? [] : ['--ignore-scripts']
/* c8 ignore stop */
return execBin(
'yarn',
[...logLevelArgs, ...ignoreScriptsArgs, ...yarnArgs, ...otherArgs],
{
__proto__: null,
...options,
} as SpawnOptions,
)
}