Skip to content

Commit

Permalink
Initial tsc attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Oct 15, 2021
1 parent ac1aafb commit 19f048a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/docusaurus-utils-plugin/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -26,6 +27,8 @@ cli
.option('--ignore <pattern>')
.action(watch);

cli.command('tsc').action(tsc);

cli.arguments('<command>').action((cmd) => {
cli.outputHelp();
console.log(` ${chalk.red(`\n Unknown command ${chalk.yellow(cmd)}.`)}`);
Expand Down
51 changes: 51 additions & 0 deletions packages/docusaurus-utils-plugin/src/tsc.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 19f048a

Please sign in to comment.