forked from babel/minify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Node API and CLI update Add yargs-parser Add key not exist case in options parser Remove deadcode Cleanup options parser Improve filtering input CLI options * file handling in cli cli options and fs add out-file, out-dir and accept alias add small validation for cli set up cli tests and help option add help options and fix stdin fix test cases Add tests for cli file/dir and help option add eslintrc for tests ignore fixtures in test suite update package deps small changes for stdin * Async fs Fix up Simplify Fix lint handle default case Fix some tests * move to spawnSync from exec to capture stderr, stdout * Move tests to async Format jest snapshots Fix yarn deps * Add node api tests Remove outFile and outDir from options passed to babili Update snapshot * use stdin stream and api test Update package.json
- Loading branch information
Showing
17 changed files
with
732 additions
and
83 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,9 +1,14 @@ | ||
{ | ||
"presets": [ | ||
["env", { | ||
"targets": { | ||
"node": 4 | ||
[ | ||
"env", | ||
{ | ||
"targets": { | ||
"node": 4 | ||
}, | ||
"include": ["transform-async-to-generator"], | ||
"exclude": ["transform-regenerator"] | ||
} | ||
}] | ||
] | ||
] | ||
} |
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
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,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 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 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 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 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 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 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 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 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.