From f0b4c2fdab332f944fdde8ff02e197dfc7c375d4 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Sun, 26 Sep 2021 12:00:56 +0800 Subject: [PATCH] Initial tsc attempt --- packages/docusaurus-utils-plugin/bin/index.js | 3 ++ packages/docusaurus-utils-plugin/src/tsc.ts | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 packages/docusaurus-utils-plugin/src/tsc.ts diff --git a/packages/docusaurus-utils-plugin/bin/index.js b/packages/docusaurus-utils-plugin/bin/index.js index 0044ac69f32c6..295ef48cd49ed 100755 --- a/packages/docusaurus-utils-plugin/bin/index.js +++ b/packages/docusaurus-utils-plugin/bin/index.js @@ -9,6 +9,7 @@ const chalk = require('chalk'); const cli = require('commander'); const build = require('../lib/build').default; const watch = require('../lib/watch').default; +const tsc = require('../lib/tsc').default; cli .command('build') @@ -26,6 +27,8 @@ cli .option('--ignore ') .action(watch); +cli.command('tsc').action(tsc); + cli.arguments('').action((cmd) => { cli.outputHelp(); console.log(` ${chalk.red(`\n Unknown command ${chalk.yellow(cmd)}.`)}`); diff --git a/packages/docusaurus-utils-plugin/src/tsc.ts b/packages/docusaurus-utils-plugin/src/tsc.ts new file mode 100644 index 0000000000000..c97bec6523a64 --- /dev/null +++ b/packages/docusaurus-utils-plugin/src/tsc.ts @@ -0,0 +1,51 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import ts from 'typescript'; + +export default function tsc(): void { + const currentDir = process.cwd(); + const configFile = ts.findConfigFile( + currentDir, + ts.sys.fileExists, + 'tsconfig.json', + ); + if (!configFile) { + throw Error('tsconfig.json not found'); + } + const {config} = ts.readConfigFile(configFile, ts.sys.readFile); + + const {options, fileNames, errors} = ts.parseJsonConfigFileContent( + config, + ts.sys, + currentDir, + ); + + const program = ts.createIncrementalProgram({ + options, + rootNames: fileNames, + configFileParsingDiagnostics: errors, + }); + + const {diagnostics, emitSkipped} = program.emit(); + + const allDiagnostics = diagnostics.concat(errors); + + if (allDiagnostics.length) { + const formatHost: ts.FormatDiagnosticsHost = { + getCanonicalFileName: (path) => path, + getCurrentDirectory: ts.sys.getCurrentDirectory, + getNewLine: () => ts.sys.newLine, + }; + const message = ts.formatDiagnostics(allDiagnostics, formatHost); + console.warn(message); + } + + if (emitSkipped) { + process.exit(1); + } +}