@jestaubach/fs-helpers 0.2.24 → 0.2.26
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/README.md +2 -2
- package/dist/__tests__/fsHelpers.spec.d.ts +1 -0
- package/dist/fs-helpers.js +161 -211
- package/dist/fs-helpers.umd.cjs +1 -1
- package/dist/src/index.d.ts +58 -0
- package/dist/{mock.d.ts → src/mock.d.ts} +2 -2
- package/dist/{types.d.ts → src/types.d.ts} +2 -3
- package/package.json +6 -1
- package/dist/index.d.ts +0 -30
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
## Badges
|
|
4
4
|
|
|
5
|
-
[](https://qlty.sh/gh/JEStaubach/projects/fs-helpers)
|
|
6
|
+
[](https://qlty.sh/gh/JEStaubach/projects/fs-helpers)
|
|
7
7
|
[](https://app.codacy.com/gh/JEStaubach/fs-helpers/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
|
|
8
8
|
[](https://app.codacy.com/gh/JEStaubach/fs-helpers/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
|
9
9
|
[](https://snyk.io/test/github/JEStaubach/fs-helpers)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/fs-helpers.js
CHANGED
|
@@ -1,36 +1,33 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const { default:
|
|
5
|
-
const
|
|
6
|
-
return
|
|
7
|
-
})(),
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
const c = z.readFileSync(e).toString();
|
|
18
|
-
S(e, c);
|
|
1
|
+
var d = Object.defineProperty;
|
|
2
|
+
var S = (t, e, r) => e in t ? d(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
+
var n = (t, e, r) => (S(t, typeof e != "symbol" ? e + "" : e, r), r);
|
|
4
|
+
const c = {}, { default: v } = (() => {
|
|
5
|
+
const t = require("fs-extra");
|
|
6
|
+
return t && t.__esModule ? t : Object.assign(/* @__PURE__ */ Object.create(null), t, { default: t, [Symbol.toStringTag]: "Module" });
|
|
7
|
+
})(), i = /* @__PURE__ */ new Map();
|
|
8
|
+
function m(t) {
|
|
9
|
+
const { root: e, dir: r } = c.parse(t), s = r.split(e).slice(1).join(e).split(c.sep);
|
|
10
|
+
for (let u = 0; u < s.length; u++) {
|
|
11
|
+
const y = [e, s.slice(0, u + 1).join(c.sep)].join("");
|
|
12
|
+
f(y);
|
|
13
|
+
}
|
|
14
|
+
const o = v.readFileSync(t).toString();
|
|
15
|
+
l(t, o);
|
|
19
16
|
}
|
|
20
|
-
function
|
|
21
|
-
const [
|
|
22
|
-
return Buffer.from(
|
|
17
|
+
function p(t, e) {
|
|
18
|
+
const [r, s] = i.get(t) || ["", ""];
|
|
19
|
+
return Buffer.from(s);
|
|
23
20
|
}
|
|
24
|
-
function
|
|
25
|
-
|
|
21
|
+
function l(t, e, r) {
|
|
22
|
+
i.set(t, ["file", e]);
|
|
26
23
|
}
|
|
27
|
-
function
|
|
28
|
-
return
|
|
24
|
+
function g(t) {
|
|
25
|
+
return t === c.resolve(".") ? !0 : i.has(t);
|
|
29
26
|
}
|
|
30
|
-
function
|
|
31
|
-
const [
|
|
32
|
-
isDirectory: () =>
|
|
33
|
-
isFile: () =>
|
|
27
|
+
function a(t, e) {
|
|
28
|
+
const [r, s] = i.get(t) || ["", ""], o = {
|
|
29
|
+
isDirectory: () => t === c.resolve(".") ? !0 : r === "dir",
|
|
30
|
+
isFile: () => i.has(t) && r === "file",
|
|
34
31
|
isBlockDevice: () => !1,
|
|
35
32
|
isCharacterDevice: () => !1,
|
|
36
33
|
isSymbolicLink: () => !1,
|
|
@@ -55,230 +52,183 @@ function b(e, t) {
|
|
|
55
52
|
ctime: /* @__PURE__ */ new Date(0),
|
|
56
53
|
birthtime: /* @__PURE__ */ new Date(0)
|
|
57
54
|
};
|
|
58
|
-
return
|
|
55
|
+
return o.isBlockDevice(), o.isCharacterDevice(), o.isSymbolicLink(), o.isFIFO(), o.isSocket(), o;
|
|
59
56
|
}
|
|
60
|
-
function
|
|
57
|
+
function b(t, e) {
|
|
61
58
|
}
|
|
62
|
-
class
|
|
63
|
-
constructor(
|
|
64
|
-
super(
|
|
65
|
-
|
|
59
|
+
class F extends Error {
|
|
60
|
+
constructor(r) {
|
|
61
|
+
super(r);
|
|
62
|
+
n(this, "code");
|
|
66
63
|
this.code = "ENOENT";
|
|
67
64
|
}
|
|
68
65
|
}
|
|
69
|
-
function
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const i = o.split(e).join(t), c = n.get(o) || /* istanbul ignore next */
|
|
75
|
-
["", ""];
|
|
76
|
-
n.delete(o), n.set(i, c);
|
|
77
|
-
}
|
|
66
|
+
function E(t, e) {
|
|
67
|
+
const r = t, s = e;
|
|
68
|
+
if (!i.has(r))
|
|
69
|
+
throw new F("source path not found");
|
|
70
|
+
h(r, s, !0);
|
|
78
71
|
}
|
|
79
|
-
function
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (i.includes(e)) {
|
|
84
|
-
const c = i.split(e).join(t);
|
|
85
|
-
n.set(
|
|
86
|
-
c,
|
|
87
|
-
n.get(i) || /* istanbul ignore next */
|
|
88
|
-
["", ""]
|
|
89
|
-
);
|
|
90
|
-
}
|
|
72
|
+
function h(t, e, r) {
|
|
73
|
+
const s = { source: t, target: e, deleteSource: r };
|
|
74
|
+
for (const o of Array.from(i.keys()))
|
|
75
|
+
D(o, s);
|
|
91
76
|
}
|
|
92
|
-
function
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
77
|
+
function D(t, e) {
|
|
78
|
+
if (!t.includes(e.source))
|
|
79
|
+
return;
|
|
80
|
+
const r = t.split(e.source).join(e.target), s = i.get(t) || /* istanbul ignore next */
|
|
81
|
+
["", ""];
|
|
82
|
+
e.deleteSource && i.delete(t), i.set(r, s);
|
|
83
|
+
}
|
|
84
|
+
function k(t, e, r) {
|
|
85
|
+
if (i.has(e))
|
|
86
|
+
throw new Error("destination directory exists");
|
|
87
|
+
h(t, e, !1);
|
|
88
|
+
}
|
|
89
|
+
function x(t) {
|
|
90
|
+
let e;
|
|
91
|
+
const r = c.resolve(".");
|
|
92
|
+
for (let s = 0; s < t.length; s++) {
|
|
93
|
+
const o = t.slice(0, s + 1).join(c.sep), u = `${r}${o}`;
|
|
94
|
+
e = w(u, o, e);
|
|
95
|
+
}
|
|
96
|
+
return e;
|
|
97
|
+
}
|
|
98
|
+
function w(t, e, r) {
|
|
99
|
+
return i.has(t) || e === "" ? r : (i.set(t, ["dir", ""]), r ?? t);
|
|
102
100
|
}
|
|
103
|
-
function
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
function f(t) {
|
|
102
|
+
if (a(t).isFile())
|
|
103
|
+
throw new Error("file exists at destination");
|
|
104
|
+
const e = t.split(c.resolve(".")).join("").split(c.sep);
|
|
105
|
+
return x(e);
|
|
106
106
|
}
|
|
107
|
-
function
|
|
108
|
-
|
|
107
|
+
function $(t) {
|
|
108
|
+
for (const e of Array.from(i.keys()))
|
|
109
|
+
e.includes(t) && i.delete(e);
|
|
109
110
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
const
|
|
115
|
-
return
|
|
111
|
+
function P(t) {
|
|
112
|
+
i.has(t) || i.set(t, ["file", ""]);
|
|
113
|
+
}
|
|
114
|
+
const A = { existsSync: g, lstatSync: a, chmodSync: b, renameSync: E, readFileSync: p, copySync: k, removeSync: $, ensureFileSync: P, mkdirpSync: f, outputFileSync: l, seedFile: m }, { default: _ } = (() => {
|
|
115
|
+
const t = require("fs-extra");
|
|
116
|
+
return t && t.__esModule ? t : Object.assign(/* @__PURE__ */ Object.create(null), t, { default: t, [Symbol.toStringTag]: "Module" });
|
|
116
117
|
})();
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
118
|
+
class M {
|
|
119
|
+
constructor(e, r) {
|
|
120
|
+
n(this, "existsSync");
|
|
121
|
+
n(this, "lstatSync");
|
|
122
|
+
n(this, "chmodSync");
|
|
123
|
+
n(this, "renameSync");
|
|
124
|
+
n(this, "readFileSync");
|
|
125
|
+
n(this, "copySync");
|
|
126
|
+
n(this, "removeSync");
|
|
127
|
+
n(this, "ensureFileSync");
|
|
128
|
+
n(this, "outputFileSync");
|
|
129
|
+
n(this, "mkdirpSync");
|
|
130
|
+
n(this, "seedFile");
|
|
131
|
+
if ({
|
|
132
|
+
existsSync: this.existsSync,
|
|
133
|
+
lstatSync: this.lstatSync,
|
|
134
|
+
chmodSync: this.chmodSync,
|
|
135
|
+
renameSync: this.renameSync,
|
|
136
|
+
readFileSync: this.readFileSync,
|
|
137
|
+
copySync: this.copySync,
|
|
138
|
+
removeSync: this.removeSync,
|
|
139
|
+
ensureFileSync: this.ensureFileSync,
|
|
140
|
+
outputFileSync: this.outputFileSync,
|
|
141
|
+
mkdirpSync: this.mkdirpSync,
|
|
142
|
+
seedFile: this.seedFile
|
|
143
|
+
} = e, this.seedFile !== void 0 && r !== void 0)
|
|
144
|
+
for (const s of r)
|
|
145
|
+
this.mockExistingFile(s);
|
|
146
|
+
}
|
|
147
|
+
mockExistingFile(e) {
|
|
148
|
+
const r = this.getAbsolutePath(e).value;
|
|
149
|
+
this.seedFile(r);
|
|
150
|
+
}
|
|
151
|
+
readFile(e, r) {
|
|
127
152
|
return {
|
|
128
153
|
success: !0,
|
|
129
|
-
value:
|
|
154
|
+
value: this.readFileSync(this.getAbsolutePath(e).value, r),
|
|
130
155
|
error: null
|
|
131
156
|
};
|
|
132
157
|
}
|
|
133
|
-
|
|
134
|
-
return
|
|
135
|
-
success: !0,
|
|
136
|
-
error: null
|
|
137
|
-
};
|
|
158
|
+
writeFile(e, r) {
|
|
159
|
+
return this.outputFileSync(this.getAbsolutePath(e).value, r), { success: !0, error: null };
|
|
138
160
|
}
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
return o
|
|
142
|
-
success: !0,
|
|
143
|
-
value: !0,
|
|
144
|
-
error: null
|
|
145
|
-
} : {
|
|
161
|
+
checkIfPathExists(e, r, s) {
|
|
162
|
+
const o = this.getAbsolutePath(e).value;
|
|
163
|
+
return this.existsSync(o) ? s(this.lstatSync(o)) ? { success: !0, value: !0, error: null } : {
|
|
146
164
|
success: !1,
|
|
147
165
|
value: !1,
|
|
148
|
-
error: `
|
|
149
|
-
} : {
|
|
150
|
-
success: !0,
|
|
151
|
-
value: !1,
|
|
152
|
-
error: null
|
|
153
|
-
};
|
|
166
|
+
error: `checkIf${r === "file" ? "File" : "Dir"}Exists: '${e}' is not a ${r}.`
|
|
167
|
+
} : { success: !0, value: !1, error: null };
|
|
154
168
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return o(s) ? i(s).isDirectory() ? {
|
|
158
|
-
success: !0,
|
|
159
|
-
value: !0,
|
|
160
|
-
error: null
|
|
161
|
-
} : {
|
|
162
|
-
success: !1,
|
|
163
|
-
value: !1,
|
|
164
|
-
error: `checkIfDirExists: '${r}' is not a directory.`
|
|
165
|
-
} : {
|
|
166
|
-
success: !0,
|
|
167
|
-
value: !1,
|
|
168
|
-
error: null
|
|
169
|
-
};
|
|
169
|
+
checkIfFileExists(e) {
|
|
170
|
+
return this.checkIfPathExists(e, "file", (r) => r.isFile());
|
|
170
171
|
}
|
|
171
|
-
|
|
172
|
+
checkIfDirExists(e) {
|
|
173
|
+
return this.checkIfPathExists(e, "directory", (r) => r.isDirectory());
|
|
174
|
+
}
|
|
175
|
+
getAbsolutePath(e) {
|
|
172
176
|
try {
|
|
173
|
-
if (
|
|
174
|
-
throw Error("Dir is undefined.");
|
|
175
|
-
if (
|
|
176
|
-
throw Error(`Dir contains unsupported characters. Received ${
|
|
177
|
-
return {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
error: null
|
|
181
|
-
};
|
|
182
|
-
} catch (s) {
|
|
183
|
-
return console.error(`Error resolving path: ${r}`), {
|
|
177
|
+
if (e === void 0)
|
|
178
|
+
throw new Error("Dir is undefined.");
|
|
179
|
+
if (e.match(/^[.a-zA-Z0-9\-_/:\\]+$/g) === null)
|
|
180
|
+
throw new Error(`Dir contains unsupported characters. Received ${e}.`);
|
|
181
|
+
return { success: !0, value: c.normalize(c.resolve(e)), error: null };
|
|
182
|
+
} catch (r) {
|
|
183
|
+
return console.error(`Error resolving path: ${e}`), {
|
|
184
184
|
success: !1,
|
|
185
185
|
value: void 0,
|
|
186
|
-
error: `Error resolving path: '${
|
|
186
|
+
error: `Error resolving path: '${e}'. Received error: '${r}'`
|
|
187
187
|
};
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
|
-
|
|
190
|
+
createDir(e) {
|
|
191
191
|
try {
|
|
192
|
-
if (
|
|
193
|
-
throw Error(`Function "createDir" expected a path. Recieved "${
|
|
194
|
-
const s =
|
|
195
|
-
return {
|
|
196
|
-
success: !0,
|
|
197
|
-
value: F(s),
|
|
198
|
-
error: null
|
|
199
|
-
};
|
|
192
|
+
if (e === void 0)
|
|
193
|
+
throw new Error(`Function "createDir" expected a path. Recieved "${e}".`);
|
|
194
|
+
const r = this.getAbsolutePath(e).value, s = this.mkdirpSync(r);
|
|
195
|
+
return { success: !0, value: c.normalize(s.replace(/^\\\\\?\\/, "")), error: null };
|
|
200
196
|
} catch {
|
|
201
|
-
return console.error(`Error creating dir: ${
|
|
202
|
-
success: !1,
|
|
203
|
-
value: void 0,
|
|
204
|
-
error: `Error creating dir: '${r}'`
|
|
205
|
-
};
|
|
197
|
+
return console.error(`Error creating dir: ${e}`), { success: !1, value: void 0, error: `Error creating dir: '${e}'` };
|
|
206
198
|
}
|
|
207
199
|
}
|
|
208
|
-
|
|
209
|
-
const
|
|
210
|
-
return
|
|
211
|
-
success: !0,
|
|
212
|
-
error: null
|
|
213
|
-
};
|
|
200
|
+
touchFile(e, r) {
|
|
201
|
+
const s = this.getAbsolutePath(e).value;
|
|
202
|
+
return this.ensureFileSync(s), r !== void 0 && this.chmodSync(s, r), { success: !0, error: null };
|
|
214
203
|
}
|
|
215
|
-
|
|
216
|
-
return
|
|
217
|
-
success: !0,
|
|
218
|
-
value: r,
|
|
219
|
-
error: null
|
|
220
|
-
}) : (console.error(`Error deleting dir: ${r}`), {
|
|
221
|
-
success: !1,
|
|
222
|
-
value: void 0,
|
|
223
|
-
error: `Error deleting dir: '${r}'`
|
|
224
|
-
});
|
|
204
|
+
rimrafDir(e) {
|
|
205
|
+
return this.getAbsolutePath(e).value !== void 0 && this.checkIfDirExists(e).value ? (this.removeSync(e), { success: !0, value: e, error: null }) : (console.error(`Error deleting dir: ${e}`), { success: !1, value: void 0, error: `Error deleting dir: '${e}'` });
|
|
225
206
|
}
|
|
226
|
-
|
|
227
|
-
return
|
|
207
|
+
rimrafDirs(e) {
|
|
208
|
+
return e.map((r) => this.rimrafDir(this.getAbsolutePath(r).value));
|
|
228
209
|
}
|
|
229
|
-
|
|
230
|
-
return
|
|
231
|
-
success: !0,
|
|
232
|
-
error: null
|
|
233
|
-
}) : (console.error("Cleaning up due to abort, no directory to clean up."), {
|
|
234
|
-
success: !1,
|
|
235
|
-
error: "Cleaning up due to abort, no directory to clean up."
|
|
236
|
-
});
|
|
210
|
+
abortDirCreation(e) {
|
|
211
|
+
return e !== null && this.checkIfDirExists(e).value ? (console.error(`Cleaning up due to abort, directories created starting at: ${JSON.stringify(e)}`), this.rimrafDir(e), { success: !0, error: null }) : (console.error("Cleaning up due to abort, no directory to clean up."), { success: !1, error: "Cleaning up due to abort, no directory to clean up." });
|
|
237
212
|
}
|
|
238
|
-
|
|
213
|
+
renameDir(e, r) {
|
|
239
214
|
try {
|
|
240
|
-
return
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
error: null
|
|
244
|
-
};
|
|
245
|
-
} catch (f) {
|
|
246
|
-
return console.error(f.code), {
|
|
247
|
-
success: !1,
|
|
248
|
-
value: void 0,
|
|
249
|
-
error: `renameDir from '${r}' to '${s} failed.`
|
|
250
|
-
};
|
|
215
|
+
return this.renameSync(this.getAbsolutePath(e).value, this.getAbsolutePath(r).value), { success: !0, value: "Successfully renamed the directory.", error: null };
|
|
216
|
+
} catch (s) {
|
|
217
|
+
return console.error(s.code), { success: !1, value: void 0, error: `renameDir from '${e}' to '${r} failed.` };
|
|
251
218
|
}
|
|
252
219
|
}
|
|
253
|
-
|
|
220
|
+
copyDirAbs(e, r) {
|
|
254
221
|
try {
|
|
255
|
-
return
|
|
256
|
-
success: !0,
|
|
257
|
-
error: null
|
|
258
|
-
};
|
|
222
|
+
return this.copySync(this.getAbsolutePath(e).value, this.getAbsolutePath(r).value, { overwrite: !1, errorOnExist: !0 }), { success: !0, error: null };
|
|
259
223
|
} catch {
|
|
260
|
-
return {
|
|
261
|
-
success: !1,
|
|
262
|
-
error: `Error copying absolute from '${r}' to '${s}'`
|
|
263
|
-
};
|
|
224
|
+
return { success: !1, error: `Error copying absolute from '${e}' to '${r}'` };
|
|
264
225
|
}
|
|
265
226
|
}
|
|
266
|
-
return {
|
|
267
|
-
getAbsolutePath: u,
|
|
268
|
-
abortDirCreation: C,
|
|
269
|
-
checkIfDirExists: v,
|
|
270
|
-
copyDirAbs: T,
|
|
271
|
-
createDir: M,
|
|
272
|
-
renameDir: I,
|
|
273
|
-
rimrafDir: y,
|
|
274
|
-
rimrafDirs: A,
|
|
275
|
-
checkIfFileExists: x,
|
|
276
|
-
readFile: w,
|
|
277
|
-
writeFile: j,
|
|
278
|
-
touchFile: O
|
|
279
|
-
};
|
|
280
227
|
}
|
|
281
|
-
|
|
228
|
+
function I(t, e) {
|
|
229
|
+
return new M(t, e);
|
|
230
|
+
}
|
|
231
|
+
const O = { use: I, default: _, mock: A };
|
|
282
232
|
export {
|
|
283
|
-
|
|
233
|
+
O as default
|
|
284
234
|
};
|
package/dist/fs-helpers.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(n,c){typeof exports=="object"&&typeof module<"u"?module.exports=c():typeof define=="function"&&define.amd?define(c):(n=typeof globalThis<"u"?globalThis:n||self,n["fs-helpers"]=c())})(this,function(){"use strict";var M=Object.defineProperty;var j=(n,c,i)=>c in n?M(n,c,{enumerable:!0,configurable:!0,writable:!0,value:i}):n[c]=i;var u=(n,c,i)=>(j(n,typeof c!="symbol"?c+"":c,i),i);const n={},{default:c}=(()=>{const t=require("fs-extra");return t&&t.__esModule?t:Object.assign(Object.create(null),t,{default:t,[Symbol.toStringTag]:"Module"})})(),i=new Map;function y(t){const{root:e,dir:r}=n.parse(t),s=r.split(e).slice(1).join(e).split(n.sep);for(let l=0;l<s.length;l++){const _=[e,s.slice(0,l+1).join(n.sep)].join("");d(_)}const o=c.readFileSync(t).toString();a(t,o)}function S(t,e){const[r,s]=i.get(t)||["",""];return Buffer.from(s)}function a(t,e,r){i.set(t,["file",e])}function v(t){return t===n.resolve(".")?!0:i.has(t)}function h(t,e){const[r,s]=i.get(t)||["",""],o={isDirectory:()=>t===n.resolve(".")?!0:r==="dir",isFile:()=>i.has(t)&&r==="file",isBlockDevice:()=>!1,isCharacterDevice:()=>!1,isSymbolicLink:()=>!1,isFIFO:()=>!1,isSocket:()=>!1,dev:0,ino:0,mode:0,nlink:0,uid:0,gid:0,rdev:0,size:0,blksize:0,blocks:0,atimeMs:0,mtimeMs:0,ctimeMs:0,birthtimeMs:0,atime:new Date(0),mtime:new Date(0),ctime:new Date(0),birthtime:new Date(0)};return o.isBlockDevice(),o.isCharacterDevice(),o.isSymbolicLink(),o.isFIFO(),o.isSocket(),o}function m(t,e){}class p extends Error{constructor(r){super(r);u(this,"code");this.code="ENOENT"}}function g(t,e){const r=t,s=e;if(!i.has(r))throw new p("source path not found");f(r,s,!0)}function f(t,e,r){const s={source:t,target:e,deleteSource:r};for(const o of Array.from(i.keys()))b(o,s)}function b(t,e){if(!t.includes(e.source))return;const r=t.split(e.source).join(e.target),s=i.get(t)||["",""];e.deleteSource&&i.delete(t),i.set(r,s)}function F(t,e,r){if(i.has(e))throw new Error("destination directory exists");f(t,e,!1)}function E(t){let e;const r=n.resolve(".");for(let s=0;s<t.length;s++){const o=t.slice(0,s+1).join(n.sep),l=`${r}${o}`;e=D(l,o,e)}return e}function D(t,e,r){return i.has(t)||e===""?r:(i.set(t,["dir",""]),r??t)}function d(t){if(h(t).isFile())throw new Error("file exists at destination");const e=t.split(n.resolve(".")).join("").split(n.sep);return E(e)}function k(t){for(const e of Array.from(i.keys()))e.includes(t)&&i.delete(e)}function x(t){i.has(t)||i.set(t,["file",""])}const w={existsSync:v,lstatSync:h,chmodSync:m,renameSync:g,readFileSync:S,copySync:F,removeSync:k,ensureFileSync:x,mkdirpSync:d,outputFileSync:a,seedFile:y},{default:$}=(()=>{const t=require("fs-extra");return t&&t.__esModule?t:Object.assign(Object.create(null),t,{default:t,[Symbol.toStringTag]:"Module"})})();class P{constructor(e,r){u(this,"existsSync");u(this,"lstatSync");u(this,"chmodSync");u(this,"renameSync");u(this,"readFileSync");u(this,"copySync");u(this,"removeSync");u(this,"ensureFileSync");u(this,"outputFileSync");u(this,"mkdirpSync");u(this,"seedFile");if({existsSync:this.existsSync,lstatSync:this.lstatSync,chmodSync:this.chmodSync,renameSync:this.renameSync,readFileSync:this.readFileSync,copySync:this.copySync,removeSync:this.removeSync,ensureFileSync:this.ensureFileSync,outputFileSync:this.outputFileSync,mkdirpSync:this.mkdirpSync,seedFile:this.seedFile}=e,this.seedFile!==void 0&&r!==void 0)for(const s of r)this.mockExistingFile(s)}mockExistingFile(e){const r=this.getAbsolutePath(e).value;this.seedFile(r)}readFile(e,r){return{success:!0,value:this.readFileSync(this.getAbsolutePath(e).value,r),error:null}}writeFile(e,r){return this.outputFileSync(this.getAbsolutePath(e).value,r),{success:!0,error:null}}checkIfPathExists(e,r,s){const o=this.getAbsolutePath(e).value;return this.existsSync(o)?s(this.lstatSync(o))?{success:!0,value:!0,error:null}:{success:!1,value:!1,error:`checkIf${r==="file"?"File":"Dir"}Exists: '${e}' is not a ${r}.`}:{success:!0,value:!1,error:null}}checkIfFileExists(e){return this.checkIfPathExists(e,"file",r=>r.isFile())}checkIfDirExists(e){return this.checkIfPathExists(e,"directory",r=>r.isDirectory())}getAbsolutePath(e){try{if(e===void 0)throw new Error("Dir is undefined.");if(e.match(/^[.a-zA-Z0-9\-_/:\\]+$/g)===null)throw new Error(`Dir contains unsupported characters. Received ${e}.`);return{success:!0,value:n.normalize(n.resolve(e)),error:null}}catch(r){return console.error(`Error resolving path: ${e}`),{success:!1,value:void 0,error:`Error resolving path: '${e}'. Received error: '${r}'`}}}createDir(e){try{if(e===void 0)throw new Error(`Function "createDir" expected a path. Recieved "${e}".`);const r=this.getAbsolutePath(e).value,s=this.mkdirpSync(r);return{success:!0,value:n.normalize(s.replace(/^\\\\\?\\/,"")),error:null}}catch{return console.error(`Error creating dir: ${e}`),{success:!1,value:void 0,error:`Error creating dir: '${e}'`}}}touchFile(e,r){const s=this.getAbsolutePath(e).value;return this.ensureFileSync(s),r!==void 0&&this.chmodSync(s,r),{success:!0,error:null}}rimrafDir(e){return this.getAbsolutePath(e).value!==void 0&&this.checkIfDirExists(e).value?(this.removeSync(e),{success:!0,value:e,error:null}):(console.error(`Error deleting dir: ${e}`),{success:!1,value:void 0,error:`Error deleting dir: '${e}'`})}rimrafDirs(e){return e.map(r=>this.rimrafDir(this.getAbsolutePath(r).value))}abortDirCreation(e){return e!==null&&this.checkIfDirExists(e).value?(console.error(`Cleaning up due to abort, directories created starting at: ${JSON.stringify(e)}`),this.rimrafDir(e),{success:!0,error:null}):(console.error("Cleaning up due to abort, no directory to clean up."),{success:!1,error:"Cleaning up due to abort, no directory to clean up."})}renameDir(e,r){try{return this.renameSync(this.getAbsolutePath(e).value,this.getAbsolutePath(r).value),{success:!0,value:"Successfully renamed the directory.",error:null}}catch(s){return console.error(s.code),{success:!1,value:void 0,error:`renameDir from '${e}' to '${r} failed.`}}}copyDirAbs(e,r){try{return this.copySync(this.getAbsolutePath(e).value,this.getAbsolutePath(r).value,{overwrite:!1,errorOnExist:!0}),{success:!0,error:null}}catch{return{success:!1,error:`Error copying absolute from '${e}' to '${r}'`}}}}function A(t,e){return new P(t,e)}return{use:A,default:$,mock:w}});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import fsExtra from 'fs-extra';
|
|
3
|
+
import { RetBool, RetPath, RetString, RetVal, RetBuffer } from './types';
|
|
4
|
+
declare class FsHelpers {
|
|
5
|
+
private readonly existsSync;
|
|
6
|
+
private readonly lstatSync;
|
|
7
|
+
private readonly chmodSync;
|
|
8
|
+
private readonly renameSync;
|
|
9
|
+
private readonly readFileSync;
|
|
10
|
+
private readonly copySync;
|
|
11
|
+
private readonly removeSync;
|
|
12
|
+
private readonly ensureFileSync;
|
|
13
|
+
private readonly outputFileSync;
|
|
14
|
+
private readonly mkdirpSync;
|
|
15
|
+
private readonly seedFile;
|
|
16
|
+
constructor(fsLibrary: any, seedFiles?: string[]);
|
|
17
|
+
private mockExistingFile;
|
|
18
|
+
readFile(fileName: string, options?: {
|
|
19
|
+
encoding: BufferEncoding;
|
|
20
|
+
flag?: string;
|
|
21
|
+
} | BufferEncoding): RetBuffer;
|
|
22
|
+
writeFile(fileName: string, data: string): RetVal;
|
|
23
|
+
private checkIfPathExists;
|
|
24
|
+
checkIfFileExists(filePath: string): RetBool;
|
|
25
|
+
checkIfDirExists(dir: string | undefined): RetBool;
|
|
26
|
+
getAbsolutePath(dir: string | undefined): RetPath;
|
|
27
|
+
createDir(dir: string | undefined): RetPath;
|
|
28
|
+
touchFile(filePath: string, perms?: number): RetVal;
|
|
29
|
+
rimrafDir(dir: string | undefined): RetPath;
|
|
30
|
+
rimrafDirs(dirs: string[]): RetPath[];
|
|
31
|
+
abortDirCreation(dir: string | null): RetVal;
|
|
32
|
+
renameDir(oldPath: string, newPath: string): RetString;
|
|
33
|
+
copyDirAbs(src: string, dest: string): RetVal;
|
|
34
|
+
}
|
|
35
|
+
declare function use(fsLibrary: any, seedFiles?: string[]): FsHelpers;
|
|
36
|
+
declare const _default: {
|
|
37
|
+
use: typeof use;
|
|
38
|
+
default: typeof fsExtra;
|
|
39
|
+
mock: {
|
|
40
|
+
existsSync: (filePath: fsExtra.PathLike) => boolean;
|
|
41
|
+
lstatSync: (filePath: fsExtra.PathLike, _options?: (fsExtra.StatOptions & {
|
|
42
|
+
bigint?: false | undefined;
|
|
43
|
+
}) | undefined) => fsExtra.Stats;
|
|
44
|
+
chmodSync: (_filePath: fsExtra.PathLike, _mode: fsExtra.Mode) => void;
|
|
45
|
+
renameSync: (oldPath: fsExtra.PathLike, newPath: fsExtra.PathLike) => void;
|
|
46
|
+
readFileSync: (filePath: string, _options?: BufferEncoding | {
|
|
47
|
+
encoding: BufferEncoding;
|
|
48
|
+
flag?: string | undefined;
|
|
49
|
+
} | undefined) => Buffer;
|
|
50
|
+
copySync: (src: string, dest: string, _options?: fsExtra.CopyOptionsSync | undefined) => void;
|
|
51
|
+
removeSync: (filePath: string) => void;
|
|
52
|
+
ensureFileSync: (filename: string) => void;
|
|
53
|
+
mkdirpSync: (dir: string) => string | undefined;
|
|
54
|
+
outputFileSync: (filename: string, data: string, _options?: fsExtra.WriteFileOptions | undefined) => void;
|
|
55
|
+
seedFile: (fileName: string) => void;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
export default _default;
|
|
@@ -6,7 +6,7 @@ declare function readFileSync(filePath: string, _options?: {
|
|
|
6
6
|
encoding: BufferEncoding;
|
|
7
7
|
flag?: string;
|
|
8
8
|
} | BufferEncoding): Buffer;
|
|
9
|
-
declare function outputFileSync(filename: string, data: string, _options?:
|
|
9
|
+
declare function outputFileSync(filename: string, data: string, _options?: fs.WriteFileOptions): void;
|
|
10
10
|
declare function existsSync(filePath: fs.PathLike): boolean;
|
|
11
11
|
declare function lstatSync(filePath: fs.PathLike, _options?: fs.StatOptions & {
|
|
12
12
|
bigint?: false;
|
|
@@ -14,7 +14,7 @@ declare function lstatSync(filePath: fs.PathLike, _options?: fs.StatOptions & {
|
|
|
14
14
|
declare function chmodSync(_filePath: fs.PathLike, _mode: fs.Mode): void;
|
|
15
15
|
declare function renameSync(oldPath: fs.PathLike, newPath: fs.PathLike): void;
|
|
16
16
|
declare function copySync(src: string, dest: string, _options?: fs.CopyOptionsSync): void;
|
|
17
|
-
declare function mkdirpSync(dir: string):
|
|
17
|
+
declare function mkdirpSync(dir: string): string | undefined;
|
|
18
18
|
declare function removeSync(filePath: string): void;
|
|
19
19
|
declare function ensureFileSync(filename: string): void;
|
|
20
20
|
declare const _default: {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
type Path = string;
|
|
3
2
|
type RetVal = {
|
|
4
3
|
success: boolean;
|
|
5
4
|
error?: string | null;
|
|
@@ -14,6 +13,6 @@ interface RetBool extends RetVal {
|
|
|
14
13
|
value?: boolean;
|
|
15
14
|
}
|
|
16
15
|
interface RetPath extends RetVal {
|
|
17
|
-
value?:
|
|
16
|
+
value?: string;
|
|
18
17
|
}
|
|
19
|
-
export type {
|
|
18
|
+
export type { RetBool, RetString, RetVal, RetPath, RetBuffer, };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jestaubach/fs-helpers",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.26",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/fs-helpers.umd.cjs",
|
|
6
6
|
"module": "./dist/fs-helpers.js",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
],
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsc && vite build",
|
|
33
|
+
"lint": "eslint . --max-warnings 0",
|
|
33
34
|
"test": "vitest run --coverage --reporter dot"
|
|
34
35
|
},
|
|
35
36
|
"dependencies": {
|
|
@@ -43,7 +44,11 @@
|
|
|
43
44
|
"@types/fs-extra": "^11.0.2",
|
|
44
45
|
"@types/lodash": "^4.14.199",
|
|
45
46
|
"@types/node": "^20.7.0",
|
|
47
|
+
"@eslint/js": "^8.57.0",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
|
49
|
+
"@typescript-eslint/parser": "^6.21.0",
|
|
46
50
|
"@vitest/coverage-istanbul": "^0.34.5",
|
|
51
|
+
"eslint": "^8.57.0",
|
|
47
52
|
"typescript": "^5.2.2",
|
|
48
53
|
"vite": "^4.4.9",
|
|
49
54
|
"vite-plugin-dts": "^3.6.0",
|
package/dist/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import fsExtra from 'fs-extra';
|
|
4
|
-
declare function use(fsLibrary: any, seedFiles?: string[]): any;
|
|
5
|
-
declare const _default: {
|
|
6
|
-
use: typeof use;
|
|
7
|
-
default: typeof fsExtra;
|
|
8
|
-
mock: {
|
|
9
|
-
existsSync: (filePath: fsExtra.PathLike) => boolean;
|
|
10
|
-
lstatSync: (filePath: fsExtra.PathLike, _options?: (fsExtra.StatOptions & {
|
|
11
|
-
bigint?: false | undefined;
|
|
12
|
-
}) | undefined) => fsExtra.Stats;
|
|
13
|
-
chmodSync: (_filePath: fsExtra.PathLike, _mode: fsExtra.Mode) => void;
|
|
14
|
-
renameSync: (oldPath: fsExtra.PathLike, newPath: fsExtra.PathLike) => void;
|
|
15
|
-
readFileSync: (filePath: string, _options?: BufferEncoding | {
|
|
16
|
-
encoding: BufferEncoding;
|
|
17
|
-
flag?: string | undefined;
|
|
18
|
-
} | undefined) => Buffer;
|
|
19
|
-
copySync: (src: string, dest: string, _options?: fsExtra.CopyOptionsSync | undefined) => void;
|
|
20
|
-
removeSync: (filePath: string) => void;
|
|
21
|
-
ensureFileSync: (filename: string) => void;
|
|
22
|
-
mkdirpSync: (dir: string) => any;
|
|
23
|
-
outputFileSync: (filename: string, data: string, _options?: string | (fsExtra.ObjectEncodingOptions & import("events").Abortable & {
|
|
24
|
-
mode?: fsExtra.Mode | undefined;
|
|
25
|
-
flag?: string | undefined;
|
|
26
|
-
}) | null | undefined) => void;
|
|
27
|
-
seedFile: (fileName: string) => void;
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
export default _default;
|