Skip to content

Commit 3af214f

Browse files
committed
docs
1 parent 3e88713 commit 3af214f

1 file changed

Lines changed: 12 additions & 34 deletions

File tree

README.md

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,11 @@ You can use some helpers that are passed to the preset function:
194194

195195
```typescript
196196
export const secretaryGeneralLogStatement: import('eslint-plugin-codegen').Preset = ({cache, dependencies}) => {
197-
const result = cache({maxAge: '1 year'}, () => {
198-
const res = dependencies.fetchSync('https://www.un.org/sg')
199-
const secretaryGeneral = (/Secretary-General ([A-Z].+?) [a-z]/.exec(res.text))?.[1]
197+
const result = cache({maxAge: '4 weeks'}, () => {
198+
const res = dependencies.fetchSync('https://en.wikipedia.org/wiki/Secretary-General_of_the_United_Nations')
199+
const $ = dependencies.cheerio.load(res.text)
200+
201+
const secretaryGeneral = $('.infobox td:contains("Incumbent") a').text()
200202
return `console.log('The UN Secretary-General is ${secretaryGeneral}')`
201203
})
202204
return result
@@ -212,8 +214,8 @@ export const secretaryGeneralLogStatement: import('eslint-plugin-codegen').Prese
212214
return cache({maxAge: '4 weeks'}, () => {
213215
const res = dependencies.fetchSync('https://en.wikipedia.org/wiki/Secretary-General_of_the_United_Nations')
214216
const $ = dependencies.cheerio.load(res.text)
215-
const incumbent = $('.infobox div:contains("Incumbent")')
216-
const secretaryGeneral = incumbent.find('a').text()
217+
218+
const secretaryGeneral = $('.infobox td:contains("Incumbent") a').text()
217219
return `console.log('The UN Secretary-General is ${secretaryGeneral}')`
218220
})
219221
return result
@@ -225,37 +227,13 @@ console.log('The UN Secretary-General is António Guterres')
225227
// codegen:end
226228
```
227229

228-
It will not re-run unless the input has changed, the output hash doesn't match, or until 1 year after the recorded timestamp. "The input" is a hash of the following:
230+
Since hitting wikipedia servers is slow and unreliable, you don't want to do it every time you lint. The codegen will be a no-op and leave the content untouched unless:
229231

230-
- the filename the directive appears in
231-
- the source code _excluding_ any existing content between the `codegen:start` and `codegen:end` directives
232-
- the options passed to the preset
232+
- 4 weeks has passed since the `timestamp`
233+
- the output hash doesn't match the generated content (this can happen if someone manually changes the generated content)
234+
- the input hash doesn't match the values passed to the preset
233235

234-
The output (i.e. the generated code between the start and end directives) is also hashed and written to the hash directive.
235-
236-
If the the generated code doesn't match the output hash, the generator function will re-run.
237-
238-
This means that if you change the filename, or any of the source code, it will re-run. You can control this behaviour when defining your generator function:
239-
240-
```ts
241-
export const secretaryGeneralLogStatement: import('eslint-plugin-codegen').Preset =
242-
({cache, dependencies}) => {
243-
return cache({maxAge: '4 weeks'}, () => {
244-
const res = dependencies.fetchSync(
245-
'https://en.wikipedia.org/wiki/Secretary-General_of_the_United_Nations',
246-
)
247-
const $ = dependencies.cheerio.load(res.text)
248-
const incumbent = $('.infobox div:contains("Incumbent")')
249-
const secretaryGeneral = incumbent.find('a').text()
250-
return `console.log('The UN Secretary-General is ${secretaryGeneral}')`
251-
})
252-
}
253-
254-
// codegen:start {preset: custom, export: secretaryGeneralLogStatement}
255-
// codegen:hash {input: 4119892f2e6eaf56ae5c346de91be718, output: eed0d07c81b82bff1d3e4751073b0112, timestamp: 2025-03-05T18:58:13.921Z}
256-
console.log('The UN Secretary-General is António Guterres')
257-
// codegen:end
258-
```
236+
Note that in the example above, we are using `cheerio` without having to import it - you don't even need it installed in your devDependencies (it's a dependency of eslint-plugin-codegen - so it does live in your node_modules, you just don't need to manage it or worry about tree-shaking).
259237

260238
The helpers that are provided to the generator function via the `dependencies` prop are listed below. You can use all of them in a type-safe way in your generator function, without having to add them as dependencies or even devDependencies:
261239

0 commit comments

Comments
 (0)