import type {Async} from '@atlaspack/types'; import type {SharedReference} from '@atlaspack/workers'; import type {StaticRunOpts} from '../RequestTracker'; import type {AssetGroup} from '../types'; import type {ConfigAndCachePath} from './AtlaspackConfigRequest'; import nullthrows from 'nullthrows'; import {AtlaspackConfig} from '../AtlaspackConfig'; import {report} from '../ReporterRunner'; import Validation from '../Validation'; import createAtlaspackConfigRequest from './AtlaspackConfigRequest'; import {requestTypes} from '../RequestTracker'; type ValidationRequest = { id: string; readonly type: typeof requestTypes.validation_request; run: (arg1: RunOpts) => Async; input: ValidationRequestInput; }; type RunOpts = { input: ValidationRequestInput; } & StaticRunOpts; type ValidationRequestInput = { assetRequests: Array; optionsRef: SharedReference; }; export default function createValidationRequest( input: ValidationRequestInput, ): ValidationRequest { return { id: 'validation', type: requestTypes.validation_request, run: async ({input: {assetRequests, optionsRef}, api, options, farm}) => { let {config: processedConfig, cachePath} = nullthrows( await api.runRequest( createAtlaspackConfigRequest(), ), ); let config = new AtlaspackConfig(processedConfig, options); let trackedRequestsDesc = assetRequests.filter((request) => { // @ts-expect-error TS2345 return config.getValidatorNames(request.filePath).length > 0; }); // Schedule validations on workers for all plugins that implement the one-asset-at-a-time "validate" method. let promises = trackedRequestsDesc.map( async (request) => (await farm.createHandle('runValidate'))({ requests: [request], optionsRef: optionsRef, configCachePath: cachePath, }) as undefined, ); // Skip sending validation requests if no validators were configured if (trackedRequestsDesc.length === 0) { return; } // Schedule validations on the main thread for all validation plugins that implement "validateAll". promises.push( // @ts-expect-error TS2345 new Validation({ requests: trackedRequestsDesc, options, config, // @ts-expect-error TS2322 report, dedicatedThread: true, }).run(), ); await Promise.all(promises); }, input, }; }