Skip to content

Commit

Permalink
Use dynamic import rather than require() for loading formatters (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager authored Jun 26, 2020
1 parent c917e5b commit 63eec5a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/alfa-cli/bin/alfa/command/audit/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const run: Command.Runner<typeof Flags, typeof Arguments> = async ({
flags,
args: { url: target },
}) => {
const formatter = Formatter.load<Input, Target, Question>(flags.format);
const formatter = await Formatter.load<Input, Target, Question>(flags.format);

if (formatter.isErr()) {
return formatter;
Expand Down
3 changes: 1 addition & 2 deletions packages/alfa-formatter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
],
"dependencies": {
"@siteimprove/alfa-act": "^0.2.0",
"@siteimprove/alfa-result": "^0.2.0",
"@types/node": "^14.0.11"
"@siteimprove/alfa-result": "^0.2.0"
},
"devDependencies": {
"@siteimprove/alfa-test": "^0.2.0"
Expand Down
14 changes: 7 additions & 7 deletions packages/alfa-formatter/src/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/// <reference types="node" />

import { Outcome } from "@siteimprove/alfa-act";
import { Result, Ok, Err } from "@siteimprove/alfa-result";
import { Result, Err } from "@siteimprove/alfa-result";

export type Formatter<I, T, Q> = (
export type Formatter<I, T = unknown, Q = never> = (
input: I,
outcomes: Iterable<Outcome<I, T, Q>>
) => string;

export namespace Formatter {
export function load<I, T, Q>(
export async function load<I, T = unknown, Q = never>(
name: string,
defaultScope: string = "@siteimprove"
): Result<Formatter<I, T, Q>, string> {
): Promise<Result<Formatter<I, T, Q>, string>> {
let scope: string | undefined;

if (name.startsWith("@")) {
Expand All @@ -32,7 +30,9 @@ export namespace Formatter {

for (const pattern of patterns) {
try {
return Ok.of(require(pattern).default());
const module = await import(pattern);

return Result.of(module.default());
} catch {
continue;
}
Expand Down

0 comments on commit 63eec5a

Please sign in to comment.