This repository has been archived by the owner on Nov 3, 2019. It is now read-only.
forked from jamsyoung/dust-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dust-compiler.js
executable file
·151 lines (124 loc) · 4.6 KB
/
dust-compiler.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#! /usr/bin/env node
/*jslint node: true */
'use strict';
var fs = require('fs'),
log,
path = require('path'),
argv = require('optimist')
.usage('Usage: dust-compiler -s|--source source_path -d|--destination destination_path [--bootstrap] [--nonotify] [--includepath]')
.demand(['source', 'destination'])
.alias('source', 's')
.alias('destination', 'd')
.describe('source', 'The source .dust templates to watch.')
.describe('destination', 'The destination destination to write compiles .js files out to.')
.describe('bootstrap', 'Does not replicate. Displays the curl command that would be used.')
.describe('nonotify', 'Do not notify.')
.describe('includepath', 'Include the path in the name of the compiled dust template.')
.argv,
dust = require('dustjs-linkedin'),
watch = require('watch'),
colors = require('colors'),
mkdirp = require('mkdirp'),
wrench = require('wrench'),
source = argv.s.match('/$') ? path.normalize(argv.s) : path.normalize(argv.s + '/'), // must end in slash
destination = argv.d.match('/$') ? path.normalize(argv.d) : path.normalize(argv.d + '/'); // must end in slash
if (argv.nonotify) {
log = console.log;
} else {
log = (function () {
var notifier;
switch (process.platform) {
case 'darwin':
notifier = require('terminal-notifier');
return function (message) {
notifier(message.stripColors, {
title: 'Dust Compiler',
activate: 'com.apple.Terminal'
});
console.log(message);
};
case 'linux':
notifier = require('notify-send');
return function (message) {
notifier.notify('Dust Compiler', message.stripColors);
console.log(message);
};
default:
return console.log;
}
}());
}
function compile(src, curr, prev) {
var data,
error = false,
filename,
filepath,
basename,
compileFilename;
src = path.normalize(src);
if (path.extname(src) === '.dust') {
fs.stat(src, function (err, stat) {
if (err) {
log('[ERROR]'.red + ' fs.stat');
throw err;
}
if (!stat.isDirectory()) {
filename = src.substring(source.length);
basename = filename.substring(0, filename.length - 5);
compileFilename = argv.includepath ? destination + basename : basename;
filepath = destination + basename + '.js';
fs.readFile(src, function (err, data) {
if (err) {
log('[ERROR]'.red + ' fs.readFile');
throw err;
}
try {
data = dust.compile(data.toString(), compileFilename);
} catch (e) {
error = true;
log(e);
}
if (!error) {
mkdirp.mkdirp(path.dirname(filepath), function (err) {
if (err) {
log('[ERROR]'.red + ' mkdirp.mkdirp: ' + err);
throw err;
}
fs.writeFile(filepath, data, function (err) {
if (err) {
log('[ERROR]'.red + ' fs.writeFile: ' + err);
throw err;
}
log('Compiled '.green + filepath + ' as '.green + compileFilename);
});
});
}
});
}
});
}
}
if (argv.bootstrap) {
wrench.readdirRecursive(source, function (error, fileList) {
if (fileList) {
fileList.forEach(function (filename) {
compile(source + filename);
});
}
});
}
if (!fs.existsSync(source)) {
console.log('[ERROR]'.red + ' ' + source + ' does not exist');
process.exit();
}
if (!fs.existsSync(destination)) {
console.log('[ERROR]'.red + ' ' + destination + ' does not exist');
process.exit();
}
watch.createMonitor(source, function (monitor) {
log('Watching '.green + source);
log('Will write to '.yellow + destination.yellow);
log('Type ^C to cancel'.yellow);
monitor.on('created', compile);
monitor.on('changed', compile);
});