Skip to content

Commit

Permalink
feat: multi file input/output (resolves #21)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 12, 2024
1 parent 40e49d9 commit de6d9c8
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 31 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"defu": "^6.1.4",
"destr": "^2.0.2",
"didyoumean2": "^6.0.1",
"globby": "^14.0.1",
"magic-string": "^0.30.7",
"omark": "^0.1.0",
"pathe": "^1.1.2",
Expand Down
46 changes: 35 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 39 additions & 7 deletions src/automd.ts
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,
};
}
33 changes: 29 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env node

import { relative } from "pathe";
import { defineCommand, runMain } from "citty";
import consola from "consola";
import { getColor } from "consola/utils";
import { name, description, version } from "../package.json";
import { automd } from "./automd";

Expand All @@ -27,20 +29,43 @@ const main = defineCommand({
},
},
async setup({ args }) {
const { updates, config } = await automd({
const fileUpdates = await automd({
dir: args.dir,
input: args.input,
output: args.output,
});

if (updates.length === 0) {
consola.warn(`No updates applied to \`${config.input}\``);
if (fileUpdates.length === 0) {
consola.warn(`No files processed!`);
process.exit(1);
}

consola.success(
`Updated \`${config.input}\` in \`${updates.length}\` sections.`,
`Automd updated in \`${relative(process.cwd(), fileUpdates[0]._config.dir)}\``,
);

const changeTypes = {
updated: { label: "updated", color: getColor("blue") },
noChanges: { label: "no changes", color: getColor("green") },
alreadyUpdate: { label: "already up-to-date", color: getColor("gray") },
};

for (const f of fileUpdates) {
const [input, output] = [f.input, f.output].map((i) =>
relative(f._config.dir, i),
);
const fileStr =
input === output ? ` ${input}` : ` ${input} ~> ${output}`;

const changesStr =

Check warning on line 60 in src/cli.ts

View workflow job for this annotation

GitHub Actions / ci

'changesStr' is assigned a value but never used

Check warning on line 60 in src/cli.ts

View workflow job for this annotation

GitHub Actions / autofix

'changesStr' is assigned a value but never used
f.updates.map((u) => u.block.generator).join(", ") || "-";

const t =
// prettier-ignore
f.updates.length === 0 ? changeTypes.alreadyUpdate : (f.hasChanged ? changeTypes.updated : changeTypes.noChanges);

consola.log(t.color(` ─ ${fileStr} ${t.label}`));
}
},
});

Expand Down
19 changes: 10 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ export interface Config {
dir?: string;

/**
* Name or path of the input file.
* Name or path to the input file or files with glob patterns.
*
* Defaults to `README.md`
* Default is `README.md`.
*/
input?: string;
input?: string | string[];

/**
* Name or path of the output file.
* Name or path of the output files.
*
* Defaults to the same as `input`
* Default output is same as input.
*/
output?: string;

Expand All @@ -31,6 +31,8 @@ const RESOLVED_CONFIG_SYMBOL = Symbol("automdConfig");

export type ResolvedConfig = { [P in keyof Config]-?: Config[P] } & {
[RESOLVED_CONFIG_SYMBOL]: true;
input: string[];
output?: string;
};

export function resolveConfig(
Expand All @@ -49,11 +51,10 @@ export function resolveConfig(
};

_config.dir = resolve(_config.dir);
_config.input = resolve(_config.dir, _config.input);

_config.output = _config.output
? resolve(_config.dir, _config.output)
: _config.input;
_config.input = (
Array.isArray(_config.input) ? _config.input : [_config.input]
).filter(Boolean);

return _config;
}
Expand Down

0 comments on commit de6d9c8

Please sign in to comment.