Skip to content

Commit

Permalink
Node API and CLI (babel#507)
Browse files Browse the repository at this point in the history
* 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
boopathi authored Aug 11, 2017
1 parent 56e87e6 commit dd7dc67
Show file tree
Hide file tree
Showing 17 changed files with 732 additions and 83 deletions.
13 changes: 9 additions & 4 deletions .babelrc
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"]
}
}]
]
]
}
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const OFF = "off";
module.exports = {
extends: "eslint:recommended",
parserOptions: {
ecmaVersion: 7,
ecmaVersion: 2017,
sourceType: "module"
},
env: {
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
".*": "<rootDir>/node_modules/babel-jest"
},
"testEnvironment": "node",
"testPathIgnorePatterns": [
"/node_modules/",
"/fixtures/"
],
"roots": [
"packages"
],
"transformIgnorePatterns": [
"/node_modules/"
],
"coverageDirectory": "./coverage/"
},
"devDependencies": {
Expand All @@ -51,6 +52,7 @@
"codecov": "^2.3.0",
"commander": "^2.11.0",
"eslint": "^4.4.0",
"fs-readdir-recursive": "^1.0.0",
"google-closure-compiler-js": "^20170626.0.0",
"gulp": "github:gulpjs/gulp#4.0",
"gulp-babel": "^6.1.2",
Expand All @@ -62,6 +64,7 @@
"markdown-table": "^1.1.1",
"prettier": "^1.5.3",
"request": "^2.81.0",
"rimraf": "^2.6.1",
"through2": "^2.0.3",
"uglify-js": "^3.0.27"
},
Expand Down
31 changes: 31 additions & 0 deletions packages/babili/__tests__/__snapshots__/cli-tests.js.snap
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)}",
}
`;
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)}"`;
103 changes: 103 additions & 0 deletions packages/babili/__tests__/cli-tests.js
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();
});
});
6 changes: 6 additions & 0 deletions packages/babili/__tests__/fixtures/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
"no-unused-vars": "off",
"no-undef": "off"
}
}
1 change: 1 addition & 0 deletions packages/babili/__tests__/fixtures/out-dir/a/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let foo = 10;
5 changes: 5 additions & 0 deletions packages/babili/__tests__/fixtures/out-file/foo.js
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);
}
26 changes: 26 additions & 0 deletions packages/babili/__tests__/node-api-tests.js
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();
});
});
2 changes: 1 addition & 1 deletion packages/babili/bin/babili.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require("../lib/index");
require("../lib/cli");
13 changes: 8 additions & 5 deletions packages/babili/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
"bin": {
"babili": "./bin/babili.js"
},
"keywords": [
"babel-preset"
],
"keywords": ["babel-preset"],
"dependencies": {
"babel-cli": "^6.24.1",
"babel-preset-babili": "^0.1.4"
"babel-core": "^6.24.1",
"babel-preset-babili": "^0.1.4",
"fs-readdir-recursive": "^1.0.0",
"mkdirp": "^0.5.1",
"output-file-sync": "^2.0.0",
"util.promisify": "^1.0.0",
"yargs-parser": "^5.0.0"
}
}
Loading

0 comments on commit dd7dc67

Please sign in to comment.