ubuntu-service-detect 0.0.0-stage โ 1.0.0
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/README.md +83 -3
- package/package.json +28 -5
- package/src/index.js +2 -0
- package/src/v1/index.js +10 -0
- package/src/v1/inspectors/detector.js +157 -0
- package/src/v1/inspectors/parser.js +104 -0
package/README.md
CHANGED
|
@@ -1,3 +1,83 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# ubuntu-service-detect
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/ubuntu-service-detect)
|
|
4
|
+
[](https://keshavsoft.github.io/ubuntu-service-docs/detect/)
|
|
5
|
+
[](https://keshavsoft.github.io/ubuntu-service-docs/)
|
|
6
|
+
[](https://github.com/keshavsoft/ubuntu-service-detect)
|
|
7
|
+
|
|
8
|
+
> Context-aware detection of systemd services matching the current or specified working directory on an Ubuntu VPS.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## ๐ Documentation & Links
|
|
13
|
+
|
|
14
|
+
- **Dedicated Documentation:** [https://keshavsoft.github.io/ubuntu-service-docs/detect/](https://keshavsoft.github.io/ubuntu-service-docs/detect/)
|
|
15
|
+
- **Full Suite Hub:** [https://keshavsoft.github.io/ubuntu-service-docs/](https://keshavsoft.github.io/ubuntu-service-docs/)
|
|
16
|
+
- **All 8 Repositories in Family:** [https://keshavsoft.github.io/ubuntu-service-docs/repos/](https://keshavsoft.github.io/ubuntu-service-docs/repos/)
|
|
17
|
+
- **npm Package:** [https://www.npmjs.com/package/ubuntu-service-detect](https://www.npmjs.com/package/ubuntu-service-detect)
|
|
18
|
+
- **Sister Packages:**
|
|
19
|
+
- `ubuntu-service`: [npm](https://www.npmjs.com/package/ubuntu-service) | [docs](https://keshavsoft.github.io/ubuntu-service-docs/service/)
|
|
20
|
+
- `ubuntu-service-restart`: [npm](https://www.npmjs.com/package/ubuntu-service-restart) | [docs](https://keshavsoft.github.io/ubuntu-service-docs/restart/)
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## ๐ฏ Purpose
|
|
25
|
+
|
|
26
|
+
Given `process.cwd()` (or any target folder path), `ubuntu-service-detect` scans `/etc/systemd/system/` (and fallback directories) to find the exact `.service` file responsible for running that directory.
|
|
27
|
+
|
|
28
|
+
### Key Capabilities:
|
|
29
|
+
- **Zero side-effects:** Pure read/query operation; never triggers `systemctl restart` or modifies files.
|
|
30
|
+
- **Robust INI parsing:** Safely handles single-string or multiple `ExecStart=` entries (arrays).
|
|
31
|
+
- **Multi-strategy matching:**
|
|
32
|
+
1. `WorkingDirectory` exact match (100 score)
|
|
33
|
+
2. Subpath match (80 score)
|
|
34
|
+
3. `ExecStart` path containment (70 score)
|
|
35
|
+
4. Package name / directory basename heuristic (50/40 score)
|
|
36
|
+
- **Automatic Port Extraction:** Detects internal ports assigned via `Environment="PORT=..."`, `--port`, or local `.env`.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## ๐ Installation & Usage
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm install ubuntu-service-detect
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
import { findServiceForDirectory } from "ubuntu-service-detect";
|
|
48
|
+
|
|
49
|
+
const result = findServiceForDirectory(process.cwd());
|
|
50
|
+
|
|
51
|
+
if (result.found) {
|
|
52
|
+
console.log(`Matched Unit: ${result.unitName}`);
|
|
53
|
+
console.log(`Assigned Port: ${result.port}`);
|
|
54
|
+
console.log(`Working Directory: ${result.workingDirectory}`);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## ๐งฉ Architecture
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
ubuntu-service-detect/
|
|
64
|
+
โโโ src/
|
|
65
|
+
โ โโโ index.js # Root exports
|
|
66
|
+
โ โโโ v1/
|
|
67
|
+
โ โโโ index.js # v1 entry point
|
|
68
|
+
โ โโโ inspectors/
|
|
69
|
+
โ โโโ detector.js # Directory matching algorithms
|
|
70
|
+
โ โโโ parser.js # Safe systemd INI & port parsing
|
|
71
|
+
โโโ test/
|
|
72
|
+
โ โโโ detect.test.js # Node native test runner (100% passing)
|
|
73
|
+
โโโ README.md
|
|
74
|
+
โโโ package.json
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## ๐งช Tests
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
npm test
|
|
83
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ubuntu-service-detect",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
1
|
+
{
|
|
2
|
+
"name": "ubuntu-service-detect",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Context-aware detection of systemd services matching the current or specified working directory.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "node --test"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"ubuntu",
|
|
19
|
+
"systemd",
|
|
20
|
+
"detect",
|
|
21
|
+
"vps",
|
|
22
|
+
"working-directory"
|
|
23
|
+
],
|
|
24
|
+
"author": "KeshavSoft",
|
|
25
|
+
"license": "ISC",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18.0.0"
|
|
28
|
+
}
|
|
6
29
|
}
|
package/src/index.js
ADDED
package/src/v1/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { parseServiceFile, detectPort } from "./inspectors/parser.js";
|
|
2
|
+
export {
|
|
3
|
+
findServiceForDirectory,
|
|
4
|
+
normalizePath,
|
|
5
|
+
inspectDirectoryMetadata,
|
|
6
|
+
DEFAULT_SYSTEMD_DIRS
|
|
7
|
+
} from "./inspectors/detector.js";
|
|
8
|
+
|
|
9
|
+
import { findServiceForDirectory } from "./inspectors/detector.js";
|
|
10
|
+
export default findServiceForDirectory;
|
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
"/lib/systemd/system",
|
|
9
|
+
"/usr/lib/systemd/system"
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export function normalizePath(p) {
|
|
13
|
+
if (!p) return "";
|
|
14
|
+
let resolved = path.resolve(p).replace(/\\/g, "/");
|
|
15
|
+
if (resolved.endsWith("/")) {
|
|
16
|
+
resolved = resolved.slice(0, -1);
|
|
17
|
+
}
|
|
18
|
+
return resolved;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function inspectDirectoryMetadata(targetDir) {
|
|
22
|
+
const meta = {
|
|
23
|
+
name: null,
|
|
24
|
+
version: null,
|
|
25
|
+
isGitRepo: false
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
if (!targetDir || !fs.existsSync(targetDir)) {
|
|
29
|
+
return meta;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const pkgPath = path.join(targetDir, "package.json");
|
|
33
|
+
if (fs.existsSync(pkgPath)) {
|
|
34
|
+
try {
|
|
35
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
36
|
+
meta.name = pkg.name || null;
|
|
37
|
+
meta.version = pkg.version || null;
|
|
38
|
+
} catch {
|
|
39
|
+
// Ignore parse errors
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const gitPath = path.join(targetDir, ".git");
|
|
44
|
+
meta.isGitRepo = fs.existsSync(gitPath);
|
|
45
|
+
|
|
46
|
+
return meta;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function findServiceForDirectory(targetDir = process.cwd(), options = {}) {
|
|
50
|
+
const normTarget = normalizePath(targetDir);
|
|
51
|
+
const meta = inspectDirectoryMetadata(targetDir);
|
|
52
|
+
|
|
53
|
+
const dirsToScan = options.systemdDir
|
|
54
|
+
? [path.resolve(options.systemdDir)]
|
|
55
|
+
: DEFAULT_SYSTEMD_DIRS;
|
|
56
|
+
|
|
57
|
+
const matchedUnits = [];
|
|
58
|
+
|
|
59
|
+
for (const scanDir of dirsToScan) {
|
|
60
|
+
if (!fs.existsSync(scanDir)) continue;
|
|
61
|
+
|
|
62
|
+
let files = [];
|
|
63
|
+
try {
|
|
64
|
+
files = fs.readdirSync(scanDir).filter(f => f.endsWith(".service"));
|
|
65
|
+
} catch {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (const file of files) {
|
|
70
|
+
const filePath = path.join(scanDir, file);
|
|
71
|
+
const parsed = parseServiceFile(filePath);
|
|
72
|
+
if (!parsed) continue;
|
|
73
|
+
|
|
74
|
+
const serviceSection = parsed.Service || {};
|
|
75
|
+
const unitSection = parsed.Unit || {};
|
|
76
|
+
const workingDir = serviceSection.WorkingDirectory;
|
|
77
|
+
const execStart = serviceSection.ExecStart || "";
|
|
78
|
+
|
|
79
|
+
let matchType = null;
|
|
80
|
+
let score = 0;
|
|
81
|
+
|
|
82
|
+
if (workingDir) {
|
|
83
|
+
const normWorking = normalizePath(workingDir);
|
|
84
|
+
if (normWorking === normTarget) {
|
|
85
|
+
matchType = "exact_working_directory";
|
|
86
|
+
score = 100;
|
|
87
|
+
} else if (normTarget.startsWith(normWorking + "/") || normWorking.startsWith(normTarget + "/")) {
|
|
88
|
+
matchType = "subpath_working_directory";
|
|
89
|
+
score = 80;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!matchType && execStart) {
|
|
94
|
+
const rawExec = Array.isArray(execStart) ? execStart.join(" ") : String(execStart);
|
|
95
|
+
const normExec = rawExec.replace(/\\/g, "/");
|
|
96
|
+
if (normExec.includes(normTarget)) {
|
|
97
|
+
matchType = "exec_start_path";
|
|
98
|
+
score = 70;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!matchType) {
|
|
103
|
+
const baseName = path.basename(normTarget).toLowerCase();
|
|
104
|
+
const unitBase = file.replace(/\.service$/, "").toLowerCase();
|
|
105
|
+
|
|
106
|
+
if (meta.name && unitBase.includes(meta.name.toLowerCase())) {
|
|
107
|
+
matchType = "package_name_match";
|
|
108
|
+
score = 50;
|
|
109
|
+
} else if (unitBase.includes(baseName) || baseName.includes(unitBase)) {
|
|
110
|
+
matchType = "directory_name_match";
|
|
111
|
+
score = 40;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (matchType) {
|
|
116
|
+
const detectedPort = detectPort(parsed, targetDir);
|
|
117
|
+
matchedUnits.push({
|
|
118
|
+
unitName: file,
|
|
119
|
+
unitPath: filePath,
|
|
120
|
+
workingDirectory: workingDir || null,
|
|
121
|
+
workingDirectoryExists: workingDir ? fs.existsSync(workingDir) : false,
|
|
122
|
+
execStart,
|
|
123
|
+
description: unitSection.Description || null,
|
|
124
|
+
user: serviceSection.User || null,
|
|
125
|
+
port: detectedPort,
|
|
126
|
+
matchType,
|
|
127
|
+
score,
|
|
128
|
+
raw: parsed
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (matchedUnits.length === 0) {
|
|
135
|
+
return {
|
|
136
|
+
found: false,
|
|
137
|
+
targetDir: normTarget,
|
|
138
|
+
meta,
|
|
139
|
+
message: `No systemd service found matching directory: ${normTarget}`
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
matchedUnits.sort((a, b) => b.score - a.score);
|
|
144
|
+
const bestMatch = matchedUnits[0];
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
found: true,
|
|
148
|
+
targetDir: normTarget,
|
|
149
|
+
meta,
|
|
150
|
+
...bestMatch,
|
|
151
|
+
alternatives: matchedUnits.slice(1).map(u => ({
|
|
152
|
+
unitName: u.unitName,
|
|
153
|
+
matchType: u.matchType,
|
|
154
|
+
score: u.score
|
|
155
|
+
}))
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export function parseServiceFile(filePath) {
|
|
5
|
+
if (!fs.existsSync(filePath)) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
10
|
+
const lines = content.split(/\r?\n/);
|
|
11
|
+
const result = {
|
|
12
|
+
Unit: {},
|
|
13
|
+
Service: {},
|
|
14
|
+
Install: {},
|
|
15
|
+
raw: content
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
let currentSection = null;
|
|
19
|
+
|
|
20
|
+
for (const rawLine of lines) {
|
|
21
|
+
const line = rawLine.trim();
|
|
22
|
+
if (!line || line.startsWith("#") || line.startsWith(";")) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const sectionMatch = line.match(/^\[(.*?)\]$/);
|
|
27
|
+
if (sectionMatch) {
|
|
28
|
+
currentSection = sectionMatch[1];
|
|
29
|
+
if (!result[currentSection]) {
|
|
30
|
+
result[currentSection] = {};
|
|
31
|
+
}
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const eqIdx = line.indexOf("=");
|
|
36
|
+
if (eqIdx !== -1 && currentSection) {
|
|
37
|
+
const key = line.slice(0, eqIdx).trim();
|
|
38
|
+
let value = line.slice(eqIdx + 1).trim();
|
|
39
|
+
|
|
40
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
41
|
+
value = value.slice(1, -1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (result[currentSection][key] !== undefined) {
|
|
45
|
+
if (Array.isArray(result[currentSection][key])) {
|
|
46
|
+
result[currentSection][key].push(value);
|
|
47
|
+
} else {
|
|
48
|
+
result[currentSection][key] = [result[currentSection][key], value];
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
result[currentSection][key] = value;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function detectPort(parsedService, targetDir) {
|
|
60
|
+
if (!parsedService) return null;
|
|
61
|
+
|
|
62
|
+
const envValue = parsedService.Service?.Environment;
|
|
63
|
+
if (envValue) {
|
|
64
|
+
const envList = Array.isArray(envValue) ? envValue : [envValue];
|
|
65
|
+
for (const env of envList) {
|
|
66
|
+
const match = String(env).match(/PORT\s*=\s*(\d+)/i);
|
|
67
|
+
if (match) {
|
|
68
|
+
return parseInt(match[1], 10);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const execStart = parsedService.Service?.ExecStart;
|
|
74
|
+
if (execStart) {
|
|
75
|
+
const execList = Array.isArray(execStart) ? execStart : [execStart];
|
|
76
|
+
for (const exec of execList) {
|
|
77
|
+
const portFlagMatch = String(exec).match(/(?:--port|-p)\s+(\d+)/i);
|
|
78
|
+
if (portFlagMatch) {
|
|
79
|
+
return parseInt(portFlagMatch[1], 10);
|
|
80
|
+
}
|
|
81
|
+
const portAssignMatch = String(exec).match(/PORT\s*=\s*(\d+)/i);
|
|
82
|
+
if (portAssignMatch) {
|
|
83
|
+
return parseInt(portAssignMatch[1], 10);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (targetDir && fs.existsSync(targetDir)) {
|
|
89
|
+
const envPath = path.join(targetDir, ".env");
|
|
90
|
+
if (fs.existsSync(envPath)) {
|
|
91
|
+
try {
|
|
92
|
+
const envContent = fs.readFileSync(envPath, "utf-8");
|
|
93
|
+
const match = envContent.match(/^PORT\s*=\s*(\d+)/m);
|
|
94
|
+
if (match) {
|
|
95
|
+
return parseInt(match[1], 10);
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
// Ignore unreadable .env
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return null;
|
|
104
|
+
}
|