Debian Codegraph @

readme.markdown

raw
1 # outpipe
2
3 write output to a file through shell commands
4
5 # purpose
6
7 Suppose you have a tool like
8 [watchify](https://npmjs.com/package/watchify)
9 or [factor-bundle](https://npmjs.com/package/factor-bundle)
10 that write to multiple files or write to the same file more than once.
11
12 If you want to pipe the output of these tools to other programs, such as
13 minification with the `uglify` command, it's very difficult! You might need to
14 use the tool's API or use a separate command to watch for changes to the output
15 files. Ick.
16
17 You don't get the elegance of something like:
18
19 ``` sh
20 $ browserify main.js | uglifyjs -cm | gzip > bundle.js.gz
21 ```
22
23 Until now! With this library and a hypothetical version of watchify, you could
24 do:
25
26 ``` sh
27 $ watchify main.js -dv -o 'uglifyjs -cm | gzip > bundle.js.gz'
28 ```
29
30 # example
31
32 Here's a small watcher program that will just copy input files to a destination,
33 but transforms can be applied along the way with shell pipes and redirects.
34
35 ``` js
36 var outpipe = require('outpipe');
37 var gaze = require('gaze');
38 var fs = require('fs');
39
40 var minimist = require('minimist');
41 var argv = minimist(process.argv.slice(2), {
42 alias: { o: 'output' }
43 });
44
45 var file = argv._[0];
46 gaze(file, function (err, w) {
47 w.on('changed', read);
48 });
49 read();
50
51 function read () {
52 var r = fs.createReadStream(file);
53 r.pipe(outpipe(argv.output));
54 }
55 ```
56
57 We can run the program with a single output file:
58
59 ``` sh
60 $ node watch.js input/x.js -o output/hmm.js
61 ```
62
63 which just copies `x.js` to `output/hmm.js` whenever `x.js` changes.
64
65 We could also run a minification step using the `uglify` command:
66
67 ``` sh
68 $ node watch.js input/x.js -o 'uglifyjs -cm > output/wow.js'
69 ```
70
71 or we can just print the size of the minified and gzipped output to stdout:
72
73 ``` sh
74 $ node watch.js input/x.js -o 'uglifyjs -cm | gzip | wc -c'
75 123
76 ```
77
78 or we could write that size to a file:
79
80 ``` sh
81 $ node watch.js input/x.js -o 'uglifyjs -cm | gzip | wc -c > size.txt'
82 ```
83
84 # methods
85
86 ``` js
87 var outpipe = require('outpipe')
88 ```
89
90 ## var w = outpipe(cmd, opts={})
91
92 Return a writable stream `w` that will pipe output to the command string `cmd`.
93
94 If `cmd` has no operators (`|` or `>`), it will write to a file.
95
96 Otherwise, each command between pipes will be executed and output is written to
97 a file if `>` is given.
98
99 `opts` can be:
100
101 * `opts.env` - an object mapping environment variables to their values or a
102 `function (key) {}` that returns the values.
103
104 stdout and stderr are forwarded to process.stdout and process.stderr if
105 unhandled in the command.
106
107 # install
108
109 With [npm](https://npmjs.org) do:
110
111 ```
112 npm install outpipe
113 ```
114
115 # license
116
117 MIT