-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·125 lines (101 loc) · 2.83 KB
/
index.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const fs = require('fs')
const path = require('path')
const minimist = require('minimist')
const mkdirp = require('mkdirp')
const chokidar = require('chokidar')
const pkg = require('./package')
const compile = require('mineral/compilers/html')
const parse = require('mineral/parser')
const argv = minimist(process.argv.slice(2))
// TODO: get a pre-cache all mixins
if (argv.h) {
console.log(`
Usage:
min FILE1, ... [options]
Options:
-w Watch for changes and recompile
-o DIR Output directory
-d '...' A string of JSON, used as locals
--data FILE A path to a JSON file to use as locals
`)
process.exit(0)
}
if (argv.v) {
console.log(pkg.version)
}
let data = {}
if (argv.d) {
try {
data = JSON.parse(argv.d)
} catch (ex) {
console.error('Unable to parse data')
process.exit(1)
}
} else if (argv.data) {
try {
data = require(path.resolve(argv.data))
} catch (ex) {
console.error('Unable to read file')
process.exit(1)
}
}
const deps = {}
function trunc (s) {
const parts = s.split('/')
if (parts.length > 3) return s.replace(process.cwd(), '').slice(1)
return s
}
function findCommonPath () {
if (argv._.length === 1) {
return path.dirname(argv._[0])
}
const p = argv._.reduce((a, b) => {
a = Array.isArray(a) ? a : a.split(path.sep)
b = Array.isArray(b) ? b : b.split(path.sep)
return a.filter((s, i) => s === b[i])
})
return p.join(path.sep)
}
function compileFile (file) {
const sourcefile = file
if (deps[file]) file = deps[file]
const sourcepath = path.resolve(file)
const sourcetree = fs.readFileSync(sourcepath, 'utf8')
const html = compile(parse(sourcetree), data, sourcepath)
if (!argv.o) {
return process.stdout.write(html + '\n')
}
let common = path.resolve(findCommonPath())
const out = path.join(
path.resolve(argv.o),
path.dirname(sourcepath.replace(common, ''))
)
mkdirp.sync(out)
try {
const destfile = sourcepath.replace(/\.min$/, '.html')
const target = path.join(out, path.basename(destfile))
fs.writeFileSync(target, html)
console.log(' write: %s <- %s', trunc(target), trunc(sourcefile))
} catch (ex) {
console.error(ex)
process.exit(1)
}
}
if (!argv.w) {
argv._.forEach(compileFile)
} else {
global.watcher = chokidar
.watch(argv._, { persistent: true, atomic: true })
.on('add', path => compileFile(path))
.on('change', path => compileFile(path))
.on('unlink', path => compileFile(path))
.on('addDir', path => compileFile(path))
.on('unlinkDir', path => compileFile(path))
global.addToWatcher = (origin, p) => {
const target = path.resolve(path.dirname(origin), p)
if (deps[target]) return
deps[target] = origin
global.watcher.add(target)
console.log(' watch: %s -> %s', trunc(target), trunc(origin))
}
}