Skip to content

Commit 2381934

Browse files
committed
refactor: split parse utils
1 parent 33e440f commit 2381934

File tree

3 files changed

+56
-30
lines changed

3 files changed

+56
-30
lines changed

src/_parse.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { destr } from "destr";
2+
3+
const AUTOMD_RE =
4+
/^(?<open><!--\s*AUTOMD_START\s*(?<args>.*?)\s*-->)(?<contents>.+?)(?<close><!--\s*AUTOMD_END\s*-->)/gms;
5+
6+
export function findAutoMdBlocks(md: string) {
7+
const blocks: {
8+
loc: { start: number; end: number };
9+
rawArgs: string;
10+
contents: string;
11+
}[] = [];
12+
13+
for (const match of md.matchAll(AUTOMD_RE)) {
14+
if (match.index === undefined || !match.groups) {
15+
continue;
16+
}
17+
18+
const start = match.index + match.groups.open.length;
19+
const end = start + match.groups.contents.length;
20+
21+
blocks.push({
22+
loc: { start, end },
23+
rawArgs: match.groups.args,
24+
contents: match.groups.contents,
25+
});
26+
}
27+
28+
return blocks;
29+
}
30+
31+
export function parseRawArgs(rawArgs: string) {
32+
return Object.fromEntries(
33+
[...rawArgs.matchAll(/(?<key>\w+)=?(["'])?(?<value>[^\2]+?)\2/g)].map(
34+
(m) => [m.groups?.key, destr(m.groups?.value)],
35+
),
36+
);
37+
}

src/_utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { readPackageJSON } from "pkg-types";
2+
import _consola from "consola";
3+
4+
export const consola = _consola.withTag("automd");
25

36
export async function getPkg(
47
dir: string,

src/automd.ts

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { readFile, writeFile } from "node:fs/promises";
22
import { resolve } from "node:path";
33
import { existsSync } from "node:fs";
44
import MagicString from "magic-string";
5-
import { destr } from "destr";
65
import generators from "./generators";
76
import { GenerateContext, GenerateResult } from "./generator";
8-
9-
const AUTOMD_RE =
10-
/^(?<open><!--\s*AUTOMD_START\s*(?<args>.*?)\s*-->)(?<contents>.+?)(?<close><!--\s*AUTOMD_END\s*-->)/gms;
7+
import { findAutoMdBlocks, parseRawArgs } from "./_parse";
8+
import { consola } from "./_utils";
119

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

5250
type UpdateEntry = {
51+
block: ReturnType<typeof findAutoMdBlocks>[0];
5352
generatorName: string;
5453
context: GenerateContext;
55-
loc: { start: number; end: number };
5654
};
5755
const updates: UpdateEntry[] = [];
5856

59-
for (const match of fileContents.matchAll(AUTOMD_RE)) {
60-
if (match.index === undefined || !match.groups) {
61-
continue;
62-
}
63-
64-
const start = match.index + match.groups.open.length;
65-
const end = start + match.groups.contents.length;
66-
67-
const args = Object.fromEntries(
68-
[
69-
...match.groups.args.matchAll(/(?<key>\w+)=(["'])(?<value>[^\2]+?)\2/g),
70-
].map((m) => [m.groups?.key, destr(m.groups?.value)]),
71-
);
72-
57+
const blocks = findAutoMdBlocks(fileContents);
58+
for (const block of blocks) {
59+
const args = parseRawArgs(block.rawArgs);
7360
const generatorName = args.generator;
7461
const generator = generators[generatorName];
7562
if (!generator) {
76-
// TODO: Warn?
63+
consola.warn(`Unknown generator: \`${generatorName}\``);
7764
continue;
7865
}
7966

80-
const generateContext: GenerateContext = {
67+
const context: GenerateContext = {
8168
args,
8269
options,
83-
oldContents: match.groups.contents,
70+
oldContents: block.contents,
8471
};
8572

86-
const generateResult: GenerateResult =
87-
await generator.generate(generateContext);
73+
const generateResult: GenerateResult = await generator.generate(context);
8874

89-
updates.push({
90-
generatorName,
91-
context: generateContext,
92-
loc: { start, end },
93-
});
75+
updates.push({ block, context, generatorName });
9476

95-
fileEditor.overwrite(start, end, `\n\n${generateResult.contents}\n\n`);
77+
fileEditor.overwrite(
78+
block.loc.start,
79+
block.loc.end,
80+
`\n\n${generateResult.contents}\n\n`,
81+
);
9682
}
9783

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

0 commit comments

Comments
 (0)