Permalink
Browse files

#59 actions interface was rewritten

  • Loading branch information...
1 parent 72871b5 commit e0753eb477749981b4e68397c54dd19dd96a213a @azproduction committed Nov 28, 2012
View
@@ -1,4 +1,22 @@
-exports.help = function (content, errorMessage) {
+require('colors');
+
+var LogWriter = function (stream) {
+ this.stream = stream;
+};
+
+LogWriter.prototype.log = function (message) {
+ this.stream.write(message + '\n');
+};
+
+LogWriter.prototype.ok = function (message) {
+ this.log('info'.green + ': ' + message);
+};
+
+LogWriter.prototype.warn = function (message) {
+ this.log('warn'.red + ': ' + message);
+};
+
+LogWriter.prototype.help = function (content, errorMessage) {
var help = [
'',
'LMD Builder',
@@ -15,13 +33,7 @@ exports.help = function (content, errorMessage) {
return 'help'.cyan + ': ' + line;
}).join('\n');
- console.log(help);
-};
-
-exports.ok = function (message) {
- console.log('info'.green + ': ' + message);
+ this.log(help);
};
-exports.warn = function (message) {
- console.log('warn'.red + ': ' + message);
-};
+exports.LogWriter = LogWriter;
View
@@ -13,11 +13,6 @@ var fs = require('fs'),
require('colors');
-var argv = require('optimist').argv;
-
-// lmd init
-var action = process.argv[2];
-
var allowedActions = {
init: 'Initializes LMD for project',
create: 'To create new LMD config',
@@ -55,7 +50,7 @@ var actionsAliases = {
'dry-run': 'info'
};
-function printHelp(errorMessage) {
+function printHelp(cli, errorMessage) {
var help = [
'Usage:'.bold.white.underline,
'',
@@ -76,17 +71,29 @@ function printHelp(errorMessage) {
cli.help(help, errorMessage);
}
-if (process.argv.length === 3 && argv.v || argv.version) {
- console.log(require(__dirname + '/../package.json').version);
+function init(stdout, argv, cwd) {
+ var options = require('optimist').parse(argv),
+ action = argv[2],
+ logWriter = new cli.LogWriter(stdout);
+
+ if (argv.length === 3 && options.v || options.version) {
+ stdout.write(require(__dirname + '/../package.json').version + '\n');
+
+ } else if (!actionsAliases.hasOwnProperty(action)) {
+ if (argv.length >= 4) {
+ require(__dirname + '/lmd_actions/old.js')(logWriter, argv.slice(2), cwd);
+ } else {
+ printHelp(logWriter);
+ }
-} else if (!actionsAliases.hasOwnProperty(action)) {
- if (process.argv.length >= 4) {
- require('./lmd_actions/old.js')(process.argv);
} else {
- printHelp();
- }
+ require(__dirname + '/lmd_actions/' + actionsAliases[action] + '.js')(logWriter, argv.slice(2), cwd);
-} else {
- require('./lmd_actions/' + actionsAliases[action] + '.js')(process.argv);
+ }
+}
+exports.init = init;
+// if !main
+if (!module.parent || (module.parent && module.parent.filename.match(/\/lmd\/bin\/lmd$/))) {
+ init(process.stdout, process.argv, process.cwd());
}
View
@@ -2,14 +2,12 @@ require('colors');
var fs = require('fs'),
path = require('path'),
- cli = require(__dirname + '/../cli_messages.js'),
+ optimist = require('optimist'),
init = require(__dirname + '/init.js'),
create = require(__dirname + '/create.js'),
lmdPackage = require(__dirname + '/../lmd_builder.js');
-var optimist = require('optimist');
-
-function printHelp(errorMessage) {
+function printHelp(cli, errorMessage) {
var help = [
'Usage:'.bold.white.underline,
'',
@@ -40,11 +38,10 @@ var createWritableFile = function (fileName) {
});
};
-module.exports = function () {
- var cwd = process.cwd(),
- status;
+module.exports = function (cli, argv, cwd) {
+ argv = optimist.parse(argv);
- var argv = optimist.argv,
+ var status,
buildName,
mixinBuilds = argv._[1];
@@ -57,19 +54,19 @@ module.exports = function () {
delete argv._;
delete argv.$0;
- if (!init.check()) {
+ if (!init.check(cli, cwd)) {
return;
}
if (!buildName) {
- printHelp();
+ printHelp(cli);
return;
}
status = create.checkFile(cwd, buildName);
if (status !== true) {
- printHelp(status === false ? 'build `' + buildName + '` is not exists' : status);
+ printHelp(cli, status === false ? 'build `' + buildName + '` is not exists' : status);
return;
}
@@ -79,7 +76,7 @@ module.exports = function () {
status = create.checkFile(cwd, buildName);
if (status !== true) {
- printHelp(status === false ? 'mixin build `' + buildName + '` is not exists' : status);
+ printHelp(cli, status === false ? 'mixin build `' + buildName + '` is not exists' : status);
return false;
}
return true;
@@ -125,13 +122,13 @@ module.exports = function () {
if (buildConfig.output && buildConfig.output) {
buildResult.pipe(createWritableFile(path.join(configDir, buildConfig.output)));
if (buildConfig.log) {
- buildResult.log.pipe(process.stdout);
+ buildResult.log.pipe(cli.stream);
buildResult.on('end', function () {
cli.ok('Writing LMD Package to ' + buildConfig.output.green);
});
}
} else {
- buildResult.pipe(process.stdout);
+ buildResult.pipe(cli.stream);
}
};
View
@@ -2,13 +2,11 @@ require('colors');
var fs = require('fs'),
path = require('path'),
- cli = require(__dirname + '/../cli_messages.js'),
+ optimist = require('optimist'),
init = require(__dirname + '/init.js'),
common = require(__dirname + '/../../lib/lmd_common.js');
-var optimist = require('optimist');
-
-function printHelp(errorMessage) {
+function printHelp(cli, errorMessage) {
var help = [
'Usage:'.bold.white.underline,
'',
@@ -72,38 +70,37 @@ function createBuild(cwd, buildName, parentBuild, options) {
fs.writeFileSync(lmdConfig, template(buildName, parentConfig, options), 'utf8');
}
-module.exports = function () {
- var cwd = process.cwd(),
- status;
+module.exports = function (cli, argv, cwd) {
+ argv = optimist.parse(argv);
- var argv = optimist.argv,
+ var status,
buildName = argv._[1],
parentBuild = argv._[2];
delete argv._;
delete argv.$0;
- if (!init.check()) {
+ if (!init.check(cli, cwd)) {
return;
}
if (!buildName) {
- printHelp();
+ printHelp(cli);
return;
}
status = checkFile(cwd, buildName);
if (status) {
- printHelp(status === true ? 'build `' + buildName + '` is already exists' : status);
+ printHelp(cli, status === true ? 'build `' + buildName + '` is already exists' : status);
return;
}
if (parentBuild) {
status = checkFile(cwd, parentBuild);
if (status !== true) {
- printHelp(status === false ? 'parent build `' + parentBuild + '` is not exists' : status);
+ printHelp(cli, status === false ? 'parent build `' + parentBuild + '` is not exists' : status);
return;
}
}
View
@@ -2,7 +2,6 @@ require('colors');
var fs = require('fs'),
path = require('path'),
- cli = require(__dirname + '/../cli_messages.js'),
init = require(__dirname + '/init.js'),
create = require(__dirname + '/create.js'),
common = require(__dirname + '/../../lib/lmd_common.js'),
@@ -23,7 +22,7 @@ var YES = '✔'.green,
NO = ''.red,
NO_YES = [NO, YES];
-function printHelp(errorMessage) {
+function printHelp(cli, errorMessage) {
var help = [
'Usage:'.bold.white.underline,
'',
@@ -75,7 +74,7 @@ function printValue(value) {
return value;
}
-function printModules(config, deepModulesInfo, sortColumnName) {
+function printModules(cli, config, deepModulesInfo, sortColumnName) {
var modules = config.modules || {};
var modulesNames = Object.keys(modules);
@@ -168,7 +167,7 @@ function printModules(config, deepModulesInfo, sortColumnName) {
cli.ok('');
}
-function printFlags(config, availableFlags) {
+function printFlags(cli, config, availableFlags) {
var longestName = availableFlags.reduce(function (max, current) {
if (typeof config[current] === "undefined") {
return max;
@@ -201,7 +200,7 @@ function printFlags(config, availableFlags) {
cli.ok('');
}
-function printModulePathsAndDepends(config, deepModulesInfo, isDeepAnalytics) {
+function printModulePathsAndDepends(cli, config, deepModulesInfo, isDeepAnalytics) {
var modules = config.modules || {},
modulesNames = Object.keys(modules),
globalsNames = common.getGlobals(config);
@@ -250,12 +249,11 @@ function printModulePathsAndDepends(config, deepModulesInfo, isDeepAnalytics) {
cli.ok('');
}
-module.exports = function () {
- var cwd = process.cwd(),
- status;
+module.exports = function (cli, argv, cwd) {
+ argv = optimist.parse(argv);
- var argv = optimist.argv,
- buildName,
+ var buildName,
+ status,
mixinBuilds = argv._[1],
sortOrder = argv.sort,
isDeepAnalytics = argv.deep;
@@ -273,19 +271,19 @@ module.exports = function () {
delete argv._;
delete argv.$0;
- if (!init.check()) {
+ if (!init.check(cli, cwd)) {
return;
}
if (!buildName) {
- printHelp();
+ printHelp(cli);
return;
}
status = create.checkFile(cwd, buildName);
if (status !== true) {
- printHelp(status === false ? 'build `' + buildName + '` is not exists' : status);
+ printHelp(cli, status === false ? 'build `' + buildName + '` is not exists' : status);
return;
}
@@ -295,7 +293,7 @@ module.exports = function () {
status = create.checkFile(cwd, buildName);
if (status !== true) {
- printHelp(status === false ? 'mixin build `' + buildName + '` is not exists' : status);
+ printHelp(cli, status === false ? 'mixin build `' + buildName + '` is not exists' : status);
return false;
}
return true;
@@ -340,9 +338,9 @@ module.exports = function () {
}
var deepModulesInfo = common.collectModulesInfo(config);
- printModules(config, deepModulesInfo, sortOrder);
- printModulePathsAndDepends(config, deepModulesInfo, isDeepAnalytics);
- printFlags(config, flags.concat(extraFlags));
+ printModules(cli, config, deepModulesInfo, sortOrder);
+ printModulePathsAndDepends(cli, config, deepModulesInfo, isDeepAnalytics);
+ printFlags(cli, config, flags.concat(extraFlags));
cli.ok('Paths'.white.bold.underline);
cli.ok('');
View
@@ -1,5 +1,4 @@
-var fs = require('fs'),
- cli = require(__dirname + '/../cli_messages.js');
+var fs = require('fs');
function checkLmdDir(cwd) {
var lmdDir = cwd + '/.lmd';
@@ -18,7 +17,7 @@ function createLmdStructure(cwd) {
fs.mkdirSync(lmdDir);
}
-function printHelp(errorMessage) {
+function printHelp(cli, errorMessage) {
var help = [
'Usage:'.bold.white.underline,
'',
@@ -30,12 +29,10 @@ function printHelp(errorMessage) {
cli.help(help, errorMessage);
}
-module.exports = function (argv) {
- var cwd = process.cwd();
-
+module.exports = function (cli, argv, cwd) {
var status = checkLmdDir(cwd);
if (status) {
- printHelp(status === true ? '.lmd is already initialised' : status);
+ printHelp(cli, status === true ? '.lmd is already initialised' : status);
return;
}
@@ -45,12 +42,10 @@ module.exports = function (argv) {
cli.ok('');
};
-module.exports.check = function () {
- var cwd = process.cwd();
-
+module.exports.check = function (cli, cwd) {
var status = checkLmdDir(cwd);
if (status !== true) {
- printHelp(status ? status : 'run `lmd init` to initialise LMD in ' + cwd);
+ printHelp(cli, status ? status : 'run `lmd init` to initialise LMD in ' + cwd);
return false;
}
Oops, something went wrong.

0 comments on commit e0753eb

Please sign in to comment.