install-systemd-service.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const fs = require("fs");
  2. const path = require("path");
  3. const mkdirp = require("mkdirp");
  4. const { execSync } = require("child_process");
  5. const open = require("open");
  6. let xdgConfigHome = process.env.XDG_CONFIG_HOME;
  7. let systemdUserHome = process.env.SYSTEMD_USER_HOME;
  8. if (xdgConfigHome == null) {
  9. // Note: path.join() throws when arguments are null-ish.
  10. xdgConfigHome = path.join(process.env.HOME, ".config");
  11. }
  12. if (systemdUserHome == null) {
  13. systemdUserHome = path.join(xdgConfigHome, "systemd", "user");
  14. }
  15. const targetPath = path.join(systemdUserHome, "oasis.service");
  16. if (fs.existsSync(targetPath)) {
  17. console.log("Cowardly refusing to overwrite file:", targetPath);
  18. } else {
  19. mkdirp.sync(systemdUserHome);
  20. const sourcePath = path.join(__dirname, "oasis.service");
  21. fs.copyFileSync(sourcePath, targetPath);
  22. execSync("systemctl --user daemon-reload");
  23. console.log("Service configuration has been installed to:", targetPath);
  24. }
  25. // Since this isn't in a post-install script we can enable, start, and open it.
  26. execSync("systemctl --user enable oasis");
  27. execSync("systemctl --user start oasis");
  28. open("http://localhost:4515");