Skip to content

Commit aa69c29

Browse files
authored
Merge pull request #18 from stackhtml/accept-stream
Accept stream input for `html`
2 parents 002e7e3 + d6dba36 commit aa69c29

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Modular HTML bundler.
1818
Start bundling HTML
1919
$ documentify .
2020
21+
Bundle HTML from a stream
22+
$ cat index.html | documentify
23+
2124
Running into trouble? Feel free to file an issue:
2225
https://github.com/stackhtml/documentify/issues/new
2326
@@ -63,9 +66,11 @@ function transform (opts) {
6366

6467
## API
6568
### `document = documentify(entry, [html], [opts])`
66-
Create a new documentify instance. If `entry` is a `.html` file, it'll be used
67-
as the source. Otherwise uses an empty HTML file with just a body and head as
68-
the source.
69+
Create a new documentify instance. If `entry` is a `.html` file, it'll be
70+
used as the source. If `entry` is falsy and `html` is a string or readable
71+
stream, that will be used as the input instead. Otherwise if `entry` is falsy
72+
and `html` is omitted, an empty HTML file with just a body and head will be
73+
used as the source.
6974

7075
### `document.transform(fn, [opts])`
7176
Pass a transform to the document instance

bin.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,22 @@ var argv = subarg(process.argv.slice(2), {
4545

4646
;(function main (argv) {
4747
var entry = argv._[0]
48+
var html = null
4849

4950
// If entry isn't an absolute path, convert to absolute path
5051
if (!entry) entry = process.cwd()
5152
if (!/^\//.test(entry)) entry = path.join(process.cwd(), entry)
5253

54+
if (!process.stdin.isTTY) {
55+
html = process.stdin
56+
}
57+
5358
if (argv.help) {
5459
console.log(USAGE)
5560
} else if (argv.version) {
5661
console.log(require('./package.json').version)
5762
} else {
58-
var bundler = Documentify(entry, {
63+
var bundler = Documentify(entry, html, {
5964
transform: normalizeTransforms(argv.transform)
6065
})
6166
pump(bundler.bundle(), process.stdout, function (err) {
@@ -72,6 +77,7 @@ function clr (text, color) {
7277
}
7378

7479
function normalizeTransforms (transforms) {
80+
if (!transforms) return []
7581
if (!Array.isArray(transforms)) transforms = [transforms]
7682
return transforms.map(function (t) {
7783
if (typeof t === 'object' && Array.isArray(t._)) {

index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,26 @@ module.exports = Documentify
1515

1616
var defaultHtml = '<!DOCTYPE html><html><head></head><body></body></html>'
1717

18+
function isStream (maybe) {
19+
return maybe !== null && typeof maybe === 'object' && typeof maybe.pipe === 'function'
20+
}
21+
1822
function Documentify (entry, html, opts) {
1923
if (!(this instanceof Documentify)) return new Documentify(entry, html, opts)
2024

2125
EventEmitter.call(this)
2226

23-
assert.equal(typeof entry, 'string', 'documentify: entry should be type string')
27+
if (entry) {
28+
assert.equal(typeof entry, 'string', 'documentify: entry should be type string')
29+
}
2430

25-
if (typeof html === 'object') {
31+
if (typeof html === 'object' && !isStream(html)) {
2632
opts = html
2733
html = null
2834
}
2935

30-
if (html) {
31-
assert.equal(typeof html, 'string', 'documentify: html should be type string')
36+
if (html && !isStream(html)) {
37+
assert.equal(typeof html, 'string', 'documentify: html should be type string or stream')
3238
}
3339

3440
opts = opts || {}
@@ -119,7 +125,7 @@ Documentify.prototype.bundle = function () {
119125
return pts
120126

121127
function findTransforms (done) {
122-
var entry = path.join(path.dirname(self.entry), path.basename(self.entry))
128+
var entry = self.entry ? path.join(path.dirname(self.entry), path.basename(self.entry)) : self.basedir
123129
findup(entry, 'package.json', function (err, pathname) {
124130
// no package.json found - just run local transforms
125131
if (err) return done()
@@ -163,8 +169,8 @@ Documentify.prototype.bundle = function () {
163169
}
164170

165171
function createSource (done) {
166-
if (typeof self.html === 'string') {
167-
source = fromString(self.html)
172+
if (typeof self.html === 'string' || isStream(self.html)) {
173+
source = isStream(self.html) ? self.html : fromString(self.html)
168174
return done()
169175
}
170176
resolve(self.entry, { extensions: [ '.html' ] }, function (err, entry) {

test/input.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,25 @@ var documentify = require('../')
44
var concat = require('concat-stream')
55
var assertHtml = require('assert-html')
66
var dedent = require('dedent')
7+
var fromString = require('from2-string')
78

89
test('input', function (t) {
10+
t.test('throws if the input is not a string', function (t) {
11+
t.plan(3)
12+
13+
var truthyButNotAString = [
14+
12,
15+
{foo: 'bar'},
16+
true
17+
]
18+
19+
for (var i = 0; i < truthyButNotAString.length; i++) {
20+
t.throws(function () {
21+
documentify(truthyButNotAString[i]).bundle()
22+
}, /entry should be type string/)
23+
}
24+
})
25+
926
t.test('uses input text if specified', function (t) {
1027
t.plan(10)
1128
var sourceHtml = `
@@ -27,6 +44,52 @@ test('input', function (t) {
2744
}))
2845
})
2946

47+
t.test('accepts a html stream', function (t) {
48+
t.plan(10)
49+
50+
var sourceHtml = `
51+
<!DOCTYPE html>
52+
<html>
53+
<head>
54+
<title>test</title>
55+
</head>
56+
<body>
57+
beep boop
58+
</body>
59+
</html>
60+
`.replace(/\n +/g, '')
61+
var sourceStream = fromString(sourceHtml)
62+
63+
documentify(null, sourceStream, {})
64+
.bundle()
65+
.pipe(concat({ encoding: 'string' }, function (html) {
66+
assertHtml(t, sourceHtml, html)
67+
}))
68+
})
69+
70+
t.test('accepts a html stream without options provided', function (t) {
71+
t.plan(10)
72+
73+
var sourceHtml = `
74+
<!DOCTYPE html>
75+
<html>
76+
<head>
77+
<title>test</title>
78+
</head>
79+
<body>
80+
beep boop
81+
</body>
82+
</html>
83+
`.replace(/\n +/g, '')
84+
var sourceStream = fromString(sourceHtml)
85+
86+
documentify(null, sourceStream)
87+
.bundle()
88+
.pipe(concat({ encoding: 'string' }, function (html) {
89+
assertHtml(t, sourceHtml, html)
90+
}))
91+
})
92+
3093
t.test('defaults to empty document', function (t) {
3194
t.plan(7)
3295
documentify(path.join(__dirname, 'test.html'))

0 commit comments

Comments
 (0)