Skip to content

Commit 2191bb0

Browse files
use internal labeled pipeline, fixes #15 (#20)
Transform order can be configured by setting `{ order: 'start' }` or `{ order: 'end' }` in transform options.
1 parent b52205e commit 2191bb0

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

index.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var parallel = require('async-collection').parallel
2+
var splicer = require('labeled-stream-splicer')
23
var fromString = require('from2-string')
34
var stream = require('readable-stream')
45
var parse = require('fast-json-parse')
@@ -67,6 +68,9 @@ var placeholder = null
6768
Documentify.prototype.transform = function (transform, opts) {
6869
var self = this
6970
opts = opts || {}
71+
72+
if (!opts.order) opts = Object.assign({}, opts, { order: 'main' })
73+
7074
if (typeof transform === 'string') {
7175
var index = this.transforms.length
7276
var basedir = opts.basedir || this.basedir
@@ -91,6 +95,24 @@ Documentify.prototype.transform = function (transform, opts) {
9195
return this
9296
}
9397

98+
Documentify.prototype.createPipeline = function () {
99+
var pipeline = splicer([
100+
'start', [],
101+
'main', [],
102+
'end', []
103+
])
104+
105+
this.transforms.forEach(function (tuple) {
106+
var fn = tuple[0]
107+
var opts = tuple[1]
108+
var label = opts && opts.order
109+
var transform = fn(opts)
110+
pipeline.get(label).push(transform)
111+
})
112+
113+
return pipeline
114+
}
115+
94116
Documentify.prototype.bundle = function () {
95117
var pts = new stream.PassThrough()
96118
var self = this
@@ -109,16 +131,9 @@ Documentify.prototype.bundle = function () {
109131
]
110132
parallel(tasks, function (err) {
111133
if (err) return pts.emit('error', err)
112-
var args = self.transforms.reduce(function (arr, tuple) {
113-
var fn = tuple[0]
114-
var opts = tuple[1] || {}
115-
var transform = fn(opts)
116-
arr.push(transform)
117-
return arr
118-
}, [source])
119-
120-
args.push(pts)
121-
pump.apply(pump, args)
134+
var pipeline = self.createPipeline()
135+
136+
pump(source, pipeline, pts)
122137
})
123138
}
124139

@@ -159,6 +174,9 @@ Documentify.prototype.bundle = function () {
159174
opts = transform[1]
160175
}
161176

177+
// Run package.json transforms first by default.
178+
if (!opts.order) opts.order = 'start'
179+
162180
return function (done) {
163181
resolve(name, { basedir: basedir }, function (err, resolved) {
164182
if (err) return done(err)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"findup": "^0.1.5",
1717
"from2-string": "^1.1.0",
1818
"inherits": "^2.0.3",
19+
"labeled-stream-splicer": "^2.0.0",
1920
"pump": "^1.0.2",
2021
"readable-stream": "^2.3.3",
2122
"resolve": "^1.4.0",

test/order/transform.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
var PassThrough = require('readable-stream').PassThrough
1+
var Transform = require('readable-stream').Transform
22

33
module.exports = function () {
4-
var ps = PassThrough()
5-
ps.push('package.json prepend\n')
6-
return ps
4+
var first = true
5+
return Transform({
6+
transform (chunk, enc, cb) {
7+
if (first) this.push('package.json prepend\n')
8+
first = false
9+
this.push(chunk)
10+
cb(null)
11+
}
12+
})
713
}

test/transforms.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var test = require('tape')
22
var path = require('path')
33
var Transform = require('readable-stream').Transform
4-
var PassThrough = require('readable-stream').PassThrough
54
var concat = require('concat-stream')
65
var documentify = require('../')
76

@@ -47,18 +46,24 @@ test('transforms', function (t) {
4746
.bundle()
4847
.pipe(concat({ encoding: 'string' }, function (result) {
4948
var lines = result.split(/\n/g)
50-
t.equal(lines.shift(), '.transform prepend')
51-
t.equal(lines.shift(), 'package.json prepend')
52-
t.equal(lines.pop(), '.transform append')
53-
t.equal(lines.pop(), 'package.json append')
49+
t.equal(lines[0], '.transform prepend')
50+
t.equal(lines[1], 'package.json prepend')
51+
t.equal(lines[lines.length - 2], 'package.json append')
52+
t.equal(lines[lines.length - 1], '.transform append')
5453
}))
5554
})
5655
})
5756

5857
function prepend (text) {
59-
var ps = PassThrough()
60-
ps.push(text + '\n')
61-
return ps
58+
var first = true
59+
return Transform({
60+
write (chunk, enc, cb) {
61+
if (first) this.push(text + '\n')
62+
first = false
63+
this.push(chunk)
64+
cb()
65+
}
66+
})
6267
}
6368
function append (text) {
6469
return Transform({

0 commit comments

Comments
 (0)