Permalink
Browse files

[dist api refactor] Improve `flatiron create` command to support both…

… HTTP and CLI apps. Fixes #15
  • Loading branch information...
1 parent 754d71e commit 0448fb09496ccd9870921ac5db58cf4ba6102508 @indexzero indexzero committed Feb 8, 2012
View
@@ -1,74 +0,0 @@
-var fs = require('fs'),
- path = require('path'),
- common = require('broadway').common,
- directories = common.directories,
- app = require('../../lib/flatiron').app;
-
-var create = module.exports = function create(name, callback) {
- name = name || 'flatiron-app';
-
- var root = path.join(process.cwd(), name),
- dirs;
-
- dirs = directories.normalize(root, {
- config: '#ROOT/config',
- lib: '#ROOT/lib',
- commands: '#ROOT/lib/commands',
- test: '#ROOT/test'
- });
-
- Object.keys(dirs).forEach(function (name) {
- app.log.info('Creating directory ' + name.grey);
- });
-
- directories.create(dirs, function() {
- var pkg = {
- name: name,
- private: true,
- version: '0.0.0',
- dependencies: {
- flatiron: JSON.parse(fs.readFileSync(__dirname + '/../../package.json')).version
- },
- devDependencies: {
- vows: '0.6.x'
- },
- scripts: {
- test: 'vows',
- start: 'node app.js'
- }
- }
-
- var template = [
- 'var flatiron = require(\'flatiron\'),',
- ' path = require(\'path\'),',
- ' app = flatiron.app;',
- '',
- 'app.use(flatiron.plugins.cli, {',
- ' dir: path.join(__dirname,\'lib\',\'commands\'),',
- ' usage: \'Empty Flatiron Application, please fill out commands\'',
- '});',
- '',
- 'if (require.main === module) {',
- ' app.init(function () {',
- ' app.start();',
- ' });',
- '}'
- ].join('\n') + '\n';
-
- app.log.info('Writing package.json');
- fs.writeFile(path.join(root, 'package.json'), JSON.stringify(pkg, null, ' ') + '\n');
-
- app.log.info('Writing app.js');
- fs.writeFile(path.join(root, 'app.js'), template);
-
- app.log.info('Writing lib/' + name + '.js');
- fs.writeFile(path.join(root, 'lib', name + '.js'), '');
-
- app.log.info('Application ' + name.magenta + ' is now ready');
- callback();
- });
-}
-
-create.usage = [
- 'create :appname - generate a flatiron skeleton application'
-];
View
@@ -9,19 +9,14 @@ var actions = {
};
app.use(flatiron.plugins.cli, {
- dir: path.join(__dirname, 'commands'),
+ dir: path.join(__dirname, '..', 'lib', 'flatiron', 'cli'),
usage: [
'flatiron',
'',
+ 'Commands:'
].concat(Object.keys(actions).map(function(key){
- return key + ' - ' + actions[key]
+ return ' ' + key + ' - ' + actions[key]
}))
});
-app.cmd('version', function () {
- console.log('flatiron ' + flatiron.version);
-});
-
-if (require.main === module) {
- app.start();
-}
+app.start();
View
@@ -0,0 +1,83 @@
+var fs = require('fs'),
+ path = require('path'),
+ flatiron = require('../../flatiron'),
+ common = flatiron.common,
+ async = common.async,
+ directories = common.directories,
+ cpr = common.cpr,
+ app = flatiron.app;
+
+module.exports = function create(name, callback) {
+ name = name || 'flatiron-app';
+
+ var root = path.join(process.cwd(), name),
+ type = app.argv.cli ? 'cli' : 'http',
+ scaffold = path.join(__dirname, '..', '..', '..', 'scaffolds', type);
+
+ //
+ // Creates directories specified in `/scaffolds/:type/directories.json`.
+ //
+ function createDirs(next) {
+ var dirs = directories.normalize(root,
+ JSON.parse(fs.readFileSync(path.join(scaffold, 'directories.json'), 'utf8'))
+ );
+
+ Object.keys(dirs).forEach(function (name) {
+ app.log.info('Creating directory ' + name.grey);
+ });
+
+ directories.create(dirs, next);
+ }
+
+ //
+ // Creates a templated package.json from `/scaffolds/:type/package.json`.
+ //
+ function createPackage(next) {
+ var pkg = JSON.parse(fs.readFileSync(path.join(scaffold, 'package.json'), 'utf8'));
+
+ pkg.name = name;
+ pkg.dependencies.flatiron = flatiron.version;
+
+ app.log.info('Writing ' + 'package.json'.grey);
+ fs.writeFile(path.join(root, 'package.json'), JSON.stringify(pkg, null, 2) + '\n', next);
+ }
+
+ //
+ // Writes the `app.js`
+ //
+ function writeApp(next) {
+ app.log.info('Writing ' + 'app.js'.grey);
+ cpr(path.join(scaffold, 'app.js'), path.join(root, 'app.js'), next);
+ }
+
+ //
+ // Writes the top-level include for the app
+ //
+ function writeMain(next) {
+ app.log.info('Writing ' + ('lib/index.js').grey);
+ fs.writeFile(path.join(root, 'lib', 'index.js'), '', next);
+ }
+
+ app.log.info('Creating application ' + name.magenta)
+ app.log.info('Using ' + type.yellow + ' scaffold.');
+ async.series([
+ createDirs,
+ createPackage,
+ writeApp,
+ writeMain
+ ], function onComplete(next) {
+ app.log.info('Application ' + name.magenta + ' is now ready');
+ callback();
+ }
+ );
+}
+
+module.exports.usage = [
+ 'Generates a flatiron skeleton application. By default',
+ 'an HTTP application will be created.',
+ '',
+ 'create <app-name>',
+ '',
+ 'Command options:',
+ ' --cli Creates a CLI application'
+];
@@ -143,10 +143,14 @@ exports.commands = function (options) {
app.log.help(line);
});
- app.log.help('');
- app.showOptions().split('\n').forEach(function (line) {
- app.log.help(line);
- });
+ var lines = app.showOptions().split('\n').filter(Boolean);
+
+ if (options.length) {
+ app.log.help('');
+ lines.forEach(function (line) {
+ app.log.help(line);
+ });
+ }
}
//
View
@@ -11,7 +11,7 @@
"url": "http://github.com/flatiron/flatiron.git"
},
"dependencies": {
- "broadway": "0.1.7",
+ "broadway": "0.1.9",
"optimist": "0.3.1",
"prompt": "0.1.11",
"director": "1.0.8",
View
@@ -0,0 +1,10 @@
+var flatiron = require('flatiron'),
+ path = require('path'),
+ app = flatiron.app;
+
+app.use(flatiron.plugins.cli, {
+ source: path.join(__dirname, 'lib', 'commands'),
+ usage: 'Empty Flatiron Application, please fill out commands'
+});
+
+app.start();
@@ -0,0 +1,6 @@
+{
+ "config": "#ROOT/config",
+ "lib": "#ROOT/lib",
+ "commands": "#ROOT/lib/commands",
+ "test": "#ROOT/test"
+}
View
@@ -0,0 +1,15 @@
+{
+ "description": "A Flatiron CLI application",
+ "version": "0.0.0",
+ "private": true,
+ "dependencies": {
+ },
+ "devDependencies": {
+ "cli-easy": "0.1.0",
+ "vows": "0.6.1"
+ },
+ "scripts": {
+ "test": "vows --spec",
+ "start": "node app.js"
+ }
+}
View
@@ -0,0 +1,9 @@
+var flatiron = require('flatiron'),
+ app = flatiron.app;
+
+app.use(flatiron.plugins.http);
+app.router.get('/', function () {
+ this.res.json({ 'hello': 'world' })
+});
+
+app.start(3000);
@@ -0,0 +1,5 @@
+{
+ "config": "#ROOT/config",
+ "lib": "#ROOT/lib",
+ "test": "#ROOT/test"
+}
@@ -0,0 +1,16 @@
+{
+ "description": "A Flatiron HTTP application",
+ "version": "0.0.0",
+ "private": true,
+ "dependencies": {
+ "union": "0.1.7"
+ },
+ "devDependencies": {
+ "api-easy": "0.1.0",
+ "vows": "0.6.1"
+ },
+ "scripts": {
+ "test": "vows --spec",
+ "start": "node app.js"
+ }
+}

0 comments on commit 0448fb0

Please sign in to comment.