@gjsify/fs 0.3.15 → 0.3.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1 @@
1
- //#region \0rolldown/runtime.js
2
- var __defProp = Object.defineProperty;
3
- var __exportAll = (all, no_symbols) => {
4
- let target = {};
5
- for (var name in all) {
6
- __defProp(target, name, {
7
- get: all[name],
8
- enumerable: true
9
- });
10
- }
11
- if (!no_symbols) {
12
- __defProp(target, Symbol.toStringTag, { value: "Module" });
13
- }
14
- return target;
15
- };
16
-
17
- //#endregion
18
- export { __exportAll };
1
+ var e=Object.defineProperty,t=(t,n)=>{let r={};for(var i in t)e(r,i,{get:t[i],enumerable:!0});return n||e(r,Symbol.toStringTag,{value:`Module`}),r};export{t as __exportAll};
@@ -1,356 +1 @@
1
- import { normalizePath } from "./utils.js";
2
- import { BigIntStats, STAT_ATTRIBUTES, Stats } from "./stats.js";
3
- import { FileHandle } from "./file-handle.js";
4
- import { createNodeError } from "./errors.js";
5
- import { accessSync, appendFileSync, chmodSync, chownSync, copyFileSync, mkdirSync, readFileSync, readdirSync, readlinkSync, realpathSync, renameSync, rmdirSync, truncateSync, writeFileSync } from "./sync.js";
6
- import { open as open$1, rm as rm$1 } from "./promises.js";
7
- import GLib from "@girs/glib-2.0";
8
- import Gio from "@girs/gio-2.0";
9
- import { Buffer } from "node:buffer";
10
-
11
- //#region src/callback.ts
12
- function parseOptsCb(optionsOrCallback, maybeCallback) {
13
- return typeof optionsOrCallback === "function" ? {
14
- options: {},
15
- callback: optionsOrCallback
16
- } : {
17
- options: optionsOrCallback ?? {},
18
- callback: maybeCallback
19
- };
20
- }
21
- function statImpl(path, flags, syscall, options, callback) {
22
- const pathStr = normalizePath(path);
23
- const file = Gio.File.new_for_path(pathStr);
24
- file.query_info_async(STAT_ATTRIBUTES, flags, GLib.PRIORITY_DEFAULT, null, (_s, res) => {
25
- try {
26
- const info = file.query_info_finish(res);
27
- callback(null, options?.bigint ? new BigIntStats(info, pathStr) : new Stats(info, pathStr));
28
- } catch (err) {
29
- callback(createNodeError(err, syscall, pathStr));
30
- }
31
- });
32
- }
33
- function stat(path, optionsOrCallback, maybeCallback) {
34
- const { options, callback } = parseOptsCb(optionsOrCallback, maybeCallback);
35
- statImpl(path, Gio.FileQueryInfoFlags.NONE, "stat", options, callback);
36
- }
37
- function lstat(path, optionsOrCallback, maybeCallback) {
38
- const { options, callback } = parseOptsCb(optionsOrCallback, maybeCallback);
39
- statImpl(path, Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, "lstat", options, callback);
40
- }
41
- function readdir(path, optionsOrCallback, maybeCallback) {
42
- const { options, callback } = parseOptsCb(optionsOrCallback, maybeCallback);
43
- Promise.resolve().then(() => {
44
- try {
45
- callback(null, readdirSync(path, options));
46
- } catch (err) {
47
- callback(createNodeError(err, "readdir", path));
48
- }
49
- });
50
- }
51
- function realpath(path, optionsOrCallback, maybeCallback) {
52
- const { callback } = parseOptsCb(optionsOrCallback, maybeCallback);
53
- Promise.resolve().then(() => {
54
- try {
55
- callback(null, realpathSync(path));
56
- } catch (err) {
57
- callback(err);
58
- }
59
- });
60
- }
61
- function symlink(target, path, typeOrCallback, maybeCallback) {
62
- const callback = typeof typeOrCallback === "function" ? typeOrCallback : maybeCallback;
63
- if (typeof callback !== "function") {
64
- throw new TypeError("Callback must be a function. Received " + typeof callback);
65
- }
66
- const pathStr = normalizePath(path);
67
- const targetStr = normalizePath(target);
68
- const file = Gio.File.new_for_path(pathStr);
69
- file.make_symbolic_link_async(targetStr, GLib.PRIORITY_DEFAULT, null, (_s, res) => {
70
- try {
71
- file.make_symbolic_link_finish(res);
72
- callback(null);
73
- } catch (err) {
74
- callback(createNodeError(err, "symlink", targetStr, pathStr));
75
- }
76
- });
77
- }
78
- function open(path, ...args) {
79
- let flags;
80
- let mode;
81
- let callback;
82
- switch (args.length) {
83
- case 1:
84
- callback = args[0];
85
- break;
86
- case 2:
87
- flags = args[0];
88
- callback = args[1];
89
- break;
90
- case 3:
91
- flags = args[0];
92
- mode = args[1];
93
- callback = args[2];
94
- break;
95
- default: break;
96
- }
97
- open$1(path, flags, mode).then((fileHandle) => {
98
- callback(null, fileHandle.fd);
99
- }).catch((err) => {
100
- callback(err, -1);
101
- });
102
- }
103
- function write(fd, data, ...args) {
104
- const fileHandle = FileHandle.getInstance(fd);
105
- if (typeof data === "string") {
106
- const callback = args.pop();
107
- const position = args[0];
108
- const encoding = args[1];
109
- fileHandle.write(data, position, encoding).then((res) => {
110
- callback(null, res.bytesWritten, res.buffer);
111
- }).catch((err) => {
112
- callback(err, 0, "");
113
- });
114
- return;
115
- }
116
- const callback = args[args.length - 1];
117
- const offset = args[0];
118
- const length = args[1];
119
- const position = args[2];
120
- fileHandle.write(data, offset, length, position).then((res) => {
121
- callback(null, res.bytesWritten, res.buffer);
122
- }).catch((err) => {
123
- callback(err, 0, Buffer.from([]));
124
- });
125
- }
126
- function read(fd, ...args) {
127
- const fileHandle = FileHandle.getInstance(fd);
128
- const callback = args[args.length - 1];
129
- let buffer;
130
- let offset;
131
- let length;
132
- let position;
133
- if (args.length <= 1) {} else if (typeof args[0] === "object" && !ArrayBuffer.isView(args[0])) {
134
- const options = args[0];
135
- buffer = options.buffer;
136
- offset = options.offset;
137
- length = options.length;
138
- position = options.position;
139
- } else {
140
- buffer = args[0];
141
- offset = args[1];
142
- length = args[2];
143
- position = args[3];
144
- }
145
- fileHandle.read(buffer, offset, length, position).then((res) => {
146
- callback(null, res.bytesRead, res.buffer);
147
- }).catch((err) => {
148
- callback(err, 0, Buffer.from([]));
149
- });
150
- }
151
- /**
152
- * Closes the file descriptor. No arguments other than a possible exception are
153
- * given to the completion callback.
154
- *
155
- * Calling `fs.close()` on any file descriptor (`fd`) that is currently in use
156
- * through any other `fs` operation may lead to undefined behavior.
157
- *
158
- * See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail.
159
- * @since v0.0.2
160
- */
161
- function close(fd, callback) {
162
- FileHandle.getInstance(fd).close().then(() => {
163
- callback(null);
164
- }).catch((err) => callback(err));
165
- }
166
- function rm(path, ...args) {
167
- let options = {};
168
- let callback = args[args.length - 1];
169
- if (args.length >= 2) {
170
- options = args[0];
171
- }
172
- rm$1(path, options).then(() => {
173
- callback(null);
174
- }).catch((err) => {
175
- callback(err);
176
- });
177
- }
178
- function rename(oldPath, newPath, callback) {
179
- Promise.resolve().then(() => {
180
- try {
181
- renameSync(oldPath, newPath);
182
- callback(null);
183
- } catch (err) {
184
- callback(err);
185
- }
186
- });
187
- }
188
- function copyFile(src, dest, modeOrCb, maybeCb) {
189
- const mode = typeof modeOrCb === "function" ? 0 : modeOrCb;
190
- const callback = typeof modeOrCb === "function" ? modeOrCb : maybeCb;
191
- Promise.resolve().then(() => {
192
- try {
193
- copyFileSync(src, dest, mode);
194
- callback(null);
195
- } catch (err) {
196
- callback(err);
197
- }
198
- });
199
- }
200
- function access(path, modeOrCb, maybeCb) {
201
- const mode = typeof modeOrCb === "function" ? undefined : modeOrCb;
202
- const callback = typeof modeOrCb === "function" ? modeOrCb : maybeCb;
203
- Promise.resolve().then(() => {
204
- try {
205
- accessSync(path, mode);
206
- callback(null);
207
- } catch (err) {
208
- callback(err);
209
- }
210
- });
211
- }
212
- function appendFile(path, data, optsOrCb, maybeCb) {
213
- const callback = typeof optsOrCb === "function" ? optsOrCb : maybeCb;
214
- const options = typeof optsOrCb === "function" ? undefined : optsOrCb;
215
- Promise.resolve().then(() => {
216
- try {
217
- appendFileSync(path, data, options);
218
- callback(null);
219
- } catch (err) {
220
- callback(err);
221
- }
222
- });
223
- }
224
- function readlink(path, optsOrCb, maybeCb) {
225
- const callback = typeof optsOrCb === "function" ? optsOrCb : maybeCb;
226
- const options = typeof optsOrCb === "function" ? undefined : optsOrCb;
227
- Promise.resolve().then(() => {
228
- try {
229
- callback(null, readlinkSync(path, options));
230
- } catch (err) {
231
- callback(err, "");
232
- }
233
- });
234
- }
235
- function truncate(path, lenOrCb, maybeCb) {
236
- const len = typeof lenOrCb === "function" ? 0 : lenOrCb;
237
- const callback = typeof lenOrCb === "function" ? lenOrCb : maybeCb;
238
- Promise.resolve().then(() => {
239
- try {
240
- truncateSync(path, len);
241
- callback(null);
242
- } catch (err) {
243
- callback(err);
244
- }
245
- });
246
- }
247
- function chmod(path, mode, callback) {
248
- Promise.resolve().then(() => {
249
- try {
250
- chmodSync(path, mode);
251
- callback(null);
252
- } catch (err) {
253
- callback(err);
254
- }
255
- });
256
- }
257
- function chown(path, uid, gid, callback) {
258
- Promise.resolve().then(() => {
259
- try {
260
- chownSync(path, uid, gid);
261
- callback(null);
262
- } catch (err) {
263
- callback(err);
264
- }
265
- });
266
- }
267
- function mkdir(path, optsOrCb, maybeCb) {
268
- const callback = typeof optsOrCb === "function" ? optsOrCb : maybeCb;
269
- const options = typeof optsOrCb === "function" ? undefined : optsOrCb;
270
- Promise.resolve().then(() => {
271
- try {
272
- mkdirSync(path, options);
273
- callback(null);
274
- } catch (err) {
275
- callback(err);
276
- }
277
- });
278
- }
279
- function rmdir(path, optsOrCb, maybeCb) {
280
- const callback = typeof optsOrCb === "function" ? optsOrCb : maybeCb;
281
- const options = typeof optsOrCb === "function" ? undefined : optsOrCb;
282
- Promise.resolve().then(() => {
283
- try {
284
- rmdirSync(path, options);
285
- callback(null);
286
- } catch (err) {
287
- callback(err);
288
- }
289
- });
290
- }
291
- function readFile(path, optsOrCb, maybeCb) {
292
- const callback = typeof optsOrCb === "function" ? optsOrCb : maybeCb;
293
- const options = typeof optsOrCb === "function" ? undefined : optsOrCb;
294
- const pathStr = normalizePath(path);
295
- Promise.resolve().then(() => {
296
- try {
297
- const readOpts = typeof options === "string" ? {
298
- encoding: options,
299
- flag: "r"
300
- } : {
301
- encoding: options?.encoding ?? null,
302
- flag: options?.flag ?? "r"
303
- };
304
- callback(null, readFileSync(pathStr, readOpts));
305
- } catch (err) {
306
- callback(err, null);
307
- }
308
- });
309
- }
310
- function writeFile(path, data, optsOrCb, maybeCb) {
311
- const callback = typeof optsOrCb === "function" ? optsOrCb : maybeCb;
312
- const pathStr = normalizePath(path);
313
- Promise.resolve().then(() => {
314
- try {
315
- writeFileSync(pathStr, data);
316
- callback(null);
317
- } catch (err) {
318
- callback(err);
319
- }
320
- });
321
- }
322
- function link(existingPath, newPath, callback) {
323
- const existingStr = normalizePath(existingPath);
324
- const newStr = normalizePath(newPath);
325
- Promise.resolve().then(() => {
326
- try {
327
- const result = GLib.spawn_command_line_sync(`ln ${existingStr} ${newStr}`);
328
- if (!result[0]) {
329
- throw Object.assign(new Error(`EPERM: operation not permitted, link '${existingStr}' -> '${newStr}'`), {
330
- code: "EPERM",
331
- errno: -1,
332
- syscall: "link",
333
- path: existingStr,
334
- dest: newStr
335
- });
336
- }
337
- callback(null);
338
- } catch (err) {
339
- callback(err);
340
- }
341
- });
342
- }
343
- function unlink(path, callback) {
344
- const pathStr = normalizePath(path);
345
- Promise.resolve().then(() => {
346
- try {
347
- GLib.unlink(pathStr);
348
- callback(null);
349
- } catch (err) {
350
- callback(err);
351
- }
352
- });
353
- }
354
-
355
- //#endregion
356
- export { access, appendFile, chmod, chown, close, copyFile, link, lstat, mkdir, open, read, readFile, readdir, readlink, realpath, rename, rm, rmdir, stat, symlink, truncate, unlink, write, writeFile };
1
+ import{normalizePath as e}from"./utils.js";import{BigIntStats as t,STAT_ATTRIBUTES as n,Stats as r}from"./stats.js";import{FileHandle as i}from"./file-handle.js";import{createNodeError as a}from"./errors.js";import{accessSync as o,appendFileSync as s,chmodSync as c,chownSync as l,copyFileSync as u,mkdirSync as d,readFileSync as f,readdirSync as p,readlinkSync as m,realpathSync as h,renameSync as g,rmdirSync as _,truncateSync as v,writeFileSync as y}from"./sync.js";import{open as b,rm as x}from"./promises.js";import S from"@girs/glib-2.0";import C from"@girs/gio-2.0";import{Buffer as w}from"node:buffer";function T(e,t){return typeof e==`function`?{options:{},callback:e}:{options:e??{},callback:t}}function E(i,o,s,c,l){let u=e(i),d=C.File.new_for_path(u);d.query_info_async(n,o,S.PRIORITY_DEFAULT,null,(e,n)=>{try{let e=d.query_info_finish(n);l(null,c?.bigint?new t(e,u):new r(e,u))}catch(e){l(a(e,s,u))}})}function D(e,t,n){let{options:r,callback:i}=T(t,n);E(e,C.FileQueryInfoFlags.NONE,`stat`,r,i)}function O(e,t,n){let{options:r,callback:i}=T(t,n);E(e,C.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,`lstat`,r,i)}function k(e,t,n){let{options:r,callback:i}=T(t,n);Promise.resolve().then(()=>{try{i(null,p(e,r))}catch(t){i(a(t,`readdir`,e))}})}function A(e,t,n){let{callback:r}=T(t,n);Promise.resolve().then(()=>{try{r(null,h(e))}catch(e){r(e)}})}function j(t,n,r,i){let o=typeof r==`function`?r:i;if(typeof o!=`function`)throw TypeError(`Callback must be a function. Received `+typeof o);let s=e(n),c=e(t),l=C.File.new_for_path(s);l.make_symbolic_link_async(c,S.PRIORITY_DEFAULT,null,(e,t)=>{try{l.make_symbolic_link_finish(t),o(null)}catch(e){o(a(e,`symlink`,c,s))}})}function M(e,...t){let n,r,i;switch(t.length){case 1:i=t[0];break;case 2:n=t[0],i=t[1];break;case 3:n=t[0],r=t[1],i=t[2];break;default:break}b(e,n,r).then(e=>{i(null,e.fd)}).catch(e=>{i(e,-1)})}function N(e,t,...n){let r=i.getInstance(e);if(typeof t==`string`){let e=n.pop(),i=n[0],a=n[1];r.write(t,i,a).then(t=>{e(null,t.bytesWritten,t.buffer)}).catch(t=>{e(t,0,``)});return}let a=n[n.length-1],o=n[0],s=n[1],c=n[2];r.write(t,o,s,c).then(e=>{a(null,e.bytesWritten,e.buffer)}).catch(e=>{a(e,0,w.from([]))})}function P(e,...t){let n=i.getInstance(e),r=t[t.length-1],a,o,s,c;if(!(t.length<=1))if(typeof t[0]==`object`&&!ArrayBuffer.isView(t[0])){let e=t[0];a=e.buffer,o=e.offset,s=e.length,c=e.position}else a=t[0],o=t[1],s=t[2],c=t[3];n.read(a,o,s,c).then(e=>{r(null,e.bytesRead,e.buffer)}).catch(e=>{r(e,0,w.from([]))})}function F(e,t){i.getInstance(e).close().then(()=>{t(null)}).catch(e=>t(e))}function I(e,...t){let n={},r=t[t.length-1];t.length>=2&&(n=t[0]),x(e,n).then(()=>{r(null)}).catch(e=>{r(e)})}function L(e,t,n){Promise.resolve().then(()=>{try{g(e,t),n(null)}catch(e){n(e)}})}function R(e,t,n,r){let i=typeof n==`function`?0:n,a=typeof n==`function`?n:r;Promise.resolve().then(()=>{try{u(e,t,i),a(null)}catch(e){a(e)}})}function z(e,t,n){let r=typeof t==`function`?void 0:t,i=typeof t==`function`?t:n;Promise.resolve().then(()=>{try{o(e,r),i(null)}catch(e){i(e)}})}function B(e,t,n,r){let i=typeof n==`function`?n:r,a=typeof n==`function`?void 0:n;Promise.resolve().then(()=>{try{s(e,t,a),i(null)}catch(e){i(e)}})}function V(e,t,n){let r=typeof t==`function`?t:n,i=typeof t==`function`?void 0:t;Promise.resolve().then(()=>{try{r(null,m(e,i))}catch(e){r(e,``)}})}function H(e,t,n){let r=typeof t==`function`?0:t,i=typeof t==`function`?t:n;Promise.resolve().then(()=>{try{v(e,r),i(null)}catch(e){i(e)}})}function U(e,t,n){Promise.resolve().then(()=>{try{c(e,t),n(null)}catch(e){n(e)}})}function W(e,t,n,r){Promise.resolve().then(()=>{try{l(e,t,n),r(null)}catch(e){r(e)}})}function G(e,t,n){let r=typeof t==`function`?t:n,i=typeof t==`function`?void 0:t;Promise.resolve().then(()=>{try{d(e,i),r(null)}catch(e){r(e)}})}function K(e,t,n){let r=typeof t==`function`?t:n,i=typeof t==`function`?void 0:t;Promise.resolve().then(()=>{try{_(e,i),r(null)}catch(e){r(e)}})}function q(t,n,r){let i=typeof n==`function`?n:r,a=typeof n==`function`?void 0:n,o=e(t);Promise.resolve().then(()=>{try{i(null,f(o,typeof a==`string`?{encoding:a,flag:`r`}:{encoding:a?.encoding??null,flag:a?.flag??`r`}))}catch(e){i(e,null)}})}function J(t,n,r,i){let a=typeof r==`function`?r:i,o=e(t);Promise.resolve().then(()=>{try{y(o,n),a(null)}catch(e){a(e)}})}function Y(t,n,r){let i=e(t),a=e(n);Promise.resolve().then(()=>{try{if(!S.spawn_command_line_sync(`ln ${i} ${a}`)[0])throw Object.assign(Error(`EPERM: operation not permitted, link '${i}' -> '${a}'`),{code:`EPERM`,errno:-1,syscall:`link`,path:i,dest:a});r(null)}catch(e){r(e)}})}function X(t,n){let r=e(t);Promise.resolve().then(()=>{try{S.unlink(r),n(null)}catch(e){n(e)}})}export{z as access,B as appendFile,U as chmod,W as chown,F as close,R as copyFile,Y as link,O as lstat,G as mkdir,M as open,P as read,q as readFile,k as readdir,V as readlink,A as realpath,L as rename,I as rm,K as rmdir,D as stat,j as symlink,H as truncate,X as unlink,N as write,J as writeFile};
package/lib/esm/cp.js CHANGED
@@ -1,233 +1 @@
1
- import { normalizePath } from "./utils.js";
2
- import { createNodeError } from "./errors.js";
3
- import Gio from "@girs/gio-2.0";
4
- import { join } from "node:path";
5
-
6
- //#region src/cp.ts
7
- function makeEEXIST(destStr) {
8
- const e = new Error(`ERR_FS_CP_EEXIST: file already exists, copyfile '${destStr}'`);
9
- e.code = "ERR_FS_CP_EEXIST";
10
- e.syscall = "copyfile";
11
- e.path = destStr;
12
- return e;
13
- }
14
- function makeEISDIR(srcStr) {
15
- const e = new Error(`ERR_FS_EISDIR: illegal operation on a directory, copyfile '${srcStr}'`);
16
- e.code = "ERR_FS_EISDIR";
17
- e.syscall = "copyfile";
18
- e.path = srcStr;
19
- return e;
20
- }
21
- function makeENOTDIR(srcStr, destStr) {
22
- const e = new Error(`ENOTDIR: not a directory, copyfile '${srcStr}' -> '${destStr}'`);
23
- e.code = "ENOTDIR";
24
- e.syscall = "copyfile";
25
- e.path = srcStr;
26
- e.dest = destStr;
27
- return e;
28
- }
29
- function makeSYMLINKLOOP(srcStr, destStr) {
30
- const e = new Error(`ELOOP: too many levels of symbolic links, copyfile '${srcStr}' -> '${destStr}'`);
31
- e.code = "ELOOP";
32
- e.syscall = "copyfile";
33
- e.path = srcStr;
34
- e.dest = destStr;
35
- return e;
36
- }
37
- function queryCopyFlags(opts) {
38
- const force = opts.force ?? true;
39
- let flags = Gio.FileCopyFlags.NONE;
40
- if (force) flags |= Gio.FileCopyFlags.OVERWRITE;
41
- if (opts.preserveTimestamps) flags |= Gio.FileCopyFlags.TARGET_DEFAULT_MODIFIED_TIME;
42
- if (!opts.dereference) flags |= Gio.FileCopyFlags.NOFOLLOW_SYMLINKS;
43
- return flags;
44
- }
45
- function copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts) {
46
- const force = opts.force ?? true;
47
- try {
48
- const destInfo = destFile.query_info("standard::type", Gio.FileQueryInfoFlags.NONE, null);
49
- if (destInfo.get_file_type() === Gio.FileType.DIRECTORY) {
50
- throw makeENOTDIR(srcStr, destStr);
51
- }
52
- if (!force) {
53
- if (opts.errorOnExist) throw makeEEXIST(destStr);
54
- return;
55
- }
56
- } catch (e) {
57
- if (typeof e.code === "string") throw e;
58
- }
59
- const flags = queryCopyFlags(opts);
60
- try {
61
- srcFile.copy(destFile, flags, null, null);
62
- } catch (err) {
63
- throw createNodeError(err, "copyfile", srcStr, destStr);
64
- }
65
- }
66
- function cpOneDirSync(srcFile, destFile, srcStr, destStr, opts) {
67
- const sep = srcStr.endsWith("/") ? "" : "/";
68
- if (destStr.startsWith(srcStr + sep) && destStr !== srcStr) {
69
- throw makeSYMLINKLOOP(srcStr, destStr);
70
- }
71
- if (!destFile.query_exists(null)) {
72
- try {
73
- destFile.make_directory_with_parents(null);
74
- } catch (err) {
75
- throw createNodeError(err, "mkdir", destStr);
76
- }
77
- } else {
78
- try {
79
- const destInfo = destFile.query_info("standard::type", Gio.FileQueryInfoFlags.NONE, null);
80
- if (destInfo.get_file_type() !== Gio.FileType.DIRECTORY) {
81
- throw makeENOTDIR(destStr, srcStr);
82
- }
83
- } catch (e) {
84
- if (typeof e.code === "string") throw e;
85
- }
86
- }
87
- let enumerator;
88
- try {
89
- enumerator = srcFile.enumerate_children("standard::name,standard::type,standard::is-symlink", opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
90
- } catch (err) {
91
- throw createNodeError(err, "scandir", srcStr);
92
- }
93
- let childInfo = enumerator.next_file(null);
94
- while (childInfo !== null) {
95
- const name = childInfo.get_name();
96
- const childSrc = join(srcStr, name);
97
- const childDest = join(destStr, name);
98
- const filter = opts.filter;
99
- if (filter && !filter(childSrc, childDest)) {
100
- childInfo = enumerator.next_file(null);
101
- continue;
102
- }
103
- cpSyncInternal(childSrc, childDest, opts);
104
- childInfo = enumerator.next_file(null);
105
- }
106
- }
107
- function cpSyncInternal(srcStr, destStr, opts) {
108
- const srcFile = Gio.File.new_for_path(srcStr);
109
- const destFile = Gio.File.new_for_path(destStr);
110
- let info;
111
- try {
112
- info = srcFile.query_info("standard::type,standard::is-symlink", opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
113
- } catch (err) {
114
- throw createNodeError(err, "stat", srcStr);
115
- }
116
- const type = info.get_file_type();
117
- if (type === Gio.FileType.DIRECTORY) {
118
- if (!opts.recursive) throw makeEISDIR(srcStr);
119
- cpOneDirSync(srcFile, destFile, srcStr, destStr, opts);
120
- } else {
121
- copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts);
122
- }
123
- }
124
- function cpSync(src, dest, options) {
125
- const srcStr = normalizePath(src);
126
- const destStr = normalizePath(dest);
127
- const opts = options ?? {};
128
- const srcFile = Gio.File.new_for_path(srcStr);
129
- const filter = opts.filter;
130
- if (filter && !filter(srcStr, destStr)) return;
131
- try {
132
- const info = srcFile.query_info("standard::type", opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
133
- if (info.get_file_type() === Gio.FileType.DIRECTORY && !opts.recursive) {
134
- throw makeEISDIR(srcStr);
135
- }
136
- } catch (e) {
137
- if (typeof e.code === "string") throw e;
138
- throw createNodeError(e, "stat", srcStr);
139
- }
140
- cpSyncInternal(srcStr, destStr, opts);
141
- }
142
- function cp(src, dest, options, callback) {
143
- let opts;
144
- let cb;
145
- if (typeof options === "function") {
146
- cb = options;
147
- opts = {};
148
- } else {
149
- cb = callback;
150
- opts = options;
151
- }
152
- const asyncFilter = opts.filter;
153
- if (asyncFilter) {
154
- Promise.resolve(asyncFilter(normalizePath(src), normalizePath(dest))).then((include) => {
155
- if (!include) {
156
- cb(null);
157
- return;
158
- }
159
- try {
160
- cpPromises(src, dest, opts).then(() => cb(null)).catch(cb);
161
- } catch (e) {
162
- cb(e);
163
- }
164
- }).catch(cb);
165
- return;
166
- }
167
- Promise.resolve().then(() => {
168
- try {
169
- cpSync(src, dest, opts);
170
- cb(null);
171
- } catch (e) {
172
- cb(e);
173
- }
174
- });
175
- }
176
- async function cpPromisesDir(srcStr, destStr, opts) {
177
- const sep = srcStr.endsWith("/") ? "" : "/";
178
- if (destStr.startsWith(srcStr + sep) && destStr !== srcStr) {
179
- throw makeSYMLINKLOOP(srcStr, destStr);
180
- }
181
- const destFile = Gio.File.new_for_path(destStr);
182
- if (!destFile.query_exists(null)) {
183
- try {
184
- destFile.make_directory_with_parents(null);
185
- } catch (err) {
186
- throw createNodeError(err, "mkdir", destStr);
187
- }
188
- }
189
- const srcFile = Gio.File.new_for_path(srcStr);
190
- let enumerator;
191
- try {
192
- enumerator = srcFile.enumerate_children("standard::name,standard::type", opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
193
- } catch (err) {
194
- throw createNodeError(err, "scandir", srcStr);
195
- }
196
- let childInfo = enumerator.next_file(null);
197
- while (childInfo !== null) {
198
- const name = childInfo.get_name();
199
- const childSrc = join(srcStr, name);
200
- const childDest = join(destStr, name);
201
- const filter = opts.filter;
202
- if (filter) {
203
- const include = await Promise.resolve(filter(childSrc, childDest));
204
- if (!include) {
205
- childInfo = enumerator.next_file(null);
206
- continue;
207
- }
208
- }
209
- await cpPromises(childSrc, childDest, opts);
210
- childInfo = enumerator.next_file(null);
211
- }
212
- }
213
- async function cpPromises(src, dest, opts = {}) {
214
- const srcStr = normalizePath(src);
215
- const destStr = normalizePath(dest);
216
- const srcFile = Gio.File.new_for_path(srcStr);
217
- let info;
218
- try {
219
- info = srcFile.query_info("standard::type", opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
220
- } catch (err) {
221
- throw createNodeError(err, "stat", srcStr);
222
- }
223
- if (info.get_file_type() === Gio.FileType.DIRECTORY) {
224
- if (!opts.recursive) throw makeEISDIR(srcStr);
225
- await cpPromisesDir(srcStr, destStr, opts);
226
- } else {
227
- const destFile = Gio.File.new_for_path(destStr);
228
- copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts);
229
- }
230
- }
231
-
232
- //#endregion
233
- export { cp, cpPromises as cpAsync, cpSync };
1
+ import{normalizePath as e}from"./utils.js";import{createNodeError as t}from"./errors.js";import n from"@girs/gio-2.0";import{join as r}from"node:path";function i(e){let t=Error(`ERR_FS_CP_EEXIST: file already exists, copyfile '${e}'`);return t.code=`ERR_FS_CP_EEXIST`,t.syscall=`copyfile`,t.path=e,t}function a(e){let t=Error(`ERR_FS_EISDIR: illegal operation on a directory, copyfile '${e}'`);return t.code=`ERR_FS_EISDIR`,t.syscall=`copyfile`,t.path=e,t}function o(e,t){let n=Error(`ENOTDIR: not a directory, copyfile '${e}' -> '${t}'`);return n.code=`ENOTDIR`,n.syscall=`copyfile`,n.path=e,n.dest=t,n}function s(e,t){let n=Error(`ELOOP: too many levels of symbolic links, copyfile '${e}' -> '${t}'`);return n.code=`ELOOP`,n.syscall=`copyfile`,n.path=e,n.dest=t,n}function c(e){let t=e.force??!0,r=n.FileCopyFlags.NONE;return t&&(r|=n.FileCopyFlags.OVERWRITE),e.preserveTimestamps&&(r|=n.FileCopyFlags.TARGET_DEFAULT_MODIFIED_TIME),e.dereference||(r|=n.FileCopyFlags.NOFOLLOW_SYMLINKS),r}function l(e,r,a,s,l){let u=l.force??!0;try{if(r.query_info(`standard::type`,n.FileQueryInfoFlags.NONE,null).get_file_type()===n.FileType.DIRECTORY)throw o(a,s);if(!u){if(l.errorOnExist)throw i(s);return}}catch(e){if(typeof e.code==`string`)throw e}let d=c(l);try{e.copy(r,d,null,null)}catch(e){throw t(e,`copyfile`,a,s)}}function u(e,i,a,c,l){let u=a.endsWith(`/`)?``:`/`;if(c.startsWith(a+u)&&c!==a)throw s(a,c);if(i.query_exists(null))try{if(i.query_info(`standard::type`,n.FileQueryInfoFlags.NONE,null).get_file_type()!==n.FileType.DIRECTORY)throw o(c,a)}catch(e){if(typeof e.code==`string`)throw e}else try{i.make_directory_with_parents(null)}catch(e){throw t(e,`mkdir`,c)}let f;try{f=e.enumerate_children(`standard::name,standard::type,standard::is-symlink`,l.dereference?n.FileQueryInfoFlags.NONE:n.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,null)}catch(e){throw t(e,`scandir`,a)}let p=f.next_file(null);for(;p!==null;){let e=p.get_name(),t=r(a,e),n=r(c,e),i=l.filter;if(i&&!i(t,n)){p=f.next_file(null);continue}d(t,n,l),p=f.next_file(null)}}function d(e,r,i){let o=n.File.new_for_path(e),s=n.File.new_for_path(r),c;try{c=o.query_info(`standard::type,standard::is-symlink`,i.dereference?n.FileQueryInfoFlags.NONE:n.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,null)}catch(n){throw t(n,`stat`,e)}if(c.get_file_type()===n.FileType.DIRECTORY){if(!i.recursive)throw a(e);u(o,s,e,r,i)}else l(o,s,e,r,i)}function f(r,i,o){let s=e(r),c=e(i),l=o??{},u=n.File.new_for_path(s),f=l.filter;if(!(f&&!f(s,c))){try{if(u.query_info(`standard::type`,l.dereference?n.FileQueryInfoFlags.NONE:n.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,null).get_file_type()===n.FileType.DIRECTORY&&!l.recursive)throw a(s)}catch(e){throw typeof e.code==`string`?e:t(e,`stat`,s)}d(s,c,l)}}function p(t,n,r,i){let a,o;typeof r==`function`?(o=r,a={}):(o=i,a=r);let s=a.filter;if(s){Promise.resolve(s(e(t),e(n))).then(e=>{if(!e){o(null);return}try{h(t,n,a).then(()=>o(null)).catch(o)}catch(e){o(e)}}).catch(o);return}Promise.resolve().then(()=>{try{f(t,n,a),o(null)}catch(e){o(e)}})}async function m(e,i,a){let o=e.endsWith(`/`)?``:`/`;if(i.startsWith(e+o)&&i!==e)throw s(e,i);let c=n.File.new_for_path(i);if(!c.query_exists(null))try{c.make_directory_with_parents(null)}catch(e){throw t(e,`mkdir`,i)}let l=n.File.new_for_path(e),u;try{u=l.enumerate_children(`standard::name,standard::type`,a.dereference?n.FileQueryInfoFlags.NONE:n.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,null)}catch(n){throw t(n,`scandir`,e)}let d=u.next_file(null);for(;d!==null;){let t=d.get_name(),n=r(e,t),o=r(i,t),s=a.filter;if(s&&!await Promise.resolve(s(n,o))){d=u.next_file(null);continue}await h(n,o,a),d=u.next_file(null)}}async function h(r,i,o={}){let s=e(r),c=e(i),u=n.File.new_for_path(s),d;try{d=u.query_info(`standard::type`,o.dereference?n.FileQueryInfoFlags.NONE:n.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,null)}catch(e){throw t(e,`stat`,s)}if(d.get_file_type()===n.FileType.DIRECTORY){if(!o.recursive)throw a(s);await m(s,c,o)}else l(u,n.File.new_for_path(c),s,c,o)}export{p as cp,h as cpAsync,f as cpSync};