Skip to content

Instantly share code, notes, and snippets.

@Fluidbyte Fluidbyte/deploy.js
Created May 20, 2014

Embed
What would you like to do?
Deploy to multiple environments on Nodejitsu with the same application
// Nodejitsu multiple environment deploy
//
// Prerequisits:
// Jitsu deploy CLI
//
// Usage:
// Include this file in root of project along with configured
// jitsu_conf.json file
// Run by calling:
// node deploy [ENV]
var fs = require('fs');
var child_process = require('child_process');
var tty = require('tty');
var backup_pkg = './package.json.BAK';
// Helper
var copyFileSync = function(srcFile, destFile, encoding) {
var content = fs.readFileSync(srcFile, encoding);
fs.writeFileSync(destFile, content, encoding);
};
// Get the stuff
var pkg = require('./package.json');
var config = require('./jitsu_conf.json');
// Create temp copy of package.json
copyFileSync('./package.json', backup_pkg, 'utf-8');
// Deploy env argument
var env = process.argv[2];
// Set params
for (var key in config[env]) {
pkg[key] = config[env][key];
}
// Write temp package.json for deploy
fs.writeFile('./package.json', JSON.stringify(pkg, null, 4), function (err) {
// Write error
if (err) {
throw err;
}
// Run the deploy process
var runner = child_process.spawn('jitsu', [ 'deploy' ]);
// Process STDIN
function inData(c) {
runner.stdin.write(c);
}
// Process STDOUT
function outData(c) {
process.stdout.write(c);
}
process.stdin.resume();
process.stdin.on('data', inData);
runner.stdout.on('data', outData);
tty.setRawMode(true);
// On complete
runner.on('exit', function(code) {
tty.setRawMode(false);
process.stdin.pause();
process.stdin.removeListener('data', inData);
runner.stdout.removeListener('data', outData);
// Get original package.json
var backup = fs.readFileSync(backup_pkg);
// Re-Write original package.json
fs.writeFile('./package.json', backup, function (err) {
if (err) {
console.log('ERROR: Could not reset package.json');
} else {
// Remove backup
fs.unlinkSync(backup_pkg);
}
});
});
});
{
"dev": {
"name": "myapp-dev",
"subdomain": "myapp-dev",
"domains": [
"myapp-dev.com"
]
},
"test": {
"name": "myapp-test",
"subdomain": "myapp-test",
"domains": [
"myapp-test.com"
]
},
"prod": {
"name": "myapp-prod",
"subdomain": "myapp-prod",
"domains": [
"myapp-prod.com"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.