Skip to content

Commit

Permalink
Lazy-load dependencies in CLI entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Jul 2, 2023
1 parent 3ec0617 commit a6951cc
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';

import arrify from 'arrify';
import ciParallelVars from 'ci-parallel-vars';
import figures from 'figures';
import yargs from 'yargs';
import {hideBin} from 'yargs/helpers'; // eslint-disable-line n/file-extension-in-import
Expand All @@ -14,16 +12,12 @@ import {chalk} from './chalk.js';
import validateEnvironmentVariables from './environment-variables.js';
import normalizeExtensions from './extensions.js';
import {normalizeGlobs, normalizePattern} from './globs.js';
import {controlFlow} from './ipc-flow-control.cjs';
import isCi from './is-ci.js';
import {splitPatternAndLineNumbers} from './line-numbers.js';
import {loadConfig} from './load-config.js';
import normalizeModuleTypes from './module-types.js';
import normalizeNodeArguments from './node-arguments.js';
import pkg from './pkg.cjs';
import providerManager from './provider-manager.js';
import DefaultReporter from './reporters/default.js';
import TapReporter from './reporters/tap.js';

function exit(message) {
console.error(`\n ${chalk.red(figures.cross)} ${message}`);
Expand Down Expand Up @@ -348,6 +342,7 @@ export default async function loadCli() { // eslint-disable-line complexity

const providers = [];
if (Object.hasOwn(conf, 'typescript')) {
const {default: providerManager} = await import('./provider-manager.js');
try {
const {identifier: protocol, level, main} = await providerManager.typescript(projectDir, {fullConfig: conf});
providers.push({
Expand Down Expand Up @@ -397,9 +392,12 @@ export default async function loadCli() { // eslint-disable-line complexity
}

let parallelRuns = null;
if (isCi && ciParallelVars && combined.utilizeParallelBuilds !== false) {
const {index: currentIndex, total: totalRuns} = ciParallelVars;
parallelRuns = {currentIndex, totalRuns};
if (isCi && combined.utilizeParallelBuilds !== false) {
const {default: ciParallelVars} = await import('ci-parallel-vars');
if (ciParallelVars) {
const {index: currentIndex, total: totalRuns} = ciParallelVars;
parallelRuns = {currentIndex, totalRuns};
}
}

const match = combined.match === '' ? [] : arrify(combined.match);
Expand Down Expand Up @@ -441,29 +439,39 @@ export default async function loadCli() { // eslint-disable-line complexity
workerArgv: argv['--'],
});

const reporter = combined.tap && !argv.watch && debug === null ? new TapReporter({
extensions: globs.extensions,
projectDir,
reportStream: process.stdout,
stdStream: process.stderr,
}) : new DefaultReporter({
extensions: globs.extensions,
projectDir,
reportStream: process.stdout,
stdStream: process.stderr,
watching: argv.watch,
});

api.on('run', plan => {
reporter.startRun(plan);
let reporter;
if (combined.tap && !argv.watch && debug === null) {
const {default: TapReporter} = await import('./reporters/tap.js');
reporter = new TapReporter({
extensions: globs.extensions,
projectDir,
reportStream: process.stdout,
stdStream: process.stderr,
});
} else {
const {default: Reporter} = await import('./reporters/default.js');
reporter = new Reporter({
extensions: globs.extensions,
projectDir,
reportStream: process.stdout,
stdStream: process.stderr,
watching: argv.watch,
});
}

if (process.env.TEST_AVA) {
const bufferedSend = controlFlow(process);
if (process.env.TEST_AVA) {
const {controlFlow} = await import('./ipc-flow-control.cjs');
const bufferedSend = controlFlow(process);

api.on('run', plan => {
plan.status.on('stateChange', evt => {
bufferedSend(evt);
});
}
});
}

api.on('run', plan => {
reporter.startRun(plan);

plan.status.on('stateChange', evt => {
if (evt.type === 'interrupt') {
Expand Down

0 comments on commit a6951cc

Please sign in to comment.