help-search.js 5.5 KB

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