fs_paths.ts

Gro's boundary from paths that arrive from outside to the fs host module's handles.

A path from a CLI arg, a config, or one of Gro's well-known paths names a file by absolute or project-relative path, while a handle takes only relative paths beneath it. An FsPaths converts one into the other where the path enters code that reaches the filesystem. It follows symlinks the way fs.open_dir does, the same inside and outside the project, so the confinement of a handle applies only to the paths code resolves beneath a handle it already holds.

It opens any path, so it's built from the root Fs and never from a handle: a composition root that imports the host modules builds one with create_fs_paths(fs, {cwd: cwd(), home: env.get('HOME')}) and passes it down, and code given one reaches what the root's fs reaches. Code that holds a handle gets narrower handles from it, never wider ones by path. The root reads the working directory and HOME for the directories its removals refuse, so this module reads nothing of the process and imports no host module.

view source

Declarations
#

4 declarations

create_fs_paths
#

fs_paths.ts view source

(fs: Fs, options: FsPathsOptions): FsPaths import {create_fs_paths} from '@fuzdev/gro/fs_paths.js';

Builds the boundary over the fs root, resolving relative paths against fs.project.

fs

the fs host module's root, fs from fs.ts or a mock of it

type Fs

options

the working directory and $HOME, which its removals refuse

returns

FsPaths

throws

  • TypeError - when `options.cwd` isn't an absolute path

FsOpenedPath
#

fs_paths.ts view source

FsOpenedPath import type {FsOpenedPath} from '@fuzdev/gro/fs_paths.js';

A path resolved to a handle and the relative path beneath it.

dir

type FsDir

path

Relative to dir, '' for dir itself, keeping a requested trailing /.

type string

FsPaths
#

fs_paths.ts view source

FsPaths import type {FsPaths} from '@fuzdev/gro/fs_paths.js';

Opens paths from outside as handles. A relative path is relative to project, resolved lexically, its .. segments before any symlink is followed, as Node's path.resolve does. Then it's resolved to its real path, every symlink in it followed, a dangling final one to its missing target, and a missing part appended to its nearest existing ancestor's real path. A real path at or beneath the project is relative to project; any other opens its parent, or the parent's nearest existing ancestor, with fs.open_dir. So the op on the result follows no symlink but ones created since, and a symlink out of the project, at the end of the path or before it, reaches what it points at.

project

The project directory, the fs.project of the Fs this was built from, the dir of every path that resolves beneath it.

type FsDir

readonly

open_path

Converts a path into a handle and the relative path beneath it.

type (path: string): Promise<FsOpenedPath>

path

absolute, or relative to the project; a trailing / is kept

type string
returns Promise<FsOpenedPath>

throws

  • FsError - `not_a_directory` when, outside the project, an ancestor of the path is a file (beneath the project, the op on the result throws it instead); `io_error` for a symlink loop (ELOOP); `permission_denied` or `io_error` when reading a directory or symlink on the way fails otherwise

open_dir_path

Opens a directory, resolved as open_path resolves it: beneath the project, project and the directory's path beneath it, else the directory as its own handle, so what's found beneath it reaches nothing above.

type (path: string): Promise<FsOpenedPath | null>

path

absolute, or relative to the project

type string
returns Promise<FsOpenedPath | null>

the handle and the directory's path beneath it; null when nothing is at a path outside the project, while beneath it the result is project and the path, missing or not, and the op on it reports a missing directory

throws

  • FsError - `not_a_directory` when `path` is a file outside the project (beneath the project, the op on the result throws it instead), otherwise as `open_path` does

search_path

Searches a directory as fs_search does, with ids that start with the path as named, made absolute against the project. The directory is opened as open_dir_path opens it, so the search reaches nothing above it.

type (path: string, options?: Omit<FsSearchOptions, "path" | "id_dir"> | undefined): Promise<FsSearchResult[]>

path

absolute, or relative to the project

type string

options?

type Omit<FsSearchOptions, "path" | "id_dir">
optional
returns Promise<FsSearchResult[]>

the found paths, or an empty array when nothing is at path

throws

  • FsError - when `path` isn't a directory, or a listing fails

copy_path

Copies a file, or with recursive a directory tree, as FsDir.copy does, symlinks in a tree copied as links. Both paths are resolved as open_path resolves them, and the copy runs beneath project when both are in the project, else beneath their nearest common directory.

type (from: string, to: string, options?: FsCopyOptions | undefined): Promise<void>

from

absolute, or relative to the project

type string

to

absolute, or relative to the project

type string

options?

optional
returns Promise<void>

throws

  • FsError - as `open_path` and `FsDir.copy` do

empty_dir_path

Empties a directory, its symlinks followed, removing everything in it but never the directory itself, so a symlink to it is kept.

type (path: string): Promise<void>

path

absolute, or relative to the project

type string
returns Promise<void>

throws

  • FsError - `io_error` when the directory is the project, the working directory or `$HOME` it was built with (`FsPathsOptions`), or an ancestor of one; `not_a_directory` when it's a file; as `fs.real_path` throws for `$HOME`, unless it's missing, before anything is removed; otherwise as `open_dir_path` and `FsDir.remove` do

remove_path

Removes the entry at a path, never what a final symlink points at: the parent is resolved as open_path resolves it, symlinks followed, and the final entry is removed as FsDir.remove does, a symlink removed itself. A trailing / removes only a directory, as in FsDir.remove.

type (path: string, options?: FsRemoveOptions | undefined): Promise<void>

path

absolute, or relative to the project

type string

options?

optional
returns Promise<void>

throws

  • FsError - `io_error` for the project, the working directory or `$HOME` it was built with (`FsPathsOptions`), or an ancestor of one; as `fs.real_path` throws for `$HOME`, unless it's missing, before anything is removed; otherwise as `open_path` and `FsDir.remove` do

FsPathsOptions
#

fs_paths.ts view source

FsPathsOptions import type {FsPathsOptions} from '@fuzdev/gro/fs_paths.js';

What an FsPaths protects beyond its project: the directories its remove_path and empty_dir_path refuse, with their ancestors and /, beside the project. A root reads both from the host modules, so leaving one out fails to typecheck.

cwd

The process's working directory, an absolute real path, as cwd() from the cwd host module returns, protected as given whatever project the FsPaths is over.

type string

home

$HOME, env.get('HOME') from the env host module, protected as given and as its real path, which the first removal or emptying resolves through fs.real_path. undefined, empty, or relative, as an unset or malformed HOME reads, protects nothing. On fgro, reading HOME takes --allow-env=HOME, and resolving its real path takes --allow-read on $HOME, or on its nearest existing ancestor when it's missing, without which the read is refused and the removal fails before it acts.

type string | undefined

Depends on
#

Imported by
#