upgrade packages, included lock file, fixed types

This commit is contained in:
cupcakearmy 2020-06-25 09:16:53 +02:00
parent 1a891fffbd
commit b332897713
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
4 changed files with 1627 additions and 95 deletions

2
.gitignore vendored
View File

@ -3,8 +3,6 @@
# Node
node_modules/
package-lock.json
yarn.lock
# Build & Runtime
bin

View File

@ -10,21 +10,21 @@
"docs:dev": "codedoc serve"
},
"devDependencies": {
"@codedoc/cli": "^0.1.4",
"@types/js-yaml": "^3.12.1",
"@types/minimist": "^1.2.0",
"@types/node": "^12.11.7",
"pkg": "^4.4.0",
"@codedoc/cli": "0.1.x",
"@types/js-yaml": "3.x.x",
"@types/minimist": "1.x.x",
"@types/node": "14.x.x",
"pkg": "4.4.x",
"ts-node-dev": "^1.0.0-pre.40",
"typescript": "^3.7"
"typescript": "3.9.x"
},
"dependencies": {
"axios": "^0.19.0",
"axios": "0.19.x",
"clitastic": "0.0.1",
"colors": "^1.3.3",
"cron-parser": "^2.13.0",
"js-yaml": "^3.13.1",
"minimist": "^1.2.0",
"uhrwerk": "^1.0.0"
"colors": "1.x.x",
"cron-parser": "2.x.x",
"js-yaml": "3.x.x",
"minimist": "1.x.x",
"uhrwerk": "1.x.x"
}
}

View File

@ -9,113 +9,97 @@ import { flags } from './autorestic'
import { Backend, Config } from './types'
import { makeArrayIfIsNot, makeObjectKeysLowercase, rand } from './utils'
export enum LocationFromPrefixes {
Filesystem,
DockerVolume
Filesystem,
DockerVolume,
}
export const normalizeAndCheckBackends = (config: Config) => {
config.backends = makeObjectKeysLowercase(config.backends)
config.backends = makeObjectKeysLowercase(config.backends)
for (const [name, { type, path, key, ...rest }] of Object.entries(
config.backends,
)) {
if (!type || !path)
throw new Error(
`The backend "${name}" is missing some required attributes`,
)
for (const [name, { type, path, key, ...rest }] of Object.entries(config.backends)) {
if (!type || !path) throw new Error(`The backend "${name}" is missing some required attributes`)
const tmp: any = {
type,
path,
key: key || rand(128),
}
for (const [key, value] of Object.entries(rest))
tmp[key.toUpperCase()] = value
const tmp: any = {
type,
path,
key: key || rand(128),
}
for (const [key, value] of Object.entries(rest)) tmp[key.toUpperCase()] = value
config.backends[name] = tmp as Backend
}
config.backends[name] = tmp as Backend
}
}
export const normalizeAndCheckLocations = (config: Config) => {
config.locations = makeObjectKeysLowercase(config.locations)
const backends = Object.keys(config.backends)
config.locations = makeObjectKeysLowercase(config.locations)
const backends = Object.keys(config.backends)
const checkDestination = (backend: string, location: string) => {
if (!backends.includes(backend))
throw new Error(`Cannot find the backend "${backend}" for "${location}"`)
}
const checkDestination = (backend: string, location: string) => {
if (!backends.includes(backend)) throw new Error(`Cannot find the backend "${backend}" for "${location}"`)
}
for (const [name, { from, to, cron, ...rest }] of Object.entries(config.locations)) {
if (!from)
throw new Error(`The location "${name.blue}" is missing the "${'from'.underline.red}" source folder. See https://git.io/Jf0xw`)
if (!to || (Array.isArray(to) && !to.length))
throw new Error(`The location "${name.blue}" has no backend "${'to'.underline.red}" to save the backups. See https://git.io/Jf0xw`)
for (const [name, { from, to, cron, ...rest }] of Object.entries(config.locations)) {
if (!from) throw new Error(`The location "${name.blue}" is missing the "${'from'.underline.red}" source folder. See https://git.io/Jf0xw`)
if (!to || (Array.isArray(to) && !to.length))
throw new Error(`The location "${name.blue}" has no backend "${'to'.underline.red}" to save the backups. See https://git.io/Jf0xw`)
for (const t of makeArrayIfIsNot(to))
checkDestination(t, name)
for (const t of makeArrayIfIsNot(to)) checkDestination(t, name)
if (cron) {
try {
CronParser.parseExpression(cron)
} catch {
throw new Error(`The location "${name.blue}" has an invalid ${'cron'.underline.red} entry. See https://git.io/Jf0xP`)
}
}
}
if (cron) {
try {
CronParser.parseExpression(cron)
} catch {
throw new Error(`The location "${name.blue}" has an invalid ${'cron'.underline.red} entry. See https://git.io/Jf0xP`)
}
}
}
}
const findConfigFile = (): string => {
const config = '.autorestic.yml'
const paths = [
resolve(flags.config || ''),
resolve('./' + config),
homedir() + '/' + config,
]
for (const path of paths) {
try {
const file = statSync(path)
if (file.isFile()) return path
} catch (e) {
}
}
throw new Error('Config file not found')
const config = '.autorestic.yml'
const paths = [resolve(flags.config || ''), resolve('./' + config), homedir() + '/' + config]
for (const path of paths) {
try {
const file = statSync(path)
if (file.isFile()) return path
} catch (e) {}
}
throw new Error('Config file not found')
}
export let CONFIG_FILE: string = ''
export const init = (): Config => {
const file = findConfigFile()
CONFIG_FILE = file
const file = findConfigFile()
CONFIG_FILE = file
const raw: Config = makeObjectKeysLowercase(
yaml.safeLoad(readFileSync(CONFIG_FILE).toString()),
)
const parsed = yaml.safeLoad(readFileSync(CONFIG_FILE).toString())
if (!parsed || typeof parsed === 'string') throw new Error('Could not parse the config file')
const raw: Config = makeObjectKeysLowercase(parsed)
const current = JSON.stringify(raw)
const current = JSON.stringify(raw)
normalizeAndCheckBackends(raw)
normalizeAndCheckLocations(raw)
normalizeAndCheckBackends(raw)
normalizeAndCheckLocations(raw)
const changed = JSON.stringify(raw) !== current
const changed = JSON.stringify(raw) !== current
if (changed) {
const OLD_CONFIG_FILE = CONFIG_FILE + '.old'
copyFileSync(CONFIG_FILE, OLD_CONFIG_FILE)
writeFileSync(CONFIG_FILE, yaml.safeDump(raw))
console.log(
'\n' +
'⚠️ MOVED OLD CONFIG FILE TO: ⚠️'.red.underline.bold +
'\n' +
OLD_CONFIG_FILE +
'\n' +
'What? Why? '.grey + 'https://git.io/Jf0xK'.underline.grey +
'\n'
)
}
if (changed) {
const OLD_CONFIG_FILE = CONFIG_FILE + '.old'
copyFileSync(CONFIG_FILE, OLD_CONFIG_FILE)
writeFileSync(CONFIG_FILE, yaml.safeDump(raw))
console.log(
'\n' +
'⚠️ MOVED OLD CONFIG FILE TO: ⚠️'.red.underline.bold +
'\n' +
OLD_CONFIG_FILE +
'\n' +
'What? Why? '.grey +
'https://git.io/Jf0xK'.underline.grey +
'\n'
)
}
return raw
return raw
}

1550
yarn.lock Normal file

File diff suppressed because it is too large Load Diff