Remove absolute paths

This commit is contained in:
CrazyMax
2020-04-08 00:33:20 +02:00
parent ff47e64685
commit fce5f2e313
342 changed files with 6646 additions and 7223 deletions
+77 -23
View File
@@ -2,12 +2,15 @@
const fs = require('fs');
const path = require('path');
const pify = require('pify');
const semver = require('semver');
const defaults = {
mode: 0o777 & (~process.umask()),
fs
};
const useNativeRecursiveOption = semver.satisfies(process.version, '>=10.12.0');
// https://github.com/nodejs/node/issues/8987
// https://github.com/libuv/libuv/pull/1088
const checkPath = pth => {
@@ -15,27 +18,56 @@ const checkPath = pth => {
const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, ''));
if (pathHasInvalidWinCharacters) {
const err = new Error(`Path contains invalid characters: ${pth}`);
err.code = 'EINVAL';
throw err;
const error = new Error(`Path contains invalid characters: ${pth}`);
error.code = 'EINVAL';
throw error;
}
}
};
module.exports = (input, opts) => Promise.resolve().then(() => {
checkPath(input);
opts = Object.assign({}, defaults, opts);
const permissionError = pth => {
// This replicates the exception of `fs.mkdir` with native the
// `recusive` option when run on an invalid drive under Windows.
const error = new Error(`operation not permitted, mkdir '${pth}'`);
error.code = 'EPERM';
error.errno = -4048;
error.path = pth;
error.syscall = 'mkdir';
return error;
};
const mkdir = pify(opts.fs.mkdir);
const stat = pify(opts.fs.stat);
const makeDir = (input, options) => Promise.resolve().then(() => {
checkPath(input);
options = Object.assign({}, defaults, options);
// TODO: Use util.promisify when targeting Node.js 8
const mkdir = pify(options.fs.mkdir);
const stat = pify(options.fs.stat);
if (useNativeRecursiveOption && options.fs.mkdir === fs.mkdir) {
const pth = path.resolve(input);
return mkdir(pth, {
mode: options.mode,
recursive: true
}).then(() => pth);
}
const make = pth => {
return mkdir(pth, opts.mode)
return mkdir(pth, options.mode)
.then(() => pth)
.catch(err => {
if (err.code === 'ENOENT') {
if (err.message.includes('null bytes') || path.dirname(pth) === pth) {
throw err;
.catch(error => {
if (error.code === 'EPERM') {
throw error;
}
if (error.code === 'ENOENT') {
if (path.dirname(pth) === pth) {
throw permissionError(pth);
}
if (error.message.includes('null bytes')) {
throw error;
}
return make(path.dirname(pth)).then(() => make(pth));
@@ -44,7 +76,7 @@ module.exports = (input, opts) => Promise.resolve().then(() => {
return stat(pth)
.then(stats => stats.isDirectory() ? pth : Promise.reject())
.catch(() => {
throw err;
throw error;
});
});
};
@@ -52,17 +84,39 @@ module.exports = (input, opts) => Promise.resolve().then(() => {
return make(path.resolve(input));
});
module.exports.sync = (input, opts) => {
module.exports = makeDir;
module.exports.default = makeDir;
module.exports.sync = (input, options) => {
checkPath(input);
opts = Object.assign({}, defaults, opts);
options = Object.assign({}, defaults, options);
if (useNativeRecursiveOption && options.fs.mkdirSync === fs.mkdirSync) {
const pth = path.resolve(input);
fs.mkdirSync(pth, {
mode: options.mode,
recursive: true
});
return pth;
}
const make = pth => {
try {
opts.fs.mkdirSync(pth, opts.mode);
} catch (err) {
if (err.code === 'ENOENT') {
if (err.message.includes('null bytes') || path.dirname(pth) === pth) {
throw err;
options.fs.mkdirSync(pth, options.mode);
} catch (error) {
if (error.code === 'EPERM') {
throw error;
}
if (error.code === 'ENOENT') {
if (path.dirname(pth) === pth) {
throw permissionError(pth);
}
if (error.message.includes('null bytes')) {
throw error;
}
make(path.dirname(pth));
@@ -70,11 +124,11 @@ module.exports.sync = (input, opts) => {
}
try {
if (!opts.fs.statSync(pth).isDirectory()) {
if (!options.fs.statSync(pth).isDirectory()) {
throw new Error('The path is not a directory');
}
} catch (_) {
throw err;
throw error;
}
}