1 |
|
2 |
|
3 | import gensync from "gensync";
|
4 |
|
5 | import loadConfig, { type InputOptions, type ResolvedConfig } from "./config";
|
6 | import {
|
7 | run,
|
8 | type FileResult,
|
9 | type FileResultCallback,
|
10 | } from "./transformation";
|
11 | import * as fs from "./gensync-utils/fs";
|
12 |
|
13 | import typeof * as transformFileBrowserType from "./transform-file-browser";
|
14 | import typeof * as transformFileType from "./transform-file";
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | ((({}: any): $Exact<transformFileBrowserType>): $Exact<transformFileType>);
|
20 |
|
21 | type TransformFile = {
|
22 | (filename: string, callback: FileResultCallback): void,
|
23 | (filename: string, opts: ?InputOptions, callback: FileResultCallback): void,
|
24 | };
|
25 |
|
26 | const transformFileRunner = gensync<[string, ?InputOptions], FileResult | null>(
|
27 | function* (filename, opts) {
|
28 | const options = { ...opts, filename };
|
29 |
|
30 | const config: ResolvedConfig | null = yield* loadConfig(options);
|
31 | if (config === null) return null;
|
32 |
|
33 | const code = yield* fs.readFile(filename, "utf8");
|
34 | return yield* run(config, code);
|
35 | },
|
36 | );
|
37 |
|
38 | export const transformFile: TransformFile = transformFileRunner.errback;
|
39 | export const transformFileSync = transformFileRunner.sync;
|
40 | export const transformFileAsync = transformFileRunner.async;
|