1
1
/* eslint-disable no-console */
2
2
3
+ import fs from 'fs-extra'
3
4
import { globby } from 'globby'
5
+ import kleur from 'kleur'
4
6
import Listr from 'listr'
7
+ import merge from 'merge-options'
5
8
import { compileSnippets } from 'typescript-docs-verifier'
6
- import { hasTsconfig } from './utils.js'
9
+ import { formatCode , formatError , fromRoot , hasTsconfig , readJson } from './utils.js'
7
10
/**
8
11
* @typedef {import("./types").GlobalOptions } GlobalOptions
9
12
* @typedef {import("./types").DocsVerifierOptions } DocsVerifierOptions
@@ -23,33 +26,61 @@ const tasks = new Listr(
23
26
* @param {Task } task
24
27
*/
25
28
task : async ( ctx , task ) => {
26
- let tsconfigPath = 'tsconfig.json'
27
- let markdownFiles = [ 'README.md' ]
29
+ const isWindows = process . platform === 'win32'
28
30
29
- if ( ctx . tsConfigPath ) {
30
- tsconfigPath = `${ ctx . tsConfigPath } /tsconfig.json`
31
- }
31
+ if ( ! isWindows ) {
32
+ const configPath = './tsconfig-doc-check.aegir.json'
32
33
33
- if ( ctx . inputFiles ) {
34
- markdownFiles = await globby ( ctx . inputFiles )
35
- }
34
+ let userTSConfig = { }
35
+ let markdownFiles = [ 'README.md' ]
36
+
37
+ if ( ctx . tsConfigPath && ctx . tsConfigPath !== '.' ) {
38
+ userTSConfig = readJson ( `${ ctx . tsConfigPath } /tsconfig.json` )
39
+ } else {
40
+ userTSConfig = readJson ( fromRoot ( 'tsconfig.json' ) )
41
+ }
42
+
43
+ if ( ctx . inputFiles ) {
44
+ markdownFiles = await globby ( ctx . inputFiles )
45
+ }
46
+
47
+ try {
48
+ fs . writeJsonSync (
49
+ configPath ,
50
+ merge . apply ( { concatArrays : true } , [
51
+ userTSConfig ,
52
+ {
53
+ compilerOptions : {
54
+ target : 'esnext' ,
55
+ module : 'esnext' ,
56
+ noImplicitAny : true ,
57
+ noEmit : true
58
+ }
59
+ }
60
+ ] )
61
+ )
62
+
63
+ const results = await compileSnippets ( { markdownFiles, project : configPath } )
36
64
37
- compileSnippets ( { markdownFiles, project : tsconfigPath } )
38
- . then ( ( results ) => {
39
65
results . forEach ( ( result ) => {
40
66
if ( result . error ) {
41
- console . log ( `Error compiling example code block ${ result . index } in file ${ result . file } ` )
42
- console . log ( result . error . message )
43
- console . log ( 'Original code:' )
44
- console . log ( result . snippet )
67
+ process . exitCode = 1
68
+ console . log ( kleur . red ( ) . bold ( `Error compiling example code block ${ result . index } in file ${ result . file } :` ) )
69
+ console . log ( formatError ( result . error ) )
70
+ console . log ( kleur . blue ( ) . bold ( 'Original code:' ) )
71
+ console . log ( formatCode ( result . snippet , result . linesWithErrors ) )
45
72
}
46
73
} )
47
- } )
48
- . catch ( ( error ) => {
49
- console . error ( 'Error compiling TypeScript snippets' , error )
50
- } )
74
+ } catch ( err ) {
75
+ console . log ( 'Error in trying to compile Typescript code' , err )
76
+ } finally {
77
+ fs . removeSync ( configPath )
78
+ fs . removeSync ( fromRoot ( 'dist' , 'tsconfig-doc-check.aegir.tsbuildinfo' ) )
79
+ }
80
+ } else {
81
+ 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.' ) )
82
+ }
51
83
}
52
-
53
84
}
54
85
] ,
55
86
{
0 commit comments