jdm
Version:
jdm
125 lines (110 loc) • 3.58 kB
JavaScript
var prompt = require('prompt');
var fs = require('fs');
var mkdirp = require('mkdirp');
var colors = require('colors');
var currDir = process.cwd();
var chokidar = require('chokidar');
var TPL_HTML = '<!DOCTYPE html>\n'
+'<html lang="zh-CN">\n'
+'<head>\n'
+' <meta charset="utf-8">\n'
+' <title>Hello Galaxy</title>\n'
+'</head>\n'
+'<body>\n'
+' <h1>Hello Galaxy.</h1>\n'
+'</body>\n'
+'</html>';
var TPL_CSS = 'body {font-size:12px; color:#666; padding:200px;}';
function createFile(file, str, callback) {
fs.exists(file, function(exists) {
if ( exists ) {
console.log('File already exists.'.red);
} else {
fs.writeFile(file, str, function(err) {
if ( err ) {
console.log('Write file error.'.red);
console.log(err);
} else {
console.log(( 'Success to create file: ' + file ).replace(currDir + '/', '').green);
if ( callback ) {
callback();
}
}
});
}
});
}
function generateBolierplate(res) {
var currPath = currDir;
var isNewCMD = typeof res !== 'undefined' && typeof res['name'] !== 'undefined' && res['name'] !== '';
if ( isNewCMD ) {
currPath = currDir + '/' + res['name'];
}
var dirs = [
currPath,
currPath + '/html',
currPath + '/app/css',
currPath + '/app/js',
currPath + '/doc'
];
var config = {
name: '',
description: '',
author: ''
};
if ( res ) {
config = res;
}
function showMsg(isNew) {
console.log('--------------------------------------------------'.grey);
console.log('Everything is ok, please try:'.green);
if ( isNew ) {
console.log(( '> cd ' + res['name'] ).grey);
}
console.log(( '> jdm server' ).grey);
}
for (var i = 0; i < dirs.length; i++) {
(function(k) {
mkdirp(dirs[k], function(err) {
//console.log(arguments[1]);
if ( err ) {
console.log(( 'Error to create directory: ' + dirs[k]).red );
} else {
console.log(( 'Success to create directory: ' + dirs[k] ).replace(currDir + '/', '').green );
if ( k == dirs.length - 1 ) {
createFile(currPath + '/config.js', JSON.stringify(config, undefined, 4), function() {
});
createFile(dirs[1] + '/index.html', TPL_HTML, function() {
showMsg(isNewCMD);
});
}
}
});
})(i);
}
}
function watch() {
var watcher = chokidar.watch(currDir, {
ignored: /[\/\\]\./,
persistent: true
});
watcher
.on('change', function(path) { console.log('File', path, 'has been changed');})
}
exports.new = function() {
prompt.start();
prompt.get(['name', 'description', 'author'], function (err, result) {
generateBolierplate(result);
});
};
exports.init = function() {
generateBolierplate();
};
exports.watch = function() {
watch();
};
exports.test = function() {
fs.readdir(currDir, function(err, files) {
console.log(files);
});
}