char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somport file extension to typescriptdea05b4

main
5.3 KiB188 linesraw
1import {
2  op_file_chmod_sync as chmodOp,
3  op_file_chown_sync as chownOp,
4  op_file_copy_sync as copyOp,
5  op_file_lchown_sync as lchownOp,
6  op_file_link_sync as linkOp,
7  op_file_lutime_sync as lutimeOp,
8  op_file_mkdir_sync as mkdirOp,
9  op_file_open_sync as openOp,
10  op_file_path_stat_sync as pathStatOp,
11  op_file_path_truncate_sync as truncatePathOp,
12  op_file_read_dir_sync as readDirOp,
13  op_file_read_link_sync as readLinkOp,
14  op_file_real_path_sync as realPathOp,
15  op_file_remove_sync as removeOp,
16  op_file_rename_sync as renameOp,
17  op_file_symlink_sync as symlinkOp,
18  op_file_utime_sync as utimeOp,
19} from "ext:core/ops";
20import { dictionary, required } from "ext:web/idl.js";
21import { RangeError, TypeError } from "ext:core/primordials";
22import {
23  args,
24  fileHandle,
25  integer,
26  metadata,
27  mode,
28  openArgs,
29  owner,
30  path,
31  stamp,
32} from "./main.js";
33import { result } from "./errors.js";
34
35export { errors, FileHandle, SeekMode } from "./main.js";
36
37export function open(pathname: unknown, options: unknown = undefined) {
38  required(arguments.length, 1);
39  return fileHandle(result(openOp(openArgs(pathname, options))));
40}
41export function create(pathname: unknown) {
42  required(arguments.length, 1);
43  return open(pathname, {
44    read: true,
45    write: true,
46    create: true,
47    truncate: true,
48  });
49}
50export function stat(pathname: unknown) {
51  required(arguments.length, 1);
52  return metadata(
53    result(pathStatOp(args(path(pathname), true))),
54  );
55}
56export function lstat(pathname: unknown) {
57  required(arguments.length, 1);
58  return metadata(
59    result(pathStatOp(args(path(pathname), false))),
60  );
61}
62export function* readDir(
63  pathname: unknown,
64): Generator<
65  { name: string; isFile: boolean; isDirectory: boolean; isSymlink: boolean }
66> {
67  required(arguments.length, 1);
68  const entries = result(readDirOp(path(pathname)));
69  for (let i = 0; i < entries.length; i++) {
70    const row = entries[i];
71    yield {
72      name: row[0],
73      isFile: row[1],
74      isDirectory: row[2],
75      isSymlink: row[3],
76    };
77  }
78}
79export function mkdir(pathname: unknown, options: unknown = undefined): void {
80  required(arguments.length, 1);
81  const filename = path(pathname), mode = dictionary(options).mode;
82  if (mode !== undefined && integer(mode) > 0o7777) {
83    throw new RangeError("Invalid directory mode");
84  }
85  result(
86    mkdirOp(args(filename, mode === undefined ? null : mode as number)),
87  );
88}
89export function remove(pathname: unknown, options: unknown = undefined): void {
90  required(arguments.length, 1);
91  const filename = path(pathname), recursive = dictionary(options).recursive;
92  if (recursive !== undefined && typeof recursive !== "boolean") {
93    throw new TypeError("recursive must be boolean");
94  }
95  result(removeOp(args(filename, recursive === true)));
96}
97export function rename(source: unknown, destination: unknown): void {
98  required(arguments.length, 2);
99  result(renameOp(args(path(source), path(destination))));
100}
101export function copyFile(source: unknown, destination: unknown): void {
102  required(arguments.length, 2);
103  result(copyOp(args(path(source), path(destination))));
104}
105export function symlink(
106  target: unknown,
107  linkPath: unknown,
108  options: unknown = undefined,
109): void {
110  required(arguments.length, 2);
111  const contents = path(target),
112    link = path(linkPath),
113    kind = dictionary(options).type;
114  if (kind !== undefined && kind !== "file" && kind !== "dir") {
115    throw new TypeError("Symlink type must be file or dir");
116  }
117  result(
118    symlinkOp(args(contents, link, kind === undefined ? null : kind)),
119  );
120}
121export function readLink(pathname: unknown): string {
122  required(arguments.length, 1);
123  return result(readLinkOp(path(pathname)));
124}
125export function realPath(pathname: unknown): string {
126  required(arguments.length, 1);
127  return result(realPathOp(path(pathname)));
128}
129export function chmod(pathname: unknown, fileMode: number): void {
130  required(arguments.length, 2);
131  result(chmodOp(args(path(pathname), mode(fileMode))));
132}
133export function chown(
134  pathname: unknown,
135  uid: number | null | undefined,
136  gid: number | null | undefined,
137): void {
138  required(arguments.length, 3);
139  result(
140    chownOp(args(path(pathname), owner(uid), owner(gid))),
141  );
142}
143export function lchown(
144  pathname: unknown,
145  uid: number | null | undefined,
146  gid: number | null | undefined,
147): void {
148  required(arguments.length, 3);
149  result(
150    lchownOp(args(path(pathname), owner(uid), owner(gid))),
151  );
152}
153export function utime(
154  pathname: unknown,
155  atime: number | Date,
156  mtime: number | Date,
157): void {
158  required(arguments.length, 3);
159  const before = stamp(atime), after = stamp(mtime);
160  result(
161    utimeOp(
162      args(path(pathname), before[0], before[1], after[0], after[1]),
163    ),
164  );
165}
166export function lutime(
167  pathname: unknown,
168  atime: number | Date,
169  mtime: number | Date,
170): void {
171  required(arguments.length, 3);
172  const before = stamp(atime), after = stamp(mtime);
173  result(
174    lutimeOp(
175      args(path(pathname), before[0], before[1], after[0], after[1]),
176    ),
177  );
178}
179export function link(oldpath: unknown, newpath: unknown): void {
180  required(arguments.length, 2);
181  result(linkOp(args(path(oldpath), path(newpath))));
182}
183export function truncate(pathname: unknown, length = 0): void {
184  required(arguments.length, 1);
185  result(
186    truncatePathOp(args(path(pathname), integer(length))),
187  );
188}