/
yarn.ts
161 lines (151 loc) · 5.26 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
import is from '@sindresorhus/is';
import { readFile, remove } from 'fs-extra';
import { 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 { 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 {
let installYarn = 'npm i -g yarn';
const yarnCompatibility = config.compatibility?.yarn;
if (validRange(yarnCompatibility)) {
installYarn += `@${quote(yarnCompatibility)}`;
}
const preCommands = [installYarn];
if (
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 =
'--ignore-engines --ignore-platform --network-timeout 100000';
if (global.trustLevel !== 'high' || config.ignoreScripts) {
cmdOptions += ' --ignore-scripts';
}
const tagConstraint = await getNodeConstraint(config);
const execOptions: ExecOptions = {
cwd,
extraEnv: {
NPM_CONFIG_CACHE: env.NPM_CONFIG_CACHE,
npm_config_store: env.npm_config_store,
},
docker: {
image: 'renovate/node',
tagScheme: 'npm',
tagConstraint,
preCommands,
},
};
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)
.map((upgrade) => upgrade.depName);
if (lockUpdates.length) {
logger.debug('Performing lockfileUpdate (yarn)');
commands.push(
`yarn upgrade ${lockUpdates.join(' ')} ${cmdOptions}`.trim()
);
}
// postUpdateOptions
if (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 (config.postUpdateOptions?.includes('yarnDedupeHighest')) {
logger.debug('Performing yarn dedupe highest');
commands.push('npx yarn-deduplicate --strategy highest');
// Run yarn again in case any changes are necessary
commands.push(`yarn install ${cmdOptions}`.trim());
}
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 };
}