diff --git a/build.js b/build.js index e789bb7..b0970f1 100644 --- a/build.js +++ b/build.js @@ -11,46 +11,8 @@ var step = require('step'), fs = require('fs'), - path = require('path'); - -function mkdirP ( p, mode, made ) { - if (mode === undefined) { - mode = 0777 & (~process.umask()); - } - if (!made) made = null; - - if (typeof mode === 'string') mode = parseInt(mode, 8); - p = path.resolve(p); - - try { - fs.mkdirSync(p, mode); - made = made || p; - } - catch (err0) { - switch (err0.code) { - case 'ENOENT' : - made = mkdirP(path.dirname(p), mode, made); - mkdirP(p, mode, made); - break; - - // In the case of any other error, just see if there's a dir - // there already. If so, then hooray! If not, then something - // is borked. - default: - var stat; - try { - stat = fs.statSync(p); - } - catch (err1) { - throw err0; - } - if (!stat.isDirectory()) throw err0; - break; - } - } - - return made; -} + path = require('path'), + util = require('./util.js'); // Try to download binding.node from github. function download() { @@ -116,9 +78,7 @@ function download() { } // start to download. - var options = url.parse( downloadUrl + modPath ), - dest = './build/Release/binding.node', - client; + var dest = './build/Release/binding.node'; if ( fs.existsSync( dest ) ) { console.log( 'The binding.node file exist, skip download.' ); @@ -126,41 +86,11 @@ function download() { return; } - console.log('Downloading', options.href ); - client = https.get( options, function( res ) { - var count = 0, - notifiedCount = 0, - outFile; - - if ( res.statusCode === 200 ) { - mkdirP( path.dirname( dest ) ); - outFile = fs.openSync( dest, 'w' ); - - res.on('data', function( data ) { - fs.writeSync(outFile, data, 0, data.length, null); - count += data.length; + util.download({ + remote: downloadUrl + modPath, + dest: './build/Release/binding.node' + }, done); - if ( (count - notifiedCount) > 1024 * 1024 ) { - console.log('Received ' + Math.floor( count / 1024 ) + 'K...'); - notifiedCount = count; - } - }); - - res.addListener('end', function() { - console.log('Received ' + Math.floor(count / 1024) + 'K total.'); - fs.closeSync( outFile ); - done( false ); - }); - - } else { - client.abort() - console.error('Error requesting archive'); - done( true ); - } - }).on('error', function(e) { - console.error( e.message ); - done( true, e ); - }); } else { done( true ); } diff --git a/package.json b/package.json index f7d33f2..52b61cb 100644 --- a/package.json +++ b/package.json @@ -26,5 +26,5 @@ "dependencies": { "step": "0.0.5" }, - "bindingsCDN": "https://raw.githubusercontent.com/fis-dev/fis-sass/master/bindings/" + "bindingsCDN": "http://git.oschina.net/2betop/static/raw/master/fis-sass/bindings/" } diff --git a/util.js b/util.js new file mode 100644 index 0000000..f0d2014 --- /dev/null +++ b/util.js @@ -0,0 +1,95 @@ +var _ = module.exports; +var path = require('path'); +var fs = require('fs'); + +_.mkdirp = function mkdirP ( p, mode, made ) { + if (mode === undefined) { + mode = 0777 & (~process.umask()); + } + if (!made) made = null; + + if (typeof mode === 'string') mode = parseInt(mode, 8); + p = path.resolve(p); + + try { + fs.mkdirSync(p, mode); + made = made || p; + } + catch (err0) { + switch (err0.code) { + case 'ENOENT' : + made = mkdirP(path.dirname(p), mode, made); + mkdirP(p, mode, made); + break; + + // In the case of any other error, just see if there's a dir + // there already. If so, then hooray! If not, then something + // is borked. + default: + var stat; + try { + stat = fs.statSync(p); + } + catch (err1) { + throw err0; + } + if (!stat.isDirectory()) throw err0; + break; + } + } + + return made; +} + +_.download = function(opt, done) { + var remote = opt.remote; + var dest = opt.dest; + var url = require('url'); + var fs = require('fs'); + var options = url.parse(remote); + var notifiedSize = opt.notifiedSize || 512 * 1024; + var http = options.protocol === 'https:' ? require('https') : require('http') + var client; + + process.stdout.write('Downloading ' + remote + ' ...\n'); + + client = http.get( options, function( res ) { + var count = 0, + notifiedCount = 0, + outFile; + + if ( res.statusCode === 200 ) { + _.mkdirp(path.dirname(dest)); + + outFile = fs.openSync( dest, 'w' ); + + res.on('data', function( data ) { + fs.writeSync(outFile, data, 0, data.length, null); + count += data.length; + + if ( (count - notifiedCount) > notifiedSize ) { + process.stdout.write('Received ' + Math.floor( count / 1024 ) + 'K...\n'); + notifiedCount = count; + } + }); + + res.addListener('end', function() { + process.stdout.write('Received ' + Math.floor(count / 1024) + 'K total.\n'); + fs.closeSync( outFile ); + done( false ); + }); + + } else if (res.statusCode === 302 && res.headers.location) { + client.abort(); + opt.remote = res.headers.location; + + process.stdout.write('Redirct to ' + opt.remote + '\n'); + _.download(opt, done); + } else { + client.abort(); + done('Error requesting archive') + } + }).on('error', function(e) { + done(e.message || 'unkown error'); + }); +} \ No newline at end of file