-
-
Notifications
You must be signed in to change notification settings - Fork 219
Node API and CLI #507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Node API and CLI #507
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
43e00b5
Node API and CLI
boopathi 956ecb1
file handling in cli
vigneshshanmugam 4e6b35e
Async fs
boopathi ac6cd49
move to spawnSync from exec to capture stderr, stdout
vigneshshanmugam b158348
Move tests to async
boopathi 7d8ee55
Add node api tests
boopathi 82dfd83
use stdin stream and api test
vigneshshanmugam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,9 +1,14 @@ | ||
| { | ||
| "presets": [ | ||
| ["env", { | ||
| "targets": { | ||
| "node": 4 | ||
| [ | ||
| "env", | ||
| { | ||
| "targets": { | ||
| "node": 4 | ||
| }, | ||
| "include": ["transform-async-to-generator"], | ||
| "exclude": ["transform-regenerator"] | ||
| } | ||
| }] | ||
| ] | ||
| ] | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`Babili CLI input dir + outdir 1`] = `"let foo=10;"`; | ||
|
|
||
| exports[`Babili CLI input file + outDir 1`] = `"function foo(){const a=x(1),b=y(2);return z(a,b)}"`; | ||
|
|
||
| exports[`Babili CLI input file + outFile 1`] = `"function foo(){const a=x(1),b=y(2);return z(a,b)}"`; | ||
|
|
||
| exports[`Babili CLI input file + stdout 1`] = ` | ||
| Object { | ||
| "stderr": "", | ||
| "stdout": "function foo(){const a=x(1),b=y(2);return z(a,b)}", | ||
| } | ||
| `; | ||
|
|
||
| exports[`Babili CLI should throw on all invalid options 1`] = ` | ||
| Object { | ||
| "code": 1, | ||
| "stderr": "Error: Invalid Options passed: foo,bar | ||
| ", | ||
| } | ||
| `; | ||
|
|
||
| exports[`Babili CLI stdin + outFile 1`] = `"function foo(){const a=x(1),b=y(2);return z(a,b)}"`; | ||
|
|
||
| exports[`Babili CLI stdin + stdout 1`] = ` | ||
| Object { | ||
| "stderr": "", | ||
| "stdout": "function a(){const a=x(1),b=y(2);return z(a,b)}", | ||
| } | ||
| `; |
5 changes: 5 additions & 0 deletions
5
packages/babili/__tests__/__snapshots__/node-api-tests.js.snap
This file contains hidden or 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,5 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`Babili Node API override default babili options 1`] = `"function foo(){const bar=x(1),baz=y(2);return z(bar,baz)}"`; | ||
|
|
||
| exports[`Babili Node API simple usage 1`] = `"function foo(){const a=x(1),b=y(2);return z(a,b)}"`; |
This file contains hidden or 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,103 @@ | ||
| jest.autoMockOff(); | ||
|
|
||
| const { spawn } = require("child_process"); | ||
| const path = require("path"); | ||
| const fs = require("fs"); | ||
| const promisify = require("util.promisify"); | ||
| const rimraf = require("rimraf"); | ||
| const babiliCli = require.resolve("../bin/babili"); | ||
|
|
||
| const readFileAsync = promisify(fs.readFile); | ||
| const readFile = file => readFileAsync(file).then(out => out.toString()); | ||
| const unlink = promisify(rimraf); | ||
|
|
||
| function runCli(args = [], stdin) { | ||
| return new Promise((resolve, reject) => { | ||
| const child = spawn(babiliCli, args, { | ||
| stdio: [stdin ? "pipe" : "inherit", "pipe", "pipe"], | ||
| shell: true | ||
| }); | ||
|
|
||
| if (stdin) { | ||
| child.stdin.end(stdin); | ||
| } | ||
|
|
||
| let stdout = ""; | ||
| let stderr = ""; | ||
|
|
||
| child.stdout.on("data", data => (stdout += data)); | ||
| child.stderr.on("data", data => (stderr += data)); | ||
| child.on( | ||
| "close", | ||
| code => | ||
| code === 0 ? resolve({ stdout, stderr }) : reject({ code, stderr }) | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| let tempSource = ` | ||
| function foo() { | ||
| const bar = x(1); | ||
| const baz = y(2); | ||
| return z(bar, baz); | ||
| } | ||
| `; | ||
|
|
||
| const sampleInputFile = path.join(__dirname, "fixtures/out-file/foo.js"); | ||
| const sampleInputDir = path.join(__dirname, "fixtures/out-dir/a"); | ||
|
|
||
| const tempOutFile = path.join(__dirname, "fixtures/out-file/foo.min.js"); | ||
| const tempOutDir = path.join(__dirname, "fixtures/out-dir/min"); | ||
| const tempOutDirFile = path.join(__dirname, "fixtures/out-dir/min/foo.js"); | ||
|
|
||
| describe("Babili CLI", () => { | ||
| afterEach(async () => { | ||
| await unlink(tempOutDir); | ||
| await unlink(tempOutFile); | ||
| }); | ||
|
|
||
| it("should show help for --help", () => { | ||
| return expect(runCli(["--help"])).resolves.toBeDefined(); | ||
| }); | ||
|
|
||
| it("should show version for --version", () => { | ||
| const { version } = require("../package"); | ||
| return expect( | ||
| runCli(["--version"]).then(({ stdout }) => stdout.trim()) | ||
| ).resolves.toBe(version); | ||
| }); | ||
|
|
||
| it("should throw on all invalid options", () => { | ||
| return expect(runCli(["--foo", "--bar"])).rejects.toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it("stdin + stdout", () => { | ||
| return expect( | ||
| runCli(["--mangle.topLevel"], tempSource) | ||
| ).resolves.toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it("stdin + outFile", async () => { | ||
| await runCli(["--out-file", tempOutFile], tempSource); | ||
| expect(await readFile(tempOutFile)).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it("input file + stdout", async () => { | ||
| return expect(runCli([sampleInputFile])).resolves.toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it("input file + outFile", async () => { | ||
| await runCli([sampleInputFile, "--out-file", tempOutFile]); | ||
| expect(await readFile(tempOutFile)).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it("input file + outDir", async () => { | ||
| await runCli([sampleInputFile, "--out-dir", tempOutDir]); | ||
| expect(await readFile(tempOutDirFile)).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it("input dir + outdir", async () => { | ||
| await runCli([sampleInputDir, "--out-dir", tempOutDir]); | ||
| expect(await readFile(tempOutDirFile)).toMatchSnapshot(); | ||
| }); | ||
| }); |
This file contains hidden or 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,6 @@ | ||
| { | ||
| "rules": { | ||
| "no-unused-vars": "off", | ||
| "no-undef": "off" | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| let foo = 10; |
This file contains hidden or 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,5 @@ | ||
| function foo() { | ||
| const bar = x(1); | ||
| const baz = y(2); | ||
| return z(bar, baz); | ||
| } |
This file contains hidden or 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,26 @@ | ||
| jest.autoMockOff(); | ||
|
|
||
| const babili = require("../src/index"); | ||
|
|
||
| const sampleInput = ` | ||
| function foo() { | ||
| const bar = x(1); | ||
| const baz = y(2); | ||
| return z(bar, baz); | ||
| } | ||
| `; | ||
|
|
||
| describe("Babili Node API", () => { | ||
| it("simple usage", () => { | ||
| expect(babili(sampleInput).code).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it("throw on invalid options", () => { | ||
| expect(() => babili(sampleInput, { foo: false, bar: true }).code).toThrow(); | ||
| }); | ||
|
|
||
| it("override default babili options", () => { | ||
| const babiliOpts = { mangle: false }; | ||
| expect(babili(sampleInput, babiliOpts).code).toMatchSnapshot(); | ||
| }); | ||
| }); |
This file contains hidden or 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,3 +1,3 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| require("../lib/index"); | ||
| require("../lib/cli"); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not required. default is node_modules