Skip to content

Commit

Permalink
feat: Add support for linting typescript snippets in markdown (#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Dec 19, 2022
1 parent 4c0c13d commit 73ff4b6
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ yarn.lock
*.log
.vscode
.coverage
.DS_Store
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Commands:
aegir build Builds a browser bundle and TS type declarations from the `src` folder.
aegir check Check project
aegir docs Generate documentation from TS type declarations.
aegir doc-check Verify TS code snippets in documentation.
aegir lint Lint all project files
aegir release Release your code onto the world
aegir test-dependant [repo] Run the tests of an module that depends on this module to see if the current changes have caused a regression
Expand Down Expand Up @@ -103,15 +104,13 @@ Aegir can be fully configured using a config file named `.aegir.js` or the packa
```js
// file: .aegir.js



/** @type {import('aegir').PartialOptions} */
module.exports = {
tsRepo: true,
release: {
build: false
}
}
build: false,
},
};
```
```json
Expand Down
1 change: 1 addition & 0 deletions md/github-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- run: npx aegir lint
- run: npx aegir build
- run: npx aegir dep-check
- run: npx aegir doc-check
- uses: ipfs/aegir/actions/bundle-size@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
"test": "node src/index.js test",
"docs": "node src/index.js docs",
"dep-check": "node src/index.js dep-check",
"doc-check": "node src/index.js doc-check",
"test:node": "node src/index.js test -t node --cov",
"test:chrome": "node src/index.js test -t browser --cov",
"test:chrome-webworker": "node src/index.js test -t webworker",
Expand Down Expand Up @@ -305,6 +306,7 @@
"typedoc": "^0.23.21",
"typedoc-plugin-mdn-links": "^2.0.0",
"typescript": "^4.6.3",
"typescript-docs-verifier": "2.4.0",
"uint8arrays": "^4.0.2",
"undici": "^5.0.0",
"update-notifier": "^6.0.2",
Expand Down
49 changes: 49 additions & 0 deletions src/cmds/document-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable no-console */

import { loadUserConfig } from '../config/user.js'
import docCheck from '../document-check.js'

/**
* @typedef {import("yargs").Argv} Argv
* @typedef {import("yargs").Arguments} Arguments
* @typedef {import("yargs").CommandModule} CommandModule
*/

const EPILOG = `
Docs are verified using typescript-docs-verifier (https://github.com/bbc/typescript-docs-verifier#typescript-docs-verifier)
For more info read: https://github.com/bbc/typescript-docs-verifier#how-it-works
`

/** @type {CommandModule} */
export default {
command: 'document-check [input...]',
aliases: ['doc-check'],
describe: 'Run `document-check` cli with aegir defaults.',
/**
* @param {Argv} yargs
*/
builder: async (yargs) => {
const userConfig = await loadUserConfig()

return yargs
.epilog(EPILOG)
.options({
inputFiles: {
array: true,
describe: 'The files to verify, defaults to `README.md`',
default: userConfig.documentCheck.inputFiles
},
tsConfigPath: {
type: 'string',
describe: 'The path to the `tsconfig.json`, defaults to the root.',
default: userConfig.documentCheck.tsConfigPath
}
})
},
/**
* @param {any} argv
*/
async handler (argv) {
await docCheck.run(argv)
}
}
8 changes: 8 additions & 0 deletions src/config/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ const defaults = {
email: 'aegir[bot]@users.noreply.github.com',
directory: '.docs'
},
// document check cmd options
documentCheck: {
inputFiles: [
'*.md',
'src/*.md'
],
tsConfigPath: '.'
},
// ts cmd options
ts: {
preset: undefined,
Expand Down
60 changes: 60 additions & 0 deletions src/document-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable no-console */

import Listr from 'listr'
import { hasTsconfig } from './utils.js'
import { globby } from 'globby'
import { compileSnippets } from 'typescript-docs-verifier'
/**
* @typedef {import("./types").GlobalOptions} GlobalOptions
* @typedef {import("./types").DocsVerifierOptions} DocsVerifierOptions
* @typedef {import("listr").ListrTaskWrapper} Task
*/

const tasks = new Listr(
[
{
title: 'typescript-doc-verify',
/**
* @param {GlobalOptions & DocsVerifierOptions} ctx
*/
enabled: ctx => hasTsconfig,
/**
* @param {GlobalOptions & DocsVerifierOptions} ctx
* @param {Task} task
*/
task: async (ctx, task) => {
let tsconfigPath = 'tsconfig.json'
let markdownFiles = ['README.md']

if (ctx.tsConfigPath) {
tsconfigPath = `${ctx.tsConfigPath}/tsconfig.json`
}

if (ctx.inputFiles) {
markdownFiles = await globby(ctx.inputFiles)
}

compileSnippets({ markdownFiles, project: tsconfigPath })
.then((results) => {
results.forEach((result) => {
if (result.error) {
console.log(`Error compiling example code block ${result.index} in file ${result.file}`)
console.log(result.error.message)
console.log('Original code:')
console.log(result.snippet)
}
})
})
.catch((error) => {
console.error('Error compiling TypeScript snippets', error)
})
}

}
],
{
renderer: 'verbose'
}
)

export default tasks
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import releaseCmd from './cmds/release.js'
import testDependantCmd from './cmds/test-dependant.js'
import testCmd from './cmds/test.js'
import docsCmd from './cmds/docs.js'
import docVerifyCmd from './cmds/document-check.js'

/**
* @typedef {import('./types').BuildOptions} BuildOptions
Expand Down Expand Up @@ -84,6 +85,7 @@ async function main () {
res.command(cleanCmd)
res.command(dependencyCheckCmd)
res.command(docsCmd)
res.command(docVerifyCmd)
res.command(lintPackageJsonCmd)
res.command(lintCmd)
res.command(releaseCmd)
Expand Down
23 changes: 22 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ interface Options extends GlobalOptions {
* Options for the `dependency-check` command
*/
dependencyCheck: DependencyCheckOptions
/**
* Options for the `document-check` command
*/
documentCheck: DocsVerifierOptions
}

/**
Expand Down Expand Up @@ -70,6 +74,10 @@ interface PartialOptions {
* Options for the `dependency-check` command
*/
dependencyCheck?: DependencyCheckOptions
/**
* Options for the `document-check` command
*/
documentCheck?: DocsVerifierOptions
}

interface GlobalOptions {
Expand Down Expand Up @@ -152,6 +160,18 @@ interface DocsOptions {
directory: string
}

interface DocsVerifierOptions {
/**
* The Markdown files to be verified, defaults to `README.md`
*/
inputFiles?: string[]

/**
* An alternative `.tsconfig.json` path to be used seperately from the default
*/
tsConfigPath?: string
}

interface LintOptions {
/**
* Automatically fix errors if possible.
Expand Down Expand Up @@ -319,5 +339,6 @@ export type {
LintOptions,
TestOptions,
ReleaseOptions,
DependencyCheckOptions
DependencyCheckOptions,
DocsVerifierOptions
}

0 comments on commit 73ff4b6

Please sign in to comment.