@jestaubach/fs-helpers 0.2.25 → 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 CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  ## Badges
4
4
 
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/4896190d45c4fb79ce45/test_coverage)](https://codeclimate.com/github/JEStaubach/fs-helpers/test_coverage)
6
- [![Maintainability](https://api.codeclimate.com/v1/badges/4896190d45c4fb79ce45/maintainability)](https://codeclimate.com/github/JEStaubach/fs-helpers/maintainability)
5
+ [![Maintainability](https://qlty.sh/gh/JEStaubach/projects/fs-helpers/maintainability.svg)](https://qlty.sh/gh/JEStaubach/projects/fs-helpers)
6
+ [![Code Coverage](https://qlty.sh/gh/JEStaubach/projects/fs-helpers/coverage.svg)](https://qlty.sh/gh/JEStaubach/projects/fs-helpers)
7
7
  [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/626bca3c1fab45fcb7b6f658b7e04604)](https://app.codacy.com/gh/JEStaubach/fs-helpers/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
8
8
  [![Codacy Badge](https://app.codacy.com/project/badge/Grade/626bca3c1fab45fcb7b6f658b7e04604)](https://app.codacy.com/gh/JEStaubach/fs-helpers/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
9
9
  [![Known Vulnerabilities](https://snyk.io/test/github/JEStaubach/fs-helpers/badge.svg)](https://snyk.io/test/github/JEStaubach/fs-helpers)
@@ -0,0 +1 @@
1
+ export {};
@@ -1,36 +1,33 @@
1
- var N = Object.defineProperty;
2
- var q = (e, t, o) => t in e ? N(e, t, { enumerable: !0, configurable: !0, writable: !0, value: o }) : e[t] = o;
3
- var p = (e, t, o) => (q(e, typeof t != "symbol" ? t + "" : t, o), o);
4
- const { default: z } = (() => {
5
- const e = require("fs-extra");
6
- return e && e.__esModule ? e : Object.assign(/* @__PURE__ */ Object.create(null), e, { default: e, [Symbol.toStringTag]: "Module" });
7
- })(), { default: l } = (() => {
8
- const e = require("path");
9
- return e && e.__esModule ? e : Object.assign(/* @__PURE__ */ Object.create(null), e, { default: e, [Symbol.toStringTag]: "Module" });
10
- })(), n = /* @__PURE__ */ new Map();
11
- function R(e) {
12
- const { root: t, dir: o } = l.parse(e), i = o.split(t).slice(1).join(t).split(l.sep);
13
- for (let a = 0; a < i.length; a++) {
14
- const d = [t, i.slice(0, a + 1).join(l.sep)].join("");
15
- g(d);
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 B(e, t) {
21
- const [o, i] = n.get(e) || ["", ""];
22
- return Buffer.from(i);
17
+ function p(t, e) {
18
+ const [r, s] = i.get(t) || ["", ""];
19
+ return Buffer.from(s);
23
20
  }
24
- function S(e, t, o) {
25
- n.set(e, ["file", t]);
21
+ function l(t, e, r) {
22
+ i.set(t, ["file", e]);
26
23
  }
27
- function L(e) {
28
- return e === l.resolve(".") ? !0 : n.has(e);
24
+ function g(t) {
25
+ return t === c.resolve(".") ? !0 : i.has(t);
29
26
  }
30
- function b(e, t) {
31
- const [o, i] = n.get(e) || ["", ""], c = {
32
- isDirectory: () => e === l.resolve(".") ? !0 : o === "dir",
33
- isFile: () => n.has(e) && o === "file",
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 c.isBlockDevice(), c.isCharacterDevice(), c.isSymbolicLink(), c.isFIFO(), c.isSocket(), c;
55
+ return o.isBlockDevice(), o.isCharacterDevice(), o.isSymbolicLink(), o.isFIFO(), o.isSocket(), o;
59
56
  }
60
- function P(e, t) {
57
+ function b(t, e) {
61
58
  }
62
- class V extends Error {
63
- constructor(o) {
64
- super(o);
65
- p(this, "code");
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 J(e, t) {
70
- if (!n.has(e))
71
- throw new V("source path not found");
72
- for (const o of Array.from(n.keys()))
73
- if (o.includes(e)) {
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 Z(e, t, o) {
80
- if (n.has(t))
81
- throw Error("destination directory exists");
82
- for (const i of Array.from(n.keys()))
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 g(e) {
93
- if (b(e).isFile())
94
- throw Error("file exists at destination");
95
- let t;
96
- const o = e.split(l.resolve(".")).join("").split(l.sep);
97
- for (let i = 0; i < o.length; i++) {
98
- const c = o.slice(0, i + 1).join(l.sep);
99
- !n.has(`${l.resolve(".")}${c}`) && c !== "" && (n.set(`${l.resolve(".")}${c}`, ["dir", ""]), t = t ?? `${l.resolve(".")}${c}`);
100
- }
101
- return t;
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 G(e) {
104
- for (const t of Array.from(n.keys()))
105
- t.includes(e) && n.delete(t);
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 H(e) {
108
- n.has(e) || n.set(e, ["file", ""]);
107
+ function $(t) {
108
+ for (const e of Array.from(i.keys()))
109
+ e.includes(t) && i.delete(e);
109
110
  }
110
- const K = { existsSync: L, lstatSync: b, chmodSync: P, renameSync: J, readFileSync: B, copySync: Z, removeSync: G, ensureFileSync: H, mkdirpSync: g, outputFileSync: S, seedFile: R }, { default: Q } = (() => {
111
- const e = require("fs-extra");
112
- return e && e.__esModule ? e : Object.assign(/* @__PURE__ */ Object.create(null), e, { default: e, [Symbol.toStringTag]: "Module" });
113
- })(), { default: h } = (() => {
114
- const e = require("path");
115
- return e && e.__esModule ? e : Object.assign(/* @__PURE__ */ Object.create(null), e, { default: e, [Symbol.toStringTag]: "Module" });
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
- function U(e, t) {
118
- const { existsSync: o, lstatSync: i, chmodSync: c, renameSync: a, readFileSync: d, copySync: D, removeSync: E, ensureFileSync: k, outputFileSync: $, mkdirpSync: F, seedFile: m } = e;
119
- if (m !== void 0 && t !== void 0)
120
- for (const r of t)
121
- _(r);
122
- function _(r) {
123
- const s = u(r).value;
124
- m(s);
125
- }
126
- function w(r, s) {
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: d(u(r).value, s),
154
+ value: this.readFileSync(this.getAbsolutePath(e).value, r),
130
155
  error: null
131
156
  };
132
157
  }
133
- function j(r, s) {
134
- return $(u(r).value, s), {
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
- function x(r) {
140
- const s = u(r).value;
141
- return o(s) ? i(s).isFile() ? {
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: `checkIfFileExists: '${r}' is not a file.`
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
- function v(r) {
156
- const s = u(r).value;
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
- function u(r) {
172
+ checkIfDirExists(e) {
173
+ return this.checkIfPathExists(e, "directory", (r) => r.isDirectory());
174
+ }
175
+ getAbsolutePath(e) {
172
176
  try {
173
- if (r === void 0)
174
- throw Error("Dir is undefined.");
175
- if (r.match(/^[.a-zA-Z0-9\-_/:\\]+$/g) === null)
176
- throw Error(`Dir contains unsupported characters. Received ${r}.`);
177
- return {
178
- success: !0,
179
- value: h.normalize(h.resolve(r)),
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: '${r}'. Received error: '${s}'`
186
+ error: `Error resolving path: '${e}'. Received error: '${r}'`
187
187
  };
188
188
  }
189
189
  }
190
- function M(r) {
190
+ createDir(e) {
191
191
  try {
192
- if (r === void 0)
193
- throw Error(`Function "createDir" expected a path. Recieved "${r}".`);
194
- const s = u(r).value;
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: ${r}`), {
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
- function O(r, s) {
209
- const f = u(r).value;
210
- return k(f), s !== void 0 && c(f, s), {
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
- function y(r) {
216
- return u(r).value !== void 0 && v(r).value ? (E(r), {
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
- function A(r) {
227
- return r.map((s) => y(u(s).value));
207
+ rimrafDirs(e) {
208
+ return e.map((r) => this.rimrafDir(this.getAbsolutePath(r).value));
228
209
  }
229
- function C(r) {
230
- return r !== null && v(r).value ? (console.error(`Cleaning up due to abort, directories created starting at: ${JSON.stringify(r)}`), y(r), {
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
- function I(r, s) {
213
+ renameDir(e, r) {
239
214
  try {
240
- return a(u(r).value, u(s).value), {
241
- success: !0,
242
- value: "Successfully renamed the directory.",
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
- function T(r, s) {
220
+ copyDirAbs(e, r) {
254
221
  try {
255
- return D(u(r).value, u(s).value, { overwrite: !1, errorOnExist: !0 }), {
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
- const X = { use: U, default: Q, mock: K };
228
+ function I(t, e) {
229
+ return new M(t, e);
230
+ }
231
+ const O = { use: I, default: _, mock: A };
282
232
  export {
283
- X as default
233
+ O as default
284
234
  };
@@ -1 +1 @@
1
- (function(a,n){typeof exports=="object"&&typeof module<"u"?module.exports=n():typeof define=="function"&&define.amd?define(n):(a=typeof globalThis<"u"?globalThis:a||self,a["fs-helpers"]=n())})(this,function(){"use strict";var Q=Object.defineProperty;var U=(a,n,s)=>n in a?Q(a,n,{enumerable:!0,configurable:!0,writable:!0,value:s}):a[n]=s;var D=(a,n,s)=>(U(a,typeof n!="symbol"?n+"":n,s),s);const{default:a}=(()=>{const e=require("fs-extra");return e&&e.__esModule?e:Object.assign(Object.create(null),e,{default:e,[Symbol.toStringTag]:"Module"})})(),{default:n}=(()=>{const e=require("path");return e&&e.__esModule?e:Object.assign(Object.create(null),e,{default:e,[Symbol.toStringTag]:"Module"})})(),s=new Map;function E(e){const{root:o,dir:i}=n.parse(e),u=i.split(o).slice(1).join(o).split(n.sep);for(let f=0;f<u.length;f++){const v=[o,u.slice(0,f+1).join(n.sep)].join("");b(v)}const c=a.readFileSync(e).toString();m(e,c)}function k(e,o){const[i,u]=s.get(e)||["",""];return Buffer.from(u)}function m(e,o,i){s.set(e,["file",o])}function $(e){return e===n.resolve(".")?!0:s.has(e)}function h(e,o){const[i,u]=s.get(e)||["",""],c={isDirectory:()=>e===n.resolve(".")?!0:i==="dir",isFile:()=>s.has(e)&&i==="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 c.isBlockDevice(),c.isCharacterDevice(),c.isSymbolicLink(),c.isFIFO(),c.isSocket(),c}function F(e,o){}class x extends Error{constructor(i){super(i);D(this,"code");this.code="ENOENT"}}function _(e,o){if(!s.has(e))throw new x("source path not found");for(const i of Array.from(s.keys()))if(i.includes(e)){const u=i.split(e).join(o),c=s.get(i)||["",""];s.delete(i),s.set(u,c)}}function j(e,o,i){if(s.has(o))throw Error("destination directory exists");for(const u of Array.from(s.keys()))if(u.includes(e)){const c=u.split(e).join(o);s.set(c,s.get(u)||["",""])}}function b(e){if(h(e).isFile())throw Error("file exists at destination");let o;const i=e.split(n.resolve(".")).join("").split(n.sep);for(let u=0;u<i.length;u++){const c=i.slice(0,u+1).join(n.sep);!s.has(`${n.resolve(".")}${c}`)&&c!==""&&(s.set(`${n.resolve(".")}${c}`,["dir",""]),o=o??`${n.resolve(".")}${c}`)}return o}function w(e){for(const o of Array.from(s.keys()))o.includes(e)&&s.delete(o)}function M(e){s.has(e)||s.set(e,["file",""])}const O={existsSync:$,lstatSync:h,chmodSync:F,renameSync:_,readFileSync:k,copySync:j,removeSync:w,ensureFileSync:M,mkdirpSync:b,outputFileSync:m,seedFile:E},{default:T}=(()=>{const e=require("fs-extra");return e&&e.__esModule?e:Object.assign(Object.create(null),e,{default:e,[Symbol.toStringTag]:"Module"})})(),{default:S}=(()=>{const e=require("path");return e&&e.__esModule?e:Object.assign(Object.create(null),e,{default:e,[Symbol.toStringTag]:"Module"})})();function A(e,o){const{existsSync:i,lstatSync:u,chmodSync:c,renameSync:f,readFileSync:v,copySync:C,removeSync:I,ensureFileSync:N,outputFileSync:q,mkdirpSync:z,seedFile:g}=e;if(g!==void 0&&o!==void 0)for(const r of o)R(r);function R(r){const t=l(r).value;g(t)}function B(r,t){return{success:!0,value:v(l(r).value,t),error:null}}function L(r,t){return q(l(r).value,t),{success:!0,error:null}}function P(r){const t=l(r).value;return i(t)?u(t).isFile()?{success:!0,value:!0,error:null}:{success:!1,value:!1,error:`checkIfFileExists: '${r}' is not a file.`}:{success:!0,value:!1,error:null}}function y(r){const t=l(r).value;return i(t)?u(t).isDirectory()?{success:!0,value:!0,error:null}:{success:!1,value:!1,error:`checkIfDirExists: '${r}' is not a directory.`}:{success:!0,value:!1,error:null}}function l(r){try{if(r===void 0)throw Error("Dir is undefined.");if(r.match(/^[.a-zA-Z0-9\-_/:\\]+$/g)===null)throw Error(`Dir contains unsupported characters. Received ${r}.`);return{success:!0,value:S.normalize(S.resolve(r)),error:null}}catch(t){return console.error(`Error resolving path: ${r}`),{success:!1,value:void 0,error:`Error resolving path: '${r}'. Received error: '${t}'`}}}function V(r){try{if(r===void 0)throw Error(`Function "createDir" expected a path. Recieved "${r}".`);const t=l(r).value;return{success:!0,value:z(t),error:null}}catch{return console.error(`Error creating dir: ${r}`),{success:!1,value:void 0,error:`Error creating dir: '${r}'`}}}function J(r,t){const d=l(r).value;return N(d),t!==void 0&&c(d,t),{success:!0,error:null}}function p(r){return l(r).value!==void 0&&y(r).value?(I(r),{success:!0,value:r,error:null}):(console.error(`Error deleting dir: ${r}`),{success:!1,value:void 0,error:`Error deleting dir: '${r}'`})}function Z(r){return r.map(t=>p(l(t).value))}function G(r){return r!==null&&y(r).value?(console.error(`Cleaning up due to abort, directories created starting at: ${JSON.stringify(r)}`),p(r),{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."})}function H(r,t){try{return f(l(r).value,l(t).value),{success:!0,value:"Successfully renamed the directory.",error:null}}catch(d){return console.error(d.code),{success:!1,value:void 0,error:`renameDir from '${r}' to '${t} failed.`}}}function K(r,t){try{return C(l(r).value,l(t).value,{overwrite:!1,errorOnExist:!0}),{success:!0,error:null}}catch{return{success:!1,error:`Error copying absolute from '${r}' to '${t}'`}}}return{getAbsolutePath:l,abortDirCreation:G,checkIfDirExists:y,copyDirAbs:K,createDir:V,renameDir:H,rimrafDir:p,rimrafDirs:Z,checkIfFileExists:P,readFile:B,writeFile:L,touchFile:J}}return{use:A,default:T,mock:O}});
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?: string | fs.WriteFileOptions): void;
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): any;
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?: Path;
16
+ value?: string;
18
17
  }
19
- export type { Path, RetBool, RetString, RetVal, RetPath, RetBuffer, };
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.25",
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;