bin.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env node
  2. var path = require('path')
  3. var fs = require('fs')
  4. var whichPmRuns = require('which-pm-runs')
  5. var napi = require('napi-build-utils')
  6. var pkg = require(path.resolve('package.json'))
  7. var rc = require('./rc')(pkg)
  8. var log = require('./log')(rc, process.env)
  9. var download = require('./download')
  10. var asset = require('./asset')
  11. var util = require('./util')
  12. var prebuildClientVersion = require('./package.json').version
  13. if (rc.version) {
  14. console.log(prebuildClientVersion)
  15. process.exit(0)
  16. }
  17. if (rc.path) process.chdir(rc.path)
  18. if (rc.runtime === 'electron' && rc.target[0] === '4' && rc.abi === '64') {
  19. log.error(`Electron version ${rc.target} found - skipping prebuild-install work due to known ABI issue`)
  20. log.error('More information about this issue can be found at https://github.com/lgeiger/node-abi/issues/54')
  21. process.exit(1)
  22. }
  23. if (!fs.existsSync('package.json')) {
  24. log.error('setup', 'No package.json found. Aborting...')
  25. process.exit(1)
  26. }
  27. if (rc.help) {
  28. console.error(fs.readFileSync(path.join(__dirname, 'help.txt'), 'utf-8'))
  29. process.exit(0)
  30. }
  31. log.info('begin', 'Prebuild-install version', prebuildClientVersion)
  32. var opts = Object.assign({}, rc, { pkg: pkg, log: log })
  33. if (napi.isNapiRuntime(rc.runtime)) napi.logUnsupportedVersion(rc.target, log)
  34. var pm = whichPmRuns()
  35. var isNpm = !pm || pm.name === 'npm'
  36. if (!isNpm && /node_modules/.test(process.cwd())) {
  37. // From yarn repository
  38. } else if (opts.force) {
  39. log.warn('install', 'prebuilt binaries enforced with --force!')
  40. log.warn('install', 'prebuilt binaries may be out of date!')
  41. } else if (!(typeof pkg._from === 'string')) {
  42. log.info('install', 'installing standalone, skipping download.')
  43. process.exit(1)
  44. } else if (pkg._from.length > 4 && pkg._from.substr(0, 4) === 'git+') {
  45. log.info('install', 'installing from git repository, skipping download.')
  46. process.exit(1)
  47. } else if (opts.compile === true || opts.prebuild === false) {
  48. log.info('install', '--build-from-source specified, not attempting download.')
  49. process.exit(1)
  50. }
  51. var startDownload = function (downloadUrl) {
  52. download(downloadUrl, opts, function (err) {
  53. if (err) {
  54. log.warn('install', err.message)
  55. return process.exit(1)
  56. }
  57. log.info('install', 'Successfully installed prebuilt binary!')
  58. })
  59. }
  60. if (opts.token) {
  61. asset(opts, function (err, assetId) {
  62. if (err) {
  63. log.warn('install', err.message)
  64. return process.exit(1)
  65. }
  66. startDownload(util.getAssetUrl(opts, assetId))
  67. })
  68. } else {
  69. startDownload(util.getDownloadUrl(opts))
  70. }