-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
yarn.ts
206 lines (192 loc) · 6.67 KB
/
yarn.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import is from '@sindresorhus/is';
import { gte, minVersion, validRange } from 'semver';
import { quote } from 'shlex';
import { join } from 'upath';
import { SYSTEM_INSUFFICIENT_DISK_SPACE } from '../../../constants/error-messages';
import { id as npmId } from '../../../datasource/npm';
import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import { ExecOptions, exec } from '../../../util/exec';
import { readFile, remove } from '../../../util/fs';
import { PostUpdateConfig, Upgrade } from '../../common';
import { getNodeConstraint } from './node-version';
export interface GenerateLockFileResult {
error?: boolean;
lockFile?: string;
stderr?: string;
}
export async function hasYarnOfflineMirror(cwd: string): Promise<boolean> {
try {
const yarnrc = await readFile(`${cwd}/.yarnrc`, 'utf8');
if (is.string(yarnrc)) {
const mirrorLine = yarnrc
.split('\n')
.find((line) => line.startsWith('yarn-offline-mirror '));
if (mirrorLine) {
return true;
}
}
} catch (err) /* istanbul ignore next */ {
// not found
}
return false;
}
export const optimizeCommand =
"sed -i 's/ steps,/ steps.slice(0,1),/' /home/ubuntu/.npm-global/lib/node_modules/yarn/lib/cli.js";
export async function generateLockFile(
cwd: string,
env: NodeJS.ProcessEnv,
config: PostUpdateConfig = {},
upgrades: Upgrade[] = []
): Promise<GenerateLockFileResult> {
const lockFileName = join(cwd, 'yarn.lock');
logger.debug(`Spawning yarn install to create ${lockFileName}`);
let lockFile = null;
try {
const yarnCompatibility = config.constraints?.yarn;
const minYarnVersion =
validRange(yarnCompatibility) && minVersion(yarnCompatibility);
const isYarn1 = !minYarnVersion || minYarnVersion.major === 1;
const isYarnDedupeAvailable =
minYarnVersion && gte(minYarnVersion, '2.2.0');
let installYarn = 'npm i -g yarn';
if (isYarn1 && minYarnVersion) {
installYarn += `@${quote(yarnCompatibility)}`;
}
const preCommands = [installYarn];
const extraEnv: ExecOptions['extraEnv'] = {
NPM_CONFIG_CACHE: env.NPM_CONFIG_CACHE,
npm_config_store: env.npm_config_store,
CI: 'true',
};
if (
isYarn1 &&
config.skipInstalls !== false &&
(await hasYarnOfflineMirror(cwd)) === false
) {
logger.debug('Updating yarn.lock only - skipping node_modules');
// The following change causes Yarn 1.x to exit gracefully after updating the lock file but without installing node_modules
preCommands.push(optimizeCommand);
}
const commands = [];
let cmdOptions = '';
if (isYarn1) {
cmdOptions +=
'--ignore-engines --ignore-platform --network-timeout 100000';
} else {
extraEnv.YARN_HTTP_TIMEOUT = '100000';
}
if (global.trustLevel !== 'high' || config.ignoreScripts) {
if (isYarn1) {
cmdOptions += ' --ignore-scripts';
} else {
extraEnv.YARN_ENABLE_SCRIPTS = '0';
}
}
const tagConstraint = await getNodeConstraint(config);
const execOptions: ExecOptions = {
cwd,
extraEnv,
docker: {
image: 'renovate/node',
tagScheme: 'npm',
tagConstraint,
preCommands,
},
};
// istanbul ignore if
if (global.trustLevel === 'high') {
execOptions.extraEnv.NPM_AUTH = env.NPM_AUTH;
execOptions.extraEnv.NPM_EMAIL = env.NPM_EMAIL;
execOptions.extraEnv.NPM_TOKEN = env.NPM_TOKEN;
}
if (config.dockerMapDotfiles) {
const homeDir =
process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const homeNpmrc = join(homeDir, '.npmrc');
execOptions.docker.volumes = [[homeNpmrc, '/home/ubuntu/.npmrc']];
}
// This command updates the lock file based on package.json
commands.push(`yarn install ${cmdOptions}`.trim());
// rangeStrategy = update-lockfile
const lockUpdates = upgrades.filter((upgrade) => upgrade.isLockfileUpdate);
if (lockUpdates.length) {
logger.debug('Performing lockfileUpdate (yarn)');
if (isYarn1) {
// `yarn upgrade` updates based on the version range specified in the package file
// note - this can hit a yarn bug, see https://github.com/yarnpkg/yarn/issues/8236
commands.push(
`yarn upgrade ${lockUpdates
.map((update) => update.depName)
.join(' ')} ${cmdOptions}`.trim()
);
} else {
// `yarn up` updates to the latest release, so the range should be specified
commands.push(
`yarn up ${lockUpdates
.map((update) => `${update.depName}@${update.newValue}`)
.join(' ')}`
);
}
}
// postUpdateOptions
if (isYarn1 && config.postUpdateOptions?.includes('yarnDedupeFewer')) {
logger.debug('Performing yarn dedupe fewer');
commands.push('npx yarn-deduplicate --strategy fewer');
// Run yarn again in case any changes are necessary
commands.push(`yarn install ${cmdOptions}`.trim());
}
if (
(isYarn1 || isYarnDedupeAvailable) &&
config.postUpdateOptions?.includes('yarnDedupeHighest')
) {
logger.debug('Performing yarn dedupe highest');
if (isYarn1) {
commands.push('npx yarn-deduplicate --strategy highest');
// Run yarn again in case any changes are necessary
commands.push(`yarn install ${cmdOptions}`.trim());
} else {
commands.push('yarn dedupe --strategy highest');
}
}
if (upgrades.find((upgrade) => upgrade.isLockFileMaintenance)) {
logger.debug(
`Removing ${lockFileName} first due to lock file maintenance upgrade`
);
try {
await remove(lockFileName);
} catch (err) /* istanbul ignore next */ {
logger.debug(
{ err, lockFileName },
'Error removing yarn.lock for lock file maintenance'
);
}
}
// Run the commands
await exec(commands, execOptions);
// Read the result
lockFile = await readFile(lockFileName, 'utf8');
} catch (err) /* istanbul ignore next */ {
logger.debug(
{
err,
type: 'yarn',
},
'lock file error'
);
if (err.stderr) {
if (err.stderr.includes('ENOSPC: no space left on device')) {
throw new Error(SYSTEM_INSUFFICIENT_DISK_SPACE);
}
if (
err.stderr.includes('The registry may be down.') ||
err.stderr.includes('getaddrinfo ENOTFOUND registry.yarnpkg.com') ||
err.stderr.includes('getaddrinfo ENOTFOUND registry.npmjs.org')
) {
throw new ExternalHostError(err, npmId);
}
}
return { error: true, stderr: err.stderr };
}
return { lockFile };
}