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