Skip to content

Commit

Permalink
feat(cli): add callbacks to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
lake2 committed Sep 13, 2024
1 parent 4e79706 commit 605a2b2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
70 changes: 70 additions & 0 deletions packages/cli/src/swc/__tests__/callback.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { swcDir } from "../index";

jest.mock("../compile", () => ({
outputResult: jest.fn(),
}));

let mockComplie: any;

jest.mock("../dirWorker", () => ({
__esModule: true,
default: () => mockComplie(),
}));

const cliOptions: any = {
outDir: "./.temp/",
watch: false,
filenames: ["./src/swcx/"],
extensions: [".ts"],
stripLeadingPaths: true,
sync: true,
};
const swcOptions: any = {
jsc: {
target: "esnext",
externalHelpers: false,
},
module: {
type: "commonjs",
},
};

describe("dir callbacks", () => {
it("onSuccess should be called", async () => {
mockComplie = () => Promise.resolve(1); // mock complie success

const onSuccess = jest.fn();
const onFail = jest.fn();

await swcDir({
cliOptions: cliOptions,
swcOptions: swcOptions,
callbacks: {
onSuccess,
onFail,
},
});

expect(onSuccess.mock.calls).toHaveLength(1);
expect(onFail.mock.calls).toHaveLength(0);
});

it("onFail should be called", async () => {
mockComplie = () => Promise.reject(new Error("fail")); // mock complie fail

const onSuccess = jest.fn();
const onFail = jest.fn();

await swcDir({
cliOptions: cliOptions,
swcOptions: swcOptions,
callbacks: {
onSuccess,
onFail,
},
});

expect(onSuccess.mock.calls).toHaveLength(0);
expect(onFail.mock.calls).toHaveLength(1);
});
});
8 changes: 6 additions & 2 deletions packages/cli/src/swc/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ async function initialCompilation(
});
results.set(filename, result);
} catch (err: any) {
console.error(err.message);
if (!callbacks?.onFail) {
console.error(err.message);
}
results.set(filename, CompileStatus.Failed);
}
}
Expand All @@ -114,7 +116,9 @@ async function initialCompilation(
);
results.set(filename, result);
} catch (err: any) {
console.error(err.message);
if (!callbacks?.onFail) {
console.error(err.message);
}
results.set(filename, CompileStatus.Failed);
}
}
Expand Down

0 comments on commit 605a2b2

Please sign in to comment.