Skip to content

Commit

Permalink
Merge pull request #212 from stealjs/better-cli
Browse files Browse the repository at this point in the history
Improvements to the cli
  • Loading branch information
matthewp committed Apr 6, 2015
2 parents 05ec9e3 + 51c7470 commit 5572f65
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,6 +9,7 @@ bower_components
test/out/
test/bundle/bundles/
test/pluginify/out.js
test/pluginify/out.js.map
test/nocallback/out.js
test/dep_plugins/bundles
test/bundles/
Expand Down
48 changes: 25 additions & 23 deletions bin/steal
Expand Up @@ -3,22 +3,29 @@ var winston = require('winston');
var path = require("path");
var yargs = require("yargs")
.usage("Usage: $0 [build|transform] --main app --config path/to/stealconfig.js")
.demand(["config", "main"])
.describe("config", "Path to the config file")
.describe("main", "The application's entry point module")
.describe("c", "Path to the config file")
.alias("c", "config")
.default("c", "package.json!npm")
.describe("m", "The application's entry point module")
.alias("m", "main")
.describe("bundles-path", "Defaults to dist/bundles, a directory to save the bundles")
.describe("bundle-steal", "Include steal.js in your main bundled JavaScript")
.describe("ignore", "For transform, a comma-separated list of modules to not include in the output")
.describe("out", "For transform, specify an output file")
.describe("o", "For transform, specify an output file")
.alias("o", "out")
.describe("source-maps", "Generate source maps")
.describe("source-maps-content", "Include the original source contents with the source maps")
.describe("no-minify", "Do not minify the output")
.describe("verbose", "Verbose output")
.describe("quiet", "Quiet output")
.version(require("../package.json").version, "version")
.help("h")
.alias("h", "help")
.check(function(argv){
var command = argv._[0];

if(!command) {
throw "Must provide a command to execute";
command = "build";
}

if(command !== "build" && command !== "transform") {
Expand All @@ -34,17 +41,27 @@ var yargs = require("yargs")
// The command-line arguments
var argv = yargs.argv;

// Use package.json as the config by default.
argv.config = argv.config;

// Determine the location of the config file
var config = argv.config[0] === "/" ?
argv.config : path.join(process.cwd(), argv.config);


// Which command to run, either `build` or `transform`
var command = argv._[0];
var command = argv._[0] || "build";

// Load steal-tools
var stealTools = require("../index");

var system = { config: config };
if(argv.main) {
system.main = argv.main;
}

var options = argv;

if(command === "build"){

return build();
Expand All @@ -57,16 +74,7 @@ if(command === "build"){

function build() {

stealTools.build({
config: config,
main: argv.main
}, {
verbose: argv.verbose,
quiet: argv.quiet,
bundleSteal: argv.bundleSteal,
bundlesPath: argv.bundlesPath,
sourceMaps: argv.sourceMaps
}).then(function(){
stealTools.build(system, options).then(function(){
winston.info('\nBuild completed successfully'.green);
});

Expand All @@ -81,13 +89,7 @@ function transform() {
ignore = argv.ignore.split(",");
}

stealTools.transform({
config: config,
main: argv.main
}, {
verbose: argv.verbose,
quiet: argv.quiet
}).then(function(transform){
stealTools.transform(system, options).then(function(transform){

var result = transform(null, {
ignore: ignore
Expand Down
4 changes: 4 additions & 0 deletions doc/command-build.md
Expand Up @@ -35,4 +35,8 @@ Or, you can access `steal-tools` in _node_modules/.bin_, like:
--config app/config.js \
--main app/app
If you are using the [npm] plugin you don't need to specify `--config` or `--main`:

> steal-tools

will default to `package.json!npm` as the config and build out to `dist/`.
33 changes: 33 additions & 0 deletions test/helpers.js
@@ -0,0 +1,33 @@
var Browser = require("zombie");
var connect = require("connect");
var path = require("path");

// Helpers
exports.find = function(browser, property, callback, done){
var start = new Date();
var check = function(){
if(browser.window && browser.window[property]) {
callback(browser.window[property]);
} else if(new Date() - start < 2000){
setTimeout(check, 20);
} else {
done("failed to find "+property+" in "+browser.window.location.href);
}
};
check();
};

exports.open = function(url, callback, done){
var server = connect().use(connect.static(path.join(__dirname,".."))).listen(8081);
var browser = Browser.create();
browser.visit("http://localhost:8081/"+url)
.then(function(){
callback(browser, function(err){
server.close();
done(err);
})
}).catch(function(e){
server.close();
done(e)
});
};
34 changes: 4 additions & 30 deletions test/test.js
Expand Up @@ -16,38 +16,12 @@ var dependencyGraph = require("../lib/graph/make_graph"),
stealExport = require('../lib/build/export'),
asap = require("pdenodeify");

System.logLevel = 3;
var find = require("./helpers").find;
var open = require("./helpers").open;

// Helpers
var find = function(browser, property, callback, done){
var start = new Date();
var check = function(){
if(browser.window && browser.window[property]) {
callback(browser.window[property]);
} else if(new Date() - start < 2000){
setTimeout(check, 20);
} else {
done("failed to find "+property+" in "+browser.window.location.href);
}
};
check();
};

var open = function(url, callback, done){
var server = connect().use(connect.static(path.join(__dirname,".."))).listen(8081);
var browser = Browser.create();
browser.visit("http://localhost:8081/"+url)
.then(function(){
callback(browser, function(err){
server.close();
done(err);
})
}).catch(function(e){
server.close();
done(e)
});
};
System.logLevel = 3;

require("./test_cli");

(function(){

Expand Down
135 changes: 135 additions & 0 deletions test/test_cli.js
@@ -0,0 +1,135 @@
var assert = require("assert");
var path = require("path");
var rmdir = require("rimraf");
var spawn = require("child_process").spawn;
var asap = require("pdenodeify");
var fs = require("fs-extra");
require("steal");

var find = require("./helpers").find;
var open = require("./helpers").open;

function stealTools(args){
return new Promise(function(resolve, reject){
var cli = path.resolve(__dirname + "/../bin/steal");
var child = spawn(cli, args || []);

/*var print = function(d){ console.log(d+""); };
child.stdout.on("data", print);
child.stderr.on("data", print);*/

child.on("close", function(code){
if(code === 1) {
var error = new Error("Exited with status " + code);
return reject(error);
}
return resolve();
});
});
}

describe("steal-tools cli", function(){
describe("build", function(){
describe("basics", function(){
beforeEach(function(){
this.cwd = process.cwd();
process.chdir(__dirname);
});

afterEach(function(){
process.chdir(this.cwd);
});

it("works", function(done){
stealTools(["build", "--config", "stealconfig.js", "--main",
"basics/basics", "--no-minify"])
.then(function(){
done();
});
});

it("uses build by default", function(done){
stealTools(["--config", "stealconfig.js", "--main",
"basics/basics", "--no-minify"])
.then(function(){
done();
});
});
});

describe("without --config or --main", function(){

beforeEach(function(done){
this.cwd = process.cwd();
process.chdir(path.resolve(__dirname + "/npm"));

rmdir = asap(rmdir);
var copy = asap(fs.copy);

rmdir(path.join(__dirname, "npm", "node_modules"))
.then(function(){
return rmdir(path.join(__dirname, "npm", "dist"));
})
.then(function(){
return copy(path.join(__dirname, "..", "node_modules","jquery"),
path.join(__dirname, "npm", "node_modules", "jquery"));
}).then(function(){
return copy(path.join(__dirname, "..", "bower_components","steal"),
path.join(__dirname, "npm", "node_modules", "steal"));
}).then(done, done);
});

afterEach(function(){
process.chdir(this.cwd);
});

it("uses package.json", function(done){
this.timeout(5000);
stealTools(["--no-minify"]).then(function(){
open("test/npm/prod.html", function(browser, close){
var h1s = browser.window.document.getElementsByTagName('h1');
assert.equal(h1s.length, 1, "Wrote H!.");
close();
}, done);
});
});
});
});

describe("transform", function(){
describe("basics", function(){
beforeEach(function(done){
this.cwd = process.cwd();
process.chdir(__dirname);

rmdir(__dirname + "/pluginify/out.js", function(error){
done(error);
});
});

afterEach(function(){
process.chdir(this.cwd);
});

it("works", function(done){
stealTools(["transform", "-c", "stealconfig.js", "-m",
"pluginify/pluginify", "--out", "pluginify/out.js"]).then(function(){

open("test/pluginify/index.html", function(browser, close){

find(browser,"RESULT", function(result){
assert(result.module.es6module, "have dependeny");
assert(result.cjs(), "cjs");
assert.equal(result.UMD, "works", "Doesn't mess with UMD modules");
assert.equal(result.define, undefined, "Not keeping a global.define");
assert.equal(result.System, undefined, "Not keeping a global.System");
close();
}, close);

}, done);

});
});
});
});
});

0 comments on commit 5572f65

Please sign in to comment.