32f993ff892997dfe9d50789d73bc1fb9a92136c
[platform/framework/web/web-ui-fw.git] / build-tools / bin / lessc
1 #!/usr/bin/env node
2
3 var path = require('path'),
4     fs = require('fs'),
5     sys = require('sys');
6
7 try {
8         // For old node.js versions
9         require.paths.unshift( path.join( __dirname, '..', 'lib' ) );
10 } catch ( ex ) {
11 }
12
13 var less = require('less');
14 var args = process.argv.slice(1);
15 var options = {
16     compress: false,
17     optimization: 1,
18     silent: false,
19     paths: [],
20     color: true
21 };
22
23 args = args.filter(function (arg) {
24     var match;
25
26     if (match = arg.match(/^-I(.+)$/)) {
27         options.paths.push(match[1]);
28         return false;
29     }
30
31     if (match = arg.match(/^--?([a-z][0-9a-z-]*)(?:=([^\s]+))?$/i)) { arg = match[1] }
32     else { return arg }
33
34     switch (arg) {
35         case 'v':
36         case 'version':
37             sys.puts("lessc " + less.version.join('.') + " (LESS Compiler) [JavaScript]");
38             process.exit(0);
39         case 'verbose':
40             options.verbose = true;
41             break;
42         case 's':
43         case 'silent':
44             options.silent = true;
45             break;
46         case 'h':
47         case 'help':
48             sys.puts("usage: lessc source [destination]");
49             process.exit(0);
50         case 'x':
51         case 'compress':
52             options.compress = true;
53             break;
54         case 'no-color':
55             options.color = false;
56             break;
57         case 'include-path':
58             options.paths = match[2].split(':')
59                 .map(function(p) {
60                     if (p && p[0] == '/') {
61                         return path.join(path.dirname(input), p);
62                     } else if (p) {
63                         return path.join(process.cwd(), p);
64                     }
65                 });
66             break;
67         case 'O0': options.optimization = 0; break;
68         case 'O1': options.optimization = 1; break;
69         case 'O2': options.optimization = 2; break;
70     }
71 });
72
73 var input = args[1];
74 if (input && input[0] != '/' && input != '-') {
75     input = path.join(process.cwd(), input);
76 }
77 var output = args[2];
78 if (output && output[0] != '/') {
79     output = path.join(process.cwd(), output);
80 }
81
82 var css, fd, tree;
83
84 if (! input) {
85     sys.puts("lessc: no input files");
86     process.exit(1);
87 }
88
89 var parseLessFile = function (e, data) {
90     if (e) {
91         sys.puts("lessc: " + e.message);
92         process.exit(1);
93     }
94
95     new(less.Parser)({
96         paths: [path.dirname(input)].concat(options.paths),
97         optimization: options.optimization,
98         filename: input
99     }).parse(data, function (err, tree) {
100         if (err) {
101             less.writeError(err, options);
102             process.exit(1);
103         } else {
104             try {
105                 css = tree.toCSS({ compress: options.compress });
106                 if (output) {
107                     fd = fs.openSync(output, "w");
108                     fs.writeSync(fd, css, 0, "utf8");
109                 } else {
110                     sys.print(css);
111                 }
112             } catch (e) {
113                 less.writeError(e, options);
114                 process.exit(2);
115             }
116         }
117     });
118 };
119
120 if (input != '-') {
121     fs.readFile(input, 'utf-8', parseLessFile);
122 } else {
123     process.stdin.resume();
124     process.stdin.setEncoding('utf8');
125
126     var buffer = '';
127     process.stdin.on('data', function(data) {
128         buffer += data;
129     });
130
131     process.stdin.on('end', function() {
132         parseLessFile(false, buffer);
133     });
134 }