-
Notifications
You must be signed in to change notification settings - Fork 417
/
Copy pathyarn.js
187 lines (164 loc) · 5.79 KB
/
yarn.js
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
'use strict';
/**
* Yarn packager.
*
* Yarn specific packagerOptions (default):
* flat (false) - Use --flat with install
* ignoreScripts (false) - Do not execute scripts during install
* noNonInteractive (false) - Disable interactive mode when using Yarn 2 or above
* noFrozenLockfile (false) - Do not require an up-to-date yarn.lock
* networkConcurrency (8) - Specify number of concurrent network requests
*/
const _ = require('lodash');
const BbPromise = require('bluebird');
const Utils = require('../utils');
const findWorkspaceRoot = require('find-yarn-workspace-root');
class Yarn {
// eslint-disable-next-line lodash/prefer-constant
static get lockfileName() {
return 'yarn.lock';
}
// eslint-disable-next-line lodash/prefer-constant
static get mustCopyModules() {
return false;
}
static copyPackageSectionNames(packagerOptions) {
const options = packagerOptions || {};
return options.copyPackageSectionNames || ['resolutions'];
}
static isBerryVersion(version) {
const versionNumber = version.charAt(0);
const mainVersion = parseInt(versionNumber);
return mainVersion > 1;
}
static getPackagerVersion(cwd) {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
const args = ['-v'];
return Utils.spawnProcess(command, args, { cwd })
.catch(err => {
return BbPromise.resolve({ stdout: err.stdout });
})
.then(processOutput => processOutput.stdout);
}
static getProdDependencies(cwd, depth) {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
const args = ['list', `--depth=${depth || 1}`, '--json', '--production'];
// If we need to ignore some errors add them here
const ignoredYarnErrors = [];
return Utils.spawnProcess(command, args, {
cwd: findWorkspaceRoot(cwd) || cwd
})
.catch(err => {
if (err instanceof Utils.SpawnError) {
// Only exit with an error if we have critical npm errors for 2nd level inside
const errors = _.split(err.stderr, '\n');
const failed = _.reduce(
errors,
(failed, error) => {
if (failed) {
return true;
}
return (
!_.isEmpty(error) &&
!_.some(ignoredYarnErrors, ignoredError => _.startsWith(error, `npm ERR! ${ignoredError.npmError}`))
);
},
false
);
if (!failed && !_.isEmpty(err.stdout)) {
return BbPromise.resolve({ stdout: err.stdout });
}
if (process.env.SLS_DEBUG) {
console.error(`DEBUG: ${err.stdout}\nSTDERR: ${err.stderr}`);
}
}
return BbPromise.reject(err);
})
.then(processOutput => processOutput.stdout)
.then(stdout =>
BbPromise.try(() => {
const lines = Utils.splitLines(stdout);
const parsedLines = _.map(lines, Utils.safeJsonParse);
return _.find(parsedLines, line => line && line.type === 'tree');
})
)
.then(parsedTree => {
const convertTrees = trees =>
_.reduce(
trees,
(__, tree) => {
const splitModule = _.split(tree.name, '@');
// If we have a scoped module we have to re-add the @
if (_.startsWith(tree.name, '@')) {
splitModule.splice(0, 1);
splitModule[0] = '@' + splitModule[0];
}
__[_.first(splitModule)] = {
version: _.join(_.tail(splitModule), '@'),
dependencies: convertTrees(tree.children)
};
return __;
},
{}
);
const trees = _.get(parsedTree, 'data.trees', []);
const result = {
problems: [],
dependencies: convertTrees(trees)
};
return result;
});
}
static rebaseLockfile(pathToPackageRoot, lockfile) {
const fileVersionMatcher = /[^"/]@(?:file:)?((?:\.\/|\.\.\/).*?)[":,]/gm;
const replacements = [];
let match;
// Detect all references and create replacement line strings
while ((match = fileVersionMatcher.exec(lockfile)) !== null) {
replacements.push({
oldRef: match[1],
newRef: _.replace(`${pathToPackageRoot}/${match[1]}`, /\\/g, '/')
});
}
// Replace all lines in lockfile
return _.reduce(replacements, (__, replacement) => _.replace(__, replacement.oldRef, replacement.newRef), lockfile);
}
static install(cwd, packagerOptions, version) {
if (packagerOptions.noInstall) {
return BbPromise.resolve();
}
const isBerry = Yarn.isBerryVersion(version);
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
const args = ['install'];
// Convert supported packagerOptions
if (!packagerOptions.noNonInteractive && !isBerry) {
args.push('--non-interactive');
}
if (!packagerOptions.noFrozenLockfile) {
if (isBerry) {
args.push('--immutable');
} else {
args.push('--frozen-lockfile');
}
}
if (packagerOptions.ignoreScripts) {
args.push('--ignore-scripts');
}
if (packagerOptions.networkConcurrency) {
args.push(`--network-concurrency ${packagerOptions.networkConcurrency}`);
}
return Utils.spawnProcess(command, args, { cwd }).return();
}
// "Yarn install" prunes automatically
static prune(cwd, packagerOptions, version) {
return Yarn.install(cwd, packagerOptions, version);
}
static runScripts(cwd, scriptNames) {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
return BbPromise.mapSeries(scriptNames, scriptName => {
const args = ['run', scriptName];
return Utils.spawnProcess(command, args, { cwd });
}).return();
}
}
module.exports = Yarn;