Skip to content

Commit 694dba9

Browse files
committed
fix: dont expose as many public properties of timers
1 parent 3cbc258 commit 694dba9

File tree

6 files changed

+53
-105
lines changed

6 files changed

+53
-105
lines changed

lib/cli/exit-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ process.on('exit', code => {
4545
// write the timing file now, this might do nothing based on the configs set.
4646
// we need to call it here in case it errors so we dont tell the user
4747
// about a timing file that doesn't exist
48-
npm.writeTimingFile()
48+
npm.finish()
4949

5050
const logsDir = npm.logsDir
5151
const logFiles = npm.logFiles

lib/npm.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Npm {
3838

3939
#display = null
4040
#logFile = new LogFile()
41-
#timers = new Timers({ start: 'npm' })
41+
#timers = new Timers()
4242

4343
// all these options are only used by tests in order to make testing more
4444
// closely resemble real world usage. for now, npm has no programmatic API so
@@ -117,8 +117,8 @@ class Npm {
117117
this.#logFile.off()
118118
}
119119

120-
writeTimingFile () {
121-
this.#timers.writeFile({
120+
finish () {
121+
this.#timers.finish({
122122
id: this.#runId,
123123
command: this.#argvClean,
124124
logfiles: this.logFiles,
@@ -209,6 +209,7 @@ class Npm {
209209

210210
this.#timers.load({
211211
path: this.config.get('timing') ? this.logPath : null,
212+
timing: this.config.get('timing'),
212213
})
213214

214215
const configScope = this.config.get('scope')

lib/utils/timers.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const { log, time } = require('proc-log')
55
const INITIAL_TIMER = 'npm'
66

77
class Timers extends EE {
8-
file = null
8+
#file
9+
#timing
910

1011
#unfinished = new Map()
1112
#finished = {}
@@ -17,14 +18,6 @@ class Timers extends EE {
1718
this.started = this.#unfinished.get(INITIAL_TIMER)
1819
}
1920

20-
get unfinished () {
21-
return this.#unfinished
22-
}
23-
24-
get finished () {
25-
return this.#finished
26-
}
27-
2821
on () {
2922
process.on('time', this.#timeHandler)
3023
}
@@ -33,36 +26,46 @@ class Timers extends EE {
3326
process.off('time', this.#timeHandler)
3427
}
3528

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`
4032
}
4133

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
4443
return
4544
}
4645

4746
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}`)
6049
} catch (e) {
61-
this.file = null
6250
log.warn('timing', `could not write timing file: ${e}`)
6351
}
6452
}
6553

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+
6669
#timeHandler = (level, name) => {
6770
const now = Date.now()
6871
switch (level) {
@@ -76,7 +79,7 @@ class Timers extends EE {
7679
this.#unfinished.delete(name)
7780
log.timing(name, `Completed in ${ms}ms`)
7881
} 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}`)
8083
}
8184
}
8285
}

test/fixtures/mock-npm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ const setupMockNpm = async (t, {
295295
.join('\n')
296296
},
297297
timingFile: async () => {
298-
const data = await fs.readFile(npm.timingFile, 'utf8')
298+
const data = await fs.readFile(npm.logPath + 'timing.json', 'utf8')
299299
return JSON.parse(data)
300300
},
301301
}

test/lib/npm.js

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -380,48 +380,14 @@ t.test('cache dir', async t => {
380380
})
381381

382382
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-
415383
t.test('writes timings file', async t => {
416-
const { npm, cache, timingFile } = await loadMockNpm(t, {
384+
const { npm, timingFile } = await loadMockNpm(t, {
417385
config: { timing: true },
418386
})
419387
time.start('foo')
420388
time.end('foo')
421389
time.start('bar')
422-
npm.writeTimingFile()
423-
t.match(npm.timingFile, cache)
424-
t.match(npm.timingFile, /-timing.json$/)
390+
npm.finish()
425391
const timings = await timingFile()
426392
t.match(timings, {
427393
metadata: {
@@ -431,7 +397,6 @@ t.test('timings', async t => {
431397
},
432398
unfinishedTimers: {
433399
bar: [Number, Number],
434-
npm: [Number, Number],
435400
},
436401
timers: {
437402
foo: Number,
@@ -444,7 +409,7 @@ t.test('timings', async t => {
444409
const { npm, timingFile } = await loadMockNpm(t, {
445410
config: { timing: false },
446411
})
447-
npm.writeTimingFile()
412+
npm.finish()
448413
await t.rejects(() => timingFile())
449414
})
450415

test/lib/utils/timers.js

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fs = require('graceful-fs')
44
const { log, time } = require('proc-log')
55
const tmock = require('../../fixtures/tmock')
66

7-
const mockTimers = (t, options) => {
7+
const mockTimers = (t) => {
88
const logs = log.LEVELS.reduce((acc, l) => {
99
acc[l] = []
1010
return acc
@@ -14,40 +14,20 @@ const mockTimers = (t, options) => {
1414
}
1515
process.on('log', logHandler)
1616
const Timers = tmock(t, '{LIB}/utils/timers')
17-
const timers = new Timers(options)
17+
const timers = new Timers()
1818
t.teardown(() => {
1919
timers.off()
2020
process.off('log', logHandler)
2121
})
2222
return { timers, logs }
2323
}
2424

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-
4325
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)
4727
time.start('foo')
4828
time.start('bar')
4929
time.end('bar')
50-
timers.off(listener)
30+
timers.off()
5131
time.end('foo')
5232
t.equal(logs.timing.length, 1)
5333
t.match(logs.timing[0], /^bar Completed in [0-9]ms/)
@@ -64,14 +44,15 @@ t.test('writes file', async (t) => {
6444
const dir = t.testdir()
6545
time.start('foo')
6646
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' })
6950
const data = JSON.parse(fs.readFileSync(resolve(dir, 'TIMING_FILE-timing.json')))
7051
t.match(data, {
7152
metadata: { some: 'data' },
72-
timers: { foo: Number },
53+
timers: { foo: Number, npm: Number },
7354
unfinishedTimers: {
74-
npm: [Number, Number],
55+
ohno: [Number, Number],
7556
},
7657
})
7758
})
@@ -80,20 +61,18 @@ t.test('fails to write file', async (t) => {
8061
const { logs, timers } = mockTimers(t)
8162
const dir = t.testdir()
8263

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()
8566

8667
t.match(logs.warn, ['timing could not write timing file:'])
87-
t.equal(timers.file, null)
8868
})
8969

9070
t.test('no dir and no file', async (t) => {
9171
const { logs, timers } = mockTimers(t)
9272

9373
timers.load()
94-
timers.writeFile()
74+
timers.finish()
9575

9676
t.strictSame(logs.warn, [])
9777
t.strictSame(logs.silly, [])
98-
t.equal(timers.file, null)
9978
})

0 commit comments

Comments
 (0)