polycopy 0.0.1 → 0.0.3

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/cli.js CHANGED
@@ -3,6 +3,7 @@
3
3
  const child_process = require("child_process");
4
4
  const fs = require("fs");
5
5
  const path = require("path");
6
+ const paths = require("./paths-C5xeA_8E.js");
6
7
  function _interopNamespace(e) {
7
8
  if (e && e.__esModule) return e;
8
9
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
@@ -22,34 +23,31 @@ function _interopNamespace(e) {
22
23
  }
23
24
  const fs__namespace = /* @__PURE__ */ _interopNamespace(fs);
24
25
  const path__namespace = /* @__PURE__ */ _interopNamespace(path);
25
- const PID_DIR = path__namespace.join(process.env.HOME || process.cwd(), ".polycopy");
26
- const PID_FILE = path__namespace.join(PID_DIR, "polycopy.pid");
27
- const LOGS_DIR = path__namespace.join(process.cwd(), "logs");
28
26
  function ensurePidDir() {
29
- if (!fs__namespace.existsSync(PID_DIR)) {
30
- fs__namespace.mkdirSync(PID_DIR, { recursive: true });
27
+ if (!fs__namespace.existsSync(paths.PID_DIR)) {
28
+ fs__namespace.mkdirSync(paths.PID_DIR, { recursive: true });
31
29
  }
32
30
  }
33
31
  function getRunningPid() {
34
- if (!fs__namespace.existsSync(PID_FILE)) {
32
+ if (!fs__namespace.existsSync(paths.PID_FILE)) {
35
33
  return null;
36
34
  }
37
35
  try {
38
- const pid = parseInt(fs__namespace.readFileSync(PID_FILE, "utf-8").trim());
36
+ const pid = parseInt(fs__namespace.readFileSync(paths.PID_FILE, "utf-8").trim());
39
37
  process.kill(pid, 0);
40
38
  return pid;
41
39
  } catch {
42
- fs__namespace.unlinkSync(PID_FILE);
40
+ fs__namespace.unlinkSync(paths.PID_FILE);
43
41
  return null;
44
42
  }
45
43
  }
46
44
  function savePid(pid) {
47
45
  ensurePidDir();
48
- fs__namespace.writeFileSync(PID_FILE, pid.toString());
46
+ fs__namespace.writeFileSync(paths.PID_FILE, pid.toString());
49
47
  }
50
48
  function removePid() {
51
- if (fs__namespace.existsSync(PID_FILE)) {
52
- fs__namespace.unlinkSync(PID_FILE);
49
+ if (fs__namespace.existsSync(paths.PID_FILE)) {
50
+ fs__namespace.unlinkSync(paths.PID_FILE);
53
51
  }
54
52
  }
55
53
  function showHelp() {
@@ -76,13 +74,8 @@ PolyMarket 跟单机器人
76
74
  `);
77
75
  }
78
76
  function showVersion() {
79
- const packagePath = path__namespace.join(__dirname, "..", "package.json");
80
- try {
81
- const pkg = JSON.parse(fs__namespace.readFileSync(packagePath, "utf-8"));
82
- console.log(`polycopy v${pkg.version}`);
83
- } catch {
84
- console.log("polycopy v1.0.0");
85
- }
77
+ const pkg = require("../package.json");
78
+ console.log(`polycopy v${pkg.version}`);
86
79
  }
87
80
  async function startForeground() {
88
81
  const runningPid = getRunningPid();
@@ -107,31 +100,16 @@ async function startForeground() {
107
100
  const mainPath = path__namespace.join(__dirname, "index.js");
108
101
  await import(mainPath);
109
102
  }
110
- function startDaemon() {
103
+ async function startDaemon() {
111
104
  const runningPid = getRunningPid();
112
105
  if (runningPid) {
113
106
  console.log(`⚠️ 已有进程运行中 (PID: ${runningPid})`);
114
107
  console.log(` 使用 'polycopy stop' 停止后再启动`);
115
108
  process.exit(1);
116
109
  }
117
- ensurePidDir();
110
+ process.env.POLYCOPY_DAEMON = "1";
118
111
  const mainPath = path__namespace.join(__dirname, "index.js");
119
- const child = child_process.spawn(process.execPath, [mainPath], {
120
- detached: true,
121
- stdio: "ignore",
122
- env: { ...process.env, POLYCOPY_DAEMON: "1" }
123
- });
124
- if (child.pid) {
125
- savePid(child.pid);
126
- console.log(`✅ 已在后台启动 (PID: ${child.pid})`);
127
- console.log(` 日志目录: ${LOGS_DIR}`);
128
- console.log(` 使用 'polycopy stop' 停止`);
129
- child.unref();
130
- process.exit(0);
131
- } else {
132
- console.error("❌ 启动失败");
133
- process.exit(1);
134
- }
112
+ await import(mainPath);
135
113
  }
136
114
  function stopDaemon() {
137
115
  const pid = getRunningPid();
@@ -169,11 +147,11 @@ async function runConfig() {
169
147
  await import(configPath);
170
148
  }
171
149
  function getLatestLogFile() {
172
- if (!fs__namespace.existsSync(LOGS_DIR)) {
150
+ if (!fs__namespace.existsSync(paths.LOGS_DIR)) {
173
151
  return null;
174
152
  }
175
- const files = fs__namespace.readdirSync(LOGS_DIR).filter((f) => f.endsWith(".log")).sort().reverse();
176
- return files.length > 0 ? path__namespace.join(LOGS_DIR, files[0]) : null;
153
+ const files = fs__namespace.readdirSync(paths.LOGS_DIR).filter((f) => f.endsWith(".log")).sort().reverse();
154
+ return files.length > 0 ? path__namespace.join(paths.LOGS_DIR, files[0]) : null;
177
155
  }
178
156
  function showLogs() {
179
157
  const runningPid = getRunningPid();
@@ -182,7 +160,7 @@ function showLogs() {
182
160
  console.log(" 使用 'polycopy -d' 启动后台进程");
183
161
  return;
184
162
  }
185
- if (!fs__namespace.existsSync(LOGS_DIR)) {
163
+ if (!fs__namespace.existsSync(paths.LOGS_DIR)) {
186
164
  console.log("ℹ️ 日志目录不存在");
187
165
  return;
188
166
  }
@@ -199,7 +177,7 @@ function showLogs() {
199
177
  let tail = child_process.spawn("tail", ["-f", "-n", "100", currentLogFile], {
200
178
  stdio: "inherit"
201
179
  });
202
- const watcher = fs__namespace.watch(LOGS_DIR, (eventType, filename) => {
180
+ const watcher = fs__namespace.watch(paths.LOGS_DIR, (eventType, filename) => {
203
181
  if (eventType === "rename" && filename?.endsWith(".log")) {
204
182
  const newLogFile = getLatestLogFile();
205
183
  if (newLogFile && newLogFile !== currentLogFile) {
@@ -252,7 +230,7 @@ async function main() {
252
230
  break;
253
231
  case "-d":
254
232
  case "--daemon":
255
- startDaemon();
233
+ await startDaemon();
256
234
  break;
257
235
  case void 0:
258
236
  case "start":
package/dist/config.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const init = require("./init-C8vHJzZn.js");
2
+ const init = require("./init-C2C8sTcQ.js");
3
3
  async function main() {
4
4
  try {
5
5
  await init.runConfigWizard();
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const init = require("./init-C8vHJzZn.js");
2
+ const init = require("./init-C2C8sTcQ.js");
3
3
  const grammy = require("grammy");
4
4
  const wallet = require("@ethersproject/wallet");
5
5
  const clobClient = require("@polymarket/clob-client");
@@ -9,8 +9,29 @@ const accounts = require("viem/accounts");
9
9
  const chains = require("viem/chains");
10
10
  const builderRelayerClient = require("@polymarket/builder-relayer-client");
11
11
  const setPromiseInterval = require("set-promise-interval");
12
+ const child_process = require("child_process");
13
+ const fs = require("fs");
14
+ const paths = require("./paths-C5xeA_8E.js");
12
15
  const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
16
+ function _interopNamespace(e) {
17
+ if (e && e.__esModule) return e;
18
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
19
+ if (e) {
20
+ for (const k in e) {
21
+ if (k !== "default") {
22
+ const d = Object.getOwnPropertyDescriptor(e, k);
23
+ Object.defineProperty(n, k, d.get ? d : {
24
+ enumerable: true,
25
+ get: () => e[k]
26
+ });
27
+ }
28
+ }
29
+ }
30
+ n.default = e;
31
+ return Object.freeze(n);
32
+ }
13
33
  const setPromiseInterval__default = /* @__PURE__ */ _interopDefault(setPromiseInterval);
34
+ const fs__namespace = /* @__PURE__ */ _interopNamespace(fs);
14
35
  class Notifier {
15
36
  bot = null;
16
37
  adminChatId = null;
@@ -959,6 +980,29 @@ class Trader {
959
980
  }
960
981
  }
961
982
  const trader = new Trader();
983
+ function enterDaemonMode() {
984
+ if (!fs__namespace.existsSync(paths.PID_DIR)) {
985
+ fs__namespace.mkdirSync(paths.PID_DIR, { recursive: true });
986
+ }
987
+ const child = child_process.spawn(process.execPath, [__filename], {
988
+ detached: true,
989
+ stdio: "ignore",
990
+ env: { ...process.env, POLYCOPY_DAEMON: void 0 }
991
+ });
992
+ if (child.pid) {
993
+ fs__namespace.writeFileSync(paths.PID_FILE, child.pid.toString());
994
+ init.logger.blank();
995
+ init.logger.success(`已进入后台模式 (PID: ${child.pid})`);
996
+ init.logger.line("", `日志目录: ${paths.LOGS_DIR}`);
997
+ init.logger.line("", `使用 'polycopy stop' 停止`);
998
+ init.logger.line("", `使用 'polycopy log' 查看日志`);
999
+ child.unref();
1000
+ process.exit(0);
1001
+ } else {
1002
+ init.logger.error("进入后台模式失败");
1003
+ process.exit(1);
1004
+ }
1005
+ }
962
1006
  async function main() {
963
1007
  init.logger.init();
964
1008
  init.logger.section("PolyMarket 跟单机器人");
@@ -1036,6 +1080,10 @@ async function main() {
1036
1080
  };
1037
1081
  process.on("SIGINT", shutdown);
1038
1082
  process.on("SIGTERM", shutdown);
1083
+ if (process.env.POLYCOPY_DAEMON === "1") {
1084
+ enterDaemonMode();
1085
+ return;
1086
+ }
1039
1087
  await listener.start(() => {
1040
1088
  if (trader.isInitialized()) {
1041
1089
  trader.startRedeemer();
@@ -2,6 +2,7 @@
2
2
  const readline = require("readline");
3
3
  const fsExtra = require("fs-extra");
4
4
  const path = require("path");
5
+ const paths = require("./paths-C5xeA_8E.js");
5
6
  const grammy = require("grammy");
6
7
  const winston = require("winston");
7
8
  const DailyRotateFile = require("winston-daily-rotate-file");
@@ -32,7 +33,7 @@ class LocalStorage {
32
33
  dbPath;
33
34
  data;
34
35
  constructor(dbFileName) {
35
- this.dbPath = path__default.default.join(process.cwd(), `localstorage/${dbFileName}.json`);
36
+ this.dbPath = path__default.default.join(paths.LOCALSTORAGE_DIR, `${dbFileName}.json`);
36
37
  if (fsExtra__default.default.existsSync(this.dbPath)) {
37
38
  this.data = fsExtra__default.default.readJSONSync(this.dbPath);
38
39
  } else {
@@ -69,7 +70,6 @@ function hasTradingConfig(config) {
69
70
  function canAutoTrade(config) {
70
71
  return hasPolyMarketConfig(config) && hasTradingConfig(config);
71
72
  }
72
- const LOG_DIR = path__default.default.resolve(process.cwd(), "logs");
73
73
  const formatTimestamp = () => {
74
74
  const now = /* @__PURE__ */ new Date();
75
75
  const pad = (n) => n.toString().padStart(2, "0");
@@ -79,7 +79,7 @@ const customFormat = winston__default.default.format.printf(({ message }) => {
79
79
  return `[${formatTimestamp()}] ${message}`;
80
80
  });
81
81
  const fileTransport = new DailyRotateFile__default.default({
82
- dirname: LOG_DIR,
82
+ dirname: paths.LOGS_DIR,
83
83
  filename: "%DATE%",
84
84
  extension: ".log",
85
85
  datePattern: "YYYY-MM-DD",
@@ -105,7 +105,7 @@ class Logger {
105
105
  if (this.initialized) return;
106
106
  this.initialized = true;
107
107
  console.log(`
108
- 📁 日志目录: ${LOG_DIR}`);
108
+ 📁 日志目录: ${paths.LOGS_DIR}`);
109
109
  }
110
110
  /**
111
111
  * 获取带时间前缀的消息(用于控制台输出)
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ const path = require("path");
3
+ const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
4
+ const path__default = /* @__PURE__ */ _interopDefault(path);
5
+ const PROJECT_ROOT = path__default.default.join(__dirname, "..", "..");
6
+ const LOGS_DIR = path__default.default.join(PROJECT_ROOT, "logs");
7
+ const LOCALSTORAGE_DIR = path__default.default.join(PROJECT_ROOT, "localstorage");
8
+ const PID_DIR = path__default.default.join(process.env.HOME || PROJECT_ROOT, ".polycopy");
9
+ const PID_FILE = path__default.default.join(PID_DIR, "polycopy.pid");
10
+ exports.LOCALSTORAGE_DIR = LOCALSTORAGE_DIR;
11
+ exports.LOGS_DIR = LOGS_DIR;
12
+ exports.PID_DIR = PID_DIR;
13
+ exports.PID_FILE = PID_FILE;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polycopy",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "polycopy test",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -37,5 +37,6 @@
37
37
  },
38
38
  "files": [
39
39
  "dist"
40
- ]
40
+ ],
41
+ "license": "MIT"
41
42
  }