@cocalc/backend
Version:
CoCalc backend functionality: functionality used by either the hub, the next.js server or the project.
39 lines • 1.45 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fs_1 = require("node:fs");
const promises_1 = require("node:fs/promises");
const misc_1 = require("@cocalc/util/misc");
const abspath_1 = __importDefault(require("./abspath"));
// Make sure that that the directory containing the file indicated by
// the path exists and has restrictive permissions.
async function ensureContainingDirectoryExists(path) {
path = (0, abspath_1.default)(path);
const containingDirectory = (0, misc_1.path_split)(path).head; // containing path
if (!containingDirectory)
return;
try {
await (0, promises_1.access)(containingDirectory, node_fs_1.constants.R_OK | node_fs_1.constants.W_OK);
// it exists, yeah!
return;
}
catch (err) {
// Doesn't exist, so create, via recursion:
try {
await (0, promises_1.mkdir)(containingDirectory, { mode: 0o700, recursive: true });
}
catch (err) {
if (err?.code === "EEXIST") {
// no problem -- it exists.
return;
}
else {
throw err;
}
}
}
}
exports.default = ensureContainingDirectoryExists;
//# sourceMappingURL=ensure-containing-directory-exists.js.map