Initial commit
[yaffs-website] / node_modules / node-sass / scripts / build.js
1 /*!
2  * node-sass: scripts/build.js
3  */
4
5 var fs = require('fs'),
6   mkdir = require('mkdirp'),
7   path = require('path'),
8   spawn = require('cross-spawn'),
9   sass = require('../lib/extensions');
10
11 /**
12  * After build
13  *
14  * @param {Object} options
15  * @api private
16  */
17
18 function afterBuild(options) {
19   var install = sass.getBinaryPath();
20   var target = path.join(__dirname, '..', 'build',
21     options.debug ? 'Debug' :
22         process.config.target_defaults
23             ?  process.config.target_defaults.default_configuration
24             : 'Release',
25     'binding.node');
26
27   mkdir(path.dirname(install), function(err) {
28     if (err && err.code !== 'EEXIST') {
29       console.error(err.message);
30       return;
31     }
32
33     fs.stat(target, function(err) {
34       if (err) {
35         console.error('Build succeeded but target not found');
36         return;
37       }
38
39       fs.rename(target, install, function(err) {
40         if (err) {
41           console.error(err.message);
42           return;
43         }
44
45         console.log('Installed to', install);
46       });
47     });
48   });
49 }
50
51 /**
52  * Build
53  *
54  * @param {Object} options
55  * @api private
56  */
57
58 function build(options) {
59   var args = [require.resolve(path.join('node-gyp', 'bin', 'node-gyp.js')), 'rebuild', '--verbose'].concat(
60     ['libsass_ext', 'libsass_cflags', 'libsass_ldflags', 'libsass_library'].map(function(subject) {
61       return ['--', subject, '=', process.env[subject.toUpperCase()] || ''].join('');
62     })).concat(options.args);
63
64   console.log('Building:', [process.execPath].concat(args).join(' '));
65
66   var proc = spawn(process.execPath, args, {
67     stdio: [0, 1, 2]
68   });
69
70   proc.on('exit', function(errorCode) {
71     if (!errorCode) {
72       afterBuild(options);
73       return;
74     }
75
76     if (errorCode === 127 ) {
77       console.error('node-gyp not found!');
78     } else {
79       console.error('Build failed with error code:', errorCode);
80     }
81
82     process.exit(1);
83   });
84 }
85
86 /**
87  * Parse arguments
88  *
89  * @param {Array} args
90  * @api private
91  */
92
93 function parseArgs(args) {
94   var options = {
95     arch: process.arch,
96     platform: process.platform
97   };
98
99   options.args = args.filter(function(arg) {
100     if (arg === '-f' || arg === '--force') {
101       options.force = true;
102       return false;
103     } else if (arg.substring(0, 13) === '--target_arch') {
104       options.arch = arg.substring(14);
105     } else if (arg === '-d' || arg === '--debug') {
106       options.debug = true;
107     } else if (arg.substring(0, 13) === '--libsass_ext' && arg.substring(14) !== 'no') {
108       options.libsassExt = true;
109     }
110
111     return true;
112   });
113
114   return options;
115 }
116
117 /**
118  * Test for pre-built library
119  *
120  * @param {Object} options
121  * @api private
122  */
123
124 function testBinary(options) {
125   if (options.force || process.env.SASS_FORCE_BUILD) {
126     return build(options);
127   }
128
129   if (!sass.hasBinary(sass.getBinaryPath())) {
130     return build(options);
131   }
132
133   console.log('Binary found at', sass.getBinaryPath());
134   console.log('Testing binary');
135
136   try {
137     require('../').renderSync({
138       data: 's { a: ss }'
139     });
140
141     console.log('Binary is fine');
142   } catch (e) {
143     console.log('Binary has a problem:', e);
144     console.log('Building the binary locally');
145
146     return build(options);
147   }
148 }
149
150 /**
151  * Apply arguments and run
152  */
153
154 testBinary(parseArgs(process.argv.slice(2)));