Skip to content

Commit

Permalink
feat(scripts-tasks): implement ~36% faster type-check task (#31116)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell authored Apr 23, 2024
1 parent 4a551ed commit eafbba4
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions scripts/tasks/src/type-check.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
import * as fs from 'fs';

import { logger } from 'just-scripts';
// eslint-disable-next-line import/no-extraneous-dependencies
import { exec } from 'just-scripts-utils';

import { getTsPathAliasesConfig } from './utils';
import { type TsConfig, getTsPathAliasesConfig } from './utils';

export function typeCheck() {
const { isUsingTsSolutionConfigs, tsConfigFileContents, tsConfigs, tsConfigFilePaths } = getTsPathAliasesConfig();
export async function typeCheck() {
const { isUsingTsSolutionConfigs, tsConfigs } = getTsPathAliasesConfig();

if (!isUsingTsSolutionConfigs) {
logger.info('project is not using TS solution config. skipping...');
return;
}

const content = tsConfigFileContents.root;
const config = tsConfigs.root;
const configPath = tsConfigFilePaths.root;

if (!(content && config)) {
if (!config) {
return;
}

// turn off path aliases.
// @ts-expect-error - bad just-scripts ts types
config.compilerOptions.paths = {};
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');

const cmd = 'tsc';
const args = ['-b', '--pretty', configPath];
const program = `${cmd} ${args.join(' ')}`;

return exec(program)
.catch(err => {
console.error(err.stdout);
// restore original tsconfig.json
fs.writeFileSync(configPath, content, 'utf-8');
process.exit(1);
})
.finally(() => {
// restore original tsconfig.json
fs.writeFileSync(configPath, content, 'utf-8');
});
const tsConfigsRefs = getTsConfigs(config, { spec: false, e2e: false });

const asyncQueue = [];

for (const ref of tsConfigsRefs) {
const program = `tsc -p ${ref} --pretty --baseUrl .`;
asyncQueue.push(exec(program));
}

return Promise.all(asyncQueue).catch(err => {
console.error(err.stdout);
process.exit(1);
});
}

function getTsConfigs(solutionConfig: TsConfig, exclude: { spec: boolean; e2e: boolean }) {
const refs = solutionConfig.references ?? [];
const refsPaths: string[] = [];

for (const ref of refs) {
if (exclude.spec && ref.path.includes('spec')) {
continue;
}
if (exclude.e2e && ref.path.includes('cy')) {
continue;
}

refsPaths.push(ref.path);
}

return refsPaths;
}

0 comments on commit eafbba4

Please sign in to comment.