+53
-105
lines changedFilter options
+53
-105
lines changed Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ process.on('exit', code => {
45
45
// write the timing file now, this might do nothing based on the configs set.
46
46
// we need to call it here in case it errors so we dont tell the user
47
47
// about a timing file that doesn't exist
48
-
npm.writeTimingFile()
48
+
npm.finish()
49
49
50
50
const logsDir = npm.logsDir
51
51
const logFiles = npm.logFiles
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ class Npm {
38
38
39
39
#display = null
40
40
#logFile = new LogFile()
41
-
#timers = new Timers({ start: 'npm' })
41
+
#timers = new Timers()
42
42
43
43
// all these options are only used by tests in order to make testing more
44
44
// closely resemble real world usage. for now, npm has no programmatic API so
@@ -117,8 +117,8 @@ class Npm {
117
117
this.#logFile.off()
118
118
}
119
119
120
-
writeTimingFile () {
121
-
this.#timers.writeFile({
120
+
finish () {
121
+
this.#timers.finish({
122
122
id: this.#runId,
123
123
command: this.#argvClean,
124
124
logfiles: this.logFiles,
@@ -209,6 +209,7 @@ class Npm {
209
209
210
210
this.#timers.load({
211
211
path: this.config.get('timing') ? this.logPath : null,
212
+
timing: this.config.get('timing'),
212
213
})
213
214
214
215
const configScope = this.config.get('scope')
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ const { log, time } = require('proc-log')
5
5
const INITIAL_TIMER = 'npm'
6
6
7
7
class Timers extends EE {
8
-
file = null
8
+
#file
9
+
#timing
9
10
10
11
#unfinished = new Map()
11
12
#finished = {}
@@ -17,14 +18,6 @@ class Timers extends EE {
17
18
this.started = this.#unfinished.get(INITIAL_TIMER)
18
19
}
19
20
20
-
get unfinished () {
21
-
return this.#unfinished
22
-
}
23
-
24
-
get finished () {
25
-
return this.#finished
26
-
}
27
-
28
21
on () {
29
22
process.on('time', this.#timeHandler)
30
23
}
@@ -33,36 +26,46 @@ class Timers extends EE {
33
26
process.off('time', this.#timeHandler)
34
27
}
35
28
36
-
load ({ path } = {}) {
37
-
if (path) {
38
-
this.file = `${path}timing.json`
39
-
}
29
+
load ({ path, timing } = {}) {
30
+
this.#timing = timing
31
+
this.#file = `${path}timing.json`
40
32
}
41
33
42
-
writeFile (metadata) {
43
-
if (!this.file) {
34
+
finish (metadata) {
35
+
time.end(INITIAL_TIMER)
36
+
37
+
for (const [name, timer] of this.#unfinished) {
38
+
log.silly('unfinished npm timer', name, timer)
39
+
}
40
+
41
+
if (!this.#timing) {
42
+
// Not in timing mode, nothing else to do here
44
43
return
45
44
}
46
45
47
46
try {
48
-
const globalStart = this.started
49
-
const globalEnd = this.#finished[INITIAL_TIMER] || Date.now()
50
-
const content = {
51
-
metadata,
52
-
timers: this.#finished,
53
-
// add any unfinished timers with their relative start/end
54
-
unfinishedTimers: [...this.#unfinished.entries()].reduce((acc, [name, start]) => {
55
-
acc[name] = [start - globalStart, globalEnd - globalStart]
56
-
return acc
57
-
}, {}),
58
-
}
59
-
fs.writeFileSync(this.file, JSON.stringify(content) + '\n')
47
+
this.#writeFile(metadata)
48
+
log.info('timing', `Timing info written to: ${this.#file}`)
60
49
} catch (e) {
61
-
this.file = null
62
50
log.warn('timing', `could not write timing file: ${e}`)
63
51
}
64
52
}
65
53
54
+
#writeFile (metadata) {
55
+
const globalStart = this.started
56
+
const globalEnd = this.#finished[INITIAL_TIMER]
57
+
const content = {
58
+
metadata,
59
+
timers: this.#finished,
60
+
// add any unfinished timers with their relative start/end
61
+
unfinishedTimers: [...this.#unfinished.entries()].reduce((acc, [name, start]) => {
62
+
acc[name] = [start - globalStart, globalEnd - globalStart]
63
+
return acc
64
+
}, {}),
65
+
}
66
+
fs.writeFileSync(this.#file, JSON.stringify(content) + '\n')
67
+
}
68
+
66
69
#timeHandler = (level, name) => {
67
70
const now = Date.now()
68
71
switch (level) {
@@ -76,7 +79,7 @@ class Timers extends EE {
76
79
this.#unfinished.delete(name)
77
80
log.timing(name, `Completed in ${ms}ms`)
78
81
} else {
79
-
log.silly('timing', "Tried to end timer that doesn't exist:", name)
82
+
log.silly('timing', `Tried to end timer that doesn't exist: ${name}`)
80
83
}
81
84
}
82
85
}
Original file line number Diff line number Diff line change
@@ -295,7 +295,7 @@ const setupMockNpm = async (t, {
295
295
.join('\n')
296
296
},
297
297
timingFile: async () => {
298
-
const data = await fs.readFile(npm.timingFile, 'utf8')
298
+
const data = await fs.readFile(npm.logPath + 'timing.json', 'utf8')
299
299
return JSON.parse(data)
300
300
},
301
301
}
Original file line number Diff line number Diff line change
@@ -380,48 +380,14 @@ t.test('cache dir', async t => {
380
380
})
381
381
382
382
t.test('timings', async t => {
383
-
t.test('gets/sets timers', async t => {
384
-
const { npm, logs } = await loadMockNpm(t, {
385
-
config: {
386
-
timing: true,
387
-
},
388
-
})
389
-
time.start('foo')
390
-
time.start('bar')
391
-
t.match(npm.unfinishedTimers.get('foo'), Number, 'foo timer is a number')
392
-
t.match(npm.unfinishedTimers.get('bar'), Number, 'foo timer is a number')
393
-
time.end('foo')
394
-
time.end('bar')
395
-
time.end('baz')
396
-
// npm timer is started by default
397
-
time.end('npm')
398
-
t.match(logs.timing.byTitle('foo'), [
399
-
/Completed in [0-9]+ms/,
400
-
])
401
-
t.match(logs.timing.byTitle('bar'), [
402
-
/Completed in [0-9]+ms/,
403
-
])
404
-
t.match(logs.timing.byTitle('npm'), [
405
-
/Completed in [0-9]+ms/,
406
-
])
407
-
t.match(logs.silly.byTitle('timing'), [
408
-
`timing Tried to end timer that doesn't exist: baz`,
409
-
])
410
-
t.notOk(npm.unfinishedTimers.has('foo'), 'foo timer is gone')
411
-
t.notOk(npm.unfinishedTimers.has('bar'), 'bar timer is gone')
412
-
t.match(npm.finishedTimers, { foo: Number, bar: Number, npm: Number })
413
-
})
414
-
415
383
t.test('writes timings file', async t => {
416
-
const { npm, cache, timingFile } = await loadMockNpm(t, {
384
+
const { npm, timingFile } = await loadMockNpm(t, {
417
385
config: { timing: true },
418
386
})
419
387
time.start('foo')
420
388
time.end('foo')
421
389
time.start('bar')
422
-
npm.writeTimingFile()
423
-
t.match(npm.timingFile, cache)
424
-
t.match(npm.timingFile, /-timing.json$/)
390
+
npm.finish()
425
391
const timings = await timingFile()
426
392
t.match(timings, {
427
393
metadata: {
@@ -431,7 +397,6 @@ t.test('timings', async t => {
431
397
},
432
398
unfinishedTimers: {
433
399
bar: [Number, Number],
434
-
npm: [Number, Number],
435
400
},
436
401
timers: {
437
402
foo: Number,
@@ -444,7 +409,7 @@ t.test('timings', async t => {
444
409
const { npm, timingFile } = await loadMockNpm(t, {
445
410
config: { timing: false },
446
411
})
447
-
npm.writeTimingFile()
412
+
npm.finish()
448
413
await t.rejects(() => timingFile())
449
414
})
450
415
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ const fs = require('graceful-fs')
4
4
const { log, time } = require('proc-log')
5
5
const tmock = require('../../fixtures/tmock')
6
6
7
-
const mockTimers = (t, options) => {
7
+
const mockTimers = (t) => {
8
8
const logs = log.LEVELS.reduce((acc, l) => {
9
9
acc[l] = []
10
10
return acc
@@ -14,40 +14,20 @@ const mockTimers = (t, options) => {
14
14
}
15
15
process.on('log', logHandler)
16
16
const Timers = tmock(t, '{LIB}/utils/timers')
17
-
const timers = new Timers(options)
17
+
const timers = new Timers()
18
18
t.teardown(() => {
19
19
timers.off()
20
20
process.off('log', logHandler)
21
21
})
22
22
return { timers, logs }
23
23
}
24
24
25
-
t.test('listens/stops on process', async (t) => {
26
-
const { timers } = mockTimers(t)
27
-
time.start('foo')
28
-
time.start('bar')
29
-
time.end('bar')
30
-
t.match(timers.unfinished, new Map([['foo', Number]]))
31
-
t.match(timers.finished, { bar: Number })
32
-
timers.off()
33
-
time.start('baz')
34
-
t.notOk(timers.unfinished.get('baz'))
35
-
})
36
-
37
-
t.test('initial timer is named npm', async (t) => {
38
-
const { timers } = mockTimers(t)
39
-
time.end('npm')
40
-
t.match(timers.finished, { npm: Number })
41
-
})
42
-
43
25
t.test('logs timing events', async (t) => {
44
-
const events = []
45
-
const listener = (...args) => events.push(args)
46
-
const { timers, logs } = mockTimers(t, { listener })
26
+
const { timers, logs } = mockTimers(t)
47
27
time.start('foo')
48
28
time.start('bar')
49
29
time.end('bar')
50
-
timers.off(listener)
30
+
timers.off()
51
31
time.end('foo')
52
32
t.equal(logs.timing.length, 1)
53
33
t.match(logs.timing[0], /^bar Completed in [0-9]ms/)
@@ -64,14 +44,15 @@ t.test('writes file', async (t) => {
64
44
const dir = t.testdir()
65
45
time.start('foo')
66
46
time.end('foo')
67
-
timers.load({ path: resolve(dir, `TIMING_FILE-`) })
68
-
timers.writeFile({ some: 'data' })
47
+
time.start('ohno')
48
+
timers.load({ timing: true, path: resolve(dir, `TIMING_FILE-`) })
49
+
timers.finish({ some: 'data' })
69
50
const data = JSON.parse(fs.readFileSync(resolve(dir, 'TIMING_FILE-timing.json')))
70
51
t.match(data, {
71
52
metadata: { some: 'data' },
72
-
timers: { foo: Number },
53
+
timers: { foo: Number, npm: Number },
73
54
unfinishedTimers: {
74
-
npm: [Number, Number],
55
+
ohno: [Number, Number],
75
56
},
76
57
})
77
58
})
@@ -80,20 +61,18 @@ t.test('fails to write file', async (t) => {
80
61
const { logs, timers } = mockTimers(t)
81
62
const dir = t.testdir()
82
63
83
-
timers.load({ path: join(dir, 'does', 'not', 'exist') })
84
-
timers.writeFile()
64
+
timers.load({ timing: true, path: join(dir, 'does', 'not', 'exist') })
65
+
timers.finish()
85
66
86
67
t.match(logs.warn, ['timing could not write timing file:'])
87
-
t.equal(timers.file, null)
88
68
})
89
69
90
70
t.test('no dir and no file', async (t) => {
91
71
const { logs, timers } = mockTimers(t)
92
72
93
73
timers.load()
94
-
timers.writeFile()
74
+
timers.finish()
95
75
96
76
t.strictSame(logs.warn, [])
97
77
t.strictSame(logs.silly, [])
98
-
t.equal(timers.file, null)
99
78
})
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4