oasis/contrib/install-systemd-service.js
Justin Abrahms 84b2abc123
Fix issue w/ mkdirp race condition
Previously you would get this error due to imperfect path creation

```
node contrib/install-systemd-service.js
fs.js:114
    throw err;
    ^

Error: ENOENT: no such file or directory, copyfile '/home/justin/src/github.com/fraction/oasis/contrib/oasis.service' -> '/home/justin/.config/systemd/user/oasis.service'
    at Object.copyFileSync (fs.js:1728:3)
    at Object.<anonymous> (/home/justin/src/github.com/fraction/oasis/contrib/install-systemd-service.js:27:6)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
```
2020-03-15 13:57:35 -07:00

37 lines
1.1 KiB
JavaScript

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