Repository navigation
Expand file tree
/
Copy pathstart.cjs
More file actions
76 lines (66 loc) · 2.21 KB
/
Copy pathstart.cjs
File metadata and controls
76 lines (66 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /usr/bin/env node
// Run ampm under nodemon. It watches changes to ampm-restart.json so that ampm
// can restart itself. It also restarts on crash.
// Really should be using nodemon as a module, but:
// https://github.com/stimulant/ampm/issues/12
const path = require("path");
const child_process = require("child_process");
const fs = require("fs");
var configFiles = process.argv[2] || "ampm.json";
var configFile = path.resolve(configFiles.split(",")[0]);
var appPath = path.dirname(configFile);
var restartFile = path.join(appPath, "ampm-restart.json");
var stateFile = path.join(appPath, "ampm-state.json");
var mode = process.argv[3] || "default";
var server = path.join(__dirname, "server.cjs");
if (!fs.existsSync(restartFile)) {
fs.writeFileSync(restartFile, "");
}
if (!fs.existsSync(stateFile)) {
fs.writeFileSync(stateFile, "{}");
}
// Single-instance lock: exit cleanly if another ampm is already running.
var lockFile = path.join(appPath, ".ampm.lock");
if (fs.existsSync(lockFile)) {
var lockPid = parseInt(fs.readFileSync(lockFile, "utf8").trim());
try {
process.kill(lockPid, 0); // throws if process no longer exists
console.error("ampm is already running (PID " + lockPid + "). Exiting.");
process.exit(0);
} catch (e) {
// Stale lock file — previous run didn't clean up. Continue.
console.log("Stale ampm lock file found (PID " + lockPid + "). Starting fresh.");
}
}
fs.writeFileSync(lockFile, String(process.pid));
function cleanupLock() { try { fs.unlinkSync(lockFile); } catch (e) {} }
process.on("exit", cleanupLock);
process.on("SIGINT", function () { cleanupLock(); process.exit(0); });
process.on("SIGTERM", function () { cleanupLock(); process.exit(0); });
var args = [
"--verbose",
"--exitcrash",
"--watch",
configFile,
"--watch",
restartFile,
"--ignore",
"logs",
"--ignore",
stateFile,
server,
configFiles,
mode,
];
// If there are arguments beyond the config file and mode, pass them to nodemon.
process.argv.slice(4).forEach(function (a, i) {
args.splice(args.length - 3, 0, a);
});
function start() {
var ampm = child_process.spawn("nodemon", args, {
stdio: "inherit",
shell: process.platform === "win32",
});
ampm.on("close", start);
}
start();