Skip to content

Commit 24ccad1

Browse files
committed
feat: v0.0.7 pactman - search pact and pactflow docs
1 parent 2e3e866 commit 24ccad1

File tree

6 files changed

+426
-186
lines changed

6 files changed

+426
-186
lines changed

pactman/.env

-4
This file was deleted.

pactman/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# pactman
2-
> Search Pact documentation from the terminal
2+
> Search Pact & Pactflow documentation from the terminal
33
44
[![asciicast](https://asciinema.org/a/490546.svg?autoplay=1)](https://asciinema.org/a/490546)
55

66
## Use
77

8-
You can perform [Pact documentation](https://docs.pact.io) search without installing `pactman` permanently
8+
You can perform searches against [Pact documentation](https://docs.pact.io) & [Pactflow documentation](https://docs.pactflow.io) without installing `pactman` permanently
99

1010
```shell
1111
npx pactman
1212
```
13+
- select which site you want to search ["pact","pactflow"]
1314
- type search query
1415
- click/Press Enter on the desired result to open the browser
1516

@@ -19,6 +20,8 @@ Or you can install `pactman` globally to be quicker
1920
npm i -g pactman
2021
```
2122

23+
Then run it via
24+
2225
```shell
2326
pactman
2427
```

pactman/bin/index.js

+77-32
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
const path = require('path')
66

7-
require('dotenv').config({
8-
path: path.resolve(__dirname, '..', '.env'),
9-
})
10-
117
const updateNotifier = require('update-notifier')
128
const pkg = require(path.join('..', 'package.json'))
139
updateNotifier({ pkg }).notify()
@@ -17,8 +13,6 @@ const chalk = require('chalk')
1713
const prompts = require('prompts')
1814
const { initSearch } = require('../src/search')
1915

20-
const search = initSearch()
21-
2216
// returns either content or level highlight
2317
const getHighlightText = (hit) => {
2418
if (!hit._highlightResult) {
@@ -70,38 +64,89 @@ const getTitle = (hash) => {
7064
return result
7165
}
7266

73-
const options = {
74-
type: 'autocomplete',
75-
name: 'value',
76-
message: '🔍 Search Pact Docs',
77-
choices: [],
78-
suggest(input, choices) {
79-
choices.length = 0
80-
if (!input.length) {
81-
return Promise.resolve([])
82-
}
83-
84-
return search(input).then((results) => {
85-
if (!results.nbHits) {
86-
return []
67+
const options = [
68+
{
69+
type: 'select',
70+
name: 'selection',
71+
message: '🔍 Select which docs you want to search',
72+
choices: ['Pact', 'Pactflow'],
73+
},
74+
{
75+
type: (prev) => (prev === 0 ? 'autocomplete' : null),
76+
name: 'value',
77+
message: '🔍 Search Pact Docs',
78+
choices: [],
79+
suggest(input, choices) {
80+
81+
82+
const search = initSearch('pact')
83+
84+
choices.length = 0
85+
if (!input.length) {
86+
return Promise.resolve([])
8787
}
8888

89-
return results.hits.map((hit) => {
90-
choices.push({
91-
title: hit.url,
89+
return search(input).then((results) => {
90+
if (!results.nbHits) {
91+
return []
92+
}
93+
94+
return results.hits.map((hit) => {
95+
choices.push({
96+
title: hit.url,
97+
})
98+
const highlight = getHighlight(hit)
99+
const url = new URL(hit.url)
100+
const title = highlight ? highlight : getTitle(url.hash)
101+
return {
102+
title,
103+
value: encodeURI(hit.url),
104+
description: url.pathname,
105+
}
92106
})
93-
const highlight = getHighlight(hit)
94-
const url = new URL(hit.url)
95-
const title = highlight ? highlight : getTitle(url.hash)
96-
return {
97-
title,
98-
value: encodeURI(hit.url),
99-
description: url.pathname,
107+
})
108+
},
109+
},
110+
{
111+
type: (prev) => (prev === 1 ? 'autocomplete' : null),
112+
name: 'value',
113+
message: '🔍 Search Pactflow Docs',
114+
choices: [],
115+
suggest(input, choices) {
116+
require('dotenv').config({
117+
path: path.resolve(__dirname, '..', 'pactflow.env'),
118+
})
119+
120+
121+
const search = initSearch('pactflow')
122+
123+
choices.length = 0
124+
if (!input.length) {
125+
return Promise.resolve([])
126+
}
127+
128+
return search(input).then((results) => {
129+
if (!results.nbHits) {
130+
return []
100131
}
132+
133+
return results.hits.map((hit) => {
134+
choices.push({
135+
title: hit.url,
136+
})
137+
const highlight = getHighlight(hit)
138+
const url = new URL(hit.url)
139+
const title = highlight ? highlight : getTitle(url.hash)
140+
return {
141+
title,
142+
value: encodeURI(hit.url),
143+
description: url.pathname,
144+
}
145+
})
101146
})
102-
})
147+
},
103148
},
104-
}
149+
]
105150
const ask = async () => {
106151
const response = await prompts(options)
107152

0 commit comments

Comments
 (0)