backtrace-console 0.0.2 → 0.0.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # backtrace-console
2
2
 
3
- CLI for querying Backtrace fingerprints, collecting artifacts, and generating repair plans.
3
+ Server manager for [backtrace-console](https://github.com/your-org/backtrace-console) start, stop, and manage the Express server process from the command line.
4
4
 
5
5
  ## Install
6
6
 
@@ -8,15 +8,32 @@ CLI for querying Backtrace fingerprints, collecting artifacts, and generating re
8
8
  npm install -g backtrace-console
9
9
  ```
10
10
 
11
- ## Commands
11
+ ## Usage
12
+
13
+ Run all commands from the **backtrace-console project root directory**.
12
14
 
13
15
  ```bash
14
- backtrace list
15
- backtrace fingerprint --fingerprint <value>
16
- backtrace collect --fingerprint <value>
17
- backtrace summarize-errors --fingerprint <value>
18
- backtrace fix-plan --fingerprint <value>
19
- backtrace apply-fix --fingerprint <value>
16
+ # 前台启动(Ctrl+C 停止)
17
+ backtrace-server run
18
+
19
+ # 后台守护进程启动
20
+ backtrace-server start
21
+
22
+ # 停止后台进程
23
+ backtrace-server stop
24
+
25
+ # 查看运行状态
26
+ backtrace-server status
20
27
  ```
21
28
 
22
- Run `backtrace --help` for the full option list.
29
+ ## How it works
30
+
31
+ - `run` — loads `.env` in the current directory and starts `bin/www` in the foreground.
32
+ - `start` — spawns `bin/www` as a detached background process. PID is saved to `.backtrace-server.pid` and logs are appended to `.backtrace-server.log`.
33
+ - `stop` — reads `.backtrace-server.pid` and sends `SIGTERM` to the process.
34
+ - `status` — checks whether the recorded PID is still alive.
35
+
36
+ ## Requirements
37
+
38
+ - Node.js 18+
39
+ - Must be run inside a `backtrace-console` project directory (i.e. `bin/www` must exist).
@@ -2,66 +2,80 @@
2
2
 
3
3
  'use strict';
4
4
 
5
- require('dotenv').config();
6
-
7
5
  var path = require('node:path');
8
6
  var fs = require('node:fs');
9
7
  var { spawn } = require('node:child_process');
10
8
 
11
- var ROOT = path.resolve(__dirname, '..');
12
- var PID_FILE = path.join(ROOT, '.backtrace-server.pid');
13
- var LOG_FILE = path.join(ROOT, '.backtrace-server.log');
14
- var WWW = path.join(__dirname, 'www');
9
+ // 包自带的 bin/www,__dirname 指向全局安装目录下的 bin/
10
+ var PKG_ROOT = path.resolve(__dirname, '..');
11
+ var WWW = path.join(PKG_ROOT, 'bin', 'www');
12
+
13
+ // PID/LOG 放在用户工作目录,方便多项目隔离
14
+ var CWD = process.cwd();
15
+ var PID_FILE = path.join(CWD, '.backtrace-server.pid');
16
+ var LOG_FILE = path.join(CWD, '.backtrace-server.log');
15
17
 
16
18
  var cmd = process.argv[2];
17
19
 
18
20
  if (cmd === 'run') {
19
- // 前台运行,直接 require www(同进程)
20
- require('./www');
21
+ try { require('dotenv').config({ path: path.join(CWD, '.env') }); } catch (_) {}
22
+ require(WWW);
21
23
  return;
22
24
  }
23
25
 
24
26
  if (cmd === 'start') {
25
- // 检查是否已在运行
26
27
  if (fs.existsSync(PID_FILE)) {
27
28
  var existingPid = parseInt(fs.readFileSync(PID_FILE, 'utf8').trim(), 10);
28
29
  if (existingPid && isRunning(existingPid)) {
29
- console.log('[backtrace] server already running, pid=' + existingPid);
30
+ console.log('[backtrace-server] already running, pid=' + existingPid);
30
31
  process.exit(0);
31
32
  }
32
33
  fs.unlinkSync(PID_FILE);
33
34
  }
34
35
 
36
+ // 把当前目录的 .env 传给子进程
37
+ var env = Object.assign({}, process.env);
38
+ try {
39
+ var dotenvPath = path.join(CWD, '.env');
40
+ if (fs.existsSync(dotenvPath)) {
41
+ var lines = fs.readFileSync(dotenvPath, 'utf8').split('\n');
42
+ lines.forEach(function(line) {
43
+ var m = line.match(/^\s*([^#=\s][^=]*?)\s*=\s*(.*?)\s*$/);
44
+ if (m && !env[m[1]]) env[m[1]] = m[2].replace(/^['"]|['"]$/g, '');
45
+ });
46
+ }
47
+ } catch (_) {}
48
+
35
49
  var logFd = fs.openSync(LOG_FILE, 'a');
36
50
  var child = spawn(process.execPath, [WWW], {
37
51
  detached: true,
38
52
  stdio: ['ignore', logFd, logFd],
39
- env: process.env,
40
- cwd: ROOT,
53
+ env: env,
54
+ cwd: CWD,
41
55
  });
42
56
  child.unref();
43
57
  fs.writeFileSync(PID_FILE, String(child.pid), 'utf8');
44
- console.log('[backtrace] server started, pid=' + child.pid + ', log=' + LOG_FILE);
58
+ console.log('[backtrace-server] started, pid=' + child.pid + ', log=' + LOG_FILE);
45
59
  process.exit(0);
46
60
  }
47
61
 
48
62
  if (cmd === 'stop') {
49
63
  if (!fs.existsSync(PID_FILE)) {
50
- console.log('[backtrace] server is not running (no pid file)');
64
+ console.log('[backtrace-server] not running (no pid file)');
51
65
  process.exit(0);
52
66
  }
53
67
  var pid = parseInt(fs.readFileSync(PID_FILE, 'utf8').trim(), 10);
54
68
  if (!pid || !isRunning(pid)) {
55
- console.log('[backtrace] server is not running (pid=' + pid + ')');
69
+ console.log('[backtrace-server] not running (pid=' + pid + ')');
56
70
  fs.unlinkSync(PID_FILE);
57
71
  process.exit(0);
58
72
  }
59
73
  try {
60
74
  process.kill(pid, 'SIGTERM');
61
75
  fs.unlinkSync(PID_FILE);
62
- console.log('[backtrace] server stopped, pid=' + pid);
76
+ console.log('[backtrace-server] stopped, pid=' + pid);
63
77
  } catch (err) {
64
- console.error('[backtrace] failed to stop server:', err.message);
78
+ console.error('[backtrace-server] failed to stop:', err.message);
65
79
  process.exit(1);
66
80
  }
67
81
  process.exit(0);
@@ -69,14 +83,14 @@ if (cmd === 'stop') {
69
83
 
70
84
  if (cmd === 'status') {
71
85
  if (!fs.existsSync(PID_FILE)) {
72
- console.log('[backtrace] server is not running');
86
+ console.log('[backtrace-server] not running');
73
87
  process.exit(0);
74
88
  }
75
89
  var statusPid = parseInt(fs.readFileSync(PID_FILE, 'utf8').trim(), 10);
76
90
  if (statusPid && isRunning(statusPid)) {
77
- console.log('[backtrace] server is running, pid=' + statusPid);
91
+ console.log('[backtrace-server] running, pid=' + statusPid);
78
92
  } else {
79
- console.log('[backtrace] server is not running (stale pid=' + statusPid + ')');
93
+ console.log('[backtrace-server] not running (stale pid=' + statusPid + ')');
80
94
  fs.unlinkSync(PID_FILE);
81
95
  }
82
96
  process.exit(0);
@@ -84,7 +98,7 @@ if (cmd === 'status') {
84
98
 
85
99
  console.log([
86
100
  'Usage:',
87
- ' backtrace-server run 前台启动服务器',
101
+ ' backtrace-server run 前台启动(Ctrl+C 停止)',
88
102
  ' backtrace-server start 后台守护进程启动',
89
103
  ' backtrace-server stop 停止后台进程',
90
104
  ' backtrace-server status 查看运行状态',
@@ -92,10 +106,5 @@ console.log([
92
106
  process.exit(1);
93
107
 
94
108
  function isRunning(pid) {
95
- try {
96
- process.kill(pid, 0);
97
- return true;
98
- } catch (_) {
99
- return false;
100
- }
109
+ try { process.kill(pid, 0); return true; } catch (_) { return false; }
101
110
  }
package/package.json CHANGED
@@ -1,22 +1,21 @@
1
1
  {
2
2
  "name": "backtrace-console",
3
- "version": "0.0.2",
4
- "description": "CLI for querying Backtrace fingerprints, collecting artifacts, and generating repair plans.",
3
+ "version": "0.0.4",
4
+ "description": "CLI server manager for backtrace-console run/start/stop the Express server.",
5
5
  "private": false,
6
6
  "files": [
7
+ "bin/backtrace-server.js",
8
+ "bin/www",
7
9
  "app.js",
8
- "bin",
9
10
  "lib",
10
11
  "public",
11
12
  "routes"
12
13
  ],
13
14
  "bin": {
14
- "backtrace": "bin/backtrace-cli.js",
15
15
  "backtrace-server": "bin/backtrace-server.js"
16
16
  },
17
17
  "scripts": {
18
18
  "start": "node ./bin/www",
19
- "backtrace": "node ./bin/backtrace-cli.js",
20
19
  "server:run": "node ./bin/backtrace-server.js run",
21
20
  "server:start": "node ./bin/backtrace-server.js start",
22
21
  "server:stop": "node ./bin/backtrace-server.js stop",
@@ -27,7 +26,7 @@
27
26
  "cookie-parser": "~1.4.4",
28
27
  "debug": "~2.6.9",
29
28
  "dotenv": "^17.4.1",
30
- "express": "~4.16.1",
29
+ "express": "~4.21.0",
31
30
  "morgan": "~1.9.1",
32
31
  "node-cron": "^4.2.1",
33
32
  "undici": "^7.24.8"
@@ -1,22 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- require("dotenv").config();
4
-
5
- const { parseCliArgs, printCliUsage } = require("../lib/cli/args");
6
- const { runCliCommand } = require("../lib/cli/run");
7
-
8
- async function main() {
9
- const parsed = parseCliArgs(process.argv.slice(2));
10
- if (parsed.help) {
11
- printCliUsage();
12
- return;
13
- }
14
- await runCliCommand(parsed.options);
15
- process.exit(0);
16
- }
17
-
18
- main().catch((error) => {
19
- console.error("backtrace-cli failed:");
20
- console.error(error instanceof Error ? error.stack || error.message : error);
21
- process.exit(1);
22
- });