@@ -35,141 +35,12 @@ import colors from 'ansi-colors';
3535// @ts -expect-error
3636import colorSupport from 'color-support' ;
3737
38- import { checkNodeVersion } from '~/config/engine' ;
39- import type { Configs } from '~/config/init' ;
40- import { initConfigs } from '~/config/init' ;
41- import { getLogs } from '~/config/logs' ;
42- import { createClient as pCreateClient } from '~/createClient' ;
43- import {
44- ConfigValidationError ,
45- JobError ,
46- logCrashReport ,
47- openGitHubIssueWithCrashReport ,
48- printCrashReport ,
49- shouldReportCrash ,
50- } from '~/error' ;
51- import type { IR } from '~/ir/types' ;
52- import type { Client } from '~/types/client' ;
5338import type { UserConfig } from '~/types/config' ;
5439import type { LazyOrAsync , MaybeArray } from '~/types/utils' ;
55- import { printCliIntro } from '~/utils/cli' ;
56- import { registerHandlebarTemplates } from '~/utils/handlebars' ;
57- import { Logger } from '~/utils/logger' ;
5840
5941colors . enabled = colorSupport ( ) . hasBasic ;
6042
61- /**
62- * Generate a client from the provided configuration.
63- *
64- * @param userConfig User provided {@link UserConfig} configuration(s).
65- */
66- export const createClient = async (
67- userConfig ?: LazyOrAsync < MaybeArray < UserConfig > > ,
68- logger = new Logger ( ) ,
69- ) : Promise < ReadonlyArray < Client | IR . Context > > => {
70- const resolvedConfig =
71- typeof userConfig === 'function' ? await userConfig ( ) : userConfig ;
72- const userConfigs = resolvedConfig
73- ? resolvedConfig instanceof Array
74- ? resolvedConfig
75- : [ resolvedConfig ]
76- : [ ] ;
77-
78- let rawLogs = userConfigs . find (
79- ( config ) => getLogs ( config ) . level !== 'silent' ,
80- ) ?. logs ;
81- if ( typeof rawLogs === 'string' ) {
82- rawLogs = getLogs ( { logs : rawLogs } ) ;
83- }
84-
85- let configs : Configs | undefined ;
86-
87- try {
88- checkNodeVersion ( ) ;
89-
90- const eventCreateClient = logger . timeEvent ( 'createClient' ) ;
91-
92- const eventConfig = logger . timeEvent ( 'config' ) ;
93- configs = await initConfigs ( { logger, userConfigs } ) ;
94- const printIntro = configs . results . some (
95- ( result ) => result . config . logs . level !== 'silent' ,
96- ) ;
97- if ( printIntro ) {
98- printCliIntro ( ) ;
99- }
100- eventConfig . timeEnd ( ) ;
101-
102- const allConfigErrors = configs . results . flatMap ( ( result ) =>
103- result . errors . map ( ( error ) => ( { error, jobIndex : result . jobIndex } ) ) ,
104- ) ;
105- if ( allConfigErrors . length ) {
106- throw new ConfigValidationError ( allConfigErrors ) ;
107- }
108-
109- const eventHandlebars = logger . timeEvent ( 'handlebars' ) ;
110- const templates = registerHandlebarTemplates ( ) ;
111- eventHandlebars . timeEnd ( ) ;
112-
113- const clients = await Promise . all (
114- configs . results . map ( async ( result ) => {
115- try {
116- return await pCreateClient ( {
117- config : result . config ,
118- dependencies : configs ! . dependencies ,
119- jobIndex : result . jobIndex ,
120- logger,
121- templates,
122- } ) ;
123- } catch ( error ) {
124- throw new JobError ( '' , {
125- error,
126- jobIndex : result . jobIndex ,
127- } ) ;
128- }
129- } ) ,
130- ) ;
131- const result = clients . filter ( ( client ) => Boolean ( client ) ) as ReadonlyArray <
132- Client | IR . Context
133- > ;
134-
135- eventCreateClient . timeEnd ( ) ;
136-
137- const printLogs = configs . results . some (
138- ( result ) => result . config . logs . level === 'debug' ,
139- ) ;
140- logger . report ( printLogs ) ;
141-
142- return result ;
143- } catch ( error ) {
144- const results = configs ?. results ?? [ ] ;
145-
146- const logs =
147- results . find ( ( result ) => result . config . logs . level !== 'silent' ) ?. config
148- . logs ??
149- results [ 0 ] ?. config . logs ??
150- rawLogs ;
151- const dryRun =
152- results . some ( ( result ) => result . config . dryRun ) ??
153- userConfigs . some ( ( config ) => config . dryRun ) ??
154- false ;
155- const logPath =
156- logs ?. file && ! dryRun
157- ? logCrashReport ( error , logs . path ?? '' )
158- : undefined ;
159- if ( ! logs || logs . level !== 'silent' ) {
160- printCrashReport ( { error, logPath } ) ;
161- const isInteractive =
162- results . some ( ( result ) => result . config . interactive ) ??
163- userConfigs . some ( ( config ) => config . interactive ) ??
164- false ;
165- if ( await shouldReportCrash ( { error, isInteractive } ) ) {
166- await openGitHubIssueWithCrashReport ( error ) ;
167- }
168- }
169-
170- throw error ;
171- }
172- } ;
43+ export { createClient } from './generate' ;
17344
17445/**
17546 * Type helper for openapi-ts.config.ts, returns {@link MaybeArray<UserConfig>} object(s)
0 commit comments