UNPKG

1.53 kBJavaScriptView Raw
1const fs = require('fs')
2const path = require('path')
3const la = require('lazy-ass')
4const is = require('check-more-types')
5const debug = require('debug')('comment-value')
6
7function 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 // console.log('updating comment')
24 // console.log(c)
25 // line starts with 1
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 // console.log('updating line', line, 'with value', c.value)
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 // console.log('updated source')
38 // console.log(updatedSource)
39 fs.writeFileSync(filename, updatedSource, 'utf8')
40}
41
42module.exports = updateFile
43
44if (!module.exports) {
45 const sourceFilename = path.join(process.cwd(), process.argv[2])
46 updateFile(sourceFilename)
47}