2023-03-02 19:21:45 +01:00
|
|
|
import chalk from "chalk"
|
|
|
|
import fs from "fs"
|
2023-11-20 16:15:43 +01:00
|
|
|
import path, { join } from "path"
|
2023-03-02 19:21:45 +01:00
|
|
|
import fetch from "node-fetch"
|
2023-11-20 21:52:29 +01:00
|
|
|
|
2022-06-30 20:20:50 +02:00
|
|
|
const progress = require("cli-progress")
|
2021-02-26 14:30:24 +01:00
|
|
|
|
2023-03-03 11:03:33 +01:00
|
|
|
export function downloadFile(url: string, filePath: string) {
|
2021-02-26 14:30:24 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2023-03-03 11:03:33 +01:00
|
|
|
filePath = path.resolve(filePath)
|
|
|
|
fetch(url, {
|
|
|
|
method: "GET",
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
const writer = fs.createWriteStream(filePath)
|
|
|
|
if (response.body) {
|
|
|
|
response.body.pipe(writer)
|
|
|
|
response.body.on("end", resolve)
|
|
|
|
response.body.on("error", reject)
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`Unable to retrieve docker-compose file - ${response.status}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
throw err
|
|
|
|
})
|
2021-02-26 14:30:24 +01:00
|
|
|
})
|
|
|
|
}
|
2021-02-26 12:46:48 +01:00
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export async function httpCall(url: string, method: string) {
|
|
|
|
const response = await fetch(url, {
|
2022-10-27 10:48:37 +02:00
|
|
|
method,
|
|
|
|
})
|
2023-03-02 19:21:45 +01:00
|
|
|
return response.body
|
2022-10-27 10:48:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function getHelpDescription(str: string) {
|
|
|
|
return chalk.cyan(str)
|
2021-02-26 12:46:48 +01:00
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function getSubHelpDescription(str: string) {
|
|
|
|
return chalk.green(str)
|
2021-02-26 12:46:48 +01:00
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function error(err: string | number) {
|
|
|
|
process.exitCode = -1
|
|
|
|
return chalk.red(`Error - ${err}`)
|
2021-02-26 12:46:48 +01:00
|
|
|
}
|
2021-02-26 14:48:11 +01:00
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function success(str: string) {
|
|
|
|
return chalk.green(str)
|
2021-02-26 14:48:11 +01:00
|
|
|
}
|
2021-02-26 16:09:25 +01:00
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function info(str: string) {
|
|
|
|
return chalk.cyan(str)
|
2021-02-26 18:08:28 +01:00
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function logErrorToFile(file: string, error: string) {
|
2021-03-29 18:40:17 +02:00
|
|
|
fs.writeFileSync(path.resolve(`./${file}`), `Budibase Error\n${error}`)
|
2021-02-26 16:09:25 +01:00
|
|
|
}
|
2021-03-19 11:29:43 +01:00
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function parseEnv(env: string) {
|
2021-03-19 11:29:43 +01:00
|
|
|
const lines = env.toString().split("\n")
|
2023-03-02 19:21:45 +01:00
|
|
|
let result: Record<string, string> = {}
|
2021-03-19 11:29:43 +01:00
|
|
|
for (const line of lines) {
|
|
|
|
const match = line.match(/^([^=:#]+?)[=:](.*)/)
|
|
|
|
if (match) {
|
|
|
|
result[match[1].trim()] = match[2].trim()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2022-06-30 20:20:50 +02:00
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function progressBar(total: number) {
|
2022-06-30 20:20:50 +02:00
|
|
|
const bar = new progress.SingleBar({}, progress.Presets.shades_classic)
|
|
|
|
bar.start(total, 0)
|
|
|
|
return bar
|
|
|
|
}
|
2022-07-02 00:04:34 +02:00
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function checkSlashesInUrl(url: string) {
|
2022-07-02 00:04:34 +02:00
|
|
|
return url.replace(/(https?:\/\/)|(\/)+/g, "$1$2")
|
|
|
|
}
|
2022-09-13 19:22:15 +02:00
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function moveDirectory(oldPath: string, newPath: string) {
|
2022-09-13 19:22:15 +02:00
|
|
|
const files = fs.readdirSync(oldPath)
|
|
|
|
// check any file exists already
|
|
|
|
for (let file of files) {
|
2022-09-14 11:58:01 +02:00
|
|
|
if (fs.existsSync(join(newPath, file))) {
|
2022-09-13 19:22:15 +02:00
|
|
|
throw new Error(
|
|
|
|
"Unable to remove top level directory - some skeleton files already exist."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let file of files) {
|
|
|
|
fs.renameSync(join(oldPath, file), join(newPath, file))
|
|
|
|
}
|
|
|
|
fs.rmdirSync(oldPath)
|
|
|
|
}
|
2022-09-27 20:39:17 +02:00
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function capitaliseFirstLetter(str: string) {
|
2022-09-27 20:39:17 +02:00
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
|
|
}
|
2022-09-28 01:16:04 +02:00
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export function stringifyToDotEnv(json: Record<string, string | number>) {
|
2022-09-28 01:16:04 +02:00
|
|
|
let str = ""
|
|
|
|
for (let [key, value] of Object.entries(json)) {
|
|
|
|
str += `${key}=${value}\n`
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|