50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
|
const { resolve } = require("path")
|
||
|
const fs = require("fs")
|
||
|
const { error, success } = require("../utils")
|
||
|
const yaml = require("yaml")
|
||
|
const { getAppService } = require("./utils")
|
||
|
|
||
|
exports.watchPlugins = async pluginPath => {
|
||
|
const PLUGIN_PATH = "/plugins"
|
||
|
// get absolute path
|
||
|
pluginPath = resolve(pluginPath)
|
||
|
if (!fs.existsSync(pluginPath)) {
|
||
|
console.log(
|
||
|
error(
|
||
|
`The directory "${pluginPath}" does not exist, please create and then try again.`
|
||
|
)
|
||
|
)
|
||
|
return
|
||
|
}
|
||
|
const opts = ["docker-compose.yaml", "docker-compose.yml"]
|
||
|
let dockerFilePath = opts.find(name => fs.existsSync(name))
|
||
|
if (!dockerFilePath) {
|
||
|
console.log(error("Unable to locate docker-compose YAML."))
|
||
|
return
|
||
|
}
|
||
|
const dockerYaml = fs.readFileSync(dockerFilePath, "utf8")
|
||
|
const parsedYaml = yaml.parse(dockerYaml)
|
||
|
const service = getAppService(dockerFilePath)
|
||
|
if (!service) {
|
||
|
console.log(
|
||
|
error(
|
||
|
"Unable to locate service within compose file, is it a valid Budibase configuration?"
|
||
|
)
|
||
|
)
|
||
|
return
|
||
|
}
|
||
|
// set environment variable
|
||
|
service.environment["PLUGINS_DIR"] = PLUGIN_PATH
|
||
|
// add volumes to parsed yaml
|
||
|
if (!service.volumes) {
|
||
|
service.volumes = []
|
||
|
}
|
||
|
const found = service.volumes.find(vol => vol.includes(PLUGIN_PATH))
|
||
|
if (found) {
|
||
|
service.volumes.splice(service.volumes.indexOf(found), 1)
|
||
|
}
|
||
|
service.volumes.push(`${pluginPath}:${PLUGIN_PATH}`)
|
||
|
fs.writeFileSync(dockerFilePath, yaml.stringify(parsedYaml))
|
||
|
console.log(success("Docker compose configuration has been updated!"))
|
||
|
}
|