help-search.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. const fs = require('fs')
  2. const path = require('path')
  3. const color = require('ansicolors')
  4. const { promisify } = require('util')
  5. const glob = promisify(require('glob'))
  6. const readFile = promisify(fs.readFile)
  7. const BaseCommand = require('./base-command.js')
  8. class HelpSearch extends BaseCommand {
  9. static get description () {
  10. return 'Search npm help documentation'
  11. }
  12. /* istanbul ignore next - see test/lib/load-all-commands.js */
  13. static get name () {
  14. return 'help-search'
  15. }
  16. /* istanbul ignore next - see test/lib/load-all-commands.js */
  17. static get usage () {
  18. return ['<text>']
  19. }
  20. /* istanbul ignore next - see test/lib/load-all-commands.js */
  21. static get params () {
  22. return ['long']
  23. }
  24. exec (args, cb) {
  25. this.helpSearch(args).then(() => cb()).catch(cb)
  26. }
  27. async helpSearch (args) {
  28. if (!args.length)
  29. return this.npm.output(this.usage)
  30. const docPath = path.resolve(__dirname, '..', 'docs/content')
  31. const files = await glob(`${docPath}/*/*.md`)
  32. const data = await this.readFiles(files)
  33. const results = await this.searchFiles(args, data, files)
  34. const formatted = this.formatResults(args, results)
  35. if (!formatted.trim())
  36. this.npm.output(`No matches in help for: ${args.join(' ')}\n`)
  37. else
  38. this.npm.output(formatted)
  39. }
  40. async readFiles (files) {
  41. const res = {}
  42. await Promise.all(files.map(async file => {
  43. res[file] = (await readFile(file, 'utf8'))
  44. .replace(/^---\n(.*\n)*?---\n/, '').trim()
  45. }))
  46. return res
  47. }
  48. async searchFiles (args, data, files) {
  49. const results = []
  50. for (const [file, content] of Object.entries(data)) {
  51. const lowerCase = content.toLowerCase()
  52. // skip if no matches at all
  53. if (!args.some(a => lowerCase.includes(a.toLowerCase())))
  54. continue
  55. const lines = content.split(/\n+/)
  56. // if a line has a search term, then skip it and the next line.
  57. // if the next line has a search term, then skip all 3
  58. // otherwise, set the line to null. then remove the nulls.
  59. for (let i = 0; i < lines.length; i++) {
  60. const line = lines[i]
  61. const nextLine = lines[i + 1]
  62. let match = false
  63. if (nextLine) {
  64. match = args.some(a =>
  65. nextLine.toLowerCase().includes(a.toLowerCase()))
  66. if (match) {
  67. // skip over the next line, and the line after it.
  68. i += 2
  69. continue
  70. }
  71. }
  72. match = args.some(a => line.toLowerCase().includes(a.toLowerCase()))
  73. if (match) {
  74. // skip over the next line
  75. i++
  76. continue
  77. }
  78. lines[i] = null
  79. }
  80. // now squish any string of nulls into a single null
  81. const pruned = lines.reduce((l, r) => {
  82. if (!(r === null && l[l.length - 1] === null))
  83. l.push(r)
  84. return l
  85. }, [])
  86. if (pruned[pruned.length - 1] === null)
  87. pruned.pop()
  88. if (pruned[0] === null)
  89. pruned.shift()
  90. // now count how many args were found
  91. const found = {}
  92. let totalHits = 0
  93. for (const line of pruned) {
  94. for (const arg of args) {
  95. const hit = (line || '').toLowerCase()
  96. .split(arg.toLowerCase()).length - 1
  97. if (hit > 0) {
  98. found[arg] = (found[arg] || 0) + hit
  99. totalHits += hit
  100. }
  101. }
  102. }
  103. const cmd = 'npm help ' +
  104. path.basename(file, '.md').replace(/^npm-/, '')
  105. results.push({
  106. file,
  107. cmd,
  108. lines: pruned,
  109. found: Object.keys(found),
  110. hits: found,
  111. totalHits,
  112. })
  113. }
  114. // sort results by number of results found, then by number of hits
  115. // then by number of matching lines
  116. // coverage is ignored here because the contents of results are
  117. // nondeterministic due to either glob or readFiles or Object.entries
  118. return results.sort(/* istanbul ignore next */ (a, b) =>
  119. a.found.length > b.found.length ? -1
  120. : a.found.length < b.found.length ? 1
  121. : a.totalHits > b.totalHits ? -1
  122. : a.totalHits < b.totalHits ? 1
  123. : a.lines.length > b.lines.length ? -1
  124. : a.lines.length < b.lines.length ? 1
  125. : 0).slice(0, 10)
  126. }
  127. formatResults (args, results) {
  128. const cols = Math.min(process.stdout.columns || Infinity, 80) + 1
  129. const out = results.map(res => {
  130. const out = [res.cmd]
  131. const r = Object.keys(res.hits)
  132. .map(k => `${k}:${res.hits[k]}`)
  133. .sort((a, b) => a > b ? 1 : -1)
  134. .join(' ')
  135. out.push(' '.repeat((Math.max(1, cols - out.join(' ').length - r.length - 1))))
  136. out.push(r)
  137. if (!this.npm.config.get('long'))
  138. return out.join('')
  139. out.unshift('\n\n')
  140. out.push('\n')
  141. out.push('-'.repeat(cols - 1) + '\n')
  142. res.lines.forEach((line, i) => {
  143. if (line === null || i > 3)
  144. return
  145. if (!this.npm.color) {
  146. out.push(line + '\n')
  147. return
  148. }
  149. const hilitLine = []
  150. for (const arg of args) {
  151. const finder = line.toLowerCase().split(arg.toLowerCase())
  152. let p = 0
  153. for (const f of finder) {
  154. hilitLine.push(line.substr(p, f.length))
  155. const word = line.substr(p + f.length, arg.length)
  156. const hilit = color.bgBlack(color.red(word))
  157. hilitLine.push(hilit)
  158. p += f.length + arg.length
  159. }
  160. }
  161. out.push(hilitLine.join('') + '\n')
  162. })
  163. return out.join('')
  164. }).join('\n')
  165. const finalOut = results.length && !this.npm.config.get('long')
  166. ? 'Top hits for ' + (args.map(JSON.stringify).join(' ')) + '\n' +
  167. '—'.repeat(cols - 1) + '\n' +
  168. out + '\n' +
  169. '—'.repeat(cols - 1) + '\n' +
  170. '(run with -l or --long to see more context)'
  171. : out
  172. return finalOut.trim()
  173. }
  174. }
  175. module.exports = HelpSearch