@locusai/locus-pm2 0.22.13
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/dist/index.cjs +142 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +100 -0
- package/dist/pm2.d.ts +36 -0
- package/dist/pm2.d.ts.map +1 -0
- package/package.json +46 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/index.ts
|
|
40
|
+
var exports_src = {};
|
|
41
|
+
__export(exports_src, {
|
|
42
|
+
resolvePackageScript: () => resolvePackageScript,
|
|
43
|
+
pm2Stop: () => pm2Stop,
|
|
44
|
+
pm2Status: () => pm2Status,
|
|
45
|
+
pm2Start: () => pm2Start,
|
|
46
|
+
pm2Restart: () => pm2Restart,
|
|
47
|
+
pm2Logs: () => pm2Logs,
|
|
48
|
+
pm2Delete: () => pm2Delete
|
|
49
|
+
});
|
|
50
|
+
module.exports = __toCommonJS(exports_src);
|
|
51
|
+
|
|
52
|
+
// src/pm2.ts
|
|
53
|
+
var import_node_child_process = require("node:child_process");
|
|
54
|
+
var import_node_path = require("node:path");
|
|
55
|
+
var import_node_url = require("node:url");
|
|
56
|
+
function getPm2Bin() {
|
|
57
|
+
try {
|
|
58
|
+
const result = import_node_child_process.execSync("which pm2", {
|
|
59
|
+
encoding: "utf-8",
|
|
60
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
61
|
+
}).trim();
|
|
62
|
+
if (result)
|
|
63
|
+
return result;
|
|
64
|
+
} catch {}
|
|
65
|
+
return "npx pm2";
|
|
66
|
+
}
|
|
67
|
+
function pm2Exec(args) {
|
|
68
|
+
const pm2 = getPm2Bin();
|
|
69
|
+
try {
|
|
70
|
+
return import_node_child_process.execSync(`${pm2} ${args}`, {
|
|
71
|
+
encoding: "utf-8",
|
|
72
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
73
|
+
env: process.env
|
|
74
|
+
});
|
|
75
|
+
} catch (error) {
|
|
76
|
+
const err = error;
|
|
77
|
+
throw new Error(err.stderr?.trim() || err.message || "PM2 command failed");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function pm2Start(config) {
|
|
81
|
+
const { processName, scriptPath, scriptArgs = [] } = config;
|
|
82
|
+
const pm2 = getPm2Bin();
|
|
83
|
+
try {
|
|
84
|
+
const list = pm2Exec("jlist");
|
|
85
|
+
const processes = JSON.parse(list);
|
|
86
|
+
const existing = processes.find((p) => p.name === processName);
|
|
87
|
+
if (existing) {
|
|
88
|
+
pm2Exec(`restart ${processName}`);
|
|
89
|
+
return `Restarted ${processName}`;
|
|
90
|
+
}
|
|
91
|
+
} catch {}
|
|
92
|
+
const argsStr = scriptArgs.length > 0 ? ` -- ${scriptArgs.join(" ")}` : "";
|
|
93
|
+
import_node_child_process.execSync(`${pm2} start ${JSON.stringify(scriptPath)} --name ${processName}${argsStr}`, {
|
|
94
|
+
encoding: "utf-8",
|
|
95
|
+
stdio: "inherit",
|
|
96
|
+
env: process.env
|
|
97
|
+
});
|
|
98
|
+
return `Started ${processName}`;
|
|
99
|
+
}
|
|
100
|
+
function pm2Stop(config) {
|
|
101
|
+
pm2Exec(`stop ${config.processName}`);
|
|
102
|
+
return `Stopped ${config.processName}`;
|
|
103
|
+
}
|
|
104
|
+
function pm2Restart(config) {
|
|
105
|
+
pm2Exec(`restart ${config.processName}`);
|
|
106
|
+
return `Restarted ${config.processName}`;
|
|
107
|
+
}
|
|
108
|
+
function pm2Delete(config) {
|
|
109
|
+
pm2Exec(`delete ${config.processName}`);
|
|
110
|
+
return `Deleted ${config.processName}`;
|
|
111
|
+
}
|
|
112
|
+
function pm2Status(config) {
|
|
113
|
+
try {
|
|
114
|
+
const list = pm2Exec("jlist");
|
|
115
|
+
const processes = JSON.parse(list);
|
|
116
|
+
const proc = processes.find((p) => p.name === config.processName);
|
|
117
|
+
if (!proc)
|
|
118
|
+
return null;
|
|
119
|
+
return {
|
|
120
|
+
name: config.processName,
|
|
121
|
+
status: proc.pm2_env?.status ?? "unknown",
|
|
122
|
+
pid: proc.pid ?? null,
|
|
123
|
+
uptime: proc.pm2_env?.pm_uptime ?? null,
|
|
124
|
+
memory: proc.monit?.memory ?? null,
|
|
125
|
+
restarts: proc.pm2_env?.restart_time ?? 0
|
|
126
|
+
};
|
|
127
|
+
} catch {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function pm2Logs(config, lines = 50) {
|
|
132
|
+
try {
|
|
133
|
+
return pm2Exec(`logs ${config.processName} --nostream --lines ${lines}`);
|
|
134
|
+
} catch {
|
|
135
|
+
return "No logs available.";
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function resolvePackageScript(importMetaUrl, binName) {
|
|
139
|
+
const currentFile = import_node_url.fileURLToPath(importMetaUrl);
|
|
140
|
+
const packageRoot = import_node_path.dirname(import_node_path.dirname(currentFile));
|
|
141
|
+
return import_node_path.join(packageRoot, "bin", `${binName}.js`);
|
|
142
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EACL,SAAS,EACT,OAAO,EACP,UAAU,EACV,QAAQ,EACR,SAAS,EACT,OAAO,EACP,oBAAoB,GACrB,MAAM,UAAU,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// src/pm2.ts
|
|
2
|
+
import { execSync } from "node:child_process";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
function getPm2Bin() {
|
|
6
|
+
try {
|
|
7
|
+
const result = execSync("which pm2", {
|
|
8
|
+
encoding: "utf-8",
|
|
9
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
10
|
+
}).trim();
|
|
11
|
+
if (result)
|
|
12
|
+
return result;
|
|
13
|
+
} catch {}
|
|
14
|
+
return "npx pm2";
|
|
15
|
+
}
|
|
16
|
+
function pm2Exec(args) {
|
|
17
|
+
const pm2 = getPm2Bin();
|
|
18
|
+
try {
|
|
19
|
+
return execSync(`${pm2} ${args}`, {
|
|
20
|
+
encoding: "utf-8",
|
|
21
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
22
|
+
env: process.env
|
|
23
|
+
});
|
|
24
|
+
} catch (error) {
|
|
25
|
+
const err = error;
|
|
26
|
+
throw new Error(err.stderr?.trim() || err.message || "PM2 command failed");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function pm2Start(config) {
|
|
30
|
+
const { processName, scriptPath, scriptArgs = [] } = config;
|
|
31
|
+
const pm2 = getPm2Bin();
|
|
32
|
+
try {
|
|
33
|
+
const list = pm2Exec("jlist");
|
|
34
|
+
const processes = JSON.parse(list);
|
|
35
|
+
const existing = processes.find((p) => p.name === processName);
|
|
36
|
+
if (existing) {
|
|
37
|
+
pm2Exec(`restart ${processName}`);
|
|
38
|
+
return `Restarted ${processName}`;
|
|
39
|
+
}
|
|
40
|
+
} catch {}
|
|
41
|
+
const argsStr = scriptArgs.length > 0 ? ` -- ${scriptArgs.join(" ")}` : "";
|
|
42
|
+
execSync(`${pm2} start ${JSON.stringify(scriptPath)} --name ${processName}${argsStr}`, {
|
|
43
|
+
encoding: "utf-8",
|
|
44
|
+
stdio: "inherit",
|
|
45
|
+
env: process.env
|
|
46
|
+
});
|
|
47
|
+
return `Started ${processName}`;
|
|
48
|
+
}
|
|
49
|
+
function pm2Stop(config) {
|
|
50
|
+
pm2Exec(`stop ${config.processName}`);
|
|
51
|
+
return `Stopped ${config.processName}`;
|
|
52
|
+
}
|
|
53
|
+
function pm2Restart(config) {
|
|
54
|
+
pm2Exec(`restart ${config.processName}`);
|
|
55
|
+
return `Restarted ${config.processName}`;
|
|
56
|
+
}
|
|
57
|
+
function pm2Delete(config) {
|
|
58
|
+
pm2Exec(`delete ${config.processName}`);
|
|
59
|
+
return `Deleted ${config.processName}`;
|
|
60
|
+
}
|
|
61
|
+
function pm2Status(config) {
|
|
62
|
+
try {
|
|
63
|
+
const list = pm2Exec("jlist");
|
|
64
|
+
const processes = JSON.parse(list);
|
|
65
|
+
const proc = processes.find((p) => p.name === config.processName);
|
|
66
|
+
if (!proc)
|
|
67
|
+
return null;
|
|
68
|
+
return {
|
|
69
|
+
name: config.processName,
|
|
70
|
+
status: proc.pm2_env?.status ?? "unknown",
|
|
71
|
+
pid: proc.pid ?? null,
|
|
72
|
+
uptime: proc.pm2_env?.pm_uptime ?? null,
|
|
73
|
+
memory: proc.monit?.memory ?? null,
|
|
74
|
+
restarts: proc.pm2_env?.restart_time ?? 0
|
|
75
|
+
};
|
|
76
|
+
} catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function pm2Logs(config, lines = 50) {
|
|
81
|
+
try {
|
|
82
|
+
return pm2Exec(`logs ${config.processName} --nostream --lines ${lines}`);
|
|
83
|
+
} catch {
|
|
84
|
+
return "No logs available.";
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function resolvePackageScript(importMetaUrl, binName) {
|
|
88
|
+
const currentFile = fileURLToPath(importMetaUrl);
|
|
89
|
+
const packageRoot = dirname(dirname(currentFile));
|
|
90
|
+
return join(packageRoot, "bin", `${binName}.js`);
|
|
91
|
+
}
|
|
92
|
+
export {
|
|
93
|
+
resolvePackageScript,
|
|
94
|
+
pm2Stop,
|
|
95
|
+
pm2Status,
|
|
96
|
+
pm2Start,
|
|
97
|
+
pm2Restart,
|
|
98
|
+
pm2Logs,
|
|
99
|
+
pm2Delete
|
|
100
|
+
};
|
package/dist/pm2.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PM2 programmatic wrapper for managing Locus package processes.
|
|
3
|
+
*
|
|
4
|
+
* Parameterized by Pm2Config so any platform package (telegram, slack, etc.)
|
|
5
|
+
* can use the same PM2 lifecycle management.
|
|
6
|
+
*/
|
|
7
|
+
export interface Pm2Config {
|
|
8
|
+
/** PM2 process name (e.g. "locus-telegram"). */
|
|
9
|
+
processName: string;
|
|
10
|
+
/** Absolute path to the script to run, or an import.meta.url to resolve from. */
|
|
11
|
+
scriptPath: string;
|
|
12
|
+
/** Extra arguments passed after `--` to the script (e.g. ["bot"]). */
|
|
13
|
+
scriptArgs?: string[];
|
|
14
|
+
}
|
|
15
|
+
export interface Pm2Status {
|
|
16
|
+
name: string;
|
|
17
|
+
status: string;
|
|
18
|
+
pid: number | null;
|
|
19
|
+
uptime: number | null;
|
|
20
|
+
memory: number | null;
|
|
21
|
+
restarts: number;
|
|
22
|
+
}
|
|
23
|
+
export declare function pm2Start(config: Pm2Config): string;
|
|
24
|
+
export declare function pm2Stop(config: Pm2Config): string;
|
|
25
|
+
export declare function pm2Restart(config: Pm2Config): string;
|
|
26
|
+
export declare function pm2Delete(config: Pm2Config): string;
|
|
27
|
+
export declare function pm2Status(config: Pm2Config): Pm2Status | null;
|
|
28
|
+
export declare function pm2Logs(config: Pm2Config, lines?: number): string;
|
|
29
|
+
/**
|
|
30
|
+
* Resolve the script path for a Locus package binary.
|
|
31
|
+
*
|
|
32
|
+
* Given an `import.meta.url` from the calling package, resolves to
|
|
33
|
+
* `<packageRoot>/bin/<binName>.js`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function resolvePackageScript(importMetaUrl: string, binName: string): string;
|
|
36
|
+
//# sourceMappingURL=pm2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pm2.d.ts","sourceRoot":"","sources":["../src/pm2.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH,MAAM,WAAW,SAAS;IACxB,gDAAgD;IAChD,WAAW,EAAE,MAAM,CAAC;IACpB,iFAAiF;IACjF,UAAU,EAAE,MAAM,CAAC;IACnB,sEAAsE;IACtE,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;CACvB;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAoCD,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CA4BlD;AAED,wBAAgB,OAAO,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAGjD;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAGpD;AAED,wBAAgB,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAGnD;AAED,wBAAgB,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,GAAG,IAAI,CA4B7D;AAED,wBAAgB,OAAO,CAAC,MAAM,EAAE,SAAS,EAAE,KAAK,SAAK,GAAG,MAAM,CAM7D;AAID;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM,GACd,MAAM,CAIR"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@locusai/locus-pm2",
|
|
3
|
+
"version": "0.22.13",
|
|
4
|
+
"description": "Unified PM2 process management for Locus platform packages",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "bun build ./src/index.ts --outdir ./dist --target node --format esm && bun build ./src/index.ts --outfile ./dist/index.cjs --target node --format cjs && tsc -p tsconfig.build.json --emitDeclarationOnly",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"lint": "biome lint .",
|
|
28
|
+
"format": "biome format --write .",
|
|
29
|
+
"clean": "rm -rf dist node_modules"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"pm2": "^6.0.5"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"typescript": "^5.8.3"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"locusai-package",
|
|
39
|
+
"locus",
|
|
40
|
+
"pm2"
|
|
41
|
+
],
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=18"
|
|
44
|
+
},
|
|
45
|
+
"license": "MIT"
|
|
46
|
+
}
|