oasis/contrib/install-systemd-service.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-01-22 00:22:19 +00:00
const fs = require("fs");
const path = require("path");
const mkdirp = require("mkdirp");
const { execSync } = require("child_process");
const open = require("open");
2020-01-04 05:06:08 +00:00
2020-01-22 00:22:19 +00:00
let xdgConfigHome = process.env.XDG_CONFIG_HOME;
let systemdUserHome = process.env.SYSTEMD_USER_HOME;
2020-01-04 05:06:08 +00:00
if (xdgConfigHome == null) {
// Note: path.join() throws when arguments are null-ish.
2020-01-22 00:22:19 +00:00
xdgConfigHome = path.join(process.env.HOME, ".config");
2020-01-04 05:06:08 +00:00
}
if (systemdUserHome == null) {
2020-01-22 00:22:19 +00:00
systemdUserHome = path.join(xdgConfigHome, "systemd", "user");
2020-01-04 05:06:08 +00:00
}
2020-01-22 00:22:19 +00:00
const targetPath = path.join(systemdUserHome, "oasis.service");
2020-01-04 05:06:08 +00:00
if (fs.existsSync(targetPath)) {
2020-01-22 00:22:19 +00:00
console.log("Cowardly refusing to overwrite file:", targetPath);
2020-01-04 05:06:08 +00:00
} else {
2020-01-22 00:22:19 +00:00
mkdirp(systemdUserHome);
2020-01-04 05:06:08 +00:00
2020-01-22 00:22:19 +00:00
const sourcePath = path.join(__dirname, "oasis.service");
fs.copyFileSync(sourcePath, targetPath);
2020-01-04 05:06:08 +00:00
2020-01-22 00:22:19 +00:00
execSync("systemctl --user daemon-reload");
console.log("Service configuration has been installed to:", targetPath);
2020-01-04 05:06:08 +00:00
}
// Since this isn't in a post-install script we can enable, start, and open it.
2020-01-22 00:22:19 +00:00
execSync("systemctl --user enable oasis");
execSync("systemctl --user start oasis");
open("http://localhost:4515");