1 | const hook = require('node-hook')
|
2 | const instrumentSource = require('./instrument-source')
|
3 | const updateSourceFile = require('./update')
|
4 | const fs = require('fs')
|
5 | const debug = require('debug')('comment-value')
|
6 |
|
7 | const instrumentedFiles = []
|
8 |
|
9 | function instrumentLoadedFiles (source, filename) {
|
10 | debug('instrumenting file %s', filename)
|
11 |
|
12 | const instrumented = instrumentSource(source, filename)
|
13 | if (instrumented !== source) {
|
14 | instrumentedFiles.push(filename)
|
15 | }
|
16 |
|
17 | return instrumented
|
18 | }
|
19 | hook.hook('.js', instrumentLoadedFiles)
|
20 |
|
21 | process.on('exit', function writeResults () {
|
22 | const outputFilename = './results.json'
|
23 | if (global.__instrumenter) {
|
24 | const text = JSON.stringify(global.__instrumenter, null, 2)
|
25 | fs.writeFileSync(outputFilename, text + '\n', 'utf8')
|
26 | debug('saved values to file %s', outputFilename)
|
27 | if (instrumentedFiles.length) {
|
28 | debug('%d instrumented file(s) to update', instrumentedFiles.length)
|
29 | instrumentedFiles.forEach(updateSourceFile)
|
30 | }
|
31 | }
|
32 | })
|