5
5
* @import {ConfigTransform, PresetSupportingSpecifiers} from './configuration.js'
6
6
* @import {FileSet} from './file-set.js'
7
7
* @import {ResolveFrom} from './ignore.js'
8
- * @import {Context} from './index.js'
9
8
*/
10
9
11
10
/**
24
23
* Nothing.
25
24
*
26
25
* Note: `void` included because `promisify` otherwise fails.
27
- *
26
+ */
27
+
28
+ /**
29
+ * @typedef {Context & {code: 0 | 1} } ContextWithCode
30
+ * Processing context with code.
31
+ */
32
+
33
+ /**
28
34
* @typedef Context
29
35
* Processing context.
30
36
* @property {FileSet } fileSet
31
37
* Internally used info.
32
38
* @property {Array<VFile> } files
33
39
* Processed files.
34
- *
40
+ */
41
+
42
+ /**
35
43
* @typedef Options
36
44
* Configuration.
37
45
*
140
148
* Whether to output as a syntax tree (default: `options.tree`).
141
149
* @property {boolean | undefined } [verbose=false]
142
150
* Report extra info (default: `false`); given to the reporter.
143
- *
151
+ */
152
+
153
+ /**
144
154
* @typedef Settings
145
155
* Resolved {@link Options `Options`} passed around.
146
156
* @property {Options['processor'] } processor
181
191
* @property {Options['quiet'] } quiet
182
192
* @property {Options['frail'] } frail
183
193
* @property {Options['verbose'] } verbose
184
- *
194
+ */
195
+
196
+ /**
185
197
* @callback VFileReporter
186
198
* Reporter.
187
199
*
193
205
* Configuration.
194
206
* @returns {Promise<string> | string }
195
207
* Report.
196
- *
208
+ */
209
+
210
+ /**
197
211
* @typedef {{[Key in keyof VFileReporterKnownFields]: VFileReporterKnownFields[Key]} & Record<string, unknown> } VFileReporterOptions
198
212
* Configuration.
199
213
*
200
214
* Note: this weird type fixes TSC:
201
215
*/
202
216
217
+ import assert from 'node:assert/strict'
203
218
import process from 'node:process'
204
219
import { PassThrough } from 'node:stream'
205
220
import { fileURLToPath } from 'node:url'
@@ -209,14 +224,57 @@ import {fileSetPipeline} from './file-set-pipeline/index.js'
209
224
/**
210
225
* Process.
211
226
*
227
+ * @overload
228
+ * @param {Options } options
229
+ * @param {Callback } callback
230
+ * @returns {undefined }
231
+ *
232
+ * @overload
233
+ * @param {Options } options
234
+ * @returns {Promise<ContextWithCode> }
235
+ *
236
+ * @overload
237
+ * @param {Options } options
238
+ * @param {Callback | null | undefined } [callback]
239
+ * @returns {Promise<ContextWithCode> | undefined }
240
+ *
212
241
* @param {Options } options
213
242
* Configuration (required).
243
+ * @param {Callback | null | undefined } [callback]
244
+ * Callback.
245
+ * @returns {Promise<ContextWithCode> | undefined }
246
+ * Nothing.
247
+ */
248
+ export function engine ( options , callback ) {
249
+ if ( callback ) {
250
+ return engineCallback ( options , callback )
251
+ }
252
+
253
+ return new Promise ( function ( resolve , reject ) {
254
+ engineCallback ( options , function ( error , code , context ) {
255
+ if ( error ) {
256
+ reject ( error )
257
+ } else {
258
+ assert ( code !== undefined )
259
+ assert ( context !== undefined )
260
+ resolve ( { code, fileSet : context . fileSet , files : context . files } )
261
+ }
262
+ } )
263
+ } )
264
+ }
265
+
266
+ /**
267
+ * Process,
268
+ * always using callbacks.
269
+ *
270
+ * @param {Options } options
271
+ * Configuration.
214
272
* @param {Callback } callback
215
273
* Callback.
216
274
* @returns {undefined }
217
275
* Nothing.
218
276
*/
219
- export function engine ( options , callback ) {
277
+ function engineCallback ( options , callback ) {
220
278
/** @type {Settings } */
221
279
const settings = { }
222
280
/** @type {NodeJS.ReadStream | PassThrough } */
@@ -231,10 +289,6 @@ export function engine(options, callback) {
231
289
// Empty.
232
290
}
233
291
234
- if ( ! callback ) {
235
- throw new Error ( 'Missing `callback`' )
236
- }
237
-
238
292
if ( ! options || ! options . processor ) {
239
293
return next ( new Error ( 'Missing `processor`' ) )
240
294
}
0 commit comments