forked from glslify/glslify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·101 lines (87 loc) · 2.14 KB
/
bin.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
const glslifyBundle = require('glslify-bundle')
const glslifyDeps = require('glslify-deps')
const glslResolve = require('glsl-resolve')
const minimist = require('minimist')
const path = require('path')
const bl = require('bl')
const fs = require('fs')
const depper = glslifyDeps()
const argv = minimist(process.argv.slice(2), {
alias: {
t: 'transform',
g: 'global',
p: 'post',
o: 'output',
v: 'version',
h: 'help'
}
})
var input = ''
if (argv.version) {
const version = require('./package.json').version
process.stdout.write('v' + version + '\n')
process.exit(0)
}
if (argv.help) return help()
if (!argv._.length && process.stdin.isTTY) return help()
// Apply source transforms
argv.t = argv.t || []
argv.t = Array.isArray(argv.t) ? argv.t : [argv.t]
argv.t.forEach(function(tr) {
depper.transform(tr)
})
argv.g = argv.g || []
argv.g = Array.isArray(argv.g) ? argv.g : [argv.g]
argv.g.forEach(function(tr) {
depper.transform(tr, { global: true })
})
argv.p = argv.p || []
argv.p = Array.isArray(argv.p) ? argv.p : [argv.p]
//
// Build dependency tree, then output
//
if (argv._.length) {
return depper.add(argv._[0], output)
}
process.stdin.pipe(bl(function(err, src) {
if (err) throw err
depper.inline(src, process.cwd(), output)
}))
//
// Logs --help information out to stderr.
//
function help() {
fs.createReadStream(path.join(__dirname, 'usage.txt'))
.once('close', function() {
console.error()
process.exit(1)
})
.pipe(process.stderr)
}
//
// Finally, apply shared functions for --post transforms
// and output the result to either stdout or the output
// file.
//
function output(err, tree) {
if (err) throw err
var src = String(glslifyBundle(tree))
next()
function next() {
var tr = argv.p.shift()
if (!tr) return done()
var transform = require(tr)
transform(null, src, {
post: true
}, function(err, data) {
if (err) throw err
if (data) src = data
next()
})
}
function done() {
if (!argv.output) return process.stdout.write(src)
fs.writeFileSync(argv.output, src)
}
}