glimr/filesystem/filesystem
Filesystem
Console commands like make:controller and make:migration need to scaffold files from templates, but the file I/O boilerplate — read stub, create directories, replace variables, write output — would be duplicated across every command. This module centralizes that workflow so commands just specify which stub to use, where to write it, and what variables to substitute.
Values
pub fn directory_exists(
path: String,
) -> Result(Bool, simplifile.FileError)
Some commands need to verify that a project directory exists before generating files into it — for example, checking that src/controllers/ is there before creating a new controller. Without this check, generated files could end up in unexpected locations.
pub fn ensure_directory_exists(
file_path: String,
) -> Result(Nil, simplifile.FileError)
When you run make:controller admin/users, the target
directory src/controllers/admin/ probably doesn’t exist
yet. Nobody wants to manually mkdir before every scaffold
command, so this creates the full directory tree
automatically. It’s a no-op if the path already exists, so
it’s safe to call every time.
pub fn file_exists(
path: String,
) -> Result(Bool, simplifile.FileError)
Scaffold commands check this before writing so they can warn developers instead of silently overwriting existing code. Accidentally clobbering a controller someone has spent hours customizing would be a terrible experience — better to ask for confirmation first.
pub fn read_stub(
package: String,
stub_path: String,
) -> Result(String, Nil)
Stubs live in each package’s priv/stubs/ directory so they ship with the compiled package and are available at runtime. Using wisp.priv_directory means the path resolves correctly regardless of how or where the package is installed — no hardcoded paths that break when someone installs from Hex.
pub fn write_from_stub(
package: String,
stub_path: String,
dest_path: String,
) -> Result(Nil, Nil)
For static files like .gitkeep or boilerplate config that don’t need any variable substitution, this handles the entire read-stub-create-dirs-write workflow in one call. Most scaffold commands use the _with_variables variant instead, but this keeps the simple cases simple.
pub fn write_from_stub_with_variables(
package package: String,
stub_path stub_path: String,
dest_path dest_path: String,
variables variables: List(#(String, String)),
) -> Result(Nil, Nil)
The workhorse of file scaffolding — reads a stub template,
replaces @{{ variable }} markers with real values (module
name, table name, route path, etc.), and writes the result.
Labeled arguments are essential here because four string
parameters in a row would be a nightmare to get in the right
order.