diff --git a/src/utils.ts b/src/utils.ts index 59ff805..f9ca0fd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,7 +2,7 @@ import axios from 'axios' import { spawnSync, SpawnSyncOptions } from 'child_process' import { randomBytes } from 'crypto' import { createWriteStream } from 'fs' -import { isAbsolute, resolve, dirname } from 'path' +import { dirname, isAbsolute, resolve } from 'path' import { CONFIG_FILE } from './config' import { Location } from './types' @@ -27,6 +27,16 @@ export const exec = ( 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 = () => checkIfCommandIsAvailable( 'restic', @@ -50,9 +60,6 @@ export function rand(length = 32): string { } -export const singleToArray = (singleOrArray: T | T[]): T[] => - Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray] - export const filterObject = ( obj: { [key: string]: T }, filter: (item: [string, T]) => boolean, @@ -98,12 +105,11 @@ export const getFlagsFromLocation = (location: Location, command?: string): stri let flags: string[] = [] // Map the flags to an array for the exec function. - for (let [flag, values] of Object.entries(all)) { - if (!Array.isArray(values)) - values = [values] - - for (const value of values) + for (let [flag, values] of Object.entries(all)) + for (const value of makeArrayIfIsNot(values)) flags = [...flags, `--${String(flag)}`, String(value)] - } + return flags } + +export const makeArrayIfIsNot = (maybeArray: T | T[]): T[] => Array.isArray(maybeArray) ? maybeArray : [maybeArray]