From aee001673d0fa9aabf66f9e08e1cec055599da80 Mon Sep 17 00:00:00 2001 From: chad Date: Wed, 3 May 2023 16:13:46 -0500 Subject: [PATCH 1/9] docs: Updated doc-check to generate config + format errors --- src/document-check.js | 59 +++++++++++++++++++++++++++++++------------ src/utils.js | 26 +++++++++++++++++++ 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/document-check.js b/src/document-check.js index 17e04c181..547039199 100644 --- a/src/document-check.js +++ b/src/document-check.js @@ -1,9 +1,12 @@ /* eslint-disable no-console */ +import chalk from 'chalk' +import fs from 'fs-extra' import { globby } from 'globby' import Listr from 'listr' +import merge from 'merge-options' import { compileSnippets } from 'typescript-docs-verifier' -import { hasTsconfig } from './utils.js' +import { formatCode, formatError, fromRoot, hasTsconfig, isTypescript, readJson } from './utils.js' /** * @typedef {import("./types").GlobalOptions} GlobalOptions * @typedef {import("./types").DocsVerifierOptions} DocsVerifierOptions @@ -17,16 +20,16 @@ const tasks = new Listr( /** * @param {GlobalOptions & DocsVerifierOptions} ctx */ - enabled: ctx => hasTsconfig, + enabled: ctx => hasTsconfig && !isTypescript, /** * @param {GlobalOptions & DocsVerifierOptions} ctx * @param {Task} task */ task: async (ctx, task) => { - let tsconfigPath = 'tsconfig.json' + let tsconfigPath = 'tsconfig-doc-check.aegir.json' let markdownFiles = ['README.md'] - if (ctx.tsConfigPath) { + if (ctx.tsConfigPath && ctx.tsConfigPath !== '.') { tsconfigPath = `${ctx.tsConfigPath}/tsconfig.json` } @@ -34,20 +37,44 @@ const tasks = new Listr( 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) + const configPath = fromRoot(tsconfigPath) + const userTSConfig = readJson(fromRoot('tsconfig.json')) + + try { + fs.writeJsonSync( + configPath, + merge.apply({ concatArrays: true }, [ + userTSConfig, + { + compilerOptions: { + target: 'esnext', + module: 'esnext', + noImplicitAny: true, + noEmit: true + } } + ]) + ) + + compileSnippets({ markdownFiles, project: configPath }) + .then((results) => { + results.forEach((result) => { + if (result.error) { + process.exitCode = 1 + chalk`{red.bold Error compiling example code block ${result.index} in file ${result.file}:}` + console.log(formatError(result.error)) + console.log(chalk`{blue.bold Original code:}`) + console.log(formatCode(result.snippet, result.linesWithErrors)) + } + }) }) - }) - .catch((error) => { - console.error('Error compiling TypeScript snippets', error) - }) + .catch((error) => { + console.error('Error compiling TypeScript snippets', error) + }) + } finally { + fs.removeSync(configPath) + fs.removeSync(fromRoot('dist', 'tsconfig-doc-check.aegir.tsbuildinfo')) + } } } diff --git a/src/utils.js b/src/utils.js index 710435236..5a5441c69 100644 --- a/src/utils.js +++ b/src/utils.js @@ -8,6 +8,7 @@ import os from 'os' import path from 'path' import readline from 'readline' +import chalk from 'chalk' import { fileURLToPath } from 'url' import { constants, createBrotliCompress, createGzip } from 'zlib' import { download } from '@electron/get' @@ -513,3 +514,28 @@ async function * _glob (base, dir, pattern, options) { } } } + +/** + * + * @param {Error} error + * @returns + */ +export const formatError = (error) => " " + error.message.split("\n").join("\n ") + +/** + * + * @param {string} code + * @param {number[]} errorLines + * @returns + */ +export const formatCode = (code, errorLines) => { + const lines = code.split("\n").map((line, index) => { + const lineNumber = index + 1; + if (errorLines.includes(lineNumber)) { + return chalk`{bold.red ${String(lineNumber).padStart(2)}| ${line}}` + } else { + return `${String(lineNumber).padStart(2)}| ${line}` + } + }) + return " " + lines.join("\n ") +} \ No newline at end of file From db24aa2ac8278183d85771e96559db24dbceb06f Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 15 May 2023 16:16:06 -0500 Subject: [PATCH 2/9] feat: added color highlighting for errors in compiling + relative path for tsconfig --- src/config/tsconfig.aegir.json | 2 +- src/document-check.js | 33 +++++++++++++++------------------ src/utils.js | 14 +++++++------- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/config/tsconfig.aegir.json b/src/config/tsconfig.aegir.json index 277005b3e..63e3b5a38 100644 --- a/src/config/tsconfig.aegir.json +++ b/src/config/tsconfig.aegir.json @@ -31,7 +31,7 @@ "forceConsistentCasingInFileNames": true, "skipLibCheck": true, "stripInternal": true, - "resolveJsonModule": true + "resolveJsonModule": true, }, "include": [ "src", diff --git a/src/document-check.js b/src/document-check.js index 547039199..82f91a2a8 100644 --- a/src/document-check.js +++ b/src/document-check.js @@ -26,18 +26,17 @@ const tasks = new Listr( * @param {Task} task */ task: async (ctx, task) => { - let tsconfigPath = 'tsconfig-doc-check.aegir.json' + let configPath = './tsconfig-doc-check.aegir.json' let markdownFiles = ['README.md'] if (ctx.tsConfigPath && ctx.tsConfigPath !== '.') { - tsconfigPath = `${ctx.tsConfigPath}/tsconfig.json` + configPath = `${ctx.tsConfigPath}/tsconfig.json` } if (ctx.inputFiles) { markdownFiles = await globby(ctx.inputFiles) } - const configPath = fromRoot(tsconfigPath) const userTSConfig = readJson(fromRoot('tsconfig.json')) try { @@ -56,21 +55,19 @@ const tasks = new Listr( ]) ) - compileSnippets({ markdownFiles, project: configPath }) - .then((results) => { - results.forEach((result) => { - if (result.error) { - process.exitCode = 1 - chalk`{red.bold Error compiling example code block ${result.index} in file ${result.file}:}` - console.log(formatError(result.error)) - console.log(chalk`{blue.bold Original code:}`) - console.log(formatCode(result.snippet, result.linesWithErrors)) - } - }) - }) - .catch((error) => { - console.error('Error compiling TypeScript snippets', error) - }) + const results = await compileSnippets({ markdownFiles, project: configPath }) + + results.forEach((result) => { + if (result.error) { + process.exitCode = 1 + console.log(chalk.red.bold(`Error compiling example code block ${result.index} in file ${result.file}:`)) + console.log(formatError(result.error)) + console.log(chalk.blue.bold('Original code:')) + console.log(formatCode(result.snippet, result.linesWithErrors)) + } + }) + } catch (error) { + console.error('Error complining Typescript snippets ', error) } finally { fs.removeSync(configPath) fs.removeSync(fromRoot('dist', 'tsconfig-doc-check.aegir.tsbuildinfo')) diff --git a/src/utils.js b/src/utils.js index 5a5441c69..bb5b67b3f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -8,10 +8,10 @@ import os from 'os' import path from 'path' import readline from 'readline' -import chalk from 'chalk' import { fileURLToPath } from 'url' import { constants, createBrotliCompress, createGzip } from 'zlib' import { download } from '@electron/get' +import chalk from 'chalk' import envPaths from 'env-paths' import { execa } from 'execa' import extract from 'extract-zip' @@ -520,7 +520,7 @@ async function * _glob (base, dir, pattern, options) { * @param {Error} error * @returns */ -export const formatError = (error) => " " + error.message.split("\n").join("\n ") +export const formatError = (error) => ' ' + error.message.split('\n').join('\n ') /** * @@ -529,13 +529,13 @@ export const formatError = (error) => " " + error.message.split("\n").join("\n * @returns */ export const formatCode = (code, errorLines) => { - const lines = code.split("\n").map((line, index) => { - const lineNumber = index + 1; + const lines = code.split('\n').map((line, index) => { + const lineNumber = index + 1 if (errorLines.includes(lineNumber)) { - return chalk`{bold.red ${String(lineNumber).padStart(2)}| ${line}}` + return chalk.bold.red(`${String(lineNumber).padStart(2)}| ${line}`) } else { return `${String(lineNumber).padStart(2)}| ${line}` } }) - return " " + lines.join("\n ") -} \ No newline at end of file + return ' ' + lines.join('\n ') +} From 7e93e149e73a9df1105c2401e7978b272305a324 Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 15 May 2023 16:24:52 -0500 Subject: [PATCH 3/9] deps: added chalk for formatting error codes --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index ac3617a58..86a3fd4d1 100644 --- a/package.json +++ b/package.json @@ -247,6 +247,7 @@ "chai-parentheses": "^0.0.2", "chai-string": "^1.5.0", "chai-subset": "^1.6.0", + "chalk": "^5.2.0", "conventional-changelog-conventionalcommits": "^5.0.0", "cors": "^2.8.5", "depcheck": "^1.4.3", From 06e64de9753ec0030c088b8e9fec59eeaf61d89d Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 16 May 2023 13:48:52 -0500 Subject: [PATCH 4/9] docs: removed chalk from src + linting (#1273) --- package.json | 1 - src/config/tsconfig.aegir.json | 2 +- src/document-check.js | 8 ++++---- src/utils.js | 4 ++-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 86a3fd4d1..ac3617a58 100644 --- a/package.json +++ b/package.json @@ -247,7 +247,6 @@ "chai-parentheses": "^0.0.2", "chai-string": "^1.5.0", "chai-subset": "^1.6.0", - "chalk": "^5.2.0", "conventional-changelog-conventionalcommits": "^5.0.0", "cors": "^2.8.5", "depcheck": "^1.4.3", diff --git a/src/config/tsconfig.aegir.json b/src/config/tsconfig.aegir.json index 63e3b5a38..277005b3e 100644 --- a/src/config/tsconfig.aegir.json +++ b/src/config/tsconfig.aegir.json @@ -31,7 +31,7 @@ "forceConsistentCasingInFileNames": true, "skipLibCheck": true, "stripInternal": true, - "resolveJsonModule": true, + "resolveJsonModule": true }, "include": [ "src", diff --git a/src/document-check.js b/src/document-check.js index 82f91a2a8..c61c24bc3 100644 --- a/src/document-check.js +++ b/src/document-check.js @@ -1,8 +1,8 @@ /* eslint-disable no-console */ -import chalk from 'chalk' import fs from 'fs-extra' import { globby } from 'globby' +import kleur from 'kleur' import Listr from 'listr' import merge from 'merge-options' import { compileSnippets } from 'typescript-docs-verifier' @@ -20,7 +20,7 @@ const tasks = new Listr( /** * @param {GlobalOptions & DocsVerifierOptions} ctx */ - enabled: ctx => hasTsconfig && !isTypescript, + enabled: ctx => hasTsconfig, /** * @param {GlobalOptions & DocsVerifierOptions} ctx * @param {Task} task @@ -60,9 +60,9 @@ const tasks = new Listr( results.forEach((result) => { if (result.error) { process.exitCode = 1 - console.log(chalk.red.bold(`Error compiling example code block ${result.index} in file ${result.file}:`)) + console.log(kleur.red().bold(`Error compiling example code block ${result.index} in file ${result.file}:`)) console.log(formatError(result.error)) - console.log(chalk.blue.bold('Original code:')) + console.log(kleur.blue().bold('Original code:')) console.log(formatCode(result.snippet, result.linesWithErrors)) } }) diff --git a/src/utils.js b/src/utils.js index bb5b67b3f..b3508ec07 100644 --- a/src/utils.js +++ b/src/utils.js @@ -11,11 +11,11 @@ import readline from 'readline' import { fileURLToPath } from 'url' import { constants, createBrotliCompress, createGzip } from 'zlib' import { download } from '@electron/get' -import chalk from 'chalk' import envPaths from 'env-paths' import { execa } from 'execa' import extract from 'extract-zip' import fs from 'fs-extra' +import kleur from 'kleur' import Listr from 'listr' import { minimatch } from 'minimatch' import lockfile from 'proper-lockfile' @@ -532,7 +532,7 @@ export const formatCode = (code, errorLines) => { const lines = code.split('\n').map((line, index) => { const lineNumber = index + 1 if (errorLines.includes(lineNumber)) { - return chalk.bold.red(`${String(lineNumber).padStart(2)}| ${line}`) + return kleur.red().bold(`${String(lineNumber).padStart(2)}| ${line}`) } else { return `${String(lineNumber).padStart(2)}| ${line}` } From b256417176a1358fefdf7dced34cb947e950e18a Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 16 May 2023 13:48:52 -0500 Subject: [PATCH 5/9] docs: removed chalk from src + linting (#1273) --- src/document-check.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/document-check.js b/src/document-check.js index c61c24bc3..5c4d9a801 100644 --- a/src/document-check.js +++ b/src/document-check.js @@ -6,7 +6,7 @@ import kleur from 'kleur' import Listr from 'listr' import merge from 'merge-options' import { compileSnippets } from 'typescript-docs-verifier' -import { formatCode, formatError, fromRoot, hasTsconfig, isTypescript, readJson } from './utils.js' +import { formatCode, formatError, fromRoot, hasTsconfig, readJson } from './utils.js' /** * @typedef {import("./types").GlobalOptions} GlobalOptions * @typedef {import("./types").DocsVerifierOptions} DocsVerifierOptions From d178e4655a6b412e7d4db090c9645bd5856d238c Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 16 May 2023 22:05:34 -0500 Subject: [PATCH 6/9] test: added tests (#1273) --- src/document-check.js | 12 +++--- test/document-check.js | 37 +++++++++++++++++++ .../document-check/pass/GOODREADME.md | 3 ++ .../document-check/pass/tsconfig.json | 14 +++++++ .../ts-fail/ANOTHERBADREADME.md | 3 ++ .../document-check/ts-fail/BADREADME.md | 3 ++ .../document-check/ts-fail/tsconfig.json | 13 +++++++ .../tsconfig-fail/GOODREADME.md | 3 ++ test/node.js | 1 + 9 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 test/document-check.js create mode 100644 test/fixtures/document-check/pass/GOODREADME.md create mode 100644 test/fixtures/document-check/pass/tsconfig.json create mode 100644 test/fixtures/document-check/ts-fail/ANOTHERBADREADME.md create mode 100644 test/fixtures/document-check/ts-fail/BADREADME.md create mode 100644 test/fixtures/document-check/ts-fail/tsconfig.json create mode 100644 test/fixtures/document-check/tsconfig-fail/GOODREADME.md diff --git a/src/document-check.js b/src/document-check.js index 5c4d9a801..fc5d498cc 100644 --- a/src/document-check.js +++ b/src/document-check.js @@ -26,19 +26,21 @@ const tasks = new Listr( * @param {Task} task */ task: async (ctx, task) => { - let configPath = './tsconfig-doc-check.aegir.json' + const configPath = './tsconfig-doc-check.aegir.json' + + let userTSConfig = {} let markdownFiles = ['README.md'] if (ctx.tsConfigPath && ctx.tsConfigPath !== '.') { - configPath = `${ctx.tsConfigPath}/tsconfig.json` + userTSConfig = readJson(`${ctx.tsConfigPath}/tsconfig.json`) + } else { + userTSConfig = readJson(fromRoot('tsconfig.json')) } if (ctx.inputFiles) { markdownFiles = await globby(ctx.inputFiles) } - const userTSConfig = readJson(fromRoot('tsconfig.json')) - try { fs.writeJsonSync( configPath, @@ -66,8 +68,6 @@ const tasks = new Listr( console.log(formatCode(result.snippet, result.linesWithErrors)) } }) - } catch (error) { - console.error('Error complining Typescript snippets ', error) } finally { fs.removeSync(configPath) fs.removeSync(fromRoot('dist', 'tsconfig-doc-check.aegir.tsbuildinfo')) diff --git a/test/document-check.js b/test/document-check.js new file mode 100644 index 000000000..c4d3cb799 --- /dev/null +++ b/test/document-check.js @@ -0,0 +1,37 @@ +/* eslint-env mocha */ + +import { createRequire } from 'module' +import path from 'path' +import { fileURLToPath } from 'url' +import { execa } from 'execa' +import { expect } from '../utils/chai.js' + +const require = createRequire(import.meta.url) +const __dirname = path.dirname(fileURLToPath(import.meta.url)) +const bin = require.resolve('../src/index.js') + +describe('document check', () => { + it('should fail for errant typescript in docs', async () => { + const cwd = path.join(__dirname, 'fixtures/document-check/ts-fail') + + await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`, '--tsConfigPath', `${cwd}`])) + .to.eventually.be.rejected() + .with.property('stdout') + .that.include('Error compiling example code block 1') + }) + + it('should pass for correct typescript in docs', async () => { + const cwd = path.join(__dirname, 'fixtures/document-check/pass') + + await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`])).to.eventually.be.fulfilled() + }) + + it('should fail for missing tsconfig.json', async () => { + const cwd = path.join(__dirname, 'fixtures/document-check/tsconfig-fail') + + await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`, '--tsConfigPath', `${cwd}`])) + .to.eventually.be.rejected() + .with.property('stderr') + .that.include('no such file or directory') + }) +}) diff --git a/test/fixtures/document-check/pass/GOODREADME.md b/test/fixtures/document-check/pass/GOODREADME.md new file mode 100644 index 000000000..3097c2fae --- /dev/null +++ b/test/fixtures/document-check/pass/GOODREADME.md @@ -0,0 +1,3 @@ +```ts +export const a = 1; +``` \ No newline at end of file diff --git a/test/fixtures/document-check/pass/tsconfig.json b/test/fixtures/document-check/pass/tsconfig.json new file mode 100644 index 000000000..7b352c7fb --- /dev/null +++ b/test/fixtures/document-check/pass/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "./../../../../src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist", + "emitDeclarationOnly": true, + "isolatedModules": true + }, + "include": [ + "package.json", + "src", + "test", + "utils" + ] +} diff --git a/test/fixtures/document-check/ts-fail/ANOTHERBADREADME.md b/test/fixtures/document-check/ts-fail/ANOTHERBADREADME.md new file mode 100644 index 000000000..9b4f7fd46 --- /dev/null +++ b/test/fixtures/document-check/ts-fail/ANOTHERBADREADME.md @@ -0,0 +1,3 @@ +```typescript +still.bad.code +``` \ No newline at end of file diff --git a/test/fixtures/document-check/ts-fail/BADREADME.md b/test/fixtures/document-check/ts-fail/BADREADME.md new file mode 100644 index 000000000..50378f397 --- /dev/null +++ b/test/fixtures/document-check/ts-fail/BADREADME.md @@ -0,0 +1,3 @@ +```typescript + wrong.code() +``` \ No newline at end of file diff --git a/test/fixtures/document-check/ts-fail/tsconfig.json b/test/fixtures/document-check/ts-fail/tsconfig.json new file mode 100644 index 000000000..0d2cb5e2b --- /dev/null +++ b/test/fixtures/document-check/ts-fail/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "./../../../../src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist", + "emitDeclarationOnly": true + }, + "include": [ + "package.json", + "src", + "test", + "utils" + ] +} diff --git a/test/fixtures/document-check/tsconfig-fail/GOODREADME.md b/test/fixtures/document-check/tsconfig-fail/GOODREADME.md new file mode 100644 index 000000000..3097c2fae --- /dev/null +++ b/test/fixtures/document-check/tsconfig-fail/GOODREADME.md @@ -0,0 +1,3 @@ +```ts +export const a = 1; +``` \ No newline at end of file diff --git a/test/node.js b/test/node.js index ec45a8de0..986c3d25b 100644 --- a/test/node.js +++ b/test/node.js @@ -3,6 +3,7 @@ import './docs.js' import './lint.js' import './fixtures.js' import './dependants.js' +import './document-check.js' import './dependency-check.js' import './utils/echo-server.js' import './utils/get-port.js' From c6d1d688f8e3460aa82b75c3b9a17a2932b24841 Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 16 May 2023 22:24:02 -0500 Subject: [PATCH 7/9] fix: added catch for doc-check errors (#1273) --- src/document-check.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/document-check.js b/src/document-check.js index fc5d498cc..5a4e3abdb 100644 --- a/src/document-check.js +++ b/src/document-check.js @@ -68,6 +68,8 @@ const tasks = new Listr( console.log(formatCode(result.snippet, result.linesWithErrors)) } }) + } catch (err) { + console.log('Error in trying to compile Typescript code', err) } finally { fs.removeSync(configPath) fs.removeSync(fromRoot('dist', 'tsconfig-doc-check.aegir.tsbuildinfo')) From 765304f92f2c68b673a6627d4403c66a6b21d571 Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 22 May 2023 18:08:00 -0500 Subject: [PATCH 8/9] tests: skip tests on windows until ts-doc-verifier issue is resolved (#1273) --- test/document-check.js | 50 +++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/test/document-check.js b/test/document-check.js index c4d3cb799..b5fc243b5 100644 --- a/test/document-check.js +++ b/test/document-check.js @@ -9,29 +9,33 @@ import { expect } from '../utils/chai.js' const require = createRequire(import.meta.url) const __dirname = path.dirname(fileURLToPath(import.meta.url)) const bin = require.resolve('../src/index.js') +const isWindows = process.platform === 'win32' describe('document check', () => { - it('should fail for errant typescript in docs', async () => { - const cwd = path.join(__dirname, 'fixtures/document-check/ts-fail') - - await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`, '--tsConfigPath', `${cwd}`])) - .to.eventually.be.rejected() - .with.property('stdout') - .that.include('Error compiling example code block 1') - }) - - it('should pass for correct typescript in docs', async () => { - const cwd = path.join(__dirname, 'fixtures/document-check/pass') - - await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`])).to.eventually.be.fulfilled() - }) - - it('should fail for missing tsconfig.json', async () => { - const cwd = path.join(__dirname, 'fixtures/document-check/tsconfig-fail') - - await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`, '--tsConfigPath', `${cwd}`])) - .to.eventually.be.rejected() - .with.property('stderr') - .that.include('no such file or directory') - }) + // Skip tests on windows until https://github.com/bbc/typescript-docs-verifier/issues/26 is resolved + if (!isWindows) { + it('should fail for errant typescript in docs', async () => { + const cwd = path.join(__dirname, 'fixtures/document-check/ts-fail') + + await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`, '--tsConfigPath', `${cwd}`])) + .to.eventually.be.rejected() + .with.property('stdout') + .that.include('Error compiling example code block 1') + }) + + it('should pass for correct typescript in docs', async () => { + const cwd = path.join(__dirname, 'fixtures/document-check/pass') + + await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`])).to.eventually.be.fulfilled() + }) + + it('should fail for missing tsconfig.json', async () => { + const cwd = path.join(__dirname, 'fixtures/document-check/tsconfig-fail') + + await expect(execa(bin, ['doc-check', '--inputFiles', `${cwd}/*.md`, '--tsConfigPath', `${cwd}`])) + .to.eventually.be.rejected() + .with.property('stderr') + .that.include('no such file or directory') + }) + } }) From e15497e0a2bb7ee68caed2f700d658ae61e9eb4c Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 23 May 2023 12:10:56 -0500 Subject: [PATCH 9/9] test: added test to ensure doc-check isn't done on windows (#1273) --- src/document-check.js | 87 +++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/src/document-check.js b/src/document-check.js index 5a4e3abdb..eba44ed36 100644 --- a/src/document-check.js +++ b/src/document-check.js @@ -26,56 +26,61 @@ const tasks = new Listr( * @param {Task} task */ task: async (ctx, task) => { - const configPath = './tsconfig-doc-check.aegir.json' + const isWindows = process.platform === 'win32' - let userTSConfig = {} - let markdownFiles = ['README.md'] + if (!isWindows) { + const configPath = './tsconfig-doc-check.aegir.json' - if (ctx.tsConfigPath && ctx.tsConfigPath !== '.') { - userTSConfig = readJson(`${ctx.tsConfigPath}/tsconfig.json`) - } else { - userTSConfig = readJson(fromRoot('tsconfig.json')) - } + let userTSConfig = {} + let markdownFiles = ['README.md'] - if (ctx.inputFiles) { - markdownFiles = await globby(ctx.inputFiles) - } + if (ctx.tsConfigPath && ctx.tsConfigPath !== '.') { + userTSConfig = readJson(`${ctx.tsConfigPath}/tsconfig.json`) + } else { + userTSConfig = readJson(fromRoot('tsconfig.json')) + } + + if (ctx.inputFiles) { + markdownFiles = await globby(ctx.inputFiles) + } - try { - fs.writeJsonSync( - configPath, - merge.apply({ concatArrays: true }, [ - userTSConfig, - { - compilerOptions: { - target: 'esnext', - module: 'esnext', - noImplicitAny: true, - noEmit: true + try { + fs.writeJsonSync( + configPath, + merge.apply({ concatArrays: true }, [ + userTSConfig, + { + compilerOptions: { + target: 'esnext', + module: 'esnext', + noImplicitAny: true, + noEmit: true + } } - } - ]) - ) + ]) + ) - const results = await compileSnippets({ markdownFiles, project: configPath }) + const results = await compileSnippets({ markdownFiles, project: configPath }) - results.forEach((result) => { - if (result.error) { - process.exitCode = 1 - console.log(kleur.red().bold(`Error compiling example code block ${result.index} in file ${result.file}:`)) - console.log(formatError(result.error)) - console.log(kleur.blue().bold('Original code:')) - console.log(formatCode(result.snippet, result.linesWithErrors)) - } - }) - } catch (err) { - console.log('Error in trying to compile Typescript code', err) - } finally { - fs.removeSync(configPath) - fs.removeSync(fromRoot('dist', 'tsconfig-doc-check.aegir.tsbuildinfo')) + results.forEach((result) => { + if (result.error) { + process.exitCode = 1 + console.log(kleur.red().bold(`Error compiling example code block ${result.index} in file ${result.file}:`)) + console.log(formatError(result.error)) + console.log(kleur.blue().bold('Original code:')) + console.log(formatCode(result.snippet, result.linesWithErrors)) + } + }) + } catch (err) { + console.log('Error in trying to compile Typescript code', err) + } finally { + fs.removeSync(configPath) + fs.removeSync(fromRoot('dist', 'tsconfig-doc-check.aegir.tsbuildinfo')) + } + } else { + console.log(kleur.red('The underlying plugin used for TS-doc checks currently does not support Windows OS (See Github issue https://github.com/bbc/typescript-docs-verifier/issues/26). Skipping document check.')) } } - } ], {