]> git.r.bdr.sh - rbdr/blog/blobdiff - lib/utils.js
Remove force rms and dependency on ncp
[rbdr/blog] / lib / utils.js
diff --git a/lib/utils.js b/lib/utils.js
new file mode 100644 (file)
index 0000000..24b9407
--- /dev/null
@@ -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;
+    }
+  }
+};