-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
rtlcss.js
272 lines (251 loc) · 6.99 KB
/
rtlcss.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env node
var path = require('path'),
fs = require('fs'),
sys = require('util'),
chalk = require('chalk'),
mkdirp = require('mkdirp'),
rtlcss = require('../lib/rtlcss'),
configLoader = require('../lib/config-loader')
;
var input, output, directory, ext,
config, currentErrorcode,
arg, args = process.argv.slice(2),
shouldBreak = false
;
process.on('exit', function() { process.reallyExit(currentErrorcode) });
function printWarning (){
console.log(chalk.yellow.apply(this,printWarning.arguments));
}
function printInfo (){
console.log(chalk.green.apply(this,printInfo.arguments));
}
function printError (){
console.log(chalk.red.apply(this,printError.arguments));
}
function printHelp(){
console.log('Usage: rtlcss [option option=parameter ...] [source] [destination]');
console.log('');
var options =
[
'Option ' , 'Description ',
'--------------' , '----------------------------------------------',
'-h,--help' , 'Print help (this message) and exit.',
'-v,--version' , 'Print version number and exit.',
'-c,--config' , 'Path to configuration settings file.',
'- ,--stdin' , 'Read from stdin stream.',
'-d,--dirctory' , 'Process all *.css files from input directory (recursive).',
'-e,--ext' , 'Used with -d option to set the output files extension.\n\t\t Default: ".rtl.css".',
'-s,--silent' , 'Silent mode, no warnings or errors are printed.'
];
for(var x=0;x<options.length;x++){
console.log(options[x++],'\t',options[x]);
}
console.log('');
console.log('*If no destination is specified, output will be written to the same input folder as {source}.rtl.{ext}');
console.log('');
printInfo('RTLCSS version: ' + require('../package.json').version);
printInfo('Report issues to: https://github.com/MohammadYounes/rtlcss/issues');
}
while(arg = args.shift()){
switch(arg){
case '-h':
case '--help':
printHelp();
shouldBreak = true;
break;
case '-v':
case '--version':
printInfo('rtlcss version: ' + require('../package.json').version);
shouldBreak = true;
break;
case '-c':
case '--config':
arg = args.shift();
try
{
config = configLoader.load(path.resolve(arg));
}
catch(e)
{
printError('rtlcss: invalid config file. ', e);
shouldBreak = true;
currentErrorcode = 1;
}
break;
case '-d':
case '--directory':
directory = true;
break;
case '-e':
case '--ext':
ext = args.shift();
break;
case '-s':
case '--silent':
console.log = function(){};
break;
case '-':
case '--stdin':
input = '-';
break;
default:
if(arg[0] == '-'){
printError('rtlcss: unknown option. ' + arg);
shouldBreak = true;
}else{
if(!input)
input = path.resolve(arg);
else if(!output)
output = path.resolve(arg);
}
break;
}
}
if(shouldBreak){
return;
}
if (!directory && !input) {
printError('rtlcss: no input file');
console.log('');
printHelp();
return;
}
if(!config && input !== '-'){
try
{
var cwd = input;
if(directory !== true){
cwd = path.dirname(input);
}
config = configLoader.load(null, cwd);
}catch(e){
printError('rtlcss: invalid config file. ', e);
currentErrorcode = 1;
return;
}
}
if(!output && input !== '-'){
if(directory !== true){
output = path.extname(input) ? input.replace(/\.[^.]*$/, function(ext){ return '.rtl' + ext;}) : input + '.rtl';
}
else{
output = input;
}
}
var processCSSFile = function (e, data, outputName) {
if (e) {
printError('rtlcss: ' + e.message);
return;
}
var result, opt = { map: undefined, from:undefined, to:undefined};
if(!config){
printWarning('rtlcss: Warning! No config present, using defaults.');
result = rtlcss().process(data, opt);
}else{
if(config.map === true && input !== '-'){
opt.map = config.map;
opt.from = input;
opt.to = output;
}
result = rtlcss(config.options,
config.rules,
config.declarations,
config.properties).process(data, opt);
}
if (output) {
var savePath = outputName;
if(directory !== true){
savePath = output;
}
printInfo('Saving:', savePath);
fs.writeFile(savePath, result.css, 'utf8', function(err){ err && printError(err); });
if(opt.map == true){
fs.writeFile(savePath + '.map', result.map, 'utf8', function(err){ err && printError(err); } );
}
} else {
sys.print(result.css);
}
};
var walk = function(dir, done) {
fs.readdir(dir, function (error, list) {
if (error) {
return done(error);
}
var i = 0;
(function next() {
var file = list[i++];
if (!file) {
return done(null);
}
file = dir + path.sep + file;
fs.stat(file, function (error, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (error) {
next();
});
} else {
//process only *.css
if (/\.(css)$/.test(file)) {
//compute output directory
var relativePath = path.relative(input,file).split(path.sep);
relativePath.pop();
relativePath.push(path.basename(file).replace(".css", ext || ".rtl.css"));
//set rtl file name
var rtlFile = path.join(output,relativePath.join(path.sep));
//create output directory if it does not exist
var dirName = path.dirname(rtlFile);
if(!fs.existsSync(dirName))
mkdirp.sync(dirName);
//read and process the file.
fs.readFile(file, 'utf8',function(e,data){
try{
processCSSFile(e,data,rtlFile);
}catch(e){
printError('rtlcss: error processing file',file);
printError(e);
}
});
}
next();
}
});
})();
});
};
if (input != '-') {
if(directory !== true){
fs.stat(input, function (error, stat) {
if (stat && stat.isDirectory()){
printError('rtlcss: Input expected to be a file, use -d option to process a directory.');
}else{
fs.readFile(input, 'utf8', function(e,data){
try{
processCSSFile(e,data);
}catch(e){
printError('rtlcss: error processing file',input);
printError(e);
}
});
}
});
}else{
walk(input, function(error){
if(error)
printError('rtlcss: '+ error);
});
}
} else {
process.stdin.resume();
process.stdin.setEncoding('utf8');
var buffer = '';
process.stdin.on('data', function(data) {
buffer += data;
});
process.on('SIGINT', function(){
processCSSFile(false, buffer);
process.exit();
});
process.stdin.on('end', function() {
processCSSFile(false, buffer);
});
}