-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
yarn.js
57 lines (46 loc) · 1.61 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
'use strict';
const execa = require('execa');
const {Observable} = require('rxjs');
const {ProcessError} = require('../errors');
/**
* Runs a yarn command. Can return an Observer which allows
* listr to output the current status of yarn
*/
module.exports = function yarn(yarnArgs, options) {
options = options || {};
const observe = options.observe || false;
delete options.observe;
const verbose = options.verbose || false;
delete options.verbose;
yarnArgs = yarnArgs || [];
if (verbose) {
yarnArgs.push('--verbose');
}
const execaOpts = {...options, preferLocal: true, localDir: __dirname};
const cp = execa('yarn', yarnArgs, execaOpts);
if (!observe) {
// execa augments the error object with
// some other properties, so we just pass
// the entire error object in as options to
// the ProcessError
return cp.catch(error => Promise.reject(new ProcessError(error)));
}
return new Observable((observer) => {
const onData = data => observer.next(data.replace(/\n$/, ''));
cp.stdout.setEncoding('utf8');
cp.stdout.on('data', onData);
if (verbose) {
cp.stderr.setEncoding('utf8');
cp.stderr.on('data', onData);
}
cp.then(() => {
observer.complete();
}).catch((error) => {
// execa augments the error object with
// some other properties, so we just pass
// the entire error object in as options to
// the ProcessError
observer.error(new ProcessError(error));
});
});
};