Created
July 31, 2015 06:17
-
-
Save royhowie/ab2b126743cb7f17ed0e 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
import Promise from 'bluebird' | |
let fs = Promise.promisifyAll(require('fs')) | |
let exec = Promise.promisify(require('child_process').exec) | |
function mapCommands (file) { | |
return file.split('\n').reduce((p, c) => { | |
let separation = c.indexOf(':') | |
p[c.slice(3, separation)] = c.slice(separation + 2) | |
return p | |
}, {}) | |
} | |
function execCommands (obj) { | |
let commands = [] | |
for (let key in obj) { | |
let cmd = exec(obj[key]) | |
commands.push(cmd) | |
} | |
return Promise.all(commands) | |
} | |
export default { | |
createNew () { | |
fs.readFileAsync('C://test.txt', 'utf8') | |
.then(mapCommands) | |
.then(execCommands) | |
.then(() => { | |
// success; all commands executed | |
}).catch((err) => { | |
console.log(err) // handle err | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment