chore(backups-cli): convert to ESM

This commit is contained in:
Julien Fontanet
2022-11-17 10:25:08 +01:00
parent f73b1d8b40
commit b5988bb8b7
8 changed files with 50 additions and 57 deletions

View File

@@ -1,11 +1,10 @@
'use strict'
import { readFileSync } from 'fs'
import getopts from 'getopts'
const getopts = require('getopts')
const { version } = JSON.parse(readFileSync(new URL('package.json', import.meta.url)))
const { version } = require('./package.json')
module.exports = commands =>
async function (args, prefix) {
export function composeCommands(commands) {
return async function (args, prefix) {
const opts = getopts(args, {
alias: {
help: 'h',
@@ -30,5 +29,6 @@ xo-backups v${version}
return
}
return command.main(args.slice(1), prefix + ' ' + commandName)
return (await command.default)(args.slice(1), prefix + ' ' + commandName)
}
}

View File

@@ -1,11 +1,9 @@
'use strict'
import fs from 'fs/promises'
import { dirname } from 'path'
const { dirname } = require('path')
export * from 'fs/promises'
const fs = require('promise-toolbox/promisifyAll')(require('fs'))
module.exports = fs
fs.getSize = path =>
export const getSize = path =>
fs.stat(path).then(
_ => _.size,
error => {
@@ -16,7 +14,7 @@ fs.getSize = path =>
}
)
fs.mktree = async function mkdirp(path) {
export async function mktree(path) {
try {
await fs.mkdir(path)
} catch (error) {
@@ -26,8 +24,8 @@ fs.mktree = async function mkdirp(path) {
return
}
if (code === 'ENOENT') {
await mkdirp(dirname(path))
return mkdirp(path)
await mktree(dirname(path))
return mktree(path)
}
throw error
}
@@ -37,7 +35,7 @@ fs.mktree = async function mkdirp(path) {
// - 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 =>
export const readdir2 = path =>
fs.readdir(path).then(
entries => {
entries.forEach((entry, i) => {
@@ -59,7 +57,7 @@ fs.readdir2 = path =>
}
)
fs.symlink2 = async (target, path) => {
export async function symlink2(target, path) {
try {
await fs.symlink(target, path)
} catch (error) {

View File

@@ -1,15 +1,15 @@
'use strict'
import { asyncMap as rawAsyncMap } from '@xen-orchestra/async-map'
import { RemoteAdapter } from '@xen-orchestra/backups/RemoteAdapter.js'
import { getHandler } from '@xen-orchestra/fs'
import getopts from 'getopts'
import curryRight from 'lodash/curryRight.js'
import { resolve } from 'path'
// -----------------------------------------------------------------------------
const asyncMap = curryRight(rawAsyncMap)
const asyncMap = require('lodash/curryRight')(require('@xen-orchestra/async-map').asyncMap)
const getopts = require('getopts')
const { RemoteAdapter } = require('@xen-orchestra/backups/RemoteAdapter')
const { resolve } = require('path')
const adapter = new RemoteAdapter(getHandler({ url: 'file://' }))
const adapter = new RemoteAdapter(require('@xen-orchestra/fs').getHandler({ url: 'file://' }))
module.exports = async function main(args) {
export default async function cleanVms(args) {
const { _, fix, remove, merge } = getopts(args, {
alias: {
fix: 'f',

View File

@@ -1,13 +1,10 @@
'use strict'
import { mktree, readdir2, readFile, symlink2 } from '../_fs.mjs'
import { asyncMap } from '@xen-orchestra/async-map'
import filenamify from 'filenamify'
import get from 'lodash/get.js'
import { dirname, join, relative } from 'path'
const filenamify = require('filenamify')
const get = require('lodash/get')
const { asyncMap } = require('@xen-orchestra/async-map')
const { dirname, join, relative } = require('path')
const { mktree, readdir2, readFile, symlink2 } = require('../_fs')
module.exports = async function createSymlinkIndex([backupDir, fieldPath]) {
export default async function createSymlinkIndex([backupDir, fieldPath]) {
const indexDir = join(backupDir, 'indexes', filenamify(fieldPath))
await mktree(indexDir)

View File

@@ -1,16 +1,13 @@
'use strict'
const groupBy = require('lodash/groupBy')
const { asyncMap } = require('@xen-orchestra/async-map')
const { createHash } = require('crypto')
const { dirname, resolve } = require('path')
const { readdir2, readFile, getSize } = require('../_fs')
import { readdir2, readFile, getSize } from '../_fs.mjs'
import { asyncMap } from '@xen-orchestra/async-map'
import { createHash } from 'crypto'
import groupBy from 'lodash/groupBy.js'
import { dirname, resolve } from 'path'
const sha512 = str => createHash('sha512').update(str).digest('hex')
const sum = values => values.reduce((a, b) => a + b)
module.exports = async function info(vmDirs) {
export default async function info(vmDirs) {
const jsonFiles = (
await asyncMap(vmDirs, async vmDir => (await readdir2(vmDir)).filter(_ => _.endsWith('.json')))
).flat()

View File

@@ -1,11 +1,12 @@
#!/usr/bin/env node
import { composeCommands } from './_composeCommands.mjs'
'use strict'
const importDefault = async path => (await import(path)).default
require('./_composeCommands')({
composeCommands({
'clean-vms': {
get main() {
return require('./commands/clean-vms')
get default() {
return importDefault('./commands/clean-vms.mjs')
},
usage: `[--fix] [--merge] [--remove] xo-vm-backups/*
@@ -18,14 +19,14 @@ require('./_composeCommands')({
`,
},
'create-symlink-index': {
get main() {
return require('./commands/create-symlink-index')
get default() {
return importDefault('./commands/create-symlink-index.mjs')
},
usage: 'xo-vm-backups <field path>',
},
info: {
get main() {
return require('./commands/info')
get default() {
return importDefault('./commands/info.mjs')
},
usage: 'xo-vm-backups/*',
},

View File

@@ -1,7 +1,7 @@
{
"private": false,
"bin": {
"xo-backups": "index.js"
"xo-backups": "index.mjs"
},
"preferGlobal": true,
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
@@ -11,11 +11,10 @@
"@xen-orchestra/fs": "^3.2.0",
"filenamify": "^4.1.0",
"getopts": "^2.2.5",
"lodash": "^4.17.15",
"promise-toolbox": "^0.21.0"
"lodash": "^4.17.15"
},
"engines": {
"node": ">=7.10.1"
"node": ">=14"
},
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@xen-orchestra/backups-cli",
"name": "@xen-orchestra/backups-cli",

View File

@@ -29,6 +29,7 @@
<!--packages-start-->
- @xen-orchestra/backups-cli major
- xo-web minor
<!--packages-end-->