bin.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env node
  2. var proc = require('child_process')
  3. var os = require('os')
  4. var path = require('path')
  5. if (!buildFromSource()) {
  6. proc.exec('node-gyp-build-test', function (err) {
  7. if (err) preinstall()
  8. })
  9. } else {
  10. preinstall()
  11. }
  12. function buildFromSource () {
  13. if (!process.env.npm_config_argv) return false
  14. try {
  15. return JSON.parse(process.env.npm_config_argv).cooked.indexOf('--build-from-source') !== -1
  16. } catch (_) {
  17. return false
  18. }
  19. }
  20. function build () {
  21. var args = [ os.platform() === 'win32' ? 'node-gyp.cmd' : 'node-gyp', 'rebuild' ]
  22. try {
  23. args = [
  24. process.execPath,
  25. path.join(require.resolve('node-gyp/package.json'), '..', require('node-gyp/package.json').bin['node-gyp']),
  26. 'rebuild'
  27. ]
  28. } catch (_) {}
  29. proc.spawn(args[0], args.slice(1), {stdio: 'inherit'}).on('exit', function (code) {
  30. if (code || !process.argv[3]) process.exit(code)
  31. exec(process.argv[3]).on('exit', function (code) {
  32. process.exit(code)
  33. })
  34. })
  35. }
  36. function preinstall () {
  37. if (!process.argv[2]) return build()
  38. exec(process.argv[2]).on('exit', function (code) {
  39. if (code) process.exit(code)
  40. build()
  41. })
  42. }
  43. function exec (cmd) {
  44. if (process.platform !== 'win32') {
  45. var shell = os.platform() === 'android' ? 'sh' : '/bin/sh'
  46. return proc.spawn(shell, ['-c', cmd], {
  47. stdio: 'inherit'
  48. })
  49. }
  50. return proc.spawn(process.env.comspec || 'cmd.exe', ['/s', '/c', '"' + cmd + '"'], {
  51. windowsVerbatimArguments: true,
  52. stdio: 'inherit'
  53. })
  54. }