Set your shell free !
Combine the flexibility of a programming language with the knowledge of shell command. As developer, sometimes, we need to run shell scripts. It will be cool to do so with the familiarity of a programming language. An an ops, sometimes, we need to run complex shell scripts. It will be cool to do so with the power of a programming language.
- Light: There are no dependencies
- Easy: A small abstraction over
child_process
- Async: Work with async/await command
- Testable: Unshell script are easily testable because they yield execution control
Execute script through Shell
npm install -g unshell
Embedded script inside apps
npm install unshell
Execute script through unshell runtime
Usage:
unshell COMMAND [SCRIPT_PATH] [ARGS...]
Commands:
help Print this help message
run run a script through unshell runtime
Given the script: pause.js
to pause all docker containers
module.exports = function * pause () {
const ids = yield * fetchContainerIds()
for(const id of ids) {
yield `docker pause ${id}`
}
}
function * fetchContainerIds () {
const ids = yield `docker ps -q --no-trunc`
return ids.split('\n').filter(Boolean)
}
Run it through unshell
unshell run pause.js
Given the precedent script pause.js
Run it with zeit/micro
module.exports = (req, res) => {
const {resolve} = require('path')
const {unshell} = require('unshell')
try {
const script = require(resolve('./scripts/pause.js'))
unshell({env: process.env})(script)
res.end('OK')
} catch (err) {
res.end('NOK')
}
}
Here is some examples of what you can do with unshell
Please check out the issues labeled help wanted
or good-first-issue
. Try npx good-first-issue unshell
The code is available under the MIT license.