2021-02-26 12:46:48 +01:00
|
|
|
const chalk = require("chalk")
|
2021-02-26 14:30:24 +01:00
|
|
|
const fs = require("fs")
|
|
|
|
const axios = require("axios")
|
2021-02-26 14:33:31 +01:00
|
|
|
const path = require("path")
|
2022-06-30 20:20:50 +02:00
|
|
|
const progress = require("cli-progress")
|
2022-09-13 19:22:15 +02:00
|
|
|
const { join } = require("path")
|
2021-02-26 14:30:24 +01:00
|
|
|
|
2021-02-26 14:33:31 +01:00
|
|
|
exports.downloadFile = async (url, filePath) => {
|
|
|
|
filePath = path.resolve(filePath)
|
|
|
|
const writer = fs.createWriteStream(filePath)
|
2021-02-26 14:30:24 +01:00
|
|
|
|
|
|
|
const response = await axios({
|
|
|
|
url,
|
|
|
|
method: "GET",
|
2021-02-26 18:09:20 +01:00
|
|
|
responseType: "stream",
|
2021-02-26 14:30:24 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
response.data.pipe(writer)
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
writer.on("finish", resolve)
|
|
|
|
writer.on("error", reject)
|
|
|
|
})
|
|
|
|
}
|
2021-02-26 12:46:48 +01:00
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.getHelpDescription = string => {
|
2021-02-26 12:46:48 +01:00
|
|
|
return chalk.cyan(string)
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.getSubHelpDescription = string => {
|
2021-02-26 12:46:48 +01:00
|
|
|
return chalk.green(string)
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.error = error => {
|
2021-02-26 12:46:48 +01:00
|
|
|
return chalk.red(`Error - ${error}`)
|
|
|
|
}
|
2021-02-26 14:48:11 +01:00
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.success = success => {
|
2021-02-26 14:48:11 +01:00
|
|
|
return chalk.green(success)
|
|
|
|
}
|
2021-02-26 16:09:25 +01:00
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.info = info => {
|
2021-02-26 18:08:28 +01:00
|
|
|
return chalk.cyan(info)
|
|
|
|
}
|
|
|
|
|
2021-02-26 16:09:25 +01:00
|
|
|
exports.logErrorToFile = (file, error) => {
|
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
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.parseEnv = env => {
|
2021-03-19 11:29:43 +01:00
|
|
|
const lines = env.toString().split("\n")
|
|
|
|
let result = {}
|
|
|
|
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
|
|
|
|
|
|
|
exports.progressBar = total => {
|
|
|
|
const bar = new progress.SingleBar({}, progress.Presets.shades_classic)
|
|
|
|
bar.start(total, 0)
|
|
|
|
return bar
|
|
|
|
}
|
2022-07-02 00:04:34 +02:00
|
|
|
|
|
|
|
exports.checkSlashesInUrl = url => {
|
|
|
|
return url.replace(/(https?:\/\/)|(\/)+/g, "$1$2")
|
|
|
|
}
|
2022-09-13 19:22:15 +02:00
|
|
|
|
|
|
|
exports.moveDirectory = (oldPath, newPath) => {
|
|
|
|
const files = fs.readdirSync(oldPath)
|
|
|
|
// check any file exists already
|
|
|
|
for (let file of files) {
|
|
|
|
if (fs.existsSync(file)) {
|
|
|
|
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)
|
|
|
|
}
|