Created
April 7, 2016 12:22
-
-
Save littlefuntik/8e196209a2b02d7a4ff27da6d5e75a9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Hryhorii Furletov <[email protected]> | |
* | |
* @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