Skip to content

Commit

Permalink
[dist api refactor] Improve flatiron create command to support both…
Browse files Browse the repository at this point in the history
… HTTP and CLI apps. Fixes #15
  • Loading branch information
indexzero committed Feb 9, 2012
1 parent a9eb28c commit d85a9da
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 88 deletions.
74 changes: 0 additions & 74 deletions bin/commands/create.js

This file was deleted.

13 changes: 4 additions & 9 deletions bin/flatiron
Expand Up @@ -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();
83 changes: 83 additions & 0 deletions lib/flatiron/cli/create.js
@@ -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'
];
12 changes: 8 additions & 4 deletions lib/flatiron/plugins/cli.js
Expand Up @@ -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);
});
}
}

//
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions scaffolds/cli/app.js
@@ -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();
6 changes: 6 additions & 0 deletions scaffolds/cli/directories.json
@@ -0,0 +1,6 @@
{
"config": "#ROOT/config",
"lib": "#ROOT/lib",
"commands": "#ROOT/lib/commands",
"test": "#ROOT/test"
}
15 changes: 15 additions & 0 deletions scaffolds/cli/package.json
@@ -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"
}
}
9 changes: 9 additions & 0 deletions scaffolds/http/app.js
@@ -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);
5 changes: 5 additions & 0 deletions scaffolds/http/directories.json
@@ -0,0 +1,5 @@
{
"config": "#ROOT/config",
"lib": "#ROOT/lib",
"test": "#ROOT/test"
}
16 changes: 16 additions & 0 deletions scaffolds/http/package.json
@@ -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 d85a9da

Please sign in to comment.