The operations a program performs on Node.js, and the NodeProgram type a command-line program is. module.f.mjs declares the operations and their helpers, types.ts their types, and module.mjs is the runner that performs them against Node's own APIs.
import { exitStep, readUtf8File } from './module.f.mjs'
import { log } from '../common/module.f.mjs'
import { mapStep, step } from '../module.f.mjs'
/** Prints how many lines a file has, counting line breaks as `wc -l` does. @type {import('./types.ts').NodeProgram} */
export const main = ({ args }) => {
const count = mapStep(readUtf8File(args[0]), text => text.split('\n').length - 1)
const printed = step(count, n => log(`${n} lines`))
return exitStep(printed)
}
fjs run lines.f.mjs README.md runs it. exitStep is the exit-code policy at the end of the chain: 0 on success, and on failure the error printed to stderr and 1.
| Group | Operations |
|---|---|
| files | readFile, readUtf8File, readWhole, writeFile, writeUtf8File, writeFiles, writeBytes, writeChunks, mkdir, readdir, rm, rmdir, rename, access, exists, stat, open, fstat, pread, close, createExclusive, writeExclusive |
| network | fetch, createServer, listen, forever |
| processes | exec, spawn, childWait |
| other | inflate, now, resolveFileModule, test |
from common | write, log, error, errorExit, read, readLine, sandbox, catch_, import_, all, allOk, both, re-exported here |
Every operation answers an IoResult<T>: the value, an IoError (the host's failure, with its OS code such as 'ENOENT'), or NotImplemented. isNotFound and isDirectory test an error, so a program can forgive the one it expects: catchStep(readFile(path), orElse(isNotFound, null)).
| Runner | Use it for |
|---|---|
run(main) in module.mjs | a bin script: performs the program, then exits with its code |
runEffect(main) in module.mjs | the same, resolving to the exit code without exiting |
virtual | proofs: a synchronous, in-memory file system and console |
memory | the asynchronous interpreter for ../memory's operations |
A program does not import the runner; the fjs CLI and the tests do.