Skip to content

Commit

Permalink
build: add typechecking to instruments and modules (#8238)
Browse files Browse the repository at this point in the history
  • Loading branch information
markszutor committed Oct 9, 2023
1 parent 10d6cf5 commit 60ef085
Show file tree
Hide file tree
Showing 52 changed files with 218 additions and 224 deletions.
88 changes: 85 additions & 3 deletions build-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const dotenv = require('dotenv');
const dotenv = require("dotenv");
const path = require("path");
const childProcess = require("child_process");
const fs = require("fs");

function defineEnvVars() {
dotenv.config({ path: '.env.local' });
Expand Down Expand Up @@ -32,8 +34,11 @@ module.exports.defineEnvVars = defineEnvVars;
* @param globalName {string|undefined} the name of the global to define in the output IIFE
* @param entryPoint {string} the entrypoint path, as an absolute path
* @param outFile {string} the output file path, as a path relative to the root of the repository
* @param tsConfigDir {string?} the directory containing the tsconfig.json file to use, optionally
*
* @returns {import('esbuild').BuildOptions}
*/
function esbuildModuleBuild(projectRoot, globalName, entryPoint, outFile) {
function esbuildModuleBuild(projectRoot, globalName, entryPoint, outFile, tsConfigDir) {
const isProductionBuild = process.env.A32NX_PRODUCTION_BUILD === '1';

process.chdir(projectRoot);
Expand All @@ -43,6 +48,12 @@ function esbuildModuleBuild(projectRoot, globalName, entryPoint, outFile) {

define: { DEBUG: 'false', ...defineEnvVars() },

plugins: [
typecheckingPlugin(),
],

tsconfig: tsConfigDir !== undefined ? path.join(tsConfigDir, 'tsconfig.json') : undefined,

entryPoints: [entryPoint],
bundle: true,
treeShaking: false,
Expand All @@ -60,4 +71,75 @@ function esbuildModuleBuild(projectRoot, globalName, entryPoint, outFile) {
};
}

module.exports.esbuildModuleBuild = esbuildModuleBuild;
module.exports.createModuleBuild = esbuildModuleBuild;

/**
* Returns an esbuild plugin which runs `tsc` typechecking
*
* @returns {import('esbuild').Plugin}
*/
function typecheckingPlugin() {
return {
name: 'typecheck',
/**
* @param build {import('esbuild').PluginBuild}
*/
setup(build) {
build.onStart(() => {
if (!(process.env.FBW_TYPECHECK === '1' || process.env.FBW_TYPECHECK?.toLowerCase() === 'true')) {
return;
}

const { entryPoints } = build.initialOptions;
const entryPointDir = path.dirname(entryPoints[0]);

const tsConfigInEntryPointDir = fs.existsSync(path.join(entryPointDir, 'tsconfig.json'));

let tsConfigDir;
if (tsConfigInEntryPointDir) {
tsConfigDir = entryPointDir;
} else if (build.initialOptions.tsconfig !== undefined) {
tsConfigDir = path.dirname(build.initialOptions.tsconfig);
}

if (tsConfigDir === undefined) {
throw new Error(`Cannot run typechecking: no tsconfig.json file found in '${entryPointDir}' and tsconfig path not specified`);
}

/**
* @type {import('esbuild').PartialMessage[]}
*/
const errors = []

try {
childProcess.execSync('npx tsc --noEmit -p .', { cwd: tsConfigDir });
} catch (e) {
if (!('stdout' in e) || !e.stdout) {
throw e;
}

const tscErrors = e.stdout.toString().split('\n').filter((err) => err.trim() !== '');

errors.push(...tscErrors.map((err) => {
const match = /(.+)\((\d+),(\d+)\):\s+(.+)/.exec(err.trim());

if (match) {
const [, file, line, column, text] = match;

const filePath = path.resolve(tsConfigDir, file);
const lineText = fs.readFileSync(filePath).toString().split('\n')[line - 1];

return { text, location: { file, line: parseInt(line), column: parseInt(column) - 1, lineText } }
} else {
return { text: err };
}
}));
}

return { errors };
})
}
}
}

module.exports.typecheckingPlugin = typecheckingPlugin;
8 changes: 4 additions & 4 deletions hsim-a21n-common/src/systems/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"compilerOptions": {
"target": "ESNext",
"target": "ES2017",
"baseUrl": ".",
"resolveJsonModule" : true,
"typeRoots": [
"../typings",
"../../../node_modules/@types"
"../typings"
],
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"jsx": "react",
"skipLibCheck": true,
"paths": {
"@datalink/aoc": ["datalink/aoc/src/index.ts"],
"@datalink/atc": ["datalink/atc/src/index.ts"],
Expand All @@ -20,5 +20,5 @@
"@typings/*": ["../typings/*"],
"@flybywiresim/fbw-sdk": ["./index.ts"]
}
},
}
}
8 changes: 4 additions & 4 deletions hsim-a318-common/src/systems/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"compilerOptions": {
"target": "ESNext",
"target": "ES2017",
"baseUrl": ".",
"resolveJsonModule" : true,
"typeRoots": [
"../typings",
"../../../node_modules/@types"
"../typings"
],
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"jsx": "react",
"skipLibCheck": true,
"paths": {
"@datalink/aoc": ["datalink/aoc/src/index.ts"],
"@datalink/atc": ["datalink/atc/src/index.ts"],
Expand All @@ -20,5 +20,5 @@
"@typings/*": ["../typings/*"],
"@flybywiresim/fbw-sdk": ["./index.ts"]
}
},
}
}
4 changes: 4 additions & 0 deletions hsim-a318ceo/mach.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const postCssPlugin = require('esbuild-style-plugin');
const tailwind = require('tailwindcss');
const postCssColorFunctionalNotation = require('postcss-color-functional-notation');
const postCssInset = require('postcss-inset');
const { typecheckingPlugin } = require("#build-utils");

// process.env.FBW_TYPECHECK = "1";

/** @type { import('@synaptic-simulations/mach').MachConfig } */
module.exports = {
Expand All @@ -24,6 +27,7 @@ module.exports = {
],
}
}),
typecheckingPlugin(),
],
instruments: [
msfsAvionicsInstrument('PFD'),
Expand Down
7 changes: 4 additions & 3 deletions hsim-a318ceo/src/systems/atsu/common/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const rootDir = path.join(__dirname, '..', '..', '..', '..');
const rootDir = path.join(__dirname, '..', '..', '..', '..', '..');
const outFile = 'build-a318ceo/out/lvfr-horizonsim-airbus-a318-ceo/html_ui/JS/A318HS/atsu/common.js';
const srcDir = 'fbw-common/src/systems/datalink/common';

esbuild.build(esbuildModuleBuild('build-a318ceo', 'AtsuCommon', path.join(rootDir, '../fbw-common/src/systems/datalink/common/src/index.ts'), outFile));
esbuild.build(createModuleBuild('build-a318ceo', 'AtsuCommon', path.join(rootDir, srcDir, '/src/index.ts'), outFile, path.join(rootDir, srcDir)));
4 changes: 2 additions & 2 deletions hsim-a318ceo/src/systems/atsu/fmsclient/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const outFile = 'build-a318ceo/out/lvfr-horizonsim-airbus-a318-ceo/html_ui/JS/A318HS/atsu/fmsclient.js';

esbuild.build(esbuildModuleBuild('build-a318ceo', 'AtsuFmsClient', path.join(__dirname, 'src/index.ts'), outFile));
esbuild.build(createModuleBuild ('build-a318ceo', 'AtsuFmsClient', path.join(__dirname, 'src/index.ts'), outFile, __dirname));
4 changes: 2 additions & 2 deletions hsim-a318ceo/src/systems/extras-host/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const outFile = 'build-a318ceo/out/lvfr-horizonsim-airbus-a318-ceo/html_ui/Pages/VCockpit/Instruments/A318HS/ExtrasHost/index.js';

esbuild.build(esbuildModuleBuild('build-a318ceo', undefined, path.join(__dirname, './index.ts'), outFile));
esbuild.build(createModuleBuild('build-a318ceo', undefined, path.join(__dirname, './index.ts'), outFile, __dirname));
5 changes: 2 additions & 3 deletions hsim-a318ceo/src/systems/fmgc/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');

const { createModuleBuild } = require('#build-utils');
const outFile = 'build-a318ceo/out/lvfr-horizonsim-airbus-a318-ceo/html_ui/JS/A318HS/fmgc/fmgc.js';

esbuild.build(esbuildModuleBuild('build-a318ceo', 'Fmgc', path.join(__dirname, 'src/index.ts'), outFile));
esbuild.build(createModuleBuild('build-a318ceo', 'Fmgc', path.join(__dirname, 'src/index.ts'), outFile, __dirname));
22 changes: 0 additions & 22 deletions hsim-a318ceo/src/systems/instruments/src/Clock/rollup.config.js

This file was deleted.

4 changes: 2 additions & 2 deletions hsim-a318ceo/src/systems/sentry-client/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const outFile = 'build-a318ceo/out/lvfr-horizonsim-airbus-a318-ceo/html_ui/JS/A318HS/sentry-client/sentry-client.js';

esbuild.build(esbuildModuleBuild('build-a318ceo', undefined, path.join(__dirname, 'src/index.ts'), outFile));
esbuild.build(createModuleBuild ('build-a318ceo', undefined, path.join(__dirname, 'src/index.ts'), outFile, __dirname));
4 changes: 2 additions & 2 deletions hsim-a318ceo/src/systems/simbridge-client/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const outFile = 'build-a318ceo/out/lvfr-horizonsim-airbus-a318-ceo/html_ui/JS/A318HS/simbridge-client/simbridge-client.js';

esbuild.build(esbuildModuleBuild('build-a318ceo', 'SimBridgeClient', path.join(__dirname, 'src/index.ts'), outFile));
esbuild.build(createModuleBuild('build-a318ceo', 'SimBridgeClient', path.join(__dirname, 'src/index.ts'), outFile, __dirname));
4 changes: 2 additions & 2 deletions hsim-a318ceo/src/systems/systems-host/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const outFile = 'build-a318ceo/out/lvfr-horizonsim-airbus-a318-ceo/html_ui/Pages/VCockpit/Instruments/A318HS/SystemsHost/index.js';

esbuild.build(esbuildModuleBuild('build-a318ceo', undefined, path.join(__dirname, './index.ts'), outFile));
esbuild.build(createModuleBuild('build-a318ceo', undefined, path.join(__dirname, './index.ts'), outFile, __dirname));
4 changes: 2 additions & 2 deletions hsim-a318ceo/src/systems/tcas/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const outFile = 'build-a318ceo/out/lvfr-horizonsim-airbus-a318-ceo/html_ui/JS/A318HS/tcas/tcas.js';

esbuild.build(esbuildModuleBuild('build-a318ceo', undefined, path.join(__dirname, 'src/index.ts'), outFile));
esbuild.build(createModuleBuild('build-a318ceo', undefined, path.join(__dirname, 'src/index.ts'), outFile, __dirname));
8 changes: 4 additions & 4 deletions hsim-a319-common/src/systems/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"compilerOptions": {
"target": "ESNext",
"target": "ES2017",
"baseUrl": ".",
"resolveJsonModule" : true,
"typeRoots": [
"../typings",
"../../../node_modules/@types"
"../typings"
],
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"jsx": "react",
"skipLibCheck": true,
"paths": {
"@datalink/aoc": ["datalink/aoc/src/index.ts"],
"@datalink/atc": ["datalink/atc/src/index.ts"],
Expand All @@ -20,5 +20,5 @@
"@typings/*": ["../typings/*"],
"@flybywiresim/fbw-sdk": ["./index.ts"]
}
},
}
}
4 changes: 4 additions & 0 deletions hsim-a319ceo/mach.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const postCssPlugin = require('esbuild-style-plugin');
const tailwind = require('tailwindcss');
const postCssColorFunctionalNotation = require('postcss-color-functional-notation');
const postCssInset = require('postcss-inset');
const { typecheckingPlugin } = require("#build-utils");

// process.env.FBW_TYPECHECK = "1";

/** @type { import('@synaptic-simulations/mach').MachConfig } */
module.exports = {
Expand All @@ -24,6 +27,7 @@ module.exports = {
],
}
}),
typecheckingPlugin(),
],
instruments: [
msfsAvionicsInstrument('PFD'),
Expand Down
7 changes: 4 additions & 3 deletions hsim-a319ceo/src/systems/atsu/common/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const rootDir = path.join(__dirname, '..', '..', '..', '..');
const rootDir = path.join(__dirname, '..', '..', '..', '..', '..');
const outFile = 'build-a319ceo/out/lvfr-horizonsim-airbus-a319-ceo/html_ui/JS/A319HS/atsu/common.js';
const srcDir = 'fbw-common/src/systems/datalink/common';

esbuild.build(esbuildModuleBuild('build-a319ceo', 'AtsuCommon', path.join(rootDir, '../fbw-common/src/systems/datalink/common/src/index.ts'), outFile));
esbuild.build(createModuleBuild('build-a319ceo', 'AtsuCommon', path.join(rootDir, srcDir, '/src/index.ts'), outFile, path.join(rootDir, srcDir)));
4 changes: 2 additions & 2 deletions hsim-a319ceo/src/systems/atsu/fmsclient/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const outFile = 'build-a319ceo/out/lvfr-horizonsim-airbus-a319-ceo/html_ui/JS/A319HS/atsu/fmsclient.js';

esbuild.build(esbuildModuleBuild('build-a319ceo', 'AtsuFmsClient', path.join(__dirname, 'src/index.ts'), outFile));
esbuild.build(createModuleBuild ('build-a319ceo', 'AtsuFmsClient', path.join(__dirname, 'src/index.ts'), outFile, __dirname));
4 changes: 2 additions & 2 deletions hsim-a319ceo/src/systems/extras-host/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const outFile = 'build-a319ceo/out/lvfr-horizonsim-airbus-a319-ceo/html_ui/Pages/VCockpit/Instruments/A319HS/ExtrasHost/index.js';

esbuild.build(esbuildModuleBuild('build-a319ceo', undefined, path.join(__dirname, './index.ts'), outFile));
esbuild.build(createModuleBuild('build-a319ceo', undefined, path.join(__dirname, './index.ts'), outFile, __dirname));
4 changes: 2 additions & 2 deletions hsim-a319ceo/src/systems/fmgc/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

const esbuild = require('esbuild');
const path = require('path');
const { esbuildModuleBuild } = require('#build-utils');
const { createModuleBuild } = require('#build-utils');

const outFile = 'build-a319ceo/out/lvfr-horizonsim-airbus-a319-ceo/html_ui/JS/A319HS/fmgc/fmgc.js';

esbuild.build(esbuildModuleBuild('build-a319ceo', 'Fmgc', path.join(__dirname, 'src/index.ts'), outFile));
esbuild.build(createModuleBuild('build-a319ceo', 'Fmgc', path.join(__dirname, 'src/index.ts'), outFile, __dirname));
Loading

0 comments on commit 60ef085

Please sign in to comment.