Comparing version 0.3.9 to 0.4.0
"use strict"; | ||
var isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, startsWith = require("es5-ext/string/#/starts-with") | ||
, deferred = require("deferred") | ||
, path = require("path") | ||
, lstat = require("./lstat") | ||
, mkdir = require("./mkdir") | ||
, readdir = require("./readdir") | ||
, readlink = require("./readlink") | ||
, symlink = require("./symlink") | ||
, copyFile = require("./copy").copy; | ||
const isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, startsWith = require("es5-ext/string/#/starts-with") | ||
, deferred = require("deferred") | ||
, path = require("path") | ||
, lstat = require("./lstat") | ||
, mkdir = require("./mkdir") | ||
, readdir = require("./readdir") | ||
, readlink = require("./readlink") | ||
, symlink = require("./symlink") | ||
, copyFile = require("./copy").copy; | ||
var dirname = path.dirname, relative = path.relative, resolve = path.resolve, sep = path.sep; | ||
var copyDir = function (source, dest, options, sourceTop, destTop) { | ||
const copyDir = function (source, dest, options, sourceTop, destTop) { | ||
return readdir(source, { | ||
@@ -22,11 +20,11 @@ type: { directory: true, file: true, symbolicLink: true }, | ||
})( | ||
function (files) { | ||
return deferred.map(files, function (relativePath) { | ||
var filename = resolve(source, relativePath); | ||
return lstat(filename)(function (stats) { | ||
files => | ||
deferred.map(files, relativePath => { | ||
const filename = path.resolve(source, relativePath); | ||
return lstat(filename)(stats => { | ||
if (stats.isDirectory()) { | ||
return mkdir(resolve(dest, relativePath), { intermediate: true }); | ||
return mkdir(path.resolve(dest, relativePath), { intermediate: true }); | ||
} | ||
if (stats.isFile()) { | ||
return copyFile(filename, resolve(dest, relativePath), { | ||
return copyFile(filename, path.resolve(dest, relativePath), { | ||
intermediate: true | ||
@@ -36,14 +34,14 @@ }); | ||
if (!stats.isSymbolicLink()) return null; | ||
return readlink(filename)(function (linkPath) { | ||
linkPath = resolve(dirname(filename), linkPath); | ||
var linkDirname = dirname(linkPath); | ||
return readlink(filename)(linkPath => { | ||
linkPath = path.resolve(path.dirname(filename), linkPath); | ||
const linkDirname = path.dirname(linkPath); | ||
if ( | ||
linkDirname === sourceTop || | ||
startsWith.call(linkDirname, sourceTop + sep) | ||
startsWith.call(linkDirname, sourceTop + path.sep) | ||
) { | ||
linkPath = resolve(destTop, linkPath.slice(sourceTop.length + 1)); | ||
linkPath = path.resolve(destTop, linkPath.slice(sourceTop.length + 1)); | ||
} | ||
return symlink( | ||
relative(dirname(resolve(dest, relativePath)), linkPath), | ||
resolve(dest, relativePath), | ||
path.relative(path.dirname(path.resolve(dest, relativePath)), linkPath), | ||
path.resolve(dest, relativePath), | ||
{ intermediate: true } | ||
@@ -53,5 +51,4 @@ ); | ||
}); | ||
}); | ||
}, | ||
function (error) { | ||
}), | ||
error => { | ||
if (options.loose && error.code === "ENOENT") return false; | ||
@@ -64,4 +61,3 @@ throw error; | ||
module.exports = exports = function (source, dest/*, options, cb*/) { | ||
var options = Object(arguments[2]), cb = arguments[3]; | ||
module.exports = exports = function (source, dest, options = null, cb = null) { | ||
if (!isValue(cb) && isCallable(options)) { | ||
@@ -72,10 +68,15 @@ cb = options; | ||
source = resolve(String(source)); | ||
dest = resolve(String(dest)); | ||
return lstat(dest, { loose: true })(function (stats) { | ||
if (stats) throw new Error("Destination path exists"); | ||
return copyDir(source, dest, options, source, dest); | ||
}).cb(cb); | ||
source = path.resolve(String(source)); | ||
dest = path.resolve(String(dest)); | ||
return lstat(dest, { loose: true })(stats => { | ||
if (stats && stats.isDirectory()) { | ||
return readdir(dest).then(filenames => { | ||
if (!filenames.length) return; | ||
throw new Error("Destination path is not empty"); | ||
}); | ||
} | ||
return null; | ||
})(() => copyDir(source, dest, options, source, dest)).cb(cb); | ||
}; | ||
exports.copyDir = copyDir; | ||
exports.returnsPromise = true; |
"use strict"; | ||
var lstat = require("./lstat").lstat; | ||
const { lstat } = require("./lstat"); | ||
module.exports = function (path/*, callback*/) { | ||
return lstat(path, { loose: true })(function (stats) { | ||
module.exports = function (path, callback = null) { | ||
return lstat(path, { loose: true })(stats => { | ||
if (stats) return stats.isDirectory(); | ||
return null; | ||
}).cb(arguments[1]); | ||
}).cb(callback); | ||
}; | ||
module.exports.returnsPromise = true; |
@@ -5,44 +5,36 @@ /* eslint max-statements: off, max-lines: off */ | ||
var invoke = require("es5-ext/function/invoke") | ||
, noop = require("es5-ext/function/noop") | ||
, isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, forEach = require("es5-ext/object/for-each") | ||
, memoize = require("memoizee") | ||
, deferred = require("deferred") | ||
, ignore = require("ignore") | ||
, pathUtils = require("path") | ||
, modes = require("./lib/ignore-modes") | ||
, getMap = require("./lib/get-conf-file-map") | ||
, memoizeWatcher = require("./lib/memoize-watcher") | ||
, findRoot = require("./lib/find-root"); | ||
const invoke = require("es5-ext/function/invoke") | ||
, noop = require("es5-ext/function/noop") | ||
, isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, forEach = require("es5-ext/object/for-each") | ||
, memoize = require("memoizee") | ||
, deferred = require("deferred") | ||
, ignore = require("ignore") | ||
, pathUtils = require("path") | ||
, modes = require("./lib/ignore-modes") | ||
, getMap = require("./lib/get-conf-file-map") | ||
, memoizeWatcher = require("./lib/memoize-watcher") | ||
, findRoot = require("./lib/find-root"); | ||
var isArray = Array.isArray | ||
, push = Array.prototype.push | ||
, call = Function.prototype.call | ||
, trim = call.bind(String.prototype.trim) | ||
, dirname = pathUtils.dirname | ||
, resolve = pathUtils.resolve | ||
, sep = pathUtils.sep | ||
, ConfMap = getMap.ConfMap | ||
, applyRules | ||
, applyGlobalRules | ||
, compile | ||
, IsIgnored | ||
, isIgnored | ||
, buildMap | ||
, prepareRules | ||
, parseSrc | ||
, eolRe = /(?:\r\n|[\n\r\u2028\u2029])/; | ||
const { isArray } = Array | ||
, { push } = Array.prototype | ||
, { call } = Function.prototype | ||
, trim = call.bind(String.prototype.trim) | ||
, { dirname } = pathUtils | ||
, { resolve } = pathUtils | ||
, { sep } = pathUtils | ||
, { ConfMap } = getMap | ||
, eolRe = /(?:\r\n|[\n\r\u2028\u2029])/u; | ||
prepareRules = function (data) { return data.map(trim).filter(Boolean).reverse(); }; | ||
const prepareRules = function (data) { return data.map(trim).filter(Boolean).reverse(); }; | ||
parseSrc = function (src) { return prepareRules(String(src).split(eolRe)); }; | ||
const parseSrc = function (src) { return prepareRules(String(src).split(eolRe)); }; | ||
compile = function (maps, result) { | ||
var data = (result.data = {}), paths = (result.paths = []); | ||
const compile = function (maps, result) { | ||
const data = (result.data = {}), paths = (result.paths = []); | ||
// Merge rules found in ignorefiles | ||
maps.forEach(function (map) { | ||
forEach(map.map, function (rules, path) { | ||
maps.forEach(map => { | ||
forEach(map.map, (rules, path) => { | ||
if (!rules.length) return; | ||
@@ -60,13 +52,16 @@ if (!data[path]) { | ||
applyRules = function (rules, rootPath, path) { | ||
const applyRules = function (rules, rootPath, path) { | ||
if (!rootPath.endsWith(sep)) rootPath += sep; | ||
if (!path.startsWith(rootPath)) return { value: false, target: path }; | ||
rules = rules.slice().reverse().filter(function (rule) { return !rule.startsWith("#"); }); | ||
var ig = ignore().add(rules); | ||
var testPath = path.slice(rootPath.length); | ||
var result = ig.ignores(testPath); | ||
rules = rules | ||
.slice() | ||
.reverse() | ||
.filter(rule => !rule.startsWith("#")); | ||
const ig = ignore().add(rules); | ||
const testPath = path.slice(rootPath.length); | ||
let result = ig.ignores(testPath); | ||
if (!result) { | ||
var excludeRules = rules.filter(function (rule) { return rule.startsWith("!"); }); | ||
const excludeRules = rules.filter(rule => rule.startsWith("!")); | ||
if (excludeRules.length) { | ||
var ig2 = ignore().add(excludeRules.map(function (rule) { return rule.slice(1); })); | ||
const ig2 = ignore().add(excludeRules.map(rule => rule.slice(1))); | ||
if (!ig2.ignores(testPath)) result = null; | ||
@@ -80,16 +75,15 @@ } else { | ||
applyGlobalRules = function (path, rules) { | ||
var value; | ||
const applyGlobalRules = function (path, rules) { | ||
// Check global rules | ||
value = applyRules(rules, path.slice(0, path.indexOf(sep) + 1), path); | ||
const value = applyRules(rules, path.slice(0, path.indexOf(sep) + 1), path); | ||
return Boolean(value.value); | ||
}; | ||
buildMap = function (lDirname, lGetMap, watch) { | ||
var promise, data = {}, maps; | ||
lGetMap = lGetMap.map(function (getSubMap, index) { | ||
var map = getSubMap(lDirname); | ||
const buildMap = function (lDirname, lGetMap, watch) { | ||
let promise, maps; | ||
const data = {}; | ||
lGetMap = lGetMap.map((getSubMap, index) => { | ||
const map = getSubMap(lDirname); | ||
if (watch) { | ||
map.on("change", function (targetMap) { | ||
map.on("change", targetMap => { | ||
if (maps) { | ||
@@ -105,3 +99,3 @@ maps[index] = targetMap; | ||
if (lGetMap.length > 1) { | ||
promise = deferred.map(lGetMap)(function (result) { | ||
promise = deferred.map(lGetMap)(result => { | ||
maps = result; | ||
@@ -111,3 +105,3 @@ return compile(maps, data); | ||
} else { | ||
promise = lGetMap[0](function (map) { | ||
promise = lGetMap[0](map => { | ||
maps = [map]; | ||
@@ -123,3 +117,3 @@ return compile(maps, data); | ||
IsIgnored = function (path, watch) { | ||
const IsIgnored = function (path, watch) { | ||
this.path = path; | ||
@@ -131,21 +125,16 @@ this.dirname = dirname(path); | ||
IsIgnored.prototype = { | ||
init: function (mapPromise) { | ||
init(mapPromise) { | ||
this.mapPromise = mapPromise; | ||
this.promise = mapPromise( | ||
function (data) { | ||
this.data = data; | ||
return this.calculate(); | ||
}.bind(this) | ||
); | ||
this.promise = mapPromise(data => { | ||
this.data = data; | ||
return this.calculate(); | ||
}); | ||
if (this.watch) { | ||
mapPromise.on( | ||
"change", | ||
function () { | ||
var value = this.calculate(); | ||
if (value !== this.promise.value) { | ||
this.promise.value = value; | ||
this.promise.emit("change", value, this.path); | ||
} | ||
}.bind(this) | ||
); | ||
mapPromise.on("change", () => { | ||
const value = this.calculate(); | ||
if (value !== this.promise.value) { | ||
this.promise.value = value; | ||
this.promise.emit("change", value, this.path); | ||
} | ||
}); | ||
this.promise.close = this.close.bind(this); | ||
@@ -155,5 +144,5 @@ } | ||
}, | ||
close: function () { this.mapPromise.close(); }, | ||
calculate: function () { | ||
var current, result = false; | ||
close() { this.mapPromise.close(); }, | ||
calculate() { | ||
let current, result = false; | ||
@@ -167,11 +156,13 @@ if (!this.data.paths) return false; | ||
if (index) { | ||
var dirIgnored = this.data.paths.slice(0, index).some(function (preRulesPath) { | ||
return this.data.data[preRulesPath].some(function (rules) { | ||
return applyRules(rules, preRulesPath, rulesPath).value; | ||
const dirIgnored = this.data.paths | ||
.slice(0, index) | ||
.some(function (preRulesPath) { | ||
return this.data.data[preRulesPath].some( | ||
rules => applyRules(rules, preRulesPath, rulesPath).value, this | ||
); | ||
}, this); | ||
}, this); | ||
if (dirIgnored) return false; | ||
} | ||
this.data.data[rulesPath].forEach(function (rules) { | ||
var data = applyRules(rules, rulesPath, current); | ||
let data = applyRules(rules, rulesPath, current); | ||
if (data.value === false && current !== pathUtils) { | ||
@@ -191,4 +182,4 @@ data = applyRules(rules, rulesPath, this.path); | ||
isIgnored = function (mode, path, options) { | ||
var watch, globalRules, lIsIgnored, getMapFns, lDirname, promise; | ||
const isIgnored = function (mode, path, options) { | ||
let globalRules, getMapFns, promise; | ||
@@ -201,2 +192,6 @@ if (isValue(options.globalRules)) { | ||
const { watch } = options; | ||
const lIsIgnored = new IsIgnored(path, watch); | ||
const lDirname = lIsIgnored.dirname; | ||
if (mode) { | ||
@@ -206,15 +201,12 @@ getMapFns = []; | ||
if (!isArray(mode)) { | ||
if (!modes[mode]) throw new Error("Unknown mode '" + mode + "'"); | ||
if (!modes[mode]) throw new Error(`Unknown mode '${ mode }'`); | ||
mode = [mode]; | ||
} | ||
mode.forEach(function (name) { | ||
var lMode = modes[name]; | ||
if (!lMode) throw new Error("Unknown mode '" + name + "'"); | ||
getMapFns.push(function (pathIgnored) { | ||
return getMap(lDirname, lMode, watch, parseSrc); | ||
}); | ||
mode.forEach(name => { | ||
const lMode = modes[name]; | ||
if (!lMode) throw new Error(`Unknown mode '${ name }'`); | ||
getMapFns.push(pathIgnored => getMap(lDirname, lMode, watch, parseSrc)); | ||
if (lMode.globalRules) push.apply(globalRules, lMode.globalRules); | ||
}); | ||
} | ||
watch = options.watch; | ||
@@ -236,4 +228,2 @@ if (globalRules) { | ||
lIsIgnored = new IsIgnored(path, watch); | ||
lDirname = lIsIgnored.dirname; | ||
return lIsIgnored.init(buildMap(lDirname, getMapFns, watch)); | ||
@@ -243,8 +233,5 @@ }; | ||
module.exports = exports = function (mode, path) { | ||
var options, cb; | ||
module.exports = exports = function (mode, path, options = {}, cb = null) { | ||
path = resolve(String(path)); | ||
options = Object(arguments[2]); | ||
cb = arguments[3]; | ||
if (!cb) { | ||
@@ -265,28 +252,26 @@ if (isCallable(options)) { | ||
exports.getIsIgnored = function (modeNames, globalRules, watch) { | ||
var memo, mapGetters = [], build; | ||
const mapGetters = []; | ||
if (!globalRules) globalRules = []; | ||
memo = watch ? memoizeWatcher : memoize; | ||
modeNames.forEach(function (name) { | ||
var mode = modes[name], isRoot, readRules; | ||
if (!mode) throw new Error("Unknown mode '" + name + "'"); | ||
isRoot = memo(mode[watch ? "isRootWatcher" : "isRoot"], { primitive: true }); | ||
readRules = memo(getMap[watch ? "readRulesWatcher" : "readRules"], { primitive: true }); | ||
mapGetters.push(function (path) { | ||
var map; | ||
map = new ConfMap(path, watch); | ||
const memo = watch ? memoizeWatcher : memoize; | ||
modeNames.forEach(name => { | ||
const mode = modes[name]; | ||
if (!mode) throw new Error(`Unknown mode '${ name }'`); | ||
const isRoot = memo(mode[watch ? "isRootWatcher" : "isRoot"], { primitive: true }); | ||
const readRules = memo(getMap[watch ? "readRulesWatcher" : "readRules"], { | ||
primitive: true | ||
}); | ||
mapGetters.push(path => { | ||
const map = new ConfMap(path, watch); | ||
map.filename = mode.filename; | ||
map.readRules = readRules; | ||
map.parse = parseSrc; | ||
return map.init(findRoot(isRoot, path, { watch: watch })); | ||
return map.init(findRoot(isRoot, path, { watch })); | ||
}); | ||
if (mode.globalRules) push.apply(globalRules, mode.globalRules); | ||
}); | ||
build = memo(function (lDirname) { return buildMap(lDirname, mapGetters, watch); }, { | ||
primitive: true | ||
}); | ||
const build = memo(lDirname => buildMap(lDirname, mapGetters, watch), { primitive: true }); | ||
return { | ||
isIgnored: function (path) { | ||
var lIsIgnored; | ||
lIsIgnored = new IsIgnored(path, watch); | ||
isIgnored(path) { | ||
const lIsIgnored = new IsIgnored(path, watch); | ||
return lIsIgnored.init(build(lIsIgnored.dirname)); | ||
@@ -293,0 +278,0 @@ }, |
"use strict"; | ||
var isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, dirname = require("path").dirname | ||
, resolve = require("path").resolve | ||
, readlink = require("./readlink").readlink | ||
, realpath = require("./realpath").realpath; | ||
const isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, { dirname } = require("path") | ||
, { resolve } = require("path") | ||
, { readlink } = require("./readlink") | ||
, { realpath } = require("./realpath"); | ||
module.exports = function (path/*[, options[, callback]]*/) { | ||
module.exports = function (path, options = {}, cb = null) { | ||
path = resolve(String(path)); | ||
var options = arguments[1], cb = arguments[2]; | ||
if (!isValue(cb) && isCallable(options)) { | ||
@@ -19,9 +18,11 @@ cb = options; | ||
} | ||
var expectedLinkPath = options.linkPath ? resolve(String(options.linkPath)) : null; | ||
const expectedLinkPath = options.linkPath | ||
? resolve(dirname(path), String(options.linkPath)) | ||
: null; | ||
return readlink(path, {})( | ||
function (linkPath) { | ||
linkPath => { | ||
if (!linkPath) return null; | ||
if (!expectedLinkPath) return true; | ||
if (!options.recursive) return resolve(dirname(path), linkPath) === expectedLinkPath; | ||
return realpath(path, { loose: true })(function (finalLinkPath) { | ||
return realpath(path, { loose: true })(finalLinkPath => { | ||
if (!finalLinkPath) return false; | ||
@@ -31,3 +32,3 @@ return finalLinkPath === expectedLinkPath; | ||
}, | ||
function (error) { | ||
error => { | ||
if (error.code === "ENOENT") return null; | ||
@@ -34,0 +35,0 @@ if (error.code === "EINVAL") return false; |
{ | ||
"name": "fs2", | ||
"version": "0.3.9", | ||
"version": "0.4.0", | ||
"description": "fs (file system package) extensions", | ||
@@ -19,6 +19,3 @@ "author": "Mariusz Nowak <medyk@medikoo.com> (http://www.medikoo.com/)", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/medikoo/fs2.git" | ||
}, | ||
"repository": "medikoo/fs2", | ||
"dependencies": { | ||
@@ -29,2 +26,3 @@ "d": "^1.0.1", | ||
"event-emitter": "^0.3.5", | ||
"ext": "^1.6.0", | ||
"ignore": "^5.1.8", | ||
@@ -35,5 +33,6 @@ "memoizee": "^0.4.14", | ||
"devDependencies": { | ||
"eslint": "^7.13.0", | ||
"eslint-config-medikoo": "^3.1.0", | ||
"eslint": "^8.2.0", | ||
"eslint-config-medikoo": "^4.1.1", | ||
"git-list-updated": "^1.2.1", | ||
"github-release-from-cc-changelog": "^2.2.0", | ||
"husky": "^4.3.0", | ||
@@ -68,2 +67,11 @@ "lint-staged": "^10.5.1", | ||
} | ||
}, | ||
{ | ||
"files": [ | ||
"empty-dir-sync.js", | ||
"rmdir-sync.js" | ||
], | ||
"rules": { | ||
"no-use-before-define": "off" | ||
} | ||
} | ||
@@ -78,3 +86,4 @@ ] | ||
"files": [ | ||
"*.md" | ||
"*.md", | ||
"*.yml" | ||
], | ||
@@ -89,6 +98,8 @@ "options": { | ||
"lint": "eslint --ignore-path=.gitignore .", | ||
"lint-updated": "pipe-git-updated --ext=js -- eslint --ignore-pattern '!*'", | ||
"prettier-check-updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c", | ||
"prettify": "prettier --write --ignore-path .gitignore '**/*.{css,html,js,json,md,yaml,yml}'", | ||
"test": "node ./node_modules/tad/bin/tad" | ||
"lint:updated": "pipe-git-updated --base=main --ext=js -- eslint --ignore-pattern '!*'", | ||
"prettier-check": "prettier -c --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"", | ||
"prettier-check:updated": "pipe-git-updated --base=main --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c", | ||
"prettify": "prettier --write --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"", | ||
"prettify:updated": "pipe-git-updated ---base=main -ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier --write", | ||
"test": "tad" | ||
}, | ||
@@ -95,0 +106,0 @@ "engines": { |
@@ -36,2 +36,3 @@ # fs2 | ||
- **loose** - Do not error if source file doesn't exits, abort and resolve with `null` instead. | ||
- **intermediate** - Whether to create directories recursively (if parent is not created), | ||
@@ -61,2 +62,10 @@ ### copyDir(src, dest[, options[, cb]]) _(fs2/copy-dir)_ | ||
### emptyDirSync(path[, options]) _(fs2/empty-dir-sync)_ | ||
Empty directory synchronously | ||
Supported options: | ||
- **recursive** - Attempt to empty directory content recursively (if not set, command will succeed only on removing top level directory files) | ||
### hasAccess(path[, options[, cb]]) _(fs2/has-access)_ | ||
@@ -76,2 +85,7 @@ | ||
### isFile(path[, cb]) _(fs2/is-file)_ | ||
Whether path points to existing file | ||
Resolves with `true` if provided path points to a file, `false` if provided path points to existing non-directory file, `null` if there's no file at path. Returns promise. | ||
### isIgnored(mode, path[, options[, cb]]) _(fs2/is-ignored)_ | ||
@@ -160,2 +174,6 @@ | ||
Supported options: | ||
- **intermediate** - Whether to create directories recursively (if parent is not created) | ||
### rmdir(path[, options[, cb]]) _(fs2/rmdir)_ | ||
@@ -169,2 +187,3 @@ | ||
- **force** - Attempt to remove other files within directory as well. | ||
- **loose** - Do not error if directory doesn't exist | ||
@@ -185,4 +204,10 @@ ### rmdirSync(path[, options]) _(fs2/rmdir-sync)_ | ||
Same as [fs.symlink](http://nodejs.org/api/all.html#all_fs_symlink_srcpath_dstpath_type_callback). Returns promise. | ||
Same as [fs.symlink](http://nodejs.org/api/all.html#all_fs_symlink_srcpath_dstpath_type_callback). Returns promise that resolves with `true` if symlink was created (`false` can be returned with `loose` option, when symlink already exists) | ||
Supported options: | ||
- **intermediate** - Attempt to create directory with subdirectories in which symlink is expected to be placed if they do not exist | ||
- **loose** - Do not error if same symlink at path already exists | ||
- **force** - If there's other file at the path, remove and retry to create symlink | ||
### typeByStats(stats) _(fs2/type-by-stats)_ | ||
@@ -196,2 +221,14 @@ | ||
Supported options: | ||
- **loose** - Do not error if file doesn't exist | ||
### unlinkSync(path) _(fs2/unlink-sync)_ | ||
Same as [fs.unlinkSync](http://nodejs.org/api/all.html#all_fs_unlink_path_callback), but with support for extra options. | ||
Supported options: | ||
- **loose** - Do not error if file doesn't exist | ||
### watchPath(path) _(fs2/watch-path)_ | ||
@@ -198,0 +235,0 @@ |
"use strict"; | ||
var isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, deferred = require("deferred") | ||
, path = require("path") | ||
, original = require("fs").rename | ||
, mkdir = require("./mkdir") | ||
, copy = require("./copy") | ||
, unlink = require("./unlink") | ||
, dirname = path.dirname | ||
, resolve = path.resolve; | ||
const isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, deferred = require("deferred") | ||
, path = require("path") | ||
, original = require("fs").rename | ||
, mkdir = require("./mkdir") | ||
, stat = require("./stat") | ||
, copy = require("./copy") | ||
, copyDir = require("./copy-dir") | ||
, rmdir = require("./rmdir") | ||
, unlink = require("./unlink") | ||
, { dirname } = path | ||
, { resolve } = path; | ||
var crossDeviceRename = function (oldPath, newPath) { | ||
return copy(oldPath, newPath)(function () { return unlink(oldPath); }); | ||
const crossDeviceRename = function (oldPath, newPath) { | ||
return stat.then(stats => { | ||
if (stats.isDirectory()) { | ||
return copyDir(oldPath, newPath).then(() => | ||
rmdir(oldPath, { recursive: true, force: true }) | ||
); | ||
} | ||
return copy(oldPath, newPath).then(() => unlink(oldPath)); | ||
}); | ||
}; | ||
var rename = function (oldPath, newPath) { | ||
var def = deferred(); | ||
original(oldPath, newPath, function (err) { | ||
const rename = function (oldPath, newPath) { | ||
const def = deferred(); | ||
original(oldPath, newPath, err => { | ||
if (err) { | ||
@@ -35,4 +45,3 @@ if (err.code === "EXDEV") { | ||
module.exports = exports = function (oldPath, newPath/*, options, cb*/) { | ||
var options = Object(arguments[2]), cb = arguments[3]; | ||
module.exports = exports = function (oldPath, newPath, options = {}, cb = null) { | ||
if (!isValue(cb) && isCallable(options)) { | ||
@@ -45,5 +54,5 @@ cb = options; | ||
if (options.intermediate) { | ||
return mkdir(dirname(newPath), { intermediate: true })(function () { | ||
return rename(oldPath, newPath); | ||
}).cb(cb); | ||
return mkdir(dirname(newPath), { intermediate: true })(() => rename(oldPath, newPath)).cb( | ||
cb | ||
); | ||
} | ||
@@ -50,0 +59,0 @@ |
@@ -9,20 +9,2 @@ "use strict"; | ||
const removeDir = dirname => { | ||
let errors; | ||
for (let filename of fs.readdirSync(dirname)) { | ||
filename = path.resolve(dirname, filename); | ||
try { | ||
const stats = fs.statSync(filename); | ||
if (stats.isDirectory()) removeDir(filename); | ||
else fs.unlinkSync(filename); | ||
} catch (error) { | ||
if (!errors) errors = []; | ||
if (error.errors) errors.push(...error.errors); | ||
else errors.push(Object.assign(error, { _filename: filename })); | ||
} | ||
} | ||
if (errors) throw Object.assign(new Error("Could not remove dir"), { errors }); | ||
else fs.rmdirSync(dirname); | ||
}; | ||
module.exports = (dirnameInput, options = {}) => { | ||
@@ -36,3 +18,4 @@ const dirname = path.resolve(ensureString(dirnameInput)); | ||
try { | ||
removeDir(dirname); | ||
emptyDirSync(dirname, options); | ||
fs.rmdirSync(dirname); | ||
} catch (error) { | ||
@@ -49,1 +32,3 @@ if (!error.errors) throw error; | ||
}; | ||
const emptyDirSync = require("./empty-dir-sync"); |
73
rmdir.js
"use strict"; | ||
var partial = require("es5-ext/function/#/partial") | ||
, isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, deferred = require("deferred") | ||
, fs = require("fs") | ||
, pathUtils = require("path") | ||
, chmod = require("./chmod") | ||
, lstat = require("./lstat") | ||
, readdir = require("./readdir") | ||
, unlink = require("./unlink"); | ||
const partial = require("es5-ext/function/#/partial") | ||
, isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, deferred = require("deferred") | ||
, fs = require("fs") | ||
, pathUtils = require("path") | ||
, chmod = require("./chmod") | ||
, lstat = require("./lstat") | ||
, readdir = require("./readdir") | ||
, unlink = require("./unlink"); | ||
var original = fs.rmdir | ||
, resolve = pathUtils.resolve | ||
, sep = pathUtils.sep | ||
, rmdir | ||
, rmcontent | ||
, empty = {}; | ||
const original = fs.rmdir, { resolve } = pathUtils, { sep } = pathUtils, empty = {}; | ||
rmcontent = function (path, options) { | ||
const rmcontent = function (path, options) { | ||
return readdir(path)( | ||
function self(files, repeated) { | ||
return deferred.map(files, function (name) { | ||
var filename = path + sep + name, aborted; | ||
return deferred.map(files, name => { | ||
const filename = path + sep + name; | ||
let aborted; | ||
return lstat(filename)( | ||
function (stats) { | ||
var err; | ||
stats => { | ||
if (aborted || options.aborted) return null; | ||
if (stats.isDirectory()) { | ||
return rmcontent(filename, options)(null, function (err2) { | ||
if (options.recursive && stats.isDirectory()) { | ||
return rmcontent(filename, options)(null, err2 => { | ||
options.aborted = true; | ||
@@ -37,3 +32,3 @@ throw err2; | ||
if (options.force) { | ||
return unlink(filename)(null, function (err2) { | ||
return unlink(filename)(null, err2 => { | ||
if (err2.code === "ENOENT") return; | ||
@@ -44,3 +39,3 @@ aborted = true; | ||
} | ||
err = new Error("ENOTEMPTY rmdir '" + path + "'"); | ||
const err = new Error(`ENOTEMPTY rmdir '${ path }'`); | ||
err.errno = 53; | ||
@@ -52,3 +47,3 @@ err.code = "ENOTEMPTY"; | ||
}, | ||
function (err) { | ||
err => { | ||
if (err.code === "ENOENT") return; | ||
@@ -59,3 +54,3 @@ options.aborted = true; | ||
); | ||
})(null, function (err) { | ||
})(null, err => { | ||
if (!options.aborted && !repeated && err.code === "EACCES" && chmod) { | ||
@@ -67,3 +62,3 @@ return chmod(path, 146)(partial.call(self, files, true)); | ||
}, | ||
function (err) { | ||
err => { | ||
if (err.code === "ENOENT") return; | ||
@@ -73,5 +68,5 @@ throw err; | ||
)( | ||
function () { | ||
() => { | ||
if (options.aborted) return null; | ||
return rmdir(path, empty)(null, function (err) { | ||
return rmdir(path, empty)(null, err => { | ||
if (options.aborted) return null; | ||
@@ -86,3 +81,3 @@ if (err.code === "ENOTEMPTY") { | ||
}, | ||
function (err) { | ||
err => { | ||
if (err.code === "ENOENT") return; | ||
@@ -94,9 +89,9 @@ throw err; | ||
rmdir = function (path, options) { | ||
var def = deferred(); | ||
original(path, function (err) { | ||
const rmdir = function (path, options) { | ||
const def = deferred(); | ||
original(path, err => { | ||
if (err) { | ||
if (err.code === "ENOTEMPTY") { | ||
if (options.recursive) { | ||
def.resolve(rmcontent(path, { force: options.force })); | ||
if (options.recursive || options.force) { | ||
def.resolve(rmcontent(path, options)); | ||
return; | ||
@@ -119,8 +114,4 @@ } | ||
module.exports = exports = function (path/*, options, callback*/) { | ||
var options, cb; | ||
module.exports = exports = function (path, options = {}, cb = null) { | ||
path = resolve(String(path)); | ||
options = arguments[1]; | ||
cb = arguments[2]; | ||
@@ -127,0 +118,0 @@ if (!isValue(cb) && isCallable(options)) { |
"use strict"; | ||
var isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, isString = require("es5-ext/string/is-string") | ||
, deferred = require("deferred") | ||
, path = require("path") | ||
, original = require("fs").symlink | ||
, mkdir = require("./mkdir"); | ||
const isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, isString = require("es5-ext/string/is-string") | ||
, deferred = require("deferred") | ||
, path = require("path") | ||
, original = require("fs").symlink | ||
, mkdir = require("./mkdir") | ||
, isSymlink = require("./is-symlink") | ||
, unlink = require("./unlink"); | ||
var dirname = path.dirname, resolve = path.resolve, symlink; | ||
symlink = function (src, dest, options) { | ||
var def = deferred(); | ||
original(src, dest, options.type, function (err) { | ||
const symlink = function (src, dest, options) { | ||
const def = deferred(); | ||
original(src, dest, options.type, err => { | ||
if (err) { | ||
@@ -20,15 +20,21 @@ def.reject(err); | ||
} | ||
def.resolve(); | ||
def.resolve(true); | ||
}); | ||
return def.promise; | ||
return def.promise.catch(error => { | ||
if (!options.loose && !options.force) throw error; | ||
if (error.code === "EEXIST") { | ||
return isSymlink(dest, { linkPath: src })(result => { | ||
if (result) return Promise.resolve(false); | ||
if (!options.force) throw error; | ||
return unlink(dest).then(() => symlink(src, dest, options), () => { throw error; }); | ||
}); | ||
} | ||
throw error; | ||
}); | ||
}; | ||
symlink.returnsPromise = true; | ||
module.exports = exports = function (src, dest /* [, options[, callback]]*/) { | ||
var options, cb; | ||
module.exports = exports = function (src, dest, options = {}, cb = null) { | ||
src = String(src); | ||
dest = resolve(String(dest)); | ||
options = arguments[2]; | ||
cb = arguments[3]; | ||
dest = path.resolve(String(dest)); | ||
if (!isValue(cb) && isCallable(options)) { | ||
@@ -42,5 +48,5 @@ cb = options; | ||
if (options.intermediate) { | ||
return mkdir(dirname(dest), { intermediate: true })(function () { | ||
return symlink(src, dest, options); | ||
}).cb(cb); | ||
return mkdir(path.dirname(dest), { intermediate: true })(() => | ||
symlink(src, dest, options) | ||
).cb(cb); | ||
} | ||
@@ -47,0 +53,0 @@ |
"use strict"; | ||
var isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, deferred = require("deferred") | ||
, resolve = require("path").resolve | ||
, original = require("fs").unlink | ||
, unlink; | ||
const isCallable = require("es5-ext/object/is-callable") | ||
, isValue = require("es5-ext/object/is-value") | ||
, deferred = require("deferred") | ||
, { resolve } = require("path") | ||
, original = require("fs").unlink; | ||
unlink = function (path, options) { | ||
var def = deferred(); | ||
original(path, function (err) { | ||
const unlink = function (path, options) { | ||
const def = deferred(); | ||
original(path, err => { | ||
if (err) { | ||
@@ -24,8 +23,4 @@ if (err.code === "ENOENT" && options.loose) def.resolve(); | ||
module.exports = exports = function (path /* [, options[, callback]]*/) { | ||
var options, cb; | ||
module.exports = exports = function (path, options = {}, cb = null) { | ||
path = resolve(String(path)); | ||
options = arguments[1]; | ||
cb = arguments[2]; | ||
if (!isValue(cb) && isCallable(options)) { | ||
@@ -32,0 +27,0 @@ cb = options; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48
3079
249
103364
8
8
1
25
+ Addedext@^1.6.0