Skip to content

Commit

Permalink
This commit fixes issue Medium#14
Browse files Browse the repository at this point in the history
Implement a few simple methods to copy files using node fs calls instead of exec commands.
  • Loading branch information
KevinGrandon committed Feb 11, 2012
1 parent 9e2f182 commit c95d56e
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions bin/matador.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,49 @@
var fs = require('fs')
, exec = require('child_process').exec

/**
* Quick method to copy files
*/
, cp = function(oldFile, newFile, fn) {
var oldFileStream = fs.createReadStream(oldFile)
, newFileStream = fs.createWriteStream(newFile)
, fn = fn || function () {
console.log('Successfully created ' + newFile)
}

newFileStream.once('open', function(fd){
require('util').pump(oldFileStream, newFileStream, fn)
})
}

/**
* Quick method to copy files,
* type-R because it's fast
*/
, cpR = function(source, dest) {
// Create the desination directory
var checkDir = fs.statSync(source)
fs.mkdirSync(dest, checkDir.mode)

var files = fs.readdirSync(source)

for(var i = 0; i < files.length; i++) {
var thisFile = fs.lstatSync(source + "/" + files[i])

if(thisFile.isDirectory()) {
// Recursive case
cpR(source + "/" + files[i], dest + "/" + files[i])
} else {
// Single file case
cp(source + "/" + files[i], dest + "/" + files[i])
}
}
}

var methods = {
init: function (path) {
console.log('installing Matador into ' + path)
fs.mkdirSync('./' + path)
exec('cp -R ' + __dirname + '/../src/all/* ' + path + '/.', function (err, out) {
if (err) return console.log('error', err)
console.log('Success!')
})
cpR(__dirname + '/../src/all', path)
}
, controller: function (name) {
if (name.match(/([^\/]+)\//)) {
Expand All @@ -19,27 +54,25 @@ var methods = {
copyContents()
}
function copyContents() {
var destinationFile = './app/controllers/' + name.replace(/(?:[^\/]+)$/, function (m) {
var newFile = './app/controllers/' + name.replace(/(?:[^\/]+)$/, function (m) {
return m.replace(/(.{1})/, function (m, l) {
return l.toUpperCase()
})
}) + 'Controller.js'
console.log('generating controller ' + destinationFile)
var stub = __dirname + '/../src/StubController.js'
exec('cp ' + stub + ' ' + destinationFile, function (er, out) {
console.log('Successfully created ' + destinationFile)
})
, oldFile = __dirname + '/../src/StubController.js'
console.log('generating controller ' + newFile)

cp(oldFile, newFile)
}
}
, model: function (name) {
console.log('generating model ' + name)
var destinationFile = './app/models/' + name.replace(/^(.{1})/, function (m, l) {
var newFile = './app/models/' + name.replace(/^(.{1})/, function (m, l) {
return l.toUpperCase()
}) + 'Model.js'
var stub = __dirname + '/../src/StubModel.js'
exec('cp ' + stub + ' ' + destinationFile, function (er, out) {
console.log('Successfully created ' + destinationFile)
})
, oldFile = __dirname + '/../src/StubModel.js'

cp(oldFile, newFile)
}
}
!function (args) {
Expand Down

0 comments on commit c95d56e

Please sign in to comment.