Files
deployd/bin/dpd-export
2012-05-09 12:46:13 -07:00

122 lines
3.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* Dependencies
*/
var program = require('commander')
, dpd = require('../')
, fs = require('fs')
, path = require('path')
, exec = require('child_process').exec
;
// Options
program
.option('-s, --src <url>', 'specify where to store resources (mongodb://localhost/foo)')
.option('-d, --destination <url>', 'specify where to push to when cloning (mongodb://localhost/bar)')
.option('-j, --json', 'output as json')
;
/**
* Stdout Utils
*/
/**
* A method to output json.
*/
function json(obj) {
if(program && program.json) process.stdout.write(JSON.stringify(obj));
}
/**
* Silent if in json mode.
*/
function log() {
if(!program.json) console.log.apply(console, arguments);
}
/**
* Clone one dpd configuration into another.
*/
program
.command('clone')
.description(' - clone the configuration and files of one instance into another')
.action(function () {
var port = program.port || 2403;
if(!program.src || !program.destination) throw 'a destination (--destination) and source (--src) are required';
dpd.storage(program.src);
dpd.use('/resources').get(function (err, resources) {
if(!resources) {
json({status: 'none found'});
log('none found');
process.kill(process.pid);
return;
};
if(err) throw err;
var dest = dpd.use('/resources');
dest.storage(program.destination);
dest.post(resources, function (err) {
if(err) throw err;
var remaining = 0;
resources.forEach(function (resource) {
if(resource.type === 'Static') {
dpd.storage(program.src);
// / is an alias for /index
var collection = resource.path;
if(collection === '/') collection = '/index';
var split = collection && collection.replace('/', '').split('/');
if(split && split.length > 1) {
collection = '/' + split.join('-');
}
var srcFiles = require('mdoq').require('mdoq-mongodb').use(program.src).use(collection);
var destFiles = require('mdoq').require('mdoq-mongodb').use(program.destination).use(collection);
dpd.use(resource.path).get({}, function (err, files) {
files.forEach(function (file) {
remaining++;
var id = {_id: file};
srcFiles.get(id, function (err, res) {
if(err) log(err);
if(!err && res) {
var temp = require('fs').createWriteStream('/tmp/' + file);
res.file.stream(true).pipe(temp).on('close', function () {
destFiles.get({}).file(require('fs').createReadStream('/tmp/' + file)).post(function (err) {
remaining--;
if(!remaining) {
json({status: 'done.'});
log('done.');
process.kill(process.pid);
}
})
})
}
})
})
})
}
})
})
})
})
;
program.parse(process.argv);