Run midas tasks on nodejs. midas_node supports all of the defined effects in the latest version of the midas library.
Install the required dependencies for the node environment as well as the gleam dependency
npm install --save @zip.js/zip.js rollup @rollup/plugin-node-resolve chokidar
gleam add midas_node
Use node.run
with a task and root position in the file system.
All calls to Read
Write
or List
(contents of a directory) are relative to this path.
import midas/node
pub fn main() {
let task = // a midas task
node.run(task, "/root")
}
Use node.watch
to run a task and the rerun it when required in response to changes on the filesystem.
See the following example in the example directory.
import midas/node
// print the reason for a task failing
fn callback(result) {
case result {
Ok(Nil) -> Nil
Error(reason) -> io.println(snag.pretty_print(reason))
}
}
pub fn main() {
let task = {
use src <- t.do(t.bundle("example/client", "run"))
use page <- t.do(t.read("src/index.html"))
t.done([#("/", page), #("/main.js", <<src:utf8>>)])
}
node.watch(task, "/root", callback)
}
Watch me talk through this example
Note: watch is lazy, and will only rerun the task from the point where a file has changed.
In the previous example if index.html
is changed then the project will not be rebundled.
This makes watch more responsive. To get the most out response watch pipeline put slow tasks earlier in the task.
(asynchrony is coming soon to make things even faster but the API is still being finalised)
The result of calls to HTTP fetch are assumed unchanged so will be rerun if only a previous files value has changed.
Further documentation can be found at https://hexdocs.pm/midas_node.
gleam run # Run the project
gleam test # Run the tests