Skip to content

Commit

Permalink
Add bundle command
Browse files Browse the repository at this point in the history
Issue #610
  • Loading branch information
Manuel Mujica committed Feb 20, 2017
1 parent 0b98e16 commit afa271e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
4 changes: 3 additions & 1 deletion bin/steal
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

var path = require("path");
var cmdBuild = require("../lib/cli/cmd_build");
var cmdBundle = require("../lib/cli/cmd_bundle");
var cmdExport = require("../lib/cli/cmd_export");
var cmdTransform = require("../lib/cli/cmd_transform");
var cmdLiveReload = require("../lib/cli/cmd_live_reload");

var argv = require("yargs")
.command(cmdBuild)
.command(cmdBundle)
.command(cmdTransform)
.command(cmdLiveReload)
.command(cmdExport)
Expand All @@ -24,7 +26,7 @@ if (!argv._[0]) {
var spawn = require("child_process").spawn;
var binary = path.join(__dirname, "steal");
var rawArgs = ["build"].concat(process.argv.slice(2));

if(isWin) {
rawArgs.unshift.apply(rawArgs, ["/c", "node", binary]);
binary = "cmd";
Expand Down
57 changes: 57 additions & 0 deletions lib/cli/cmd_bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var _ = require("lodash");
var winston = require("winston");
var stealTools = require("../../index");
var clone = require("lodash/cloneDeep");
var options = clone(require("./options"));
var makeStealConfig = require("./make_steal_config");

var pathsToOmit = [
"bundles-path",
"bundle-steal",
"watch"
];

var bundleOptions = _.assign(
{},
_.omit(options, pathsToOmit),
{
dest: {
alias: "d",
type: "string",
default: "",
describe: "Defaults to root folder, a directory to save the bundles"
},
filter: {
alias: "f",
type: "string",
default: "node_modules/**/*",
describe: "Glob patter to match modules to be included in the bundle"
}
}
);

module.exports = {
command: "bundle",

describe: "Creates a custom bundle",

builder: bundleOptions,

handler: function(argv) {
var options = argv;
var config = makeStealConfig(argv);

return stealTools.bundle(config, options)
.then(function() {
winston.info("\nBundle created successfully".green);
})
.catch(function(e) {
e = typeof e === "string" ? new Error(e) : e;

winston.error(e.message.red);
winston.error("\nBuild failed".red);

process.exit(1);
});
}
};
2 changes: 1 addition & 1 deletion test/cli/cmd_build_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("cmd build module", function() {
});

it("defaults config option to package.json!npm", function() {
assert(cmdBuild.builder.config, "package.json!npm");
assert.equal(cmdBuild.builder.config.default, "package.json!npm");
});

it("handler calls steal.build", function() {
Expand Down
60 changes: 60 additions & 0 deletions test/cli/cmd_bundle_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var assert = require("assert");
var mockery = require("mockery");

describe("cmd bundle module", function() {
var cmdBundle;
var bundleArgs;
var cmdBundlePath = "../../lib/cli/cmd_bundle";

beforeEach(function() {
bundleArgs = {};

mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
});

mockery.registerAllowable(cmdBundlePath);

mockery.registerMock("../../index", {
bundle: function(system, options) {
bundleArgs.system = system;
bundleArgs.options = options;
return Promise.resolve();
}
});

cmdBundle = require(cmdBundlePath);
});

afterEach(function() {
mockery.disable();
mockery.deregisterAll();
});

it("exposes the right command", function() {
assert.equal(cmdBundle.command, "bundle");
});

it("command options have sensible defaults", function() {
var options = cmdBundle.builder;

assert.equal(options.dest.default, "");
assert.equal(options.config.default, "package.json!npm");
assert.equal(options.filter.default, "node_modules/**/*");
});

it("handler calls steal.bundle", function() {
cmdBundle.handler({
minify: true,
config: "/stealconfig.js"
});

assert.deepEqual(bundleArgs.system, {
config: "/stealconfig.js"
});

assert(bundleArgs.options.minify);
});
});
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require("./recycle_test");
require("./clean_address_test");
require("../lib/bundle/bundle_test");
require("./cli/cmd_build_test");
require("./cli/cmd_bundle_test");
require("./cli/cmd_build_int_test");
require("./cli/cmd_transform_test");
require("./cli/make_steal_config_test");
Expand Down

0 comments on commit afa271e

Please sign in to comment.