-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Add callbacks to cli (#56)
Closes #55 I rename `packages/cli/src/swc/index.ts` to `packages/cli/src/swc/bin.ts`, and export `swcDir` function via new `packages/cli/src/swc/index.ts` add doc here: swc-project/website#261
- Loading branch information
Showing
6 changed files
with
216 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/env node | ||
|
||
process.title = "swc"; | ||
require("../lib/swc"); | ||
require("../lib/swc/bin.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import dirCommand from "./dir"; | ||
import fileCommand from "./file"; | ||
import parseArgs, { initProgram } from "./options"; | ||
|
||
initProgram(); | ||
const opts = parseArgs(process.argv); | ||
const fn = opts.cliOptions.outDir ? dirCommand : fileCommand; | ||
|
||
process.on("uncaughtException", function (err) { | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
|
||
fn(opts).catch((err: Error) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,3 @@ | ||
import dirCommand from "./dir"; | ||
import fileCommand from "./file"; | ||
import parseArgs, { initProgram } from "./options"; | ||
import swcDir from "./dir"; | ||
|
||
initProgram(); | ||
const opts = parseArgs(process.argv); | ||
const fn = opts.cliOptions.outDir ? dirCommand : fileCommand; | ||
|
||
process.on("uncaughtException", function (err) { | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
|
||
fn(opts).catch((err: Error) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
export { swcDir }; |
Oops, something went wrong.