Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add typechecking to extension build script #9494

Merged
merged 5 commits into from
Jan 25, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions extensions/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,41 @@ const path = require('path');
const fs = require('fs-extra');
const esbuild = require('esbuild');
const GlobalsPlugin = require('esbuild-plugin-globals');
const ts = require('typescript');

const formatHost = {
getCanonicalFileName: (path) => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
};

function typecheck(extPath) {
return new Promise((resolve, reject) => {
const tsConfigPath = ts.findConfigFile(extPath, ts.sys.fileExists, 'tsconfig.json');

if (tsConfigPath) {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const configFile = ts.readJsonConfigFile(tsConfigPath, ts.sys.readFile);

const { fileNames, options } = ts.parseJsonSourceFileConfigFileContent(configFile, ts.sys, extPath);

const program = ts.createProgram(fileNames, { ...options, noEmit: true });
const emitResult = program.emit();

const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
const hasTsErrors = allDiagnostics.some((d) => d.category === ts.DiagnosticCategory.Error);

if (hasTsErrors) {
const tsOutput = ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost);

reject(new Error(`TypeScript errors:\n${tsOutput}}`));
return;
}
}

resolve();
});
}

const getBundleConfigs = (extPath, packageJSON, watch = false) => {
const buildConfigs = [];
Expand Down Expand Up @@ -111,6 +146,7 @@ const compile = async (name, extPath, watch = false) => {

try {
await cleanDist(name, extPath);
work.push(typecheck(extPath));
for (const config of getBundleConfigs(extPath, packageJSON, watch)) {
work.push(service.build(config));
}
Expand Down