-
Notifications
You must be signed in to change notification settings - Fork 416
/
yarn.js
153 lines (133 loc) · 4.63 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
'use strict';
/**
* Yarn packager.
*
* Yarn specific packagerOptions (default):
* flat (false) - Use --flat with install
* ignoreScripts (false) - Do not execute scripts during install
* 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');
class Yarn {
// eslint-disable-next-line lodash/prefer-constant
static get lockfileName() {
return 'yarn.lock';
}
static get copyPackageSectionNames() {
return ['resolutions'];
}
// eslint-disable-next-line lodash/prefer-constant
static get mustCopyModules() {
return false;
}
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: 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 });
}
}
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) {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
const args = [ 'install', '--non-interactive' ];
// Convert supported packagerOptions
if (!packagerOptions.noFrozenLockfile) {
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) {
return Yarn.install(cwd, packagerOptions);
}
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;