|
|
|
@ -3,8 +3,11 @@ import { spawnSync, SpawnSyncOptions } from 'child_process'
|
|
|
|
|
import { randomBytes } from 'crypto'
|
|
|
|
|
import { createWriteStream } from 'fs'
|
|
|
|
|
|
|
|
|
|
export const exec = (command: string, args: string[], { env, ...rest }: SpawnSyncOptions = {}) => {
|
|
|
|
|
|
|
|
|
|
export const exec = (
|
|
|
|
|
command: string,
|
|
|
|
|
args: string[],
|
|
|
|
|
{ env, ...rest }: SpawnSyncOptions = {}
|
|
|
|
|
) => {
|
|
|
|
|
const cmd = spawnSync(command, args, {
|
|
|
|
|
...rest,
|
|
|
|
|
env: {
|
|
|
|
@ -19,9 +22,11 @@ export const exec = (command: string, args: string[], { env, ...rest }: SpawnSyn
|
|
|
|
|
return { out, err }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const checkIfResticIsAvailable = () => checkIfCommandIsAvailable(
|
|
|
|
|
export const checkIfResticIsAvailable = () =>
|
|
|
|
|
checkIfCommandIsAvailable(
|
|
|
|
|
'restic',
|
|
|
|
|
'Restic is not installed'.red + ' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases',
|
|
|
|
|
'Restic is not installed'.red +
|
|
|
|
|
' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
|
|
|
|
@ -31,22 +36,29 @@ export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
|
|
|
|
|
|
|
|
|
|
export const makeObjectKeysLowercase = (object: Object): any =>
|
|
|
|
|
Object.fromEntries(
|
|
|
|
|
Object.entries(object)
|
|
|
|
|
.map(([key, value]) => [key.toLowerCase(), value]),
|
|
|
|
|
Object.entries(object).map(([key, value]) => [key.toLowerCase(), value])
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function rand(length = 32): string {
|
|
|
|
|
return randomBytes(length / 2).toString('hex')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const singleToArray = <T>(singleOrArray: T | T[]): T[] => Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray]
|
|
|
|
|
export const singleToArray = <T>(singleOrArray: T | T[]): T[] =>
|
|
|
|
|
Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray]
|
|
|
|
|
|
|
|
|
|
export const filterObject = <T>(obj: { [key: string]: T }, filter: (item: [string, T]) => boolean): { [key: string]: T } => Object.fromEntries(Object.entries(obj).filter(filter))
|
|
|
|
|
export const filterObject = <T>(
|
|
|
|
|
obj: { [key: string]: T },
|
|
|
|
|
filter: (item: [string, T]) => boolean
|
|
|
|
|
): { [key: string]: T } =>
|
|
|
|
|
Object.fromEntries(Object.entries(obj).filter(filter))
|
|
|
|
|
|
|
|
|
|
export const filterObjectByKey = <T>(obj: { [key: string]: T }, keys: string[]) => filterObject(obj, ([key]) => keys.includes(key))
|
|
|
|
|
export const filterObjectByKey = <T>(
|
|
|
|
|
obj: { [key: string]: T },
|
|
|
|
|
keys: string[]
|
|
|
|
|
) => filterObject(obj, ([key]) => keys.includes(key))
|
|
|
|
|
|
|
|
|
|
export const downloadFile = async (url: string, to: string) => new Promise<void>(async res => {
|
|
|
|
|
export const downloadFile = async (url: string, to: string) =>
|
|
|
|
|
new Promise<void>(async res => {
|
|
|
|
|
const { data: file } = await axios({
|
|
|
|
|
method: 'get',
|
|
|
|
|
url: url,
|
|
|
|
@ -61,3 +73,5 @@ export const downloadFile = async (url: string, to: string) => new Promise<void>
|
|
|
|
|
res()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const ConfigError = new Error('Config file not found')
|
|
|
|
|