ubuntu-service 1.2.1 → 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 -48
- package/src/cli.js +2 -2
- package/src/index.js +3 -3
- package/src/v2/actions.js +49 -58
- package/src/v2/cli.js +245 -245
- package/src/v2/detector.js +195 -195
- package/src/v2/index.js +11 -55
- package/src/v2/parser.js +113 -113
- package/src/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/actions.js +0 -58
- package/src/v1/cli.js +0 -245
- package/src/v1/detector.js +0 -194
- package/src/v1/index.js +0 -55
- package/src/v1/parser.js +0 -113
- package/src/v1/systemctl.js +0 -147
package/src/v2/detector.js
CHANGED
|
@@ -1,195 +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 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
|
-
}
|
|
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
CHANGED
|
@@ -1,55 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from "./
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
getServiceLogs
|
|
13
|
-
} from "./systemctl.js";
|
|
14
|
-
import {
|
|
15
|
-
inspectCurrentService,
|
|
16
|
-
restartCurrentService,
|
|
17
|
-
getCurrentServiceLogs
|
|
18
|
-
} from "./actions.js";
|
|
19
|
-
import { runCli, printHelp } from "./cli.js";
|
|
20
|
-
|
|
21
|
-
export {
|
|
22
|
-
parseServiceFile,
|
|
23
|
-
detectPort,
|
|
24
|
-
findServiceForDirectory,
|
|
25
|
-
normalizePath,
|
|
26
|
-
inspectDirectoryMetadata,
|
|
27
|
-
DEFAULT_SYSTEMD_DIRS,
|
|
28
|
-
isSystemctlAvailable,
|
|
29
|
-
getServiceLiveStatus,
|
|
30
|
-
restartService,
|
|
31
|
-
getServiceLogs,
|
|
32
|
-
inspectCurrentService,
|
|
33
|
-
restartCurrentService,
|
|
34
|
-
getCurrentServiceLogs,
|
|
35
|
-
runCli,
|
|
36
|
-
printHelp
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export default {
|
|
40
|
-
parseServiceFile,
|
|
41
|
-
detectPort,
|
|
42
|
-
findServiceForDirectory,
|
|
43
|
-
normalizePath,
|
|
44
|
-
inspectDirectoryMetadata,
|
|
45
|
-
DEFAULT_SYSTEMD_DIRS,
|
|
46
|
-
isSystemctlAvailable,
|
|
47
|
-
getServiceLiveStatus,
|
|
48
|
-
restartService,
|
|
49
|
-
getServiceLogs,
|
|
50
|
-
inspectCurrentService,
|
|
51
|
-
restartCurrentService,
|
|
52
|
-
getCurrentServiceLogs,
|
|
53
|
-
runCli,
|
|
54
|
-
printHelp
|
|
55
|
-
};
|
|
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;
|