/
roole
executable file
·178 lines (150 loc) · 4.5 KB
/
roole
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env node
var path = require('path');
var fs = require('fs-extra');
var program = require('commander');
var chokidar = require('chokidar');
var roole = require('..');
var ver = require('../package.json').version;
var cwd = process.cwd();
var watcher;
program
.version(ver, '-v, --version')
.usage('[options] [file|dir ...]')
.option('-s, --stdout', 'Output compiled CSS to stdout')
.option('-o, --out <dir>', 'Write compiled files to <dir>')
.option('-w, --watch', 'Watch files and re-compile if changed')
.option('-f, --force', 'Create files even when compile to empty string')
.option(' --prefixes <names>', 'comma-separated vendor names to use for prefixing')
.option(' --indent <str>', 'Indentation string to use in CSS')
.option(' --precision <num>', 'Max number of decimal digits to use in CSS', parseInt)
.option(' --skip-prefixed', 'Not generate prefixed rule that already exists')
.parse(process.argv);
if (program.out) program.out = path.resolve(program.out);
program.base = process.cwd();
if (!program.args.length) {
readStdin(function (data) {
roole.compile(data, program, function (err, css) {
if (err) {
displayError(err);
process.exit(1);
}
console.log(css);
});
});
return;
}
program.args.forEach(function (arg) {
compile(path.resolve(arg));
});
function readStdin(cb) {
var data = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) { data += chunk; });
process.stdin.on('end', function(){
cb(data);
});
}
function compile(filename, root) {
fs.stat(filename, function (err, stats) {
if (err) throw err;
if (stats.isFile(filename)) {
// skip files not ending in .roo when compiling a dir
if (root && path.extname(filename) !== '.roo') return;
return compileFile(filename, root);
}
if (!stats.isDirectory()) return;
var dirname = filename;
fs.readdir(dirname, function (err, filenames) {
if (err) throw err;
filenames.forEach(function (filename) {
filename = path.resolve(dirname, filename);
compile(filename, root || dirname);
});
});
});
}
function compileFile(filename, root) {
var outname;
if (program.out) {
if (root) {
var relative = path.relative(root, filename);
outname = path.resolve(program.out, relative);
} else {
var basename = path.basename(filename);
outname = path.resolve(program.out, basename);
}
} else {
outname = filename;
}
if (path.extname(outname) !== '.roo') outname += '.css';
else outname = outname.replace(/\.roo$/, '.css');
compileFileTo(filename, outname);
}
function compileFileTo(filename, outname) {
fs.readFile(filename, 'utf8', function (err, data) {
if (err) throw err;
var opts = Object.create(program);
opts.filename = filename;
opts.out = path.dirname(outname);
roole.compile(data, opts, function (err, css) {
if (err) {
displayError(err);
if (program.watch) {
var imports = Object.keys(opts.imports);
watchFile(filename, imports, outname);
return;
}
process.exit(1);
}
if (program.stdout) {
if (css) console.log(css);
return;
}
if (!css && !program.force) {
filename = path.relative(cwd, filename);
if (program.watch) {
console.log(' %s compiled to empty string, not writing to file', filename);
var imports = Object.keys(opts.imports);
watchFile(filename, imports, outname);
return;
}
console.log('INFO: %s compiled to empty string, not writing to file', filename);
return;
}
if (css) css += '\n';
fs.outputFile(outname, css, function (err) {
if (err) throw err;
if (!program.watch) return;
var imports = Object.keys(opts.imports);
watchFile(filename, imports, outname);
});
});
});
}
function watchFile(filename, imports, outname) {
if (watcher) {
var msg = ' compiled %s to %s';
filename = path.relative(cwd, filename);
outname = path.relative(cwd, outname);
console.log(' compiled %s to %s', filename, outname);
watcher.add(imports);
return;
}
var filenames = imports.concat(filename);
watcher = chokidar.watch(filenames, { persistent: true });
watcher.on('change', function () {
compileFileTo(filename, outname);
});
filename = path.relative(cwd, filename);
console.log(' watching %s', filename);
}
function displayError(err) {
if (!err.loc) {
console.error(err.toString() + err.stack);
return;
}
console.error(err.loc.filename + ':' + err.loc.line + ':' + err.loc.column + '\n');
console.error(err.toString() + '\n');
console.error(err.context(' '));
}