-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
106 changed files
with
11,802 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright © 2020-2022 Michael Garvin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict' | ||
|
||
const { promisify } = require('util') | ||
|
||
const handler = { | ||
get: function (target, prop, receiver) { | ||
if (typeof target[prop] !== 'function') { | ||
return target[prop] | ||
} | ||
if (target[prop][promisify.custom]) { | ||
return function () { | ||
return Reflect.get(target, prop, receiver)[promisify.custom].apply(target, arguments) | ||
} | ||
} | ||
return function () { | ||
return new Promise((resolve, reject) => { | ||
Reflect.get(target, prop, receiver).apply(target, [...arguments, function (err, result) { | ||
if (err) { | ||
return reject(err) | ||
} | ||
resolve(result) | ||
}]) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
module.exports = function (thingToPromisify) { | ||
if (typeof thingToPromisify === 'function') { | ||
return promisify(thingToPromisify) | ||
} | ||
if (typeof thingToPromisify === 'object') { | ||
return new Proxy(thingToPromisify, handler) | ||
} | ||
throw new TypeError('Can only promisify functions or objects') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "@gar/promisify", | ||
"version": "1.1.3", | ||
"description": "Promisify an entire class or object", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wraithgar/gar-promisify.git" | ||
}, | ||
"scripts": { | ||
"lint": "standard", | ||
"lint:fix": "standard --fix", | ||
"test": "lab -a @hapi/code -t 100", | ||
"posttest": "npm run lint" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promisify", | ||
"all", | ||
"class", | ||
"object" | ||
], | ||
"author": "Gar <gar+npm@danger.computer>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@hapi/code": "^8.0.1", | ||
"@hapi/lab": "^24.1.0", | ||
"standard": "^16.0.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
MIT License | ||
|
||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com) | ||
Copyright (c) npm, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a | ||
copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
const { dirname, join, resolve, relative, isAbsolute } = require('path') | ||
const rimraf_ = require('rimraf') | ||
const { promisify } = require('util') | ||
const { | ||
access: access_, | ||
accessSync, | ||
copyFile: copyFile_, | ||
copyFileSync, | ||
readdir: readdir_, | ||
readdirSync, | ||
rename: rename_, | ||
renameSync, | ||
stat: stat_, | ||
statSync, | ||
lstat: lstat_, | ||
lstatSync, | ||
symlink: symlink_, | ||
symlinkSync, | ||
readlink: readlink_, | ||
readlinkSync, | ||
} = require('fs') | ||
|
||
const access = promisify(access_) | ||
const copyFile = promisify(copyFile_) | ||
const readdir = promisify(readdir_) | ||
const rename = promisify(rename_) | ||
const stat = promisify(stat_) | ||
const lstat = promisify(lstat_) | ||
const symlink = promisify(symlink_) | ||
const readlink = promisify(readlink_) | ||
const rimraf = promisify(rimraf_) | ||
const rimrafSync = rimraf_.sync | ||
|
||
const mkdirp = require('mkdirp') | ||
|
||
const pathExists = async path => { | ||
try { | ||
await access(path) | ||
return true | ||
} catch (er) { | ||
return er.code !== 'ENOENT' | ||
} | ||
} | ||
|
||
const pathExistsSync = path => { | ||
try { | ||
accessSync(path) | ||
return true | ||
} catch (er) { | ||
return er.code !== 'ENOENT' | ||
} | ||
} | ||
|
||
const moveFile = async (source, destination, options = {}, root = true, symlinks = []) => { | ||
if (!source || !destination) { | ||
throw new TypeError('`source` and `destination` file required') | ||
} | ||
|
||
options = { | ||
overwrite: true, | ||
...options, | ||
} | ||
|
||
if (!options.overwrite && await pathExists(destination)) { | ||
throw new Error(`The destination file exists: ${destination}`) | ||
} | ||
|
||
await mkdirp(dirname(destination)) | ||
|
||
try { | ||
await rename(source, destination) | ||
} catch (error) { | ||
if (error.code === 'EXDEV' || error.code === 'EPERM') { | ||
const sourceStat = await lstat(source) | ||
if (sourceStat.isDirectory()) { | ||
const files = await readdir(source) | ||
await Promise.all(files.map((file) => | ||
moveFile(join(source, file), join(destination, file), options, false, symlinks) | ||
)) | ||
} else if (sourceStat.isSymbolicLink()) { | ||
symlinks.push({ source, destination }) | ||
} else { | ||
await copyFile(source, destination) | ||
} | ||
} else { | ||
throw error | ||
} | ||
} | ||
|
||
if (root) { | ||
await Promise.all(symlinks.map(async ({ source: symSource, destination: symDestination }) => { | ||
let target = await readlink(symSource) | ||
// junction symlinks in windows will be absolute paths, so we need to | ||
// make sure they point to the symlink destination | ||
if (isAbsolute(target)) { | ||
target = resolve(symDestination, relative(symSource, target)) | ||
} | ||
// try to determine what the actual file is so we can create the correct | ||
// type of symlink in windows | ||
let targetStat = 'file' | ||
try { | ||
targetStat = await stat(resolve(dirname(symSource), target)) | ||
if (targetStat.isDirectory()) { | ||
targetStat = 'junction' | ||
} | ||
} catch { | ||
// targetStat remains 'file' | ||
} | ||
await symlink( | ||
target, | ||
symDestination, | ||
targetStat | ||
) | ||
})) | ||
await rimraf(source) | ||
} | ||
} | ||
|
||
const moveFileSync = (source, destination, options = {}, root = true, symlinks = []) => { | ||
if (!source || !destination) { | ||
throw new TypeError('`source` and `destination` file required') | ||
} | ||
|
||
options = { | ||
overwrite: true, | ||
...options, | ||
} | ||
|
||
if (!options.overwrite && pathExistsSync(destination)) { | ||
throw new Error(`The destination file exists: ${destination}`) | ||
} | ||
|
||
mkdirp.sync(dirname(destination)) | ||
|
||
try { | ||
renameSync(source, destination) | ||
} catch (error) { | ||
if (error.code === 'EXDEV' || error.code === 'EPERM') { | ||
const sourceStat = lstatSync(source) | ||
if (sourceStat.isDirectory()) { | ||
const files = readdirSync(source) | ||
for (const file of files) { | ||
moveFileSync(join(source, file), join(destination, file), options, false, symlinks) | ||
} | ||
} else if (sourceStat.isSymbolicLink()) { | ||
symlinks.push({ source, destination }) | ||
} else { | ||
copyFileSync(source, destination) | ||
} | ||
} else { | ||
throw error | ||
} | ||
} | ||
|
||
if (root) { | ||
for (const { source: symSource, destination: symDestination } of symlinks) { | ||
let target = readlinkSync(symSource) | ||
// junction symlinks in windows will be absolute paths, so we need to | ||
// make sure they point to the symlink destination | ||
if (isAbsolute(target)) { | ||
target = resolve(symDestination, relative(symSource, target)) | ||
} | ||
// try to determine what the actual file is so we can create the correct | ||
// type of symlink in windows | ||
let targetStat = 'file' | ||
try { | ||
targetStat = statSync(resolve(dirname(symSource), target)) | ||
if (targetStat.isDirectory()) { | ||
targetStat = 'junction' | ||
} | ||
} catch { | ||
// targetStat remains 'file' | ||
} | ||
symlinkSync( | ||
target, | ||
symDestination, | ||
targetStat | ||
) | ||
} | ||
rimrafSync(source) | ||
} | ||
} | ||
|
||
module.exports = moveFile | ||
module.exports.sync = moveFileSync |
Oops, something went wrong.