aboutsummaryrefslogtreecommitdiff
path: root/examples/macros/matchInFile.tsx
blob: 4793661d99addf21c10ae3e020a95981554fabf0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// macro code
export async function matchInFile(callExpression: BunAST.CallExpression) {
  const [filePathNode, matcherNode] = callExpression.arguments;
  let filePath: string;
  filePath = filePathNode.get();

  let matcher: RegExp;
  matcher = matcherNode.get();
  const file: string = await Bun.file(Bun.cwd + filePath).text();

  return (
    <array>
      {file
        .split("\n")
        .map(line => line.match(matcher))
        .filter(Boolean)
        .reverse()
        .map(line => (
          <string value={line[0]} />
        ))}
    </array>
  );
}