function to convert a variable to an array if its not already

This commit is contained in:
cupcakearmy 2019-12-05 00:23:06 +01:00
parent f7a15c6d86
commit 504ad639ab

View File

@ -2,7 +2,7 @@ import axios from 'axios'
import { spawnSync, SpawnSyncOptions } from 'child_process' import { spawnSync, SpawnSyncOptions } from 'child_process'
import { randomBytes } from 'crypto' import { randomBytes } from 'crypto'
import { createWriteStream } from 'fs' import { createWriteStream } from 'fs'
import { isAbsolute, resolve, dirname } from 'path' import { dirname, isAbsolute, resolve } from 'path'
import { CONFIG_FILE } from './config' import { CONFIG_FILE } from './config'
import { Location } from './types' import { Location } from './types'
@ -27,6 +27,16 @@ export const exec = (
return { out, err } return { out, err }
} }
export const execPlain = (command: string, opt: SpawnSyncOptions = {}) => {
const split = command.split(' ')
if (split.length < 1) {
console.log(`The command ${command} is not valid`.red)
return
}
return exec(split[0], split.slice(1), opt)
}
export const checkIfResticIsAvailable = () => export const checkIfResticIsAvailable = () =>
checkIfCommandIsAvailable( checkIfCommandIsAvailable(
'restic', 'restic',
@ -50,9 +60,6 @@ export function rand(length = 32): string {
} }
export const singleToArray = <T>(singleOrArray: T | T[]): T[] =>
Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray]
export const filterObject = <T>( export const filterObject = <T>(
obj: { [key: string]: T }, obj: { [key: string]: T },
filter: (item: [string, T]) => boolean, filter: (item: [string, T]) => boolean,
@ -98,12 +105,11 @@ export const getFlagsFromLocation = (location: Location, command?: string): stri
let flags: string[] = [] let flags: string[] = []
// Map the flags to an array for the exec function. // Map the flags to an array for the exec function.
for (let [flag, values] of Object.entries(all)) { for (let [flag, values] of Object.entries(all))
if (!Array.isArray(values)) for (const value of makeArrayIfIsNot(values))
values = [values]
for (const value of values)
flags = [...flags, `--${String(flag)}`, String(value)] flags = [...flags, `--${String(flag)}`, String(value)]
}
return flags return flags
} }
export const makeArrayIfIsNot = <T>(maybeArray: T | T[]): T[] => Array.isArray(maybeArray) ? maybeArray : [maybeArray]