Skip to content

Commit

Permalink
refactor: split parse utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 7, 2024
1 parent 33e440f commit 2381934
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
37 changes: 37 additions & 0 deletions src/_parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { destr } from "destr";

const AUTOMD_RE =
/^(?<open><!--\s*AUTOMD_START\s*(?<args>.*?)\s*-->)(?<contents>.+?)(?<close><!--\s*AUTOMD_END\s*-->)/gms;

export function findAutoMdBlocks(md: string) {
const blocks: {
loc: { start: number; end: number };
rawArgs: string;
contents: string;
}[] = [];

for (const match of md.matchAll(AUTOMD_RE)) {
if (match.index === undefined || !match.groups) {
continue;
}

const start = match.index + match.groups.open.length;
const end = start + match.groups.contents.length;

blocks.push({
loc: { start, end },
rawArgs: match.groups.args,
contents: match.groups.contents,
});
}

return blocks;
}

export function parseRawArgs(rawArgs: string) {
return Object.fromEntries(
[...rawArgs.matchAll(/(?<key>\w+)=?(["'])?(?<value>[^\2]+?)\2/g)].map(
(m) => [m.groups?.key, destr(m.groups?.value)],
),
);
}
3 changes: 3 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { readPackageJSON } from "pkg-types";
import _consola from "consola";

export const consola = _consola.withTag("automd");

export async function getPkg(
dir: string,
Expand Down
46 changes: 16 additions & 30 deletions src/automd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { readFile, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import { existsSync } from "node:fs";
import MagicString from "magic-string";
import { destr } from "destr";
import generators from "./generators";
import { GenerateContext, GenerateResult } from "./generator";

const AUTOMD_RE =
/^(?<open><!--\s*AUTOMD_START\s*(?<args>.*?)\s*-->)(?<contents>.+?)(?<close><!--\s*AUTOMD_END\s*-->)/gms;
import { findAutoMdBlocks, parseRawArgs } from "./_parse";
import { consola } from "./_utils";

export interface AutoMDOptions {
/**
Expand Down Expand Up @@ -50,49 +48,37 @@ export async function automd(_options: Partial<AutoMDOptions> = {}) {
const fileEditor = new MagicString(fileContents);

type UpdateEntry = {
block: ReturnType<typeof findAutoMdBlocks>[0];
generatorName: string;
context: GenerateContext;
loc: { start: number; end: number };
};
const updates: UpdateEntry[] = [];

for (const match of fileContents.matchAll(AUTOMD_RE)) {
if (match.index === undefined || !match.groups) {
continue;
}

const start = match.index + match.groups.open.length;
const end = start + match.groups.contents.length;

const args = Object.fromEntries(
[
...match.groups.args.matchAll(/(?<key>\w+)=(["'])(?<value>[^\2]+?)\2/g),
].map((m) => [m.groups?.key, destr(m.groups?.value)]),
);

const blocks = findAutoMdBlocks(fileContents);
for (const block of blocks) {
const args = parseRawArgs(block.rawArgs);
const generatorName = args.generator;
const generator = generators[generatorName];
if (!generator) {
// TODO: Warn?
consola.warn(`Unknown generator: \`${generatorName}\``);
continue;
}

const generateContext: GenerateContext = {
const context: GenerateContext = {
args,
options,
oldContents: match.groups.contents,
oldContents: block.contents,
};

const generateResult: GenerateResult =
await generator.generate(generateContext);
const generateResult: GenerateResult = await generator.generate(context);

updates.push({
generatorName,
context: generateContext,
loc: { start, end },
});
updates.push({ block, context, generatorName });

fileEditor.overwrite(start, end, `\n\n${generateResult.contents}\n\n`);
fileEditor.overwrite(
block.loc.start,
block.loc.end,
`\n\n${generateResult.contents}\n\n`,
);
}

if (updates.length > 0 && fileEditor.hasChanged()) {
Expand Down

0 comments on commit 2381934

Please sign in to comment.