Skip to content

Commit 8844b5f

Browse files
authored
feat: collect fiddles (#158)
* bump debug module * feat: add collect fiddles script * add printing * refactor * test find on ci * update README
1 parent e80569c commit 8844b5f

File tree

8 files changed

+176
-359
lines changed

8 files changed

+176
-359
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@ jobs:
88
n: 3
99
command: npm test
1010

11+
test-collect:
12+
runs-on: ubuntu-20.04
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: bahmutov/npm-install@v1
16+
- name: Print fiddles 🖨️
17+
run: node bin/collect-fiddles -p cypress/e2e/many-fiddles.md
18+
env:
19+
# we do not need to install Cypress to run this test
20+
CYPRESS_INSTALL_BINARY: 0
21+
1122
release:
12-
needs: 'test'
23+
needs: ['test', 'test-collect']
1324
runs-on: ubuntu-20.04
1425
steps:
1526
- name: Checkout 🛎

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
cypress/videos
33
cypress/screenshots
44
cypress/e2e/*.js
5+
out.json

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ npx export-fiddle <path/to/md> --before-each <url>
215215
npx export-fiddle <path/to/md> --before <url>
216216
```
217217

218+
## Find fiddles
219+
220+
You can find fiddles in Markdown files using a blog pattern
221+
222+
```
223+
# find fiddles in file A.md
224+
$ npx collect-fiddles cypress/e2e/A.md
225+
# print found fiddles in two files
226+
$ npx collect-fiddles --print cypress/e2e/A.md cypress/e2e/B.md
227+
# save found fiddles into a JSON file
228+
$ npx collect-fiddles --filename out.json 'cypress/e2e/**/*.md'
229+
```
230+
218231
## Examples
219232

220233
See the presentation [Using End-to-end Tests as Documentation](https://slides.com/bahmutov/tests-are-docs)

bin/collect-fiddles.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env node
2+
// @ts-check
3+
4+
const debug = require('debug')('cypress-markdown-preprocessor')
5+
const fs = require('fs')
6+
const arg = require('arg')
7+
const globby = require('globby')
8+
const { collectFiddles } = require('../src/collect-utils')
9+
10+
const args = arg(
11+
{
12+
'--print': Boolean, // to STDOUT
13+
'--filename': String, // output filename
14+
// aliases
15+
'-p': '--print',
16+
'-f': '--filename',
17+
},
18+
{ argv: process.argv },
19+
)
20+
debug('arguments %o', args)
21+
22+
// remove "node" and the script name from the list of arguments
23+
const markdownPattern = args._.slice(2)
24+
const sourceFiles = globby.sync(markdownPattern)
25+
debug('source files')
26+
debug(sourceFiles)
27+
28+
if (!sourceFiles.length) {
29+
console.error('Could not find any Markdown files')
30+
process.exit(1)
31+
}
32+
33+
console.log(
34+
'Searching for fiddles in %d Markdown file(s)',
35+
sourceFiles.length,
36+
)
37+
38+
const fiddles = collectFiddles(sourceFiles)
39+
40+
console.log(
41+
'found %d fiddle(s) across %d Markdown file(s)',
42+
fiddles.length,
43+
sourceFiles.length,
44+
)
45+
46+
if (args['--print']) {
47+
console.log(fiddles)
48+
}
49+
50+
if (args['--filename']) {
51+
const text = JSON.stringify(fiddles, null, 2) + '\n'
52+
fs.writeFileSync(args['--filename'], text)
53+
console.log('write fiddles to JSON file %s', args['--filename'])
54+
}

cypress/e2e/many-fiddles.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Many fiddles
2+
3+
## Section 1
4+
5+
<!-- fiddle A / title a -->
6+
7+
```js
8+
9+
```
10+
11+
<!-- fiddle-end -->
12+
13+
<!-- fiddle A / title b -->
14+
15+
```js
16+
17+
```
18+
19+
<!-- fiddle-end -->
20+
21+
<!-- fiddle A / B / inner -->
22+
23+
```js
24+
25+
```
26+
27+
<!-- fiddle-end -->

0 commit comments

Comments
 (0)