bin.js 2.2 KB

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