ubuntu-service 1.0.0 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -15
- package/README.md +129 -117
- package/bin/index.js +8 -8
- package/package.json +50 -47
- package/src/cli.js +2 -2
- package/src/index.js +3 -3
- package/src/{v1 → v2}/actions.js +49 -58
- package/src/{v1 → v2}/cli.js +245 -245
- package/src/{v1 → v2}/detector.js +195 -194
- package/src/v2/index.js +11 -0
- package/src/{v1 → v2}/parser.js +113 -113
- package/src/{v1 → v2}/systemctl.js +147 -147
- package/src/v3/cli.js +126 -0
- package/src/v3/index.js +5 -0
- package/src/v3/service.js +175 -0
- package/src/v1/index.js +0 -55
|
@@ -1,194 +1,195 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { parseServiceFile, detectPort } from "./parser.js";
|
|
4
|
-
|
|
5
|
-
export const DEFAULT_SYSTEMD_DIRS = [
|
|
6
|
-
"/etc/systemd/system",
|
|
7
|
-
"/run/systemd/system",
|
|
8
|
-
"/usr/lib/systemd/system",
|
|
9
|
-
"/lib/systemd/system"
|
|
10
|
-
];
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Normalizes a directory path for reliable cross-platform comparison.
|
|
14
|
-
* Resolves symlinks if the path exists.
|
|
15
|
-
*/
|
|
16
|
-
export function normalizePath(p) {
|
|
17
|
-
if (!p) return "";
|
|
18
|
-
let resolved = path.resolve(p);
|
|
19
|
-
try {
|
|
20
|
-
if (fs.existsSync(resolved)) {
|
|
21
|
-
resolved = fs.realpathSync(resolved);
|
|
22
|
-
}
|
|
23
|
-
} catch {
|
|
24
|
-
// Fall back to resolved if realpath fails
|
|
25
|
-
}
|
|
26
|
-
// Normalize slashes to forward slashes for unified comparison
|
|
27
|
-
let normalized = resolved.replace(/\\/g, "/");
|
|
28
|
-
if (normalized.endsWith("/") && normalized.length > 1) {
|
|
29
|
-
normalized = normalized.slice(0, -1);
|
|
30
|
-
}
|
|
31
|
-
return normalized;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Inspects a directory for package.json or git config metadata.
|
|
36
|
-
*/
|
|
37
|
-
export function inspectDirectoryMetadata(dirPath) {
|
|
38
|
-
const meta = {
|
|
39
|
-
name: null,
|
|
40
|
-
gitRemote: null
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
if (!dirPath || !fs.existsSync(dirPath)) {
|
|
44
|
-
return meta;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Check package.json
|
|
48
|
-
const pkgPath = path.join(dirPath, "package.json");
|
|
49
|
-
if (fs.existsSync(pkgPath)) {
|
|
50
|
-
try {
|
|
51
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
52
|
-
meta.name = pkg.name || null;
|
|
53
|
-
} catch {
|
|
54
|
-
// ignore malformed JSON
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Check .git/config
|
|
59
|
-
const gitConfigPath = path.join(dirPath, ".git", "config");
|
|
60
|
-
if (fs.existsSync(gitConfigPath)) {
|
|
61
|
-
try {
|
|
62
|
-
const gitConfig = fs.readFileSync(gitConfigPath, "utf-8");
|
|
63
|
-
const m = gitConfig.match(/url\s*=\s*.*github\.com[/:]([^/\s]+\/[^\s.]+)/i);
|
|
64
|
-
if (m) {
|
|
65
|
-
meta.gitRemote = m[1];
|
|
66
|
-
}
|
|
67
|
-
} catch {
|
|
68
|
-
// ignore git config error
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return meta;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Finds the single systemd service that owns or runs from the given directory.
|
|
77
|
-
* @param {string} [targetDir] - Directory to check (defaults to process.cwd())
|
|
78
|
-
* @param {object} [options] - Options ({ systemdDir })
|
|
79
|
-
* @returns {object} Result with matched service or not found status
|
|
80
|
-
*/
|
|
81
|
-
export function findServiceForDirectory(targetDir = process.cwd(), options = {}) {
|
|
82
|
-
const normTarget = normalizePath(targetDir);
|
|
83
|
-
const meta = inspectDirectoryMetadata(targetDir);
|
|
84
|
-
|
|
85
|
-
const dirsToScan = options.systemdDir
|
|
86
|
-
? [path.resolve(options.systemdDir)]
|
|
87
|
-
: DEFAULT_SYSTEMD_DIRS;
|
|
88
|
-
|
|
89
|
-
const matchedUnits = [];
|
|
90
|
-
|
|
91
|
-
for (const scanDir of dirsToScan) {
|
|
92
|
-
if (!fs.existsSync(scanDir)) continue;
|
|
93
|
-
|
|
94
|
-
let files = [];
|
|
95
|
-
try {
|
|
96
|
-
files = fs.readdirSync(scanDir).filter(f => f.endsWith(".service"));
|
|
97
|
-
} catch {
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
for (const file of files) {
|
|
102
|
-
const filePath = path.join(scanDir, file);
|
|
103
|
-
const parsed = parseServiceFile(filePath);
|
|
104
|
-
if (!parsed) continue;
|
|
105
|
-
|
|
106
|
-
const serviceSection = parsed.Service || {};
|
|
107
|
-
const unitSection = parsed.Unit || {};
|
|
108
|
-
const workingDir = serviceSection.WorkingDirectory;
|
|
109
|
-
const execStart = serviceSection.ExecStart || "";
|
|
110
|
-
|
|
111
|
-
let matchType = null;
|
|
112
|
-
let score = 0;
|
|
113
|
-
|
|
114
|
-
if (workingDir) {
|
|
115
|
-
const normWorking = normalizePath(workingDir);
|
|
116
|
-
if (normWorking === normTarget) {
|
|
117
|
-
matchType = "exact_working_directory";
|
|
118
|
-
score = 100;
|
|
119
|
-
} else if (normTarget.startsWith(normWorking + "/") || normWorking.startsWith(normTarget + "/")) {
|
|
120
|
-
matchType = "subpath_working_directory";
|
|
121
|
-
score = 80;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (!matchType && execStart) {
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { parseServiceFile, detectPort } from "./parser.js";
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_SYSTEMD_DIRS = [
|
|
6
|
+
"/etc/systemd/system",
|
|
7
|
+
"/run/systemd/system",
|
|
8
|
+
"/usr/lib/systemd/system",
|
|
9
|
+
"/lib/systemd/system"
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Normalizes a directory path for reliable cross-platform comparison.
|
|
14
|
+
* Resolves symlinks if the path exists.
|
|
15
|
+
*/
|
|
16
|
+
export function normalizePath(p) {
|
|
17
|
+
if (!p) return "";
|
|
18
|
+
let resolved = path.resolve(p);
|
|
19
|
+
try {
|
|
20
|
+
if (fs.existsSync(resolved)) {
|
|
21
|
+
resolved = fs.realpathSync(resolved);
|
|
22
|
+
}
|
|
23
|
+
} catch {
|
|
24
|
+
// Fall back to resolved if realpath fails
|
|
25
|
+
}
|
|
26
|
+
// Normalize slashes to forward slashes for unified comparison
|
|
27
|
+
let normalized = resolved.replace(/\\/g, "/");
|
|
28
|
+
if (normalized.endsWith("/") && normalized.length > 1) {
|
|
29
|
+
normalized = normalized.slice(0, -1);
|
|
30
|
+
}
|
|
31
|
+
return normalized;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Inspects a directory for package.json or git config metadata.
|
|
36
|
+
*/
|
|
37
|
+
export function inspectDirectoryMetadata(dirPath) {
|
|
38
|
+
const meta = {
|
|
39
|
+
name: null,
|
|
40
|
+
gitRemote: null
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
if (!dirPath || !fs.existsSync(dirPath)) {
|
|
44
|
+
return meta;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Check package.json
|
|
48
|
+
const pkgPath = path.join(dirPath, "package.json");
|
|
49
|
+
if (fs.existsSync(pkgPath)) {
|
|
50
|
+
try {
|
|
51
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
52
|
+
meta.name = pkg.name || null;
|
|
53
|
+
} catch {
|
|
54
|
+
// ignore malformed JSON
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Check .git/config
|
|
59
|
+
const gitConfigPath = path.join(dirPath, ".git", "config");
|
|
60
|
+
if (fs.existsSync(gitConfigPath)) {
|
|
61
|
+
try {
|
|
62
|
+
const gitConfig = fs.readFileSync(gitConfigPath, "utf-8");
|
|
63
|
+
const m = gitConfig.match(/url\s*=\s*.*github\.com[/:]([^/\s]+\/[^\s.]+)/i);
|
|
64
|
+
if (m) {
|
|
65
|
+
meta.gitRemote = m[1];
|
|
66
|
+
}
|
|
67
|
+
} catch {
|
|
68
|
+
// ignore git config error
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return meta;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Finds the single systemd service that owns or runs from the given directory.
|
|
77
|
+
* @param {string} [targetDir] - Directory to check (defaults to process.cwd())
|
|
78
|
+
* @param {object} [options] - Options ({ systemdDir })
|
|
79
|
+
* @returns {object} Result with matched service or not found status
|
|
80
|
+
*/
|
|
81
|
+
export function findServiceForDirectory(targetDir = process.cwd(), options = {}) {
|
|
82
|
+
const normTarget = normalizePath(targetDir);
|
|
83
|
+
const meta = inspectDirectoryMetadata(targetDir);
|
|
84
|
+
|
|
85
|
+
const dirsToScan = options.systemdDir
|
|
86
|
+
? [path.resolve(options.systemdDir)]
|
|
87
|
+
: DEFAULT_SYSTEMD_DIRS;
|
|
88
|
+
|
|
89
|
+
const matchedUnits = [];
|
|
90
|
+
|
|
91
|
+
for (const scanDir of dirsToScan) {
|
|
92
|
+
if (!fs.existsSync(scanDir)) continue;
|
|
93
|
+
|
|
94
|
+
let files = [];
|
|
95
|
+
try {
|
|
96
|
+
files = fs.readdirSync(scanDir).filter(f => f.endsWith(".service"));
|
|
97
|
+
} catch {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
for (const file of files) {
|
|
102
|
+
const filePath = path.join(scanDir, file);
|
|
103
|
+
const parsed = parseServiceFile(filePath);
|
|
104
|
+
if (!parsed) continue;
|
|
105
|
+
|
|
106
|
+
const serviceSection = parsed.Service || {};
|
|
107
|
+
const unitSection = parsed.Unit || {};
|
|
108
|
+
const workingDir = serviceSection.WorkingDirectory;
|
|
109
|
+
const execStart = serviceSection.ExecStart || "";
|
|
110
|
+
|
|
111
|
+
let matchType = null;
|
|
112
|
+
let score = 0;
|
|
113
|
+
|
|
114
|
+
if (workingDir) {
|
|
115
|
+
const normWorking = normalizePath(workingDir);
|
|
116
|
+
if (normWorking === normTarget) {
|
|
117
|
+
matchType = "exact_working_directory";
|
|
118
|
+
score = 100;
|
|
119
|
+
} else if (normTarget.startsWith(normWorking + "/") || normWorking.startsWith(normTarget + "/")) {
|
|
120
|
+
matchType = "subpath_working_directory";
|
|
121
|
+
score = 80;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!matchType && execStart) {
|
|
126
|
+
const rawExec = Array.isArray(execStart) ? execStart.join(" ") : String(execStart);
|
|
127
|
+
const normExec = rawExec.replace(/\\/g, "/");
|
|
128
|
+
if (normExec.includes(normTarget)) {
|
|
129
|
+
matchType = "exec_start_path";
|
|
130
|
+
score = 70;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Fallback: match by package name or directory basename against service name
|
|
135
|
+
if (!matchType) {
|
|
136
|
+
const baseName = path.basename(normTarget).toLowerCase();
|
|
137
|
+
const unitBase = file.replace(/\.service$/, "").toLowerCase();
|
|
138
|
+
|
|
139
|
+
if (meta.name && unitBase.includes(meta.name.toLowerCase())) {
|
|
140
|
+
matchType = "package_name_match";
|
|
141
|
+
score = 50;
|
|
142
|
+
} else if (unitBase.includes(baseName) || baseName.includes(unitBase)) {
|
|
143
|
+
matchType = "directory_name_match";
|
|
144
|
+
score = 40;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (matchType) {
|
|
149
|
+
const port = detectPort(parsed, workingDir);
|
|
150
|
+
matchedUnits.push({
|
|
151
|
+
unitName: file,
|
|
152
|
+
filePath,
|
|
153
|
+
systemdDir: scanDir,
|
|
154
|
+
matchType,
|
|
155
|
+
score,
|
|
156
|
+
workingDirectory: workingDir || null,
|
|
157
|
+
workingDirectoryExists: workingDir ? fs.existsSync(workingDir) : false,
|
|
158
|
+
execStart,
|
|
159
|
+
description: unitSection.Description || null,
|
|
160
|
+
port,
|
|
161
|
+
serviceConfig: parsed
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (matchedUnits.length === 0) {
|
|
168
|
+
return {
|
|
169
|
+
found: false,
|
|
170
|
+
targetDir: normTarget,
|
|
171
|
+
message: `No systemd service unit found matching directory '${normTarget}'`
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Sort by best score
|
|
176
|
+
matchedUnits.sort((a, b) => b.score - a.score);
|
|
177
|
+
const primary = matchedUnits[0];
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
found: true,
|
|
181
|
+
targetDir: normTarget,
|
|
182
|
+
unitName: primary.unitName,
|
|
183
|
+
filePath: primary.filePath,
|
|
184
|
+
systemdDir: primary.systemdDir,
|
|
185
|
+
matchType: primary.matchType,
|
|
186
|
+
score: primary.score,
|
|
187
|
+
workingDirectory: primary.workingDirectory,
|
|
188
|
+
workingDirectoryExists: primary.workingDirectoryExists,
|
|
189
|
+
execStart: primary.execStart,
|
|
190
|
+
description: primary.description,
|
|
191
|
+
port: primary.port,
|
|
192
|
+
allMatches: matchedUnits.length > 1 ? matchedUnits : undefined,
|
|
193
|
+
serviceConfig: primary.serviceConfig
|
|
194
|
+
};
|
|
195
|
+
}
|
package/src/v2/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { parseServiceFile, detectPort, findServiceForDirectory, normalizePath, inspectDirectoryMetadata, DEFAULT_SYSTEMD_DIRS } from "ubuntu-service-detect";
|
|
2
|
+
export { isSystemctlAvailable, getServiceLiveStatus, restartService, getServiceLogs } from "ubuntu-service-restart";
|
|
3
|
+
export {
|
|
4
|
+
inspectCurrentService,
|
|
5
|
+
restartCurrentService,
|
|
6
|
+
getCurrentServiceLogs
|
|
7
|
+
} from "./actions.js";
|
|
8
|
+
export { runCli, printHelp } from "./cli.js";
|
|
9
|
+
|
|
10
|
+
import { inspectCurrentService } from "./actions.js";
|
|
11
|
+
export default inspectCurrentService;
|
package/src/{v1 → v2}/parser.js
RENAMED
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Parses a systemd .service INI file into structured sections.
|
|
6
|
-
* @param {string} filePath - Absolute path to .service file
|
|
7
|
-
* @returns {object} Parsed service configuration
|
|
8
|
-
*/
|
|
9
|
-
export function parseServiceFile(filePath) {
|
|
10
|
-
if (!fs.existsSync(filePath)) {
|
|
11
|
-
return null;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const content = fs.readFileSync(filePath, "utf-8");
|
|
15
|
-
const lines = content.split(/\r?\n/);
|
|
16
|
-
const result = {
|
|
17
|
-
Unit: {},
|
|
18
|
-
Service: {},
|
|
19
|
-
Install: {},
|
|
20
|
-
raw: content
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
let currentSection = null;
|
|
24
|
-
|
|
25
|
-
for (const rawLine of lines) {
|
|
26
|
-
const line = rawLine.trim();
|
|
27
|
-
if (!line || line.startsWith("#") || line.startsWith(";")) {
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const sectionMatch = line.match(/^\[(.*?)\]$/);
|
|
32
|
-
if (sectionMatch) {
|
|
33
|
-
currentSection = sectionMatch[1];
|
|
34
|
-
if (!result[currentSection]) {
|
|
35
|
-
result[currentSection] = {};
|
|
36
|
-
}
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const eqIdx = line.indexOf("=");
|
|
41
|
-
if (eqIdx !== -1 && currentSection) {
|
|
42
|
-
const key = line.slice(0, eqIdx).trim();
|
|
43
|
-
let value = line.slice(eqIdx + 1).trim();
|
|
44
|
-
|
|
45
|
-
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
46
|
-
value = value.slice(1, -1);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (result[currentSection][key] !== undefined) {
|
|
50
|
-
if (Array.isArray(result[currentSection][key])) {
|
|
51
|
-
result[currentSection][key].push(value);
|
|
52
|
-
} else {
|
|
53
|
-
result[currentSection][key] = [result[currentSection][key], value];
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
result[currentSection][key] = value;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return result;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Extracts port numbers from Environment strings, ExecStart args, or .env files.
|
|
66
|
-
* @param {object} parsedConfig - Output from parseServiceFile
|
|
67
|
-
* @param {string} [workingDir] - Service working directory
|
|
68
|
-
* @returns {number|null} Detected port or null
|
|
69
|
-
*/
|
|
70
|
-
export function detectPort(parsedConfig, workingDir) {
|
|
71
|
-
if (!parsedConfig) return null;
|
|
72
|
-
const service = parsedConfig.Service || {};
|
|
73
|
-
|
|
74
|
-
// 1. Check Environment key(s)
|
|
75
|
-
const envs = service.Environment;
|
|
76
|
-
if (envs) {
|
|
77
|
-
const envList = Array.isArray(envs) ? envs : [envs];
|
|
78
|
-
for (const e of envList) {
|
|
79
|
-
const portMatch = e.match(/(?:PORT|LISTEN_PORT|APP_PORT)\s*=\s*(\d{2,5})/i);
|
|
80
|
-
if (portMatch) {
|
|
81
|
-
return parseInt(portMatch[1], 10);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 2. Check ExecStart for --port 3000, -p 3000, or port number
|
|
87
|
-
const execStart = service.ExecStart;
|
|
88
|
-
if (execStart) {
|
|
89
|
-
const execMatch = execStart.match(/(?:--port|-p)\s+([0-9]{2,5})/i);
|
|
90
|
-
if (execMatch) {
|
|
91
|
-
return parseInt(execMatch[1], 10);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// 3. Check .env file in WorkingDirectory if it exists
|
|
96
|
-
const dir = workingDir || service.WorkingDirectory;
|
|
97
|
-
if (dir && fs.existsSync(dir)) {
|
|
98
|
-
const envFilePath = path.join(dir, ".env");
|
|
99
|
-
if (fs.existsSync(envFilePath)) {
|
|
100
|
-
try {
|
|
101
|
-
const envContent = fs.readFileSync(envFilePath, "utf-8");
|
|
102
|
-
const m = envContent.match(/(?:PORT|APP_PORT|SERVER_PORT)\s*=\s*(\d{2,5})/i);
|
|
103
|
-
if (m) {
|
|
104
|
-
return parseInt(m[1], 10);
|
|
105
|
-
}
|
|
106
|
-
} catch {
|
|
107
|
-
// ignore unreadable .env
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return null;
|
|
113
|
-
}
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parses a systemd .service INI file into structured sections.
|
|
6
|
+
* @param {string} filePath - Absolute path to .service file
|
|
7
|
+
* @returns {object} Parsed service configuration
|
|
8
|
+
*/
|
|
9
|
+
export function parseServiceFile(filePath) {
|
|
10
|
+
if (!fs.existsSync(filePath)) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
15
|
+
const lines = content.split(/\r?\n/);
|
|
16
|
+
const result = {
|
|
17
|
+
Unit: {},
|
|
18
|
+
Service: {},
|
|
19
|
+
Install: {},
|
|
20
|
+
raw: content
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
let currentSection = null;
|
|
24
|
+
|
|
25
|
+
for (const rawLine of lines) {
|
|
26
|
+
const line = rawLine.trim();
|
|
27
|
+
if (!line || line.startsWith("#") || line.startsWith(";")) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const sectionMatch = line.match(/^\[(.*?)\]$/);
|
|
32
|
+
if (sectionMatch) {
|
|
33
|
+
currentSection = sectionMatch[1];
|
|
34
|
+
if (!result[currentSection]) {
|
|
35
|
+
result[currentSection] = {};
|
|
36
|
+
}
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const eqIdx = line.indexOf("=");
|
|
41
|
+
if (eqIdx !== -1 && currentSection) {
|
|
42
|
+
const key = line.slice(0, eqIdx).trim();
|
|
43
|
+
let value = line.slice(eqIdx + 1).trim();
|
|
44
|
+
|
|
45
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
46
|
+
value = value.slice(1, -1);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (result[currentSection][key] !== undefined) {
|
|
50
|
+
if (Array.isArray(result[currentSection][key])) {
|
|
51
|
+
result[currentSection][key].push(value);
|
|
52
|
+
} else {
|
|
53
|
+
result[currentSection][key] = [result[currentSection][key], value];
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
result[currentSection][key] = value;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Extracts port numbers from Environment strings, ExecStart args, or .env files.
|
|
66
|
+
* @param {object} parsedConfig - Output from parseServiceFile
|
|
67
|
+
* @param {string} [workingDir] - Service working directory
|
|
68
|
+
* @returns {number|null} Detected port or null
|
|
69
|
+
*/
|
|
70
|
+
export function detectPort(parsedConfig, workingDir) {
|
|
71
|
+
if (!parsedConfig) return null;
|
|
72
|
+
const service = parsedConfig.Service || {};
|
|
73
|
+
|
|
74
|
+
// 1. Check Environment key(s)
|
|
75
|
+
const envs = service.Environment;
|
|
76
|
+
if (envs) {
|
|
77
|
+
const envList = Array.isArray(envs) ? envs : [envs];
|
|
78
|
+
for (const e of envList) {
|
|
79
|
+
const portMatch = e.match(/(?:PORT|LISTEN_PORT|APP_PORT)\s*=\s*(\d{2,5})/i);
|
|
80
|
+
if (portMatch) {
|
|
81
|
+
return parseInt(portMatch[1], 10);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 2. Check ExecStart for --port 3000, -p 3000, or port number
|
|
87
|
+
const execStart = service.ExecStart;
|
|
88
|
+
if (execStart) {
|
|
89
|
+
const execMatch = execStart.match(/(?:--port|-p)\s+([0-9]{2,5})/i);
|
|
90
|
+
if (execMatch) {
|
|
91
|
+
return parseInt(execMatch[1], 10);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 3. Check .env file in WorkingDirectory if it exists
|
|
96
|
+
const dir = workingDir || service.WorkingDirectory;
|
|
97
|
+
if (dir && fs.existsSync(dir)) {
|
|
98
|
+
const envFilePath = path.join(dir, ".env");
|
|
99
|
+
if (fs.existsSync(envFilePath)) {
|
|
100
|
+
try {
|
|
101
|
+
const envContent = fs.readFileSync(envFilePath, "utf-8");
|
|
102
|
+
const m = envContent.match(/(?:PORT|APP_PORT|SERVER_PORT)\s*=\s*(\d{2,5})/i);
|
|
103
|
+
if (m) {
|
|
104
|
+
return parseInt(m[1], 10);
|
|
105
|
+
}
|
|
106
|
+
} catch {
|
|
107
|
+
// ignore unreadable .env
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return null;
|
|
113
|
+
}
|