build.js
3.27 KB
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
/*!
* node-sass: scripts/build.js
*/
var fs = require('fs'),
mkdir = require('mkdirp'),
path = require('path'),
spawn = require('cross-spawn'),
sass = require('../lib/extensions');
/**
* After build
*
* @param {Object} options
* @api private
*/
function afterBuild(options) {
var install = sass.getBinaryPath();
var target = path.join(__dirname, '..', 'build',
options.debug ? 'Debug' :
process.config.target_defaults
? process.config.target_defaults.default_configuration
: 'Release',
'binding.node');
mkdir(path.dirname(install), function(err) {
if (err && err.code !== 'EEXIST') {
console.error(err.message);
return;
}
fs.stat(target, function(err) {
if (err) {
console.error('Build succeeded but target not found');
return;
}
fs.rename(target, install, function(err) {
if (err) {
console.error(err.message);
return;
}
console.log('Installed to', install);
});
});
});
}
/**
* Build
*
* @param {Object} options
* @api private
*/
function build(options) {
var args = [require.resolve(path.join('node-gyp', 'bin', 'node-gyp.js')), 'rebuild', '--verbose'].concat(
['libsass_ext', 'libsass_cflags', 'libsass_ldflags', 'libsass_library'].map(function(subject) {
return ['--', subject, '=', process.env[subject.toUpperCase()] || ''].join('');
})).concat(options.args);
console.log('Building:', [process.execPath].concat(args).join(' '));
var proc = spawn(process.execPath, args, {
stdio: [0, 1, 2]
});
proc.on('exit', function(errorCode) {
if (!errorCode) {
afterBuild(options);
return;
}
if (errorCode === 127 ) {
console.error('node-gyp not found!');
} else {
console.error('Build failed with error code:', errorCode);
}
process.exit(1);
});
}
/**
* Parse arguments
*
* @param {Array} args
* @api private
*/
function parseArgs(args) {
var options = {
arch: process.arch,
platform: process.platform,
force: process.env.npm_config_force === 'true',
};
options.args = args.filter(function(arg) {
if (arg === '-f' || arg === '--force') {
options.force = true;
return false;
} else if (arg.substring(0, 13) === '--target_arch') {
options.arch = arg.substring(14);
} else if (arg === '-d' || arg === '--debug') {
options.debug = true;
} else if (arg.substring(0, 13) === '--libsass_ext' && arg.substring(14) !== 'no') {
options.libsassExt = true;
}
return true;
});
return options;
}
/**
* Test for pre-built library
*
* @param {Object} options
* @api private
*/
function testBinary(options) {
if (options.force || process.env.SASS_FORCE_BUILD) {
return build(options);
}
if (!sass.hasBinary(sass.getBinaryPath())) {
return build(options);
}
console.log('Binary found at', sass.getBinaryPath());
console.log('Testing binary');
try {
require('../').renderSync({
data: 's { a: ss }'
});
console.log('Binary is fine');
} catch (e) {
console.log('Binary has a problem:', e);
console.log('Building the binary locally');
return build(options);
}
}
/**
* Apply arguments and run
*/
testBinary(parseArgs(process.argv.slice(2)));