``` > xo-backups create-symlink-indexes ./xo-vm-backups vm.name_label > ls ./xo-vm-backups/indexes/vm.name_label Debian\ 9.5\ 64bit\ web9 -> ../../c60dc26a-49e8-be58-6ae4-175cf03b19d5/ Prod\ VM -> ../../1498796a-3d23-d0cc-74be-b25d6e598c03/ ```
58 lines
1.2 KiB
JavaScript
58 lines
1.2 KiB
JavaScript
const { dirname } = require('path')
|
|
|
|
const fs = require('promise-toolbox/promisifyAll')(require('fs'))
|
|
module.exports = fs
|
|
|
|
fs.mktree = async function mkdirp(path) {
|
|
try {
|
|
await fs.mkdir(path)
|
|
} catch (error) {
|
|
const { code } = error
|
|
if (code === 'EEXIST') {
|
|
await fs.readdir(path)
|
|
return
|
|
}
|
|
if (code === 'ENOENT') {
|
|
await mkdirp(dirname(path))
|
|
return mkdirp(path)
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
// - easier:
|
|
// - single param for direct use in `Array#map`
|
|
// - files are prefixed with directory path
|
|
// - safer: returns empty array if path is missing or not a directory
|
|
fs.readdir2 = path =>
|
|
fs.readdir(path).then(
|
|
entries => {
|
|
entries.forEach((entry, i) => {
|
|
entries[i] = `${path}/${entry}`
|
|
})
|
|
|
|
return entries
|
|
},
|
|
error => {
|
|
if (
|
|
error != null &&
|
|
(error.code === 'ENOENT' || error.code === 'ENOTDIR')
|
|
) {
|
|
console.warn('WARN: readdir(%s)', path, error)
|
|
return []
|
|
}
|
|
throw error
|
|
}
|
|
)
|
|
|
|
fs.symlink2 = async (target, path) => {
|
|
try {
|
|
await fs.symlink(target, path)
|
|
} catch (error) {
|
|
if (error.code === 'EEXIST' && (await fs.readlink(path)) === target) {
|
|
return
|
|
}
|
|
throw error
|
|
}
|
|
}
|