-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for compiling Typescript snippets in markdown (#1134)
Closes #1117 Co-authored-by: Alex Potsides <[email protected]>
- Loading branch information
1 parent
1cf5c45
commit 055e1d5
Showing
10 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters