aboutsummaryrefslogtreecommitdiff
path: root/lib/utils.js
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2022-12-09 22:18:26 +0100
committerRuben Beltran del Rio <ruben@unlimited.pizza>2022-12-09 22:18:26 +0100
commitd3f282a164e44f54678cdb45aad7a09c8a92b89e (patch)
tree215aa2018e3e931971fee2210d18d4a48872d7d6 /lib/utils.js
parentf91c2b4feb85933bc190712b45788d2f24fe851d (diff)
Remove force rms and dependency on ncp
Diffstat (limited to 'lib/utils.js')
-rw-r--r--lib/utils.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/utils.js b/lib/utils.js
new file mode 100644
index 0000000..24b9407
--- /dev/null
+++ b/lib/utils.js
@@ -0,0 +1,36 @@
+const { access, constants, mkdir, rm } = require('fs/promises');
+const { kFileNotFoundError } = require('./constants');
+
+// File system utilities
+
+module.exports = {
+ async rmIfExists(location) {
+
+ try {
+ await access(location, constants.F_OK);
+ await rm(location, { recursive: true });
+ }
+ catch (error) {
+ if (error.code === kFileNotFoundError) {
+ return;
+ }
+
+ throw error;
+ }
+ },
+
+ async ensureDirectoryExists(directory) {
+
+ try {
+ await access(directory);
+ }
+ catch (error) {
+ if (error.code === kFileNotFoundError) {
+ await mkdir(directory, { recursive: true });
+ return;
+ }
+
+ throw error;
+ }
+ }
+};