A framework for building reusable JS tasks
npm install task-envA framework for building reusable JS tasks.
| Feature | Built With |
| ------------------------------------------- | -------------------------------------------------------------- |
| Parse CLI arguments | mri |
| Interact with the CLI | Inquirer.js |
| Execute commands | commandland |
| JSON and text store | dot-store |
``bash`
npm install --save-dev task-env
`bash`
touch run
chmod +x run
`js
#!/usr/bin/env node
require("task-env")({
args: process.argv.slice(2),
tasks: [
{
sayHello: ({ hello }) => {
console.log(">", hello)
},
},
],
})
`
`bash`
./run sayHello --hello=hi
> hi
Export task:
`js`
export function sayHello({ hello }) {
console.log(hello)
}
Require task:
`js
#!/usr/bin/env node
require("task-env")({
args: process.argv.slice(2),
tasks: [require("./say-hello")],
})
`
`js`
export async function happy({ ask }) {
let { happy } = await ask([
{
type: "confirm",
name: "happy",
message: "Are you happy?",
},
])
}
See the Inquirer.js prompt docs for available options.
`js
export function sayHello({ tasks }) {
tasks.say({ text: "hello" })
}
export function say({ text }) {
console.log(">", text)
}
`
Calling through tasks binds the CLI arguments and helper functions, as if the task were called via CLI.
`js`
export async function ls({ run }) {
await run("ls", ["/"])
}
See the commandland docs for available options.
Task env uses dot-store to provide an immutable store with atomic filesystem persistence.
Create a directory with some JSON files:
`json`
{
"users": {
"bob": {
"key": "~/.ssh/bob_rsa"
}
}
}
The stores option allows you to define multiple named stores:
`js
#!/usr/bin/env node
require("task-env")({
args: process.argv.slice(2),
stores: {
config: {
pattern: "*/",
root: __dirname,
},
},
tasks: [require("./tasks/user")],
})
`
Within your task, get and set JSON using dot-style property strings:
`jsusers.${name}.key
export async function user({ config, name, key }) {
if (key) {
await config.set(, key)
}
console.log(">", config.get(users.${name}))`
}
Run via CLI:
`bash`
./run user --name=bob --key=~/.ssh/id_rsa
> { key: "~/.ssh/id_rsa" }
| Option | Example | Purpose |
| -------- | --------------------------------------------- | -------------------------------------------- |
| alias | {h: ["help"]} | CLI arguments aliases |[config=>config]
| preSetup | | Pre-setup functions (before argv parsing) |[config=>config]
| setup | | Setup functions |{store: {root: __dirname, pattern: "*/"}}
| stores | | Store configurations |[args=>{}]
| teardown | | Teardown functions |[{ task: ({})=>{} }]` | Task functions |
| tasks |