Skip to content

Instantly share code, notes, and snippets.

@littlefuntik
Created April 7, 2016 12:22
Show Gist options
  • Save littlefuntik/8e196209a2b02d7a4ff27da6d5e75a9c to your computer and use it in GitHub Desktop.
Save littlefuntik/8e196209a2b02d7a4ff27da6d5e75a9c to your computer and use it in GitHub Desktop.
/**
* @author Hryhorii Furletov <littlefuntik@gmail.com>
*
* @example
* (new CommandReader).runYii('config/database').then((databaseConfig) => { });
* (new CommandReader).runYii('/usr/bin/php', 'configJson.php').then((databaseConfig) => { });
*/
'use strict'
const spawn = require('child_process').spawn;
const yiiCommandPath = __dirname + '/../yii';
class CommandReader {
run() {
if (!arguments.length) {
throw 'First argument required';
}
let command = arguments[0];
let args = [];
for(var i = 1, l = arguments.length; i < l; ++i) {
args.push(arguments[i]);
}
return (new Promise((resolve, reject) => {
const execCommand = spawn(command, args);
execCommand.stdout.on('data', (data) => {
resolve(data.toString());
});
execCommand.stderr.on('data', (data) => {
reject(data.toString());
});
})).then((responseText) => {
try {
var config = JSON.parse(responseText);
} catch (e) {
throw "Bad JSON syntax in response. Detail message: " + e.message;
}
return config;
});
}
runYii() {
let args = [yiiCommandPath];
for(var i = 0, l = arguments.length; i < l; ++i) {
args.push(arguments[i]);
}
return this.run.apply(this, args);
}
};
module.exports = {
'commandReader': CommandReader
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment