Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 485x 485x 948x 265x 31x 2x 2x | import { dirname } from "node:path";
import IFileSystem from "../../types/IFileSystem";
/**
* 3.1 Write: the only module that changes the filesystem (#1653, carrying
* #1451 box 1).
*
* Every file written, directory created, file deleted and file moved goes
* through here, and through the port the host injected. Reads go through the
* port directly, since reading changes nothing. A path is decided before it
* arrives: naming an output never creates its directory, and writing it does.
* `write-confined-to-3-1.test.ts` holds that no other module calls the port's
* mutating methods. The one exception is the port's own `withTempFile`: a
* scratch file an external tool reads, removed before the call returns, so
* not output (see `IFileSystem`).
*/
class Write {
/** Write `content` to `path`, creating its directory first. */
static file(fs: IFileSystem, path: string, content: string): void {
Write.directory(fs, dirname(path));
fs.writeFile(path, content);
}
/** Create `path` and its parents, if it does not exist yet. */
static directory(fs: IFileSystem, path: string): void {
if (!fs.exists(path)) {
fs.mkdir(path, { recursive: true });
}
}
/** Delete the file at `path`. */
static remove(fs: IFileSystem, path: string): void {
fs.unlink(path);
}
/** Move the file at `from` to `to`, creating `to`'s directory first. */
static move(fs: IFileSystem, from: string, to: string): void {
Write.directory(fs, dirname(to));
fs.rename(from, to);
}
}
export default Write;
|