@gjsify/fs 0.3.12 → 0.3.14

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.
package/lib/esm/cp.js CHANGED
@@ -1,253 +1,233 @@
1
- import Gio from "@girs/gio-2.0";
2
- import { join } from "node:path";
3
1
  import { normalizePath } from "./utils.js";
4
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
5
7
  function makeEEXIST(destStr) {
6
- const e = new Error(`ERR_FS_CP_EEXIST: file already exists, copyfile '${destStr}'`);
7
- e.code = "ERR_FS_CP_EEXIST";
8
- e.syscall = "copyfile";
9
- e.path = destStr;
10
- return e;
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;
11
13
  }
12
14
  function makeEISDIR(srcStr) {
13
- const e = new Error(`ERR_FS_EISDIR: illegal operation on a directory, copyfile '${srcStr}'`);
14
- e.code = "ERR_FS_EISDIR";
15
- e.syscall = "copyfile";
16
- e.path = srcStr;
17
- return e;
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;
18
20
  }
19
21
  function makeENOTDIR(srcStr, destStr) {
20
- const e = new Error(`ENOTDIR: not a directory, copyfile '${srcStr}' -> '${destStr}'`);
21
- e.code = "ENOTDIR";
22
- e.syscall = "copyfile";
23
- e.path = srcStr;
24
- e.dest = destStr;
25
- return e;
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;
26
28
  }
27
29
  function makeSYMLINKLOOP(srcStr, destStr) {
28
- const e = new Error(`ELOOP: too many levels of symbolic links, copyfile '${srcStr}' -> '${destStr}'`);
29
- e.code = "ELOOP";
30
- e.syscall = "copyfile";
31
- e.path = srcStr;
32
- e.dest = destStr;
33
- return e;
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;
34
36
  }
35
37
  function queryCopyFlags(opts) {
36
- const force = opts.force ?? true;
37
- let flags = Gio.FileCopyFlags.NONE;
38
- if (force) flags |= Gio.FileCopyFlags.OVERWRITE;
39
- if (opts.preserveTimestamps) flags |= Gio.FileCopyFlags.TARGET_DEFAULT_MODIFIED_TIME;
40
- if (!opts.dereference) flags |= Gio.FileCopyFlags.NOFOLLOW_SYMLINKS;
41
- return flags;
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;
42
44
  }
43
45
  function copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts) {
44
- const force = opts.force ?? true;
45
- try {
46
- const destInfo = destFile.query_info("standard::type", Gio.FileQueryInfoFlags.NONE, null);
47
- if (destInfo.get_file_type() === Gio.FileType.DIRECTORY) {
48
- throw makeENOTDIR(srcStr, destStr);
49
- }
50
- if (!force) {
51
- if (opts.errorOnExist) throw makeEEXIST(destStr);
52
- return;
53
- }
54
- } catch (e) {
55
- if (typeof e.code === "string") throw e;
56
- }
57
- const flags = queryCopyFlags(opts);
58
- try {
59
- srcFile.copy(destFile, flags, null, null);
60
- } catch (err) {
61
- throw createNodeError(err, "copyfile", srcStr, destStr);
62
- }
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
+ }
63
65
  }
64
66
  function cpOneDirSync(srcFile, destFile, srcStr, destStr, opts) {
65
- const sep = srcStr.endsWith("/") ? "" : "/";
66
- if (destStr.startsWith(srcStr + sep) && destStr !== srcStr) {
67
- throw makeSYMLINKLOOP(srcStr, destStr);
68
- }
69
- if (!destFile.query_exists(null)) {
70
- try {
71
- destFile.make_directory_with_parents(null);
72
- } catch (err) {
73
- throw createNodeError(err, "mkdir", destStr);
74
- }
75
- } else {
76
- try {
77
- const destInfo = destFile.query_info("standard::type", Gio.FileQueryInfoFlags.NONE, null);
78
- if (destInfo.get_file_type() !== Gio.FileType.DIRECTORY) {
79
- throw makeENOTDIR(destStr, srcStr);
80
- }
81
- } catch (e) {
82
- if (typeof e.code === "string") throw e;
83
- }
84
- }
85
- let enumerator;
86
- try {
87
- enumerator = srcFile.enumerate_children(
88
- "standard::name,standard::type,standard::is-symlink",
89
- opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
90
- null
91
- );
92
- } catch (err) {
93
- throw createNodeError(err, "scandir", srcStr);
94
- }
95
- let childInfo = enumerator.next_file(null);
96
- while (childInfo !== null) {
97
- const name = childInfo.get_name();
98
- const childSrc = join(srcStr, name);
99
- const childDest = join(destStr, name);
100
- const filter = opts.filter;
101
- if (filter && !filter(childSrc, childDest)) {
102
- childInfo = enumerator.next_file(null);
103
- continue;
104
- }
105
- cpSyncInternal(childSrc, childDest, opts);
106
- childInfo = enumerator.next_file(null);
107
- }
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
+ }
108
106
  }
109
107
  function cpSyncInternal(srcStr, destStr, opts) {
110
- const srcFile = Gio.File.new_for_path(srcStr);
111
- const destFile = Gio.File.new_for_path(destStr);
112
- let info;
113
- try {
114
- info = srcFile.query_info(
115
- "standard::type,standard::is-symlink",
116
- opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
117
- null
118
- );
119
- } catch (err) {
120
- throw createNodeError(err, "stat", srcStr);
121
- }
122
- const type = info.get_file_type();
123
- if (type === Gio.FileType.DIRECTORY) {
124
- if (!opts.recursive) throw makeEISDIR(srcStr);
125
- cpOneDirSync(srcFile, destFile, srcStr, destStr, opts);
126
- } else {
127
- copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts);
128
- }
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
+ }
129
123
  }
130
124
  function cpSync(src, dest, options) {
131
- const srcStr = normalizePath(src);
132
- const destStr = normalizePath(dest);
133
- const opts = options ?? {};
134
- const srcFile = Gio.File.new_for_path(srcStr);
135
- const filter = opts.filter;
136
- if (filter && !filter(srcStr, destStr)) return;
137
- try {
138
- const info = srcFile.query_info(
139
- "standard::type",
140
- opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
141
- null
142
- );
143
- if (info.get_file_type() === Gio.FileType.DIRECTORY && !opts.recursive) {
144
- throw makeEISDIR(srcStr);
145
- }
146
- } catch (e) {
147
- if (typeof e.code === "string") throw e;
148
- throw createNodeError(e, "stat", srcStr);
149
- }
150
- cpSyncInternal(srcStr, destStr, opts);
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);
151
141
  }
152
142
  function cp(src, dest, options, callback) {
153
- let opts;
154
- let cb;
155
- if (typeof options === "function") {
156
- cb = options;
157
- opts = {};
158
- } else {
159
- cb = callback;
160
- opts = options;
161
- }
162
- const asyncFilter = opts.filter;
163
- if (asyncFilter) {
164
- Promise.resolve(asyncFilter(normalizePath(src), normalizePath(dest))).then((include) => {
165
- if (!include) {
166
- cb(null);
167
- return;
168
- }
169
- try {
170
- cpPromises(src, dest, opts).then(() => cb(null)).catch(cb);
171
- } catch (e) {
172
- cb(e);
173
- }
174
- }).catch(cb);
175
- return;
176
- }
177
- Promise.resolve().then(() => {
178
- try {
179
- cpSync(src, dest, opts);
180
- cb(null);
181
- } catch (e) {
182
- cb(e);
183
- }
184
- });
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
+ });
185
175
  }
186
176
  async function cpPromisesDir(srcStr, destStr, opts) {
187
- const sep = srcStr.endsWith("/") ? "" : "/";
188
- if (destStr.startsWith(srcStr + sep) && destStr !== srcStr) {
189
- throw makeSYMLINKLOOP(srcStr, destStr);
190
- }
191
- const destFile = Gio.File.new_for_path(destStr);
192
- if (!destFile.query_exists(null)) {
193
- try {
194
- destFile.make_directory_with_parents(null);
195
- } catch (err) {
196
- throw createNodeError(err, "mkdir", destStr);
197
- }
198
- }
199
- const srcFile = Gio.File.new_for_path(srcStr);
200
- let enumerator;
201
- try {
202
- enumerator = srcFile.enumerate_children(
203
- "standard::name,standard::type",
204
- opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
205
- null
206
- );
207
- } catch (err) {
208
- throw createNodeError(err, "scandir", srcStr);
209
- }
210
- let childInfo = enumerator.next_file(null);
211
- while (childInfo !== null) {
212
- const name = childInfo.get_name();
213
- const childSrc = join(srcStr, name);
214
- const childDest = join(destStr, name);
215
- const filter = opts.filter;
216
- if (filter) {
217
- const include = await Promise.resolve(filter(childSrc, childDest));
218
- if (!include) {
219
- childInfo = enumerator.next_file(null);
220
- continue;
221
- }
222
- }
223
- await cpPromises(childSrc, childDest, opts);
224
- childInfo = enumerator.next_file(null);
225
- }
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
+ }
226
212
  }
227
213
  async function cpPromises(src, dest, opts = {}) {
228
- const srcStr = normalizePath(src);
229
- const destStr = normalizePath(dest);
230
- const srcFile = Gio.File.new_for_path(srcStr);
231
- let info;
232
- try {
233
- info = srcFile.query_info(
234
- "standard::type",
235
- opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
236
- null
237
- );
238
- } catch (err) {
239
- throw createNodeError(err, "stat", srcStr);
240
- }
241
- if (info.get_file_type() === Gio.FileType.DIRECTORY) {
242
- if (!opts.recursive) throw makeEISDIR(srcStr);
243
- await cpPromisesDir(srcStr, destStr, opts);
244
- } else {
245
- const destFile = Gio.File.new_for_path(destStr);
246
- copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts);
247
- }
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
+ }
248
230
  }
249
- export {
250
- cp,
251
- cpPromises as cpAsync,
252
- cpSync
253
- };
231
+
232
+ //#endregion
233
+ export { cp, cpPromises as cpAsync, cpSync };