Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 7b8281a

Browse files
zkochaniarna
authored andcommitted
feat(stdio): add child process io options and default logging of piped stdout/err (#3)
1 parent 3526a0a commit 7b8281a

File tree

7 files changed

+220
-1
lines changed

7 files changed

+220
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ jump in if you'd like to, or even ask us questions if something isn't clear.
3838

3939
#### <a name="lifecycle"></a> `> lifecycle(name, pkg, wd, [opts]) -> Promise`
4040

41+
##### Arguments
42+
43+
* `opts.stdio` - the [stdio](https://nodejs.org/api/child_process.html#child_process_options_stdio)
44+
passed to the child process. `[0, 1, 2]` by default.
45+
4146
##### Example
4247

4348
```javascript

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const chain = require('slide').chain
1212
const uidNumber = require('uid-number')
1313
const umask = require('umask')
1414
const which = require('which')
15+
const byline = require('byline')
1516

1617
let PATH = 'PATH'
1718

@@ -244,7 +245,7 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
244245
var conf = {
245246
cwd: wd,
246247
env: env,
247-
stdio: [ 0, 1, 2 ]
248+
stdio: opts.stdio || [ 0, 1, 2 ]
248249
}
249250

250251
if (!unsafe) {
@@ -282,6 +283,12 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
282283
}
283284
procError(er)
284285
})
286+
byline(proc.stdout).on('data', function (data) {
287+
opts.log.verbose('lifecycle', logid(pkg, stage), 'stdout', data.toString())
288+
})
289+
byline(proc.stderr).on('data', function (data) {
290+
opts.log.verbose('lifecycle', logid(pkg, stage), 'stderr', data.toString())
291+
})
285292
process.once('SIGTERM', procKill)
286293
process.once('SIGINT', procInterupt)
287294

package-lock.json

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"homepage": "https://github.com/npm/lifecycle#readme",
3131
"dependencies": {
32+
"byline": "^5.0.0",
3233
"graceful-fs": "^4.1.11",
3334
"slide": "^1.1.6",
3435
"uid-number": "0.0.6",
@@ -37,6 +38,7 @@
3738
},
3839
"devDependencies": {
3940
"nyc": "^11.1.0",
41+
"sinon": "^4.0.1",
4042
"standard": "^10.0.3",
4143
"standard-version": "^4.2.0",
4244
"tap": "^10.7.2",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "count-to-10",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"postinstall": "node postinstall"
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
console.log('line 1')
4+
console.log('line 2')
5+
console.error('some error')

test/index.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
'use strict'
22

33
const test = require('tap').test
4+
const sinon = require('sinon')
45
const lifecycle = require('../index.js')
6+
const path = require('path')
7+
8+
function noop () {}
59

610
test('makeEnv', function (t) {
711
const pkg = {
@@ -64,3 +68,74 @@ test('_incorrectWorkingDirectory: rejects wd from other packages', function (t)
6468
t.equal(lifecycle._incorrectWorkingDirectory(wd, pkg), true)
6569
t.end()
6670
})
71+
72+
test("reports child's output", function (t) {
73+
const fixture = path.join(__dirname, 'fixtures', 'count-to-10')
74+
75+
const verbose = sinon.spy()
76+
const silly = sinon.spy()
77+
const log = {
78+
level: 'silent',
79+
info: noop,
80+
warn: noop,
81+
silly,
82+
verbose,
83+
pause: noop,
84+
resume: noop,
85+
clearProgress: noop,
86+
showProgress: noop
87+
}
88+
const dir = path.join(__dirname, '..')
89+
90+
const pkg = require(path.join(fixture, 'package.json'))
91+
92+
lifecycle(pkg, 'postinstall', fixture, {
93+
stdio: 'pipe',
94+
log,
95+
dir,
96+
config: {}
97+
})
98+
.then(() => {
99+
t.ok(
100+
verbose.calledWithMatch(
101+
'lifecycle',
102+
'undefined~postinstall:',
103+
'stdout',
104+
'line 1'
105+
),
106+
'stdout reported'
107+
)
108+
t.ok(
109+
verbose.calledWithMatch(
110+
'lifecycle',
111+
'undefined~postinstall:',
112+
'stdout',
113+
'line 2'
114+
),
115+
'stdout reported'
116+
)
117+
t.ok(
118+
verbose.calledWithMatch(
119+
'lifecycle',
120+
'undefined~postinstall:',
121+
'stderr',
122+
'some error'
123+
),
124+
'stderr reported'
125+
)
126+
t.ok(
127+
silly.calledWithMatch(
128+
'lifecycle',
129+
'undefined~postinstall:',
130+
'Returned: code:',
131+
0,
132+
' signal:',
133+
null
134+
),
135+
'exit code reported'
136+
)
137+
138+
t.end()
139+
})
140+
.catch(t.end)
141+
})

0 commit comments

Comments
 (0)