fs-extender
Version:
Extras suite for node fs module
361 lines • 12.8 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mkdirpSync = exports.promises = exports.mkdirp = void 0;
const path_extender_1 = __importDefault(require("path-extender"));
const fs = __importStar(require("../patch"));
const utils_1 = require("@n3okill/utils");
const util = __importStar(require("../util"));
const os_1 = require("os");
/** @internal */
const isWindows = /^win/.test((0, os_1.platform)());
/** @internal */
const InvalidWin32Chars = /[<>:"|?*]/;
/** @internal */
/* istanbul ignore next */
function invalidWin32Path(p) {
const rootPath = getRootPath(p);
p = Buffer.isBuffer(p) ? utils_1.BufferUtil.toString(utils_1.BufferUtil.multiReplace(p, [rootPath], "")) : p.replace(rootPath, "");
return InvalidWin32Chars.test(p);
}
/** @internal */
/* istanbul ignore next */
function getRootPath(p) {
const pAux = Buffer.isBuffer(p)
? utils_1.BufferUtil.split(path_extender_1.default.normalize(p), path_extender_1.default.sep).map((s) => utils_1.BufferUtil.toString(s))
: path_extender_1.default.normalize(p).split(path_extender_1.default.sep);
return pAux.length > 0 ? pAux[0] : "";
}
/** @internal */
function getPaths(originalPath) {
let paths = [];
const path = Array.isArray(originalPath) ? originalPath : [originalPath];
const isBuffer = path.some((s) => Buffer.isBuffer(s));
path.forEach((p) => {
let ps = [util.toStringOrBuffer(isBuffer, p)];
/* istanbul ignore next */
if (Buffer.isBuffer(p)) {
if (utils_1.BufferUtil.toString(p).indexOf("{") !== -1) {
ps = utils_1.BufferUtil.expand(p);
}
}
else {
if (p.indexOf("{") !== -1) {
ps = utils_1.StringUtil.expand(p);
}
}
ps = Array.isArray(ps) ? ps : [ps];
paths = paths.concat(ps);
});
paths = paths.map((p) => path_extender_1.default.resolve(p));
return paths;
}
/** @internal */
function getOptions(opt) {
let mode = util.getObjectOption(opt, "mode", 0o777);
/* istanbul ignore next */
if (utils_1.Type.isNumeric(mode) && utils_1.Type.isString(mode)) {
mode = parseInt(mode, 8);
}
return {
mode: mode,
};
}
function mkdirp(path, options, cb) {
const opt = getOptions(options);
const callback = util.getCallback(options, cb);
const paths = getPaths(path);
_mkdirpp(paths, opt)
.then((items) => callback(null, items))
.catch((err) => callback(err));
}
exports.mkdirp = mkdirp;
/** @internal */
function _mkdirpp(paths, options) {
return __awaiter(this, void 0, void 0, function* () {
const done = [];
const umask = process.umask(0);
try {
while (paths.length) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const p = paths.shift();
/* istanbul ignore next */
if (isWindows && invalidWin32Path(p)) {
const e = new Error("Invalid character found in path.");
e.code = "EINVAL";
throw e;
}
const result = yield __mkdirpp(p, options);
done.push(result);
}
}
finally {
process.umask(umask);
}
return done.length === 1 ? done[0] : done;
});
}
/** @internal */
function __mkdirpp(path, options) {
return __awaiter(this, void 0, void 0, function* () {
const stack = [];
const done = [];
stack.push(path);
do {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const p = stack.pop();
try {
yield fs.promises.mkdir(p, options.mode);
}
catch (er) {
const err = er;
switch (err.code) {
case "EEXIST":
let stat;
try {
stat = yield fs.promises.stat(p);
}
catch (errStat) {
/* istanbul ignore next */
throw err;
}
if (!stat.isDirectory()) {
throw err;
}
else if (!stack.length) {
return p;
}
break;
case "ENOENT":
const parent = path_extender_1.default.dirname(p);
if (util.equal(parent, p) ||
done.indexOf(p) !== -1 ||
(Buffer.isBuffer(p) && /\0/.test(utils_1.BufferUtil.toString(p))) ||
(typeof p === "string" && /\0/.test(p))) {
/* istanbul ignore next */
throw err;
}
done.push(p);
stack.push(p);
stack.push(parent);
break;
default:
/* istanbul ignore next */
try {
const stat = yield fs.promises.stat(p);
if (stat.isDirectory()) {
return p;
}
}
catch (errStat) { }
throw err;
}
}
} while (stack.length);
return path;
});
}
// eslint-disable-next-line @typescript-eslint/no-namespace
var promises;
(function (promises) {
/**
* Asynchronously creates a directory.
*
* Will return all the paths created
*
* The optional `options` argument can be an integer specifying `mode` (permission
* and sticky bits), or an object with a `mode` property and a optional `fs` property.
*
* ```js
* import * as fs from 'fs-extender';
*
* // Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.
* fs.promises.mkdirp('/tmp/a/apple');
* ```
*
* ```js
* fs.promises.mkdirp('/path/{to1,to2}/{dir1,dir2}');
* ```
* will create the directories:
* /path/to1/dir1
* /path/to1/dir2
* /path/to2/dir1
* /path/to2/dir2
*
* On Windows, using `fs.mkdirp()` on the root directory even with recursion will
* result in an error:
*
* ```js
* fs.promises.mkdir('/');
* // => [Error: EPERM: operation not permitted, mkdir 'C:\']
* ```
*
*
* See the POSIX [`mkdir(2)`](http://man7.org/linux/man-pages/man2/mkdir.2.html) documentation for more details.
*/
function mkdirp(path, options) {
return __awaiter(this, void 0, void 0, function* () {
const opt = getOptions(options);
const paths = getPaths(path);
return _mkdirpp(paths, opt);
});
}
promises.mkdirp = mkdirp;
})(promises = exports.promises || (exports.promises = {}));
/**
* Synchronously creates a directory.
*
* Will return all the paths created
*
* The optional `options` argument can be an integer specifying `mode` (permission
* and sticky bits), or an object with a `mode` property and a optional `fs` property.
*
* ```js
* import * as fs from 'fs-extender';
*
* // Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.
* fs.mkdirpSync('/tmp/a/apple');
* ```
*
* ```js
* fs.mkdirpSync('/path/{to1,to2}/{dir1,dir2}');
* ```
* will create the directories:
* /path/to1/dir1
* /path/to1/dir2
* /path/to2/dir1
* /path/to2/dir2
*
* On Windows, using `fs.mkdirp()` on the root directory even with recursion will
* result in an error:
*
* ```js
* fs.mkdirpSync('/');
* // => [Error: EPERM: operation not permitted, mkdir 'C:\']
* ```
*
*
* See the POSIX [`mkdir(2)`](http://man7.org/linux/man-pages/man2/mkdir.2.html) documentation for more details.
*/
function mkdirpSync(path, options) {
let done = [];
const opt = getOptions(options);
const paths = getPaths(path);
const umask = process.umask(0);
try {
while (paths.length) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const p = paths.shift();
/* istanbul ignore next: tested only in windows */
if (isWindows && invalidWin32Path(p)) {
const e = new Error("Invalid character found in path.");
e.code = "EINVAL";
throw e;
}
const result = _mkdirpSync(p, opt);
done.push(result);
}
}
finally {
process.umask(umask);
}
done = done.filter((d) => !!d);
return done.length > 1 ? done : done[0];
}
exports.mkdirpSync = mkdirpSync;
/** @internal */
function _mkdirpSync(path, options) {
const stack = [];
const done = [];
stack.push(path);
do {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const p = stack.pop();
try {
fs.mkdirSync(p, options.mode);
}
catch (er) {
const err = er;
if (err.code !== "ENOENT" && err.code !== "EEXIST") {
try {
const stats = fs.statSync(p);
/* istanbul ignore next */
if (stats.isDirectory()) {
return p;
}
return p;
}
catch (err) { }
throw err;
}
else if (err.code === "ENOENT") {
const parent = path_extender_1.default.dirname(p);
if (util.equal(parent, p) ||
(Buffer.isBuffer(p) && /\0/.test(utils_1.BufferUtil.toString(p))) ||
(typeof p === "string" && /\0/.test(p))) {
/* istanbul ignore next */
throw err;
}
if (done.indexOf(p) === -1) {
done.push(p);
stack.push(p);
stack.push(parent);
}
else {
/* istanbul ignore next */
throw err;
}
}
else if (err.code === "EEXIST") {
let stat;
try {
stat = fs.statSync(p);
}
catch (errStat) {
/* istanbul ignore next */
throw err;
}
if (stat) {
if (!stat.isDirectory()) {
throw err;
}
else if (!stack.length) {
return p;
}
}
}
}
} while (stack.length);
return path;
}
//# sourceMappingURL=index.js.map
;