@@ -4,12 +4,13 @@ exports.stop = stopMetrics
4
4
exports . save = saveMetrics
5
5
exports . send = sendMetrics
6
6
7
- var fs = require ( 'fs' )
8
- var path = require ( 'path' )
9
- var npm = require ( '../npm.js' )
10
- var uuid = require ( 'uuid' )
7
+ const fs = require ( 'fs' )
8
+ const path = require ( 'path' )
9
+ const npm = require ( '../npm.js' )
10
+ const regFetch = require ( 'npm-registry-fetch' )
11
+ const uuid = require ( 'uuid' )
11
12
12
- var inMetrics = false
13
+ let inMetrics = false
13
14
14
15
function startMetrics ( ) {
15
16
if ( inMetrics ) return
@@ -59,15 +60,18 @@ function saveMetrics (itWorked) {
59
60
function sendMetrics ( metricsFile , metricsRegistry ) {
60
61
inMetrics = true
61
62
var cliMetrics = JSON . parse ( fs . readFileSync ( metricsFile ) )
62
- npm . load ( { } , function ( err ) {
63
- if ( err ) return
64
- npm . registry . config . retry . retries = 0
65
- npm . registry . sendAnonymousCLIMetrics ( metricsRegistry , cliMetrics , function ( err ) {
66
- if ( err ) {
67
- fs . writeFileSync ( path . join ( path . dirname ( metricsFile ) , 'last-send-metrics-error.txt' ) , err . stack )
68
- } else {
69
- fs . unlinkSync ( metricsFile )
70
- }
71
- } )
63
+ regFetch (
64
+ `/-/npm/anon-metrics/v1/${ encodeURIComponent ( cliMetrics . metricId ) } ` ,
65
+ // NOTE: skip npmConfig() to prevent auth
66
+ {
67
+ registry : metricsRegistry ,
68
+ method : 'PUT' ,
69
+ body : cliMetrics . metrics ,
70
+ retry : false
71
+ }
72
+ ) . then ( ( ) => {
73
+ fs . unlinkSync ( metricsFile )
74
+ } , err => {
75
+ fs . writeFileSync ( path . join ( path . dirname ( metricsFile ) , 'last-send-metrics-error.txt' ) , err . stack )
72
76
} )
73
77
}
0 commit comments