-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multi file input/output (resolves #21)
- Loading branch information
Showing
5 changed files
with
114 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,59 @@ | ||
import { existsSync, promises as fsp } from "node:fs"; | ||
import { resolve, relative } from "pathe"; | ||
import type { Config, ResolvedConfig } from "./config"; | ||
import { TransformResult, transform } from "./transform"; | ||
import { loadConfig } from "./config"; | ||
|
||
export interface AutomdResult extends TransformResult { | ||
config: ResolvedConfig; | ||
_config: ResolvedConfig; | ||
input: string; | ||
output: string; | ||
} | ||
|
||
export async function automd(_config: Config = {}): Promise<AutomdResult> { | ||
export async function automd(_config: Config = {}): Promise<AutomdResult[]> { | ||
const config = await loadConfig(_config.dir, _config); | ||
|
||
if (!existsSync(config.input)) { | ||
throw new Error(`File not found: ${config.input}`); | ||
let inputFiles = config.input; | ||
if (inputFiles.some((i) => i.includes("*"))) { | ||
const { globby } = await import("globby"); | ||
inputFiles = await globby(inputFiles, { | ||
cwd: config.dir, | ||
absolute: false, | ||
onlyFiles: true, | ||
ignore: ["node_modules", "dist"], | ||
}); | ||
} else { | ||
inputFiles = inputFiles | ||
.map((i) => resolve(config.dir, i)) | ||
.filter((i) => existsSync(i)) | ||
.map((i) => relative(config.dir, i)); | ||
} | ||
|
||
const contents = await fsp.readFile(config.input, "utf8"); | ||
return Promise.all( | ||
inputFiles.map((i) => _automd(i, config, inputFiles.length > 1)), | ||
); | ||
} | ||
|
||
export async function _automd( | ||
relativeInput: string, | ||
config: ResolvedConfig, | ||
multi: boolean, | ||
): Promise<AutomdResult> { | ||
const input = resolve(config.dir, relativeInput); | ||
const contents = await fsp.readFile(input, "utf8"); | ||
|
||
const result = await transform(contents, config); | ||
|
||
await fsp.writeFile(config.output, result.contents, "utf8"); | ||
const output = multi | ||
? resolve(config.dir, config.output || ".", relativeInput) | ||
: resolve(config.dir, config.output || relativeInput); | ||
|
||
await fsp.writeFile(output, result.contents, "utf8"); | ||
|
||
return { | ||
config, | ||
_config: config, | ||
input, | ||
output, | ||
...result, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters