Skip to content

Instantly share code, notes, and snippets.

@beck03076
Created July 24, 2014 20:57
Show Gist options
  • Save beck03076/cde928319ef9c6afd438 to your computer and use it in GitHub Desktop.
Save beck03076/cde928319ef9c6afd438 to your computer and use it in GitHub Desktop.
var fs = require('fs'), mongoose = require('mongoose'), util = require('util'), async = require('async'), types = mongoose.Schema.Types;
var mongoose_conn;
if (process.argv[3] === 'staging') {//staging
mongoose_conn = util.format('mongodb://martmobi_rw:vThPQBu32H3x@%s:%d/%s', '162.13.94.186', '27017', 'magento_sync_production');
} else if (process.argv[3] === 'production') {//live
mongoose_conn = util.format('mongodb://martmobi_rw:Dd6e3g7c8@%s:%d/%s', '95.138.168.92', '27017', 'magento_sync_production');
}
mongoose.connect(mongoose_conn, {});
// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function() {
console.log('Mongoose default connection open to ' + mongoose_conn);
});
// If the connection throws an error
mongoose.connection.on('error', function(err) {
console.log('Mongoose default connection error: ' + err);
exit_process(1);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function() {
console.log('Mongoose default connection disconnected');
exit_process(1);
});
var schema = new mongoose.Schema({
"name" : String,
"build_config" : types.Mixed
});
var mod = mongoose.model('Store', schema, 'stores');
var store_path, app_path;
if (process.platform.match(/^win/) === null) {
store_path = 'build/' + process.argv[2] + '/';
app_path = store_path + 'app/config.js';
} else {
store_path = 'build\\' + process.argv[2] + '\\';
app_path = store_path + 'app\\config.js';
}
var rem;
if (process.argv.length < 5) {
rem = 1;
var query = mod.find({
'name' : new RegExp(process.argv[2], 'i')
}, {
'build_config' : 1
});
query.exec(function(err, obj) {
if (err) {
console.log(err);
exit_process(1);
} else {
if (!obj[0].build_config) {
exit_process(1);
} else {
paste_config(obj[0].build_config, store_path, app_path);
}
}
});
} else {
rem = 2;
var file = fs.readFileSync(store_path + process.argv[4], "utf8");
var jsonConfig = JSON.parse(file.toString());
var query = mod.findOne({
'name' : new RegExp(process.argv[2], 'i')
});
query.exec(function(err, obj) {
if (err) {
console.log(err);
exit_process(1);
} else {
paste_config(jsonConfig.build_config, store_path, app_path);
for (var k in jsonConfig) {
obj._doc[k]=jsonConfig[k];
obj.markModified(k);
}
obj.save(function(err, res) {
console.log("Saved config to DB");
rem--;
exit_process(0, rem);
});
}
});
}
function paste_config(config, store_path, app_path) {
var ops = JSON.stringify(config).replace(/{\"/g, '{').replace(/\":/g, ':').replace(/,\"/g, ',');
console.log(ops);
var file2 = fs.readFileSync(app_path);
var ops2 = file2.toString();
ops2 = ops2.replace("'%config%'", ops);
ops2 = ops2.replace("'%config%'", ops);
ops2 = ops2.replace("'%config%'", ops);
fs.writeFileSync(app_path, ops2);
//generate PaymentView.js
var payment_config = config['payment_modules'] || {
'secureebs_standard' : 'ebs'
};
console.log(payment_config);
var payment_modules = {}, payment_methods = {};
//using dictionaries so that we have uniques
var _add_payment_module = function(module_key, cb) {
var payment_method = payment_config[module_key];
payment_modules["modules/plugins/payment/" + payment_method] = 1;
payment_methods[payment_method] = 1;
return cb(null);
};
async.forEach(Object.keys(payment_config), _add_payment_module, function(err_add) {
var payment_module_str = '"' + Object.keys(payment_modules).join('",\n "') + '"', payment_methods_str = 'Pay_' + Object.keys(payment_methods).join(", Pay_");
+'Pay_', payment_view_path = null;
if (process.platform.match(/^win/) === null) {
payment_view_path = 'build/' + process.argv[2] + '/app/modules/view/PaymentView.js';
} else {
payment_view_path = 'build\\' + process.argv[2] + '\\app\\modules\\view\\PaymentView.js';
}
var payment_file = fs.readFileSync(payment_view_path);
var payment_contents = payment_file.toString();
payment_contents = payment_contents.replace("%payment_config%", payment_module_str);
payment_contents = payment_contents.replace("%payment_methods%", payment_methods_str);
fs.writeFileSync(payment_view_path, payment_contents);
rem--;
exit_process(0, rem);
});
}
function exit_process(n, rem) {
if (n == 1) {
process.exit(1);
} else if (rem == 0) {
process.exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment