1 | const fs = require('fs')
|
2 | const path = require('path')
|
3 | const la = require('lazy-ass')
|
4 | const is = require('check-more-types')
|
5 | const debug = require('debug')('comment-value')
|
6 |
|
7 | function updateFile (filename) {
|
8 | debug('updating comment values in file %s', filename)
|
9 | la(is.unemptyString(filename), 'missing filename', filename)
|
10 |
|
11 | const source = fs.readFileSync(filename, 'utf8')
|
12 | const results = require(path.join(process.cwd(), 'results.json'))
|
13 |
|
14 | const forThisFile = comment => comment.filename === filename
|
15 |
|
16 | const lines = source.split('\n')
|
17 | results.comments
|
18 | .filter(forThisFile)
|
19 | .forEach(updateComment)
|
20 |
|
21 | function updateComment (c) {
|
22 | const commentStart = '//>'
|
23 |
|
24 |
|
25 |
|
26 | la(c.from.line === c.to.line, 'line mismatch', c)
|
27 | const line = lines[c.from.line - 1]
|
28 | la(line, 'missing line', c.from.line - 1, 'for comment', c)
|
29 |
|
30 | const k = line.indexOf(commentStart)
|
31 | la(k >= 0, 'line does not have comment', k, 'for comment', c)
|
32 | const updatedLine = line.substr(0, k) + commentStart + ' ' + c.value
|
33 | lines[c.from.line - 1] = updatedLine
|
34 | }
|
35 |
|
36 | const updatedSource = lines.join('\n')
|
37 |
|
38 |
|
39 | fs.writeFileSync(filename, updatedSource, 'utf8')
|
40 | }
|
41 |
|
42 | module.exports = updateFile
|
43 |
|
44 | if (!module.exports) {
|
45 | const sourceFilename = path.join(process.cwd(), process.argv[2])
|
46 | updateFile(sourceFilename)
|
47 | }
|