Files
bun.sh/examples/macros/matchInFile.tsx
Jarred Sumner e51155d8e6 Remove
2021-09-27 21:25:06 -07:00

24 lines
555 B
TypeScript

// macro code
export 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 = Bun.readFile(Bun.cwd + filePath);
return (
<array>
{file
.split("\n")
.map((line) => line.match(matcher))
.filter(Boolean)
.reverse()
.map((line) => (
<string value={line[0]} />
))}
</array>
);
}