55 * @import {ConfigTransform, PresetSupportingSpecifiers} from './configuration.js'
66 * @import {FileSet} from './file-set.js'
77 * @import {ResolveFrom} from './ignore.js'
8- * @import {Context} from './index.js'
98 */
109
1110/**
2423 * Nothing.
2524 *
2625 * 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+ /**
2834 * @typedef Context
2935 * Processing context.
3036 * @property {FileSet } fileSet
3137 * Internally used info.
3238 * @property {Array<VFile> } files
3339 * Processed files.
34- *
40+ */
41+
42+ /**
3543 * @typedef Options
3644 * Configuration.
3745 *
140148 * Whether to output as a syntax tree (default: `options.tree`).
141149 * @property {boolean | undefined } [verbose=false]
142150 * Report extra info (default: `false`); given to the reporter.
143- *
151+ */
152+
153+ /**
144154 * @typedef Settings
145155 * Resolved {@link Options `Options`} passed around.
146156 * @property {Options['processor'] } processor
181191 * @property {Options['quiet'] } quiet
182192 * @property {Options['frail'] } frail
183193 * @property {Options['verbose'] } verbose
184- *
194+ */
195+
196+ /**
185197 * @callback VFileReporter
186198 * Reporter.
187199 *
193205 * Configuration.
194206 * @returns {Promise<string> | string }
195207 * Report.
196- *
208+ */
209+
210+ /**
197211 * @typedef {{[Key in keyof VFileReporterKnownFields]: VFileReporterKnownFields[Key]} & Record<string, unknown> } VFileReporterOptions
198212 * Configuration.
199213 *
200214 * Note: this weird type fixes TSC:
201215 */
202216
217+ import assert from 'node:assert/strict'
203218import process from 'node:process'
204219import { PassThrough } from 'node:stream'
205220import { fileURLToPath } from 'node:url'
@@ -209,14 +224,57 @@ import {fileSetPipeline} from './file-set-pipeline/index.js'
209224/**
210225 * Process.
211226 *
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+ *
212241 * @param {Options } options
213242 * 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.
214272 * @param {Callback } callback
215273 * Callback.
216274 * @returns {undefined }
217275 * Nothing.
218276 */
219- export function engine ( options , callback ) {
277+ function engineCallback ( options , callback ) {
220278 /** @type {Settings } */
221279 const settings = { }
222280 /** @type {NodeJS.ReadStream | PassThrough } */
@@ -231,10 +289,6 @@ export function engine(options, callback) {
231289 // Empty.
232290 }
233291
234- if ( ! callback ) {
235- throw new Error ( 'Missing `callback`' )
236- }
237-
238292 if ( ! options || ! options . processor ) {
239293 return next ( new Error ( 'Missing `processor`' ) )
240294 }
0 commit comments