fs

The fs module provides functions for working with files and directories.

import "conjure/fs"

Path Checking

exists

Checks if a path exists (file or directory).

if fs.exists("config.json") {
    io.println("Config file found")
}

Signature:

func exists(path string) bool

isDir

Checks if a path is a directory.

if fs.isDir("./output") {
    io.println("Output directory exists")
}

Signature:

func isDir(path string) bool

isFile

Checks if a path is a regular file.

if fs.isFile("main.cjr") {
    io.println("Source file found")
}

Signature:

func isFile(path string) bool

File Operations

readFile

Reads the entire contents of a file into a string. Returns an empty string if the file cannot be read.

var content = fs.readFile("config.json")
if content.len == 0 {
    io.println("Could not read config file")
}

Signature:

func readFile(path string) string

writeFile

Writes a string to a file, creating it if it doesn’t exist. Overwrites existing content. Returns true on success, false on failure.

var success = fs.writeFile("output.txt", "Hello, World!")
if not success {
    io.println("Failed to write file")
}

Signature:

func writeFile(path string, content string) bool

Directory Operations

mkdir

Creates a directory and all parent directories if needed. Returns true on success or if the directory already exists.

fs.mkdir("./output/reports")  // Creates both 'output' and 'reports' if needed

Signature:

func mkdir(path string) bool

cwd

Returns the current working directory.

var workDir = fs.cwd()
io.println("Working in: #{workDir}")

Signature:

func cwd() string

Example: Safe File Copy

import "conjure/fs"
import "conjure/path"
import "conjure/io"

func copyFile(src string, dest string) bool {
    // Check source exists
    if not fs.isFile(src) {
        io.println("Source file not found: #{src}")
        return false
    }

    // Ensure destination directory exists
    var destDir = path.dir(dest)
    if not fs.mkdir(destDir) {
        io.println("Could not create directory: #{destDir}")
        return false
    }

    // Read and write
    var content = fs.readFile(src)
    return fs.writeFile(dest, content)
}

Example: Find Config File

import "conjure/fs"
import "conjure/path"

func findConfig() string {
    // Check common locations
    var locations = []string{
        "./config.json",
        "./config/settings.json",
        path.join(fs.cwd(), "config.json"),
    }

    for loc in locations {
        if fs.isFile(loc) {
            return loc
        }
    }

    return ""
}