Permalink
Browse files

async utils

  • Loading branch information...
1 parent 42e9c9e commit cf2cdc7272a48faf5251c32227f580d8b45ebe2a @speier speier committed May 24, 2012
Showing with 67 additions and 37 deletions.
  1. +27 −36 lib/cli/commands/create.js
  2. +39 −0 lib/cli/utils.js
  3. +1 −1 package.json
View
@@ -1,45 +1,36 @@
-var fs = require('fs');
var path = require('path');
-var mkdirp = require('mkdirp');
-var ncp = require('ncp').ncp;
+var async = require('async');
+var utils = require('../utils');
var logger = require('../clilogger');
var skeletons = require('../skeletons');
module.exports = function(name, skeleton) {
- if (!skeleton) {
- skeleton = skeletons.express;
- }
- if (!path.existsSync(skeleton)) {
- skeleton = skeletons[skeleton];
- }
- if (!skeleton) {
- return logger.error('invalid skeleton');
- }
- mkdirp(name, function(err) {
- if (err) {
- return logger.error(err);
- } else {
- ncp(skeleton, name, function(err) {
- if (err) {
- return logger.error(err);
- }
- updateProjectInfo(name);
- });
- }
+ skeleton = skeleton || skeletons.express;
+ path.exists(skeleton, function(exists) {
+ if (!exists) skeleton = skeletons[skeleton];
+ if (!skeleton) return logger.error('skeleton not found');
+ utils.copy(skeleton, name, function(err) {
+ if (err) return logger.error(err);
+ updateProjectInfo(name);
+ });
});
};
function updateProjectInfo(name) {
- // update package.json ..
- var pkg = JSON.parse(fs.readFileSync(path.join(name, 'package.json')));
- pkg.name = path.basename(name);
- fs.writeFileSync(path.join(name, 'package.json'), JSON.stringify(pkg, null, 2));
- // update readme.md ..
- var readme = fs.readFileSync(path.join(name, 'README.md'), 'utf-8');
- readme = readme.replace(/application-name/gi, pkg.name);
- fs.writeFileSync(path.join(name, 'readme.md'), readme);
- // log info ..
- logger.info('new project created');
- logger.info('dont forget to install dependencies:');
- logger.info('$ cd ' + name + ' && npm install');
-};
+ var basename = path.basename(name);
+ async.parallel({
+ package: function(cb) {
+ utils.updateJson(path.join(name, 'package.json'), 'name', basename, cb);
+ },
+ readme: function(cb) {
+ utils.updateFile(path.join(name, 'README.md'), /application-name/gi, basename, cb);
+ }
+ }, function(err) {
+ if (err) logger.error(err);
+ logger.info('new project created');
+ logger.log('dont forget to install dependencies:');
+ logger.log('$ cd %s && npm install', name);
+ logger.log('then build your project:');
+ logger.log('$ torpedo build');
+ });
+};
View
@@ -0,0 +1,39 @@
+var fs = require('fs');
+var path = require('path');
+var mkdirp = require('mkdirp');
+var ncp = require('ncp').ncp;
+var async = require('async');
+
+exports.copy = function(source, destination, callback) {
+ async.auto({
+ make_folder: function(cb) {
+ // create destionation folder if not exists
+ mkdirp(destination, cb);
+ },
+ copy_files: ['make_folder', function(cb) {
+ // copy all files and dirs recursively if folder exists
+ ncp(source, destination, cb);
+ }]
+ }, callback); // all tasks have been completed
+};
+
+exports.updateFile = function(filename, searchvalue, newvalue, callback) {
+ fs.readFile(filename, 'utf8', function(err, data) {
+ if (err) return callback(err);
+ data = data.replace(searchvalue, newvalue);
+ fs.writeFile(filename, data, function(err) {
+ return err ? callback(err) : callback(null);
+ });
+ });
+};
+
+exports.updateJson = function(filename, property, value, callback) {
+ fs.readFile(filename, 'utf8', function(err, data) {
+ if (err) return callback(err);
+ var json = JSON.parse(data);
+ json[property] = value;
+ fs.writeFile(filename, JSON.stringify(json, null, 2), function(err) {
+ return err ? callback(err) : callback(null);
+ });
+ });
+};
View
@@ -44,4 +44,4 @@
"type": "MIT",
"url": "http://github.com/speier/torpedo/raw/master/LICENSE"
}]
-}
+}

0 comments on commit cf2cdc7

Please sign in to comment.