push the uni-demo project
This commit is contained in:
32
uni-demo/node_modules/@npmcli/fs/lib/mkdir/index.js
generated
vendored
Normal file
32
uni-demo/node_modules/@npmcli/fs/lib/mkdir/index.js
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
const fs = require('../fs.js')
|
||||
const getOptions = require('../common/get-options.js')
|
||||
const node = require('../common/node.js')
|
||||
const owner = require('../common/owner.js')
|
||||
|
||||
const polyfill = require('./polyfill.js')
|
||||
|
||||
// node 10.12.0 added the options parameter, which allows recursive and mode
|
||||
// properties to be passed
|
||||
const useNative = node.satisfies('>=10.12.0')
|
||||
|
||||
// extends mkdir with the ability to specify an owner of the new dir
|
||||
const mkdir = async (path, opts) => {
|
||||
const options = getOptions(opts, {
|
||||
copy: ['mode', 'recursive', 'owner'],
|
||||
wrap: 'mode',
|
||||
})
|
||||
const { uid, gid } = await owner.validate(path, options.owner)
|
||||
|
||||
// the polyfill is tested separately from this module, no need to hack
|
||||
// process.version to try to trigger it just for coverage
|
||||
// istanbul ignore next
|
||||
const result = useNative
|
||||
? await fs.mkdir(path, options)
|
||||
: await polyfill(path, options)
|
||||
|
||||
await owner.update(path, uid, gid)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
module.exports = mkdir
|
||||
81
uni-demo/node_modules/@npmcli/fs/lib/mkdir/polyfill.js
generated
vendored
Normal file
81
uni-demo/node_modules/@npmcli/fs/lib/mkdir/polyfill.js
generated
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
const { dirname } = require('path')
|
||||
|
||||
const fileURLToPath = require('../common/file-url-to-path/index.js')
|
||||
const fs = require('../fs.js')
|
||||
|
||||
const defaultOptions = {
|
||||
mode: 0o777,
|
||||
recursive: false,
|
||||
}
|
||||
|
||||
const mkdir = async (path, opts) => {
|
||||
const options = { ...defaultOptions, ...opts }
|
||||
|
||||
// if we're not in recursive mode, just call the real mkdir with the path and
|
||||
// the mode option only
|
||||
if (!options.recursive) {
|
||||
return fs.mkdir(path, options.mode)
|
||||
}
|
||||
|
||||
const makeDirectory = async (dir, mode) => {
|
||||
// we can't use dirname directly since these functions support URL
|
||||
// objects with the file: protocol as the path input, so first we get a
|
||||
// string path, then we can call dirname on that
|
||||
const parent = dir != null && dir.href && dir.origin
|
||||
? dirname(fileURLToPath(dir))
|
||||
: dirname(dir)
|
||||
|
||||
// if the parent is the dir itself, try to create it. anything but EISDIR
|
||||
// should be rethrown
|
||||
if (parent === dir) {
|
||||
try {
|
||||
await fs.mkdir(dir, opts)
|
||||
} catch (err) {
|
||||
if (err.code !== 'EISDIR') {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
try {
|
||||
await fs.mkdir(dir, mode)
|
||||
return dir
|
||||
} catch (err) {
|
||||
// ENOENT means the parent wasn't there, so create that
|
||||
if (err.code === 'ENOENT') {
|
||||
const made = await makeDirectory(parent, mode)
|
||||
await makeDirectory(dir, mode)
|
||||
// return the shallowest path we created, i.e. the result of creating
|
||||
// the parent
|
||||
return made
|
||||
}
|
||||
|
||||
// an EEXIST means there's already something there
|
||||
// an EROFS means we have a read-only filesystem and can't create a dir
|
||||
// any other error is fatal and we should give up now
|
||||
if (err.code !== 'EEXIST' && err.code !== 'EROFS') {
|
||||
throw err
|
||||
}
|
||||
|
||||
// stat the directory, if the result is a directory, then we successfully
|
||||
// created this one so return its path. otherwise, we reject with the
|
||||
// original error by ignoring the error in the catch
|
||||
try {
|
||||
const stat = await fs.stat(dir)
|
||||
if (stat.isDirectory()) {
|
||||
// if it already existed, we didn't create anything so return
|
||||
// undefined
|
||||
return undefined
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
// if the thing that's there isn't a directory, then just re-throw
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
return makeDirectory(path, options.mode)
|
||||
}
|
||||
|
||||
module.exports = mkdir
|
||||
Reference in New Issue
Block a user