Initial commit
[yaffs-website] / node_modules / node-sass / lib / render.js
1 /*!
2  * node-sass: lib/render.js
3  */
4
5 var chalk = require('chalk'),
6   fs = require('fs'),
7   mkdirp = require('mkdirp'),
8   path = require('path'),
9   sass = require('./');
10
11 /**
12  * Render
13  *
14  * @param {Object} options
15  * @param {Object} emitter
16  * @api public
17  */
18
19 module.exports = function(options, emitter) {
20   var renderOptions = {
21     includePaths: options.includePath,
22     omitSourceMapUrl: options.omitSourceMapUrl,
23     indentedSyntax: options.indentedSyntax,
24     outFile: options.dest,
25     outputStyle: options.outputStyle,
26     precision: options.precision,
27     sourceComments: options.sourceComments,
28     sourceMapEmbed: options.sourceMapEmbed,
29     sourceMapContents: options.sourceMapContents,
30     sourceMap: options.sourceMap,
31     sourceMapRoot: options.sourceMapRoot,
32     importer: options.importer,
33     functions: options.functions,
34     indentWidth: options.indentWidth,
35     indentType: options.indentType,
36     linefeed: options.linefeed
37   };
38
39   if (options.data) {
40     renderOptions.data = options.data;
41   } else if (options.src) {
42     renderOptions.file = options.src;
43   }
44
45   var sourceMap = options.sourceMap;
46   var destination = options.dest;
47   var stdin = options.stdin;
48
49   var success = function(result) {
50     var todo = 1;
51     var done = function() {
52       if (--todo <= 0) {
53         emitter.emit('done');
54       }
55     };
56
57     if (!destination || stdin) {
58       emitter.emit('log', result.css.toString());
59
60       if (sourceMap && !options.sourceMapEmbed) {
61         emitter.emit('log', result.map.toString());
62       }
63
64       return done();
65     }
66
67     emitter.emit('warn', chalk.green('Rendering Complete, saving .css file...'));
68
69     mkdirp(path.dirname(destination), function(err) {
70       if (err) {
71         return emitter.emit('error', chalk.red(err));
72       }
73
74       fs.writeFile(destination, result.css.toString(), function(err) {
75         if (err) {
76           return emitter.emit('error', chalk.red(err));
77         }
78
79         emitter.emit('warn', chalk.green('Wrote CSS to ' + destination));
80         emitter.emit('write', err, destination, result.css.toString());
81         done();
82       });
83     });
84
85     if (sourceMap) {
86       todo++;
87
88       mkdirp(path.dirname(sourceMap), function(err) {
89         if (err) {
90           return emitter.emit('error', chalk.red(err));
91         }
92         fs.writeFile(sourceMap, result.map, function(err) {
93           if (err) {
94             return emitter.emit('error', chalk.red('Error' + err));
95           }
96
97           emitter.emit('warn', chalk.green('Wrote Source Map to ' + sourceMap));
98           emitter.emit('write-source-map', err, sourceMap, result.map);
99           done();
100         });
101       });
102     }
103
104     emitter.emit('render', result.css.toString());
105   };
106
107   var error = function(error) {
108     emitter.emit('error', chalk.red(JSON.stringify(error, null, 2)));
109   };
110
111   var renderCallback = function(err, result) {
112     if (err) {
113       error(err);
114     }
115     else {
116       success(result);
117     }
118   };
119
120   sass.render(renderOptions, renderCallback);
121 };