2 * node-sass: scripts/build.js
5 var fs = require('fs'),
6 mkdir = require('mkdirp'),
7 path = require('path'),
8 spawn = require('cross-spawn'),
9 sass = require('../lib/extensions');
14 * @param {Object} options
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
27 mkdir(path.dirname(install), function(err) {
28 if (err && err.code !== 'EEXIST') {
29 console.error(err.message);
33 fs.stat(target, function(err) {
35 console.error('Build succeeded but target not found');
39 fs.rename(target, install, function(err) {
41 console.error(err.message);
45 console.log('Installed to', install);
54 * @param {Object} options
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);
64 console.log('Building:', [process.execPath].concat(args).join(' '));
66 var proc = spawn(process.execPath, args, {
70 proc.on('exit', function(errorCode) {
76 if (errorCode === 127 ) {
77 console.error('node-gyp not found!');
79 console.error('Build failed with error code:', errorCode);
93 function parseArgs(args) {
96 platform: process.platform
99 options.args = args.filter(function(arg) {
100 if (arg === '-f' || arg === '--force') {
101 options.force = true;
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;
118 * Test for pre-built library
120 * @param {Object} options
124 function testBinary(options) {
125 if (options.force || process.env.SASS_FORCE_BUILD) {
126 return build(options);
129 if (!sass.hasBinary(sass.getBinaryPath())) {
130 return build(options);
133 console.log('Binary found at', sass.getBinaryPath());
134 console.log('Testing binary');
137 require('../').renderSync({
141 console.log('Binary is fine');
143 console.log('Binary has a problem:', e);
144 console.log('Building the binary locally');
146 return build(options);
151 * Apply arguments and run
154 testBinary(parseArgs(process.argv.slice(2)));