Take a look at Cockpit.js.
Given you've included <script src="../base1/cockpit.js"></script>
somewhere inside your package's index.html
, a cockpit
object will be available inside your package's index.js
.
There might be better solutions for sure, but something along the lines of this should work:
let myvhost = `server {
// ... server block configuration ...
}`
cockpit.spawn([ "sudo", "echo", myvhost, ">", "/etc/nginx/conf.d/myvhost.conf" ]).then(data => {
console.log("vhost created")
})
You should also check if nginx is installed so that when you install your package, it is only visible if nginx is available. And don't forget to reload nginx in order to activate your new server block configuration.
Another way would be to use Cockpit.js: File Access API
There are some examples in the cockpit repo