metrics-launch.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict'
  2. /* eslint-disable camelcase */
  3. module.exports = launchSendMetrics
  4. var fs = require('graceful-fs')
  5. var child_process = require('child_process')
  6. if (require.main === module) main()
  7. function launchSendMetrics () {
  8. var path = require('path')
  9. var npm = require('../npm.js')
  10. try {
  11. if (!npm.config.get('send-metrics')) return
  12. var cliMetrics = path.join(npm.config.get('cache'), 'anonymous-cli-metrics.json')
  13. var targetRegistry = npm.config.get('metrics-registry')
  14. fs.statSync(cliMetrics)
  15. return runInBackground(__filename, [cliMetrics, targetRegistry])
  16. } catch (ex) {
  17. // if the metrics file doesn't exist, don't run
  18. }
  19. }
  20. function runInBackground (js, args, opts) {
  21. if (!args) args = []
  22. args.unshift(js)
  23. if (!opts) opts = {}
  24. opts.stdio = 'ignore'
  25. opts.detached = true
  26. var child = child_process.spawn(process.execPath, args, opts)
  27. child.unref()
  28. return child
  29. }
  30. function main () {
  31. var sendMetrics = require('./metrics.js').send
  32. var metricsFile = process.argv[2]
  33. var metricsRegistry = process.argv[3]
  34. sendMetrics(metricsFile, metricsRegistry)
  35. }