@wizulus/dev-term v1.0.2
dev-term README
This library is a utility for running and managing development tasks in an ANSI enabled terminal. The tool allows you to define a set of tasks and execute them in a specific order. Each task is defined by a set of directives that dictate how it should be run and how it interacts with other tasks.
When the script is running, certain keys will perform different tasks:
<Left>and<Right>: Select a running task.K: Kill the selected task.R: Restart the selected task.QorCtrl-C: Kill all tasks and exit.
Directives
await <name>: [cwd] <command line>
This directive runs a process and waits for it to exit before proceeding. <name> is a unique identifier for the task, [cwd] is an optional current working directory for the process, and <command line> is the command to be executed.
await build: ./project/ npm run build
start server: ./project/ node server.jstask <name>: [cwd] <command line>
This directive creates a task but doesn't run it right away. Use restart <name> to start the task. <name> is a unique identifier for the task, [cwd] is an optional current working directory for the process, and <command line> is the command to be executed.
task watch: ./project/ npm run watch
restart watchstart <name>: [cwd] <command line>
This directive runs a process and continues with the script. <name> is a unique identifier for the task, [cwd] is an optional current working directory for the process, and <command line> is the command to be executed.
start server: ./project/ node server.jsonce <otherNames>: <text>
This directive listens for text to be emitted from other processes, then triggers child script once. <otherNames> is a comma-separated list of unique identifiers for the tasks to be listened to, and <text> is the text to be matched.
once build: Compilation complete.
start server: ./project/ node server.jswhen <otherNames>: <text>
This directive listens for text to be emitted from other processes, and triggers child script each time. <otherNames> is a comma-separated list of unique identifiers for the tasks to be listened to, and <text> is the text to be matched.
when tests: PASS
start server: ./project/ node server.jsafter <name>: <otherNames>
This directive references when and once directives. After each of them has triggered at least once, triggers child script. <name> is a unique identifier for the task, and <otherNames> is a comma-separated list of unique identifiers for the tasks that should have already triggered.
when tests: PASS
once build: Compilation complete.
after start: tests,build
start server: ./project/ node server.jsrestart <otherNames>
This directive triggers other script directives to restart. For script directives that run processes, existing processes are killed. For script directives that listen for text, resets their tracking. <otherNames> is a comma-separated list of unique identifiers for the tasks to be restarted.
start server: ./project/ node server.js
when server: [FATAL]
restart servergroup <name>:
This directive provides a logical grouping for script organization. Runs child script immediately. <name> is a unique identifier for the group.
group database:
start postgres: ./database/ postgres
start redis: ./database/ redis
start server: ./project/ node server.jskill <otherNames>
This directive kills a running process. <otherNames> is a comma-separated list of unique identifiers for the tasks to be killed.
start server: ./project/ node server.js
kill serverSample Script
- Run
npm install --save-dev @wizulus/dev-termto install this library. - Create a
dev.mjsin your project directory. - Run
chmod +x dev.mjs - Define your script:
#!/usr/bin/env node
import { script } from '@wizulus/dev-term/main.mjs'
script`
await install: ./project/ npm install
task build: ./project/ npm run build
when build: Compilation complete.
after start: build
group database:
start postgres: ./database/ postgres
start redis: ./database/ redis
start server: ./project/ node server.js
group error-check:
when server: Error listening on port 3000.
restart server
when build: ERROR
kill server
`- Run
./dev.mjsto run your script.
The sample script performs the following steps:
- Waits for dependencies to be installed in the
./project/directory. - Defines a
buildtask that runs thebuildcommand in the./project/directory but doesn't start it yet. - Waits for the
buildtask to emit the messageCompilation complete.before proceeding. - Defines an
afterdirective that waits for thebuildtask to complete before starting the rest of the script. - Defines a
databasegroup that starts thepostgresandredisservices in the./database/directory. - Starts the
server.jsscript in the./project/directory and waits for it to emit the messageServer listening on port 3000. - Defines an
error-checkgroup that listens for error messages from theserver.jsscript and thebuildtask. - If the
server.jsscript emits the messageError listening on port 3000., theserver.jsscript is restarted. - If the
buildtask emits the messageERROR, theserver.jsscript is killed.
Overall, this script demonstrates the use of various directives to manage a set of development tasks. The await directive is used to wait for dependencies to be installed, while the task directive is used to define a task that can be restarted later. The when and after directives are used to control the order in which tasks are executed, and the group directive is used to organize tasks into logical groups. Finally, the kill and restart directives are used within a child script to handle errors that may occur during the execution of the main script.