Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dist api refactor] Improve
flatiron create
command to support both…
… HTTP and CLI apps. Fixes #15
- Loading branch information
Showing
11 changed files
with
157 additions
and
88 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"config": "#ROOT/config", | ||
"lib": "#ROOT/lib", | ||
"commands": "#ROOT/lib/commands", | ||
"test": "#ROOT/test" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"config": "#ROOT/config", | ||
"lib": "#ROOT/lib", | ||
"test": "#ROOT/test" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |