pine2profit 0.0.0-stage → 0.1.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 +243 -2
- package/build/.gitkeep +0 -0
- package/build/index.cjs +1152 -0
- package/build/index.mjs +1142 -0
- package/build/modules/frontend/build/.nojekyll +0 -0
- package/build/modules/frontend/build/404/index.html +1 -0
- package/build/modules/frontend/build/404.html +1 -0
- package/build/modules/frontend/build/_next/static/chunks/11.78c5bec5897e1148.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/195.64632be71175c993.js +63 -0
- package/build/modules/frontend/build/_next/static/chunks/204.008bcbcf48844ee2.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/255-da04dc9178303a31.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/2738385b.306f7750ad5b8be4.js +372 -0
- package/build/modules/frontend/build/_next/static/chunks/338-31711140cb6d61e5.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/4bd1b696-c023c6e3521b1417.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/50e49f97.75feacc2b3cc7709.js +433 -0
- package/build/modules/frontend/build/_next/static/chunks/554.dd4ac9a0aff9b782.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/570-33c81efca66c4bc9.js +3 -0
- package/build/modules/frontend/build/_next/static/chunks/5957d0c3.7b96612772854e2d.js +2 -0
- package/build/modules/frontend/build/_next/static/chunks/619-f09bc47e24909021.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/7589b812.60f2b16513f3aa42.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/771.5c62ba94f930669e.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/796.4b04b2b64c066b03.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/897-b727cc7229802490.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/app/_not-found/page-b6d9300903c2ab41.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/app/layout-0da8bd54f47c9e1b.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/app/page-7ab911a70ef48cfe.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/app/run/page-42836fc705c46a59.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/c525c9ef.018edb9552a70fc2.js +143 -0
- package/build/modules/frontend/build/_next/static/chunks/e1865c21-8d63eb0f5b25cb0c.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/framework-28b60818f5cffd83.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/main-45ecedd1f28e3a74.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/main-app-4ff0bbe1b367fcb5.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/pages/_app-7d307437aca18ad4.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/pages/_error-cb2a52f75f2162e2.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/polyfills-42372ed130431b0a.js +1 -0
- package/build/modules/frontend/build/_next/static/chunks/webpack-bb5ccf34ea47f1d7.js +1 -0
- package/build/modules/frontend/build/_next/static/css/91363587ff8467b5.css +1 -0
- package/build/modules/frontend/build/_next/static/media/29b98dbfba401fa9-s.p.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/313510e2713fb214-s.p.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/558ca1a6aa3cb55e-s.p.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/64d784ea54a4acde-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/6d831b18ae5b01dc-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/8048a25e80cc4179-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/851c4691abdf3def-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/87bc17f7c8b98e48-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/982ceffe7b733b3b-s.p.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/a6c19694cd327cd4-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/a8a8dd7277bab4cf-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/ac0e76ddaeeb7981-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/b8bfb68ab9ab617c-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/d76fe899475ae4d7-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/e546cf32c9d4702c-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/edc640959b0c7826-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/media/ff71da380fbe67dd-s.woff2 +0 -0
- package/build/modules/frontend/build/_next/static/vHsenj52Jy0T31oXW8dJX/_buildManifest.js +1 -0
- package/build/modules/frontend/build/_next/static/vHsenj52Jy0T31oXW8dJX/_ssgManifest.js +1 -0
- package/build/modules/frontend/build/icon.svg +1 -0
- package/build/modules/frontend/build/index.html +1 -0
- package/build/modules/frontend/build/index.txt +23 -0
- package/build/modules/frontend/build/run/index.html +1 -0
- package/build/modules/frontend/build/run/index.txt +23 -0
- package/package.json +93 -4
- package/types.d.ts +124 -0
package/build/index.mjs
ADDED
|
@@ -0,0 +1,1142 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { singleshot, BehaviorSubject, errorData, getErrorMessage } from 'functools-kit';
|
|
3
|
+
import { parseArgs } from 'util';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import { realpathSync, createReadStream } from 'fs';
|
|
6
|
+
import path, { extname, dirname, join } from 'path';
|
|
7
|
+
import http from 'http';
|
|
8
|
+
import micro from 'micro';
|
|
9
|
+
import Router from 'router';
|
|
10
|
+
import finalhandler from 'finalhandler';
|
|
11
|
+
import serveHandler from 'serve-handler';
|
|
12
|
+
import mime from 'mime-types';
|
|
13
|
+
import { realpath, open, stat, readdir, readFile } from 'fs/promises';
|
|
14
|
+
import { createActivator } from 'di-kit';
|
|
15
|
+
import { createInterface } from 'readline';
|
|
16
|
+
import { createRequire } from 'module';
|
|
17
|
+
|
|
18
|
+
const getArgs = singleshot(() => {
|
|
19
|
+
const { values, positionals } = parseArgs({
|
|
20
|
+
args: process.argv.slice(2),
|
|
21
|
+
options: {
|
|
22
|
+
port: {
|
|
23
|
+
type: "string",
|
|
24
|
+
default: "",
|
|
25
|
+
},
|
|
26
|
+
host: {
|
|
27
|
+
type: "string",
|
|
28
|
+
default: "",
|
|
29
|
+
},
|
|
30
|
+
check: {
|
|
31
|
+
type: "boolean",
|
|
32
|
+
default: false,
|
|
33
|
+
},
|
|
34
|
+
help: {
|
|
35
|
+
type: "boolean",
|
|
36
|
+
default: false,
|
|
37
|
+
},
|
|
38
|
+
version: {
|
|
39
|
+
type: "boolean",
|
|
40
|
+
default: false,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
strict: false,
|
|
44
|
+
allowPositionals: true,
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
values,
|
|
48
|
+
positionals,
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/** True when this bundle is the script node was started with (bin / `node build/index.mjs`), not an import. */
|
|
53
|
+
const getEntry = (metaUrl) => {
|
|
54
|
+
if (!process.argv[1]) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const metaPath = fileURLToPath(metaUrl);
|
|
59
|
+
const realArgv = realpathSync(process.argv[1]);
|
|
60
|
+
return path.resolve(realArgv) === path.resolve(metaPath);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const HELP_TEXT = `
|
|
68
|
+
Usage:
|
|
69
|
+
npx pine2profit <dir> [flags]
|
|
70
|
+
|
|
71
|
+
<dir> Folder that contains data/ (required)
|
|
72
|
+
|
|
73
|
+
Flags:
|
|
74
|
+
|
|
75
|
+
--port <number> HTTP port (default: CC_WWWROOT_PORT or 60050)
|
|
76
|
+
--host <string> Bind address (default: CC_WWWROOT_HOST or 0.0.0.0)
|
|
77
|
+
--check Validate <dir>/data and exit: 0 when there are no errors, 1 otherwise.
|
|
78
|
+
The server is not started. The same check runs before every start.
|
|
79
|
+
--help Print this help message
|
|
80
|
+
--version Print the version
|
|
81
|
+
|
|
82
|
+
What the server does:
|
|
83
|
+
|
|
84
|
+
GET / The dashboard bundled with this package (modules/frontend, static export)
|
|
85
|
+
GET /data/<file> <dir>/data/<file> as is, Cache-Control: no-store, 404 when the file is missing
|
|
86
|
+
|
|
87
|
+
Nothing else: no API, no POST. The site on GitHub Pages makes exactly the same requests
|
|
88
|
+
for the same relative paths, so a data folder that works here works there unchanged.
|
|
89
|
+
|
|
90
|
+
The data folder (<dir>/data/), flat, no subfolders:
|
|
91
|
+
|
|
92
|
+
index_1.jsonl, index_2.jsonl, ... The list: one JSON object per line, about 1000 lines per file
|
|
93
|
+
<id>_messages.json The agent's text for one script
|
|
94
|
+
<id>_candles.json Bars, trades and equity for the chart
|
|
95
|
+
<id>_pinescript.json The author's script and the trade rule (optional)
|
|
96
|
+
|
|
97
|
+
<id> is a slug matching [a-z0-9-]+ and every file of that script starts with it.
|
|
98
|
+
|
|
99
|
+
index_N.jsonl (one line per script):
|
|
100
|
+
|
|
101
|
+
The site reads index_1, then index_2, ... and stops at the first 404. A later line with the
|
|
102
|
+
same "id" replaces the earlier one, so append updates to the last file instead of rewriting
|
|
103
|
+
old ones. Required: id, title, kind, verdict, updated. Leave a field out when it is null.
|
|
104
|
+
Dollars are integers on a notional $10,000 account.
|
|
105
|
+
|
|
106
|
+
{
|
|
107
|
+
"id": string, slug, prefix of the other files of this script
|
|
108
|
+
"title": string,
|
|
109
|
+
"kind": "indicator" | "strategy",
|
|
110
|
+
"subcategory": string, optional; any key — the list builds its filter chips from the
|
|
111
|
+
values it sees. Keys with a label on the site:
|
|
112
|
+
indicator: trend oscillator volatility volume structure signals
|
|
113
|
+
strategy: trend-following mean-reversion breakout grid-dca
|
|
114
|
+
pattern-event composite
|
|
115
|
+
"usable": boolean, optional; false = no signal is final at bar close: the page says
|
|
116
|
+
"not tradeable", shows no rule and no money numbers
|
|
117
|
+
"verdict": "YES" | "NO" | "UNKNOWN",
|
|
118
|
+
"main": number, optional; 12 months via alerts, $
|
|
119
|
+
"paper": number, optional; paper journal since cycleStart, $
|
|
120
|
+
"paperTrades": number, optional
|
|
121
|
+
"expected": [number, number], optional; expected per month on $10,000: [low, high]
|
|
122
|
+
"iterations": number, optional (default 1)
|
|
123
|
+
"updated": "YYYY-MM-DD",
|
|
124
|
+
"market": string, optional; "BTCUSDT"
|
|
125
|
+
"timeframe": string, optional; "1h"
|
|
126
|
+
"license": string, optional
|
|
127
|
+
"author": string, optional
|
|
128
|
+
"tvUrl": string, optional; link under the author's name
|
|
129
|
+
"cycleStart": "YYYY-MM-DD", optional; drawn on the chart and on the P&L line
|
|
130
|
+
"percentile": number, optional; the rule's percentile against random entries
|
|
131
|
+
"reason": string optional; one sentence under the verdict
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
Example line (one line in the file, no line breaks inside):
|
|
135
|
+
|
|
136
|
+
{"id":"sma-cross-20-50","title":"SMA Cross 20/50","kind":"strategy","subcategory":"trend-following","verdict":"NO","main":-2465,"paper":461,"paperTrades":3,"expected":[-599,181],"iterations":3,"updated":"2026-11-26","market":"BTCUSDT","timeframe":"1h","license":"MPL-2.0","author":"example_author","tvUrl":"https://www.tradingview.com/script/EXAMPLE02/","cycleStart":"2026-11-12","percentile":59,"reason":"12 months −$2,465; only 4/10 markets positive; not better than random entries (59th percentile)"}
|
|
137
|
+
|
|
138
|
+
A script that is not tradeable:
|
|
139
|
+
|
|
140
|
+
{"id":"volume-pressure","title":"Volume Pressure Delta","kind":"indicator","subcategory":"volume","usable":false,"verdict":"NO","iterations":1,"updated":"2026-11-26","market":"BTCUSDT","timeframe":"1h","reason":"not tradeable: the signal depends on realtime tick data (varip)"}
|
|
141
|
+
|
|
142
|
+
<id>_messages.json:
|
|
143
|
+
|
|
144
|
+
{
|
|
145
|
+
"head": { the same fields as this script's line in index_N.jsonl, same values;
|
|
146
|
+
"id": string, the page reads the header from here when opened by a direct link
|
|
147
|
+
"title": string, /run/?id=<id> without going through the list
|
|
148
|
+
"kind": "indicator" | "strategy",
|
|
149
|
+
"subcategory": string, optional
|
|
150
|
+
"usable": boolean, optional
|
|
151
|
+
"verdict": "YES" | "NO" | "UNKNOWN",
|
|
152
|
+
"main": number, optional
|
|
153
|
+
"paper": number, optional
|
|
154
|
+
"paperTrades": number, optional
|
|
155
|
+
"expected": [number, number], optional
|
|
156
|
+
"iterations": number, optional
|
|
157
|
+
"updated": "YYYY-MM-DD",
|
|
158
|
+
"market": string, optional
|
|
159
|
+
"timeframe": string, optional
|
|
160
|
+
"license": string, optional
|
|
161
|
+
"author": string, optional
|
|
162
|
+
"tvUrl": string, optional
|
|
163
|
+
"cycleStart": "YYYY-MM-DD", optional
|
|
164
|
+
"percentile": number, optional
|
|
165
|
+
"reason": string optional
|
|
166
|
+
},
|
|
167
|
+
"groups": [ { "code": string, "title": string } ], the left navigation, in this order
|
|
168
|
+
"messages": [
|
|
169
|
+
{
|
|
170
|
+
"code": string, optional; "A0", "C7", "E1", "RISK2" — shown on the block and in the navigation
|
|
171
|
+
"group": string, optional; navigation group; default: the leading letters of "code"
|
|
172
|
+
"title": string,
|
|
173
|
+
"md": string, markdown
|
|
174
|
+
"iteration": number, optional; messages with the same value in a row form a dated section
|
|
175
|
+
"date": "YYYY-MM-DD" optional; shown next to the section title
|
|
176
|
+
}
|
|
177
|
+
]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
Messages render in file order, nothing is deduplicated: put in the file what the page should show.
|
|
181
|
+
A message belongs to the group whose code equals its "group", or the leading letters of its "code"
|
|
182
|
+
(A0 -> A, RISK2 -> RISK). A group that appears in messages but not in "groups" shows its code as
|
|
183
|
+
the title; a group without messages is dropped. Inside a group the navigation items are the message
|
|
184
|
+
codes, or the iteration numbers when every message of the group carries "iteration". Messages
|
|
185
|
+
without "iteration" go under "Analysis".
|
|
186
|
+
|
|
187
|
+
Chart references inside "md" become buttons that frame the chart:
|
|
188
|
+
|
|
189
|
+
[[t:2026-11-12T14:00Z]] a bar
|
|
190
|
+
[[t:2026-11-12T14:00Z@41820]] a bar and a price
|
|
191
|
+
[[range:2026-11-01T00:00Z..2026-11-08T00:00Z]] a segment, both edges marked
|
|
192
|
+
[[trade:12]] a trade from <id>_candles.json
|
|
193
|
+
[see the breakout](ref:range:2026-11-01T00:00Z..2026-11-08T00:00Z) the same, as a link
|
|
194
|
+
|
|
195
|
+
Example:
|
|
196
|
+
|
|
197
|
+
{
|
|
198
|
+
"head": {"id":"sma-cross-20-50","title":"SMA Cross 20/50","kind":"strategy","subcategory":"trend-following","verdict":"NO","main":-2465,"paper":461,"paperTrades":3,"expected":[-599,181],"iterations":3,"updated":"2026-11-26","market":"BTCUSDT","timeframe":"1h","cycleStart":"2026-11-12","percentile":59,"reason":"12 months −$2,465; not better than random entries"},
|
|
199
|
+
"groups": [
|
|
200
|
+
{ "code": "A", "title": "What it is" },
|
|
201
|
+
{ "code": "C", "title": "Money" },
|
|
202
|
+
{ "code": "E", "title": "Iterations" }
|
|
203
|
+
],
|
|
204
|
+
"messages": [
|
|
205
|
+
{ "code": "A0", "title": "How to run it", "md": "Run it on **1h**. A typical trade: [[trade:137]]." },
|
|
206
|
+
{ "code": "C2", "title": "The last 12 months", "md": "Window [[range:2025-11-12T00:00Z..2026-11-12T00:00Z]]: 97 trades, -$2,465." },
|
|
207
|
+
{ "code": "E1", "title": "What happened since the previous iteration?", "md": "Three paper trades, +$461.", "iteration": 3, "date": "2026-11-26" },
|
|
208
|
+
{ "code": "E1", "title": "What happened since the previous iteration?", "md": "First week on paper.", "iteration": 2, "date": "2026-11-19" }
|
|
209
|
+
]
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
<id>_candles.json:
|
|
213
|
+
|
|
214
|
+
{
|
|
215
|
+
"timeframe": string, optional; "1h" (falls back to the index line)
|
|
216
|
+
"bars": [ { "time": ms, "open": number, "high": number, "low": number, "close": number, "volume": number } ],
|
|
217
|
+
"trades": [ { "id": number, "side": "long" | "short", "open_time": ms, "open_price": number,
|
|
218
|
+
"close_time": ms | null, "close_price": number | null, "pnl_usd": number } ], optional
|
|
219
|
+
"equity": [ { "time": ms, "backtest_usd": number | null, "paper_usd": number | null } ] optional
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
"bars" feed the chart; "trades" label and frame [[trade:N]]; "equity" draws the P&L line
|
|
223
|
+
(no line without it). No file at all: "no candles for this script", the text still renders.
|
|
224
|
+
|
|
225
|
+
Example:
|
|
226
|
+
|
|
227
|
+
{
|
|
228
|
+
"timeframe": "1h",
|
|
229
|
+
"bars": [
|
|
230
|
+
{ "time": 1732665600000, "open": 62000, "high": 62000, "low": 61498.5, "close": 61498.5, "volume": 377 },
|
|
231
|
+
{ "time": 1732669200000, "open": 61498.5, "high": 61553.9, "low": 61201.0, "close": 61340.2, "volume": 412 }
|
|
232
|
+
],
|
|
233
|
+
"trades": [
|
|
234
|
+
{ "id": 1, "side": "long", "open_time": 1732928400000, "open_price": 61642.5, "close_time": 1732996800000, "close_price": 60510.8, "pnl_usd": -195.58 }
|
|
235
|
+
],
|
|
236
|
+
"equity": [
|
|
237
|
+
{ "time": 1732665600000, "backtest_usd": 0, "paper_usd": null },
|
|
238
|
+
{ "time": 1732996800000, "backtest_usd": -195.58, "paper_usd": null }
|
|
239
|
+
]
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
<id>_pinescript.json (optional):
|
|
243
|
+
|
|
244
|
+
{
|
|
245
|
+
"source": string, the author's script: shown read-only and run on the chart
|
|
246
|
+
"rule": string optional; the agent's trade rule as a Pine strategy() — its fills
|
|
247
|
+
become the trade markers on the chart
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
No file: no code panel. No "rule": the chart runs the author's script only.
|
|
251
|
+
|
|
252
|
+
Example:
|
|
253
|
+
|
|
254
|
+
{
|
|
255
|
+
"source": "//@version=5\\nindicator(\\"EMA Ribbon\\", overlay=true)\\nplot(ta.ema(close, 20))\\nplot(ta.ema(close, 50))",
|
|
256
|
+
"rule": "//@version=5\\nstrategy(\\"EMA Ribbon rule\\", overlay=true, initial_capital=10000)\\nif ta.crossover(ta.ema(close, 20), ta.ema(close, 50))\\n strategy.entry(\\"Long\\", strategy.long)\\nif ta.crossunder(ta.ema(close, 20), ta.ema(close, 50))\\n strategy.close(\\"Long\\")"
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
Startup check:
|
|
260
|
+
|
|
261
|
+
Before listening, the folder is validated. A single error refuses to start (exit code 1);
|
|
262
|
+
warnings are printed and the server starts anyway. \`npx pine2profit <dir> --check\` runs the
|
|
263
|
+
same validation and exits without starting the server, for the agent to call after every
|
|
264
|
+
write. What is checked:
|
|
265
|
+
|
|
266
|
+
data/ exists; index_1.jsonl exists (an empty file is fine); index files are numbered without gaps
|
|
267
|
+
every index line is JSON with the fields above: required ones present, types and values right,
|
|
268
|
+
no unknown fields (a typo such as "tv_url" is reported together with the right name)
|
|
269
|
+
every script in the index has <id>_messages.json and <id>_candles.json; <id>_pinescript.json is optional
|
|
270
|
+
<id>_messages.json: "head" equals the index line, "groups" and "messages" have the right shape,
|
|
271
|
+
every [[t:...]], [[range:...]], [[trade:N]] and (ref:...) points at a real date, range or trade
|
|
272
|
+
<id>_candles.json: "bars" not empty and numeric with growing time and high >= low, trade ids unique
|
|
273
|
+
warnings only: files for ids that are not in the index, files the site never reads, a missing "head"
|
|
274
|
+
|
|
275
|
+
A minimal data folder:
|
|
276
|
+
|
|
277
|
+
<dir>/data/index_1.jsonl
|
|
278
|
+
<dir>/data/sma-cross-20-50_messages.json
|
|
279
|
+
<dir>/data/sma-cross-20-50_candles.json
|
|
280
|
+
<dir>/data/sma-cross-20-50_pinescript.json
|
|
281
|
+
|
|
282
|
+
Environment variables:
|
|
283
|
+
|
|
284
|
+
CC_WWWROOT_HOST Bind address (default: 0.0.0.0)
|
|
285
|
+
CC_WWWROOT_PORT Port (default: 60050)
|
|
286
|
+
CC_DATA_PATH Folder that contains data/ for serve() called from code without a path;
|
|
287
|
+
the CLI always takes <dir> from the command line
|
|
288
|
+
|
|
289
|
+
Examples:
|
|
290
|
+
|
|
291
|
+
npx pine2profit .
|
|
292
|
+
npx pine2profit . --check
|
|
293
|
+
npx pine2profit ./project --port 8080
|
|
294
|
+
npx pine2profit /var/agent/project --host 127.0.0.1 --port 60050
|
|
295
|
+
`.trimStart();
|
|
296
|
+
const main$1 = () => {
|
|
297
|
+
if (!getEntry(import.meta.url)) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const { values } = getArgs();
|
|
301
|
+
if (!values.help) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
process.stdout.write(`pine2profit ${"0.1.0"}\n\n`);
|
|
305
|
+
process.stdout.write(HELP_TEXT);
|
|
306
|
+
process.exit(0);
|
|
307
|
+
};
|
|
308
|
+
main$1();
|
|
309
|
+
|
|
310
|
+
const GLOBAL_CONFIG = {
|
|
311
|
+
CC_WWWROOT_PATH: "",
|
|
312
|
+
CC_WWWROOT_HOST: "",
|
|
313
|
+
CC_WWWROOT_PORT: 0,
|
|
314
|
+
CC_DATA_PATH: "",
|
|
315
|
+
};
|
|
316
|
+
Object.freeze({ ...GLOBAL_CONFIG });
|
|
317
|
+
const getConfig = () => {
|
|
318
|
+
const config = {
|
|
319
|
+
CC_WWWROOT_PATH: process.env.CC_WWWROOT_PATH || "",
|
|
320
|
+
CC_WWWROOT_HOST: process.env.CC_WWWROOT_HOST || "0.0.0.0",
|
|
321
|
+
CC_WWWROOT_PORT: parseInt(process.env.CC_WWWROOT_PORT) || 60050,
|
|
322
|
+
CC_DATA_PATH: process.env.CC_DATA_PATH || "",
|
|
323
|
+
};
|
|
324
|
+
if (GLOBAL_CONFIG.CC_WWWROOT_PATH) {
|
|
325
|
+
config.CC_WWWROOT_PATH = GLOBAL_CONFIG.CC_WWWROOT_PATH;
|
|
326
|
+
}
|
|
327
|
+
if (GLOBAL_CONFIG.CC_WWWROOT_HOST) {
|
|
328
|
+
config.CC_WWWROOT_HOST = GLOBAL_CONFIG.CC_WWWROOT_HOST;
|
|
329
|
+
}
|
|
330
|
+
if (GLOBAL_CONFIG.CC_WWWROOT_PORT) {
|
|
331
|
+
config.CC_WWWROOT_PORT = GLOBAL_CONFIG.CC_WWWROOT_PORT;
|
|
332
|
+
}
|
|
333
|
+
if (GLOBAL_CONFIG.CC_DATA_PATH) {
|
|
334
|
+
config.CC_DATA_PATH = GLOBAL_CONFIG.CC_DATA_PATH;
|
|
335
|
+
}
|
|
336
|
+
return config;
|
|
337
|
+
};
|
|
338
|
+
const setConfig = (config) => {
|
|
339
|
+
Object.assign(GLOBAL_CONFIG, config);
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
/** Project root passed to serve(); data files are served from <root>/data. */
|
|
343
|
+
const serveSubject = new BehaviorSubject();
|
|
344
|
+
|
|
345
|
+
const NOOP_LOGGER = {
|
|
346
|
+
log() {
|
|
347
|
+
},
|
|
348
|
+
debug() {
|
|
349
|
+
},
|
|
350
|
+
info() {
|
|
351
|
+
},
|
|
352
|
+
warn() {
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
/** A failing custom logger is reported once per call on stderr and never escapes into the caller. */
|
|
356
|
+
const guard = (fn) => {
|
|
357
|
+
try {
|
|
358
|
+
fn();
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
console.error("pine2profit logger failed:", error);
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
class LoggerService {
|
|
365
|
+
constructor() {
|
|
366
|
+
this._commonLogger = NOOP_LOGGER;
|
|
367
|
+
this.log = (topic, ...args) => {
|
|
368
|
+
guard(() => this._commonLogger.log(topic, ...args));
|
|
369
|
+
};
|
|
370
|
+
this.debug = (topic, ...args) => {
|
|
371
|
+
guard(() => this._commonLogger.debug(topic, ...args));
|
|
372
|
+
};
|
|
373
|
+
this.info = (topic, ...args) => {
|
|
374
|
+
guard(() => this._commonLogger.info(topic, ...args));
|
|
375
|
+
};
|
|
376
|
+
this.warn = (topic, ...args) => {
|
|
377
|
+
guard(() => this._commonLogger.warn(topic, ...args));
|
|
378
|
+
};
|
|
379
|
+
this.setLogger = (logger) => {
|
|
380
|
+
this._commonLogger = logger;
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const { provide, inject, init, override } = createActivator("pine2profit");
|
|
386
|
+
|
|
387
|
+
const baseServices$1 = {
|
|
388
|
+
loggerService: Symbol("loggerService"),
|
|
389
|
+
};
|
|
390
|
+
const viewServices$1 = {
|
|
391
|
+
dataViewService: Symbol("dataViewService"),
|
|
392
|
+
};
|
|
393
|
+
const validationServices$1 = {
|
|
394
|
+
dataValidationService: Symbol("dataValidationService"),
|
|
395
|
+
};
|
|
396
|
+
const TYPES = {
|
|
397
|
+
...baseServices$1,
|
|
398
|
+
...viewServices$1,
|
|
399
|
+
...validationServices$1,
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const DATA_DIR$1 = "data";
|
|
403
|
+
const isInside = (root, target) => target === root || target.startsWith(root + path.sep);
|
|
404
|
+
/** Drops empty, "." and ".." segments so a request can never climb above the root. */
|
|
405
|
+
const normalizeSegments = (relPath) => relPath.split(/[\\/]+/).filter((segment) => segment && segment !== "." && segment !== "..");
|
|
406
|
+
class DataViewService {
|
|
407
|
+
constructor() {
|
|
408
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
409
|
+
/** <root>/data; serve() resolves root once (its cwd argument, else CC_DATA_PATH, else process.cwd()). */
|
|
410
|
+
this.getRoot = () => {
|
|
411
|
+
return path.resolve(serveSubject.data || process.cwd(), DATA_DIR$1);
|
|
412
|
+
};
|
|
413
|
+
/**
|
|
414
|
+
* Absolute path of a file inside the data root, or null when the request points
|
|
415
|
+
* outside of it: checked on the lexical path and again on the real path, so a
|
|
416
|
+
* symlink inside data/ cannot lead out either.
|
|
417
|
+
*/
|
|
418
|
+
this.resolve = async (relPath) => {
|
|
419
|
+
this.loggerService.log("dataViewService resolve", { relPath });
|
|
420
|
+
const root = this.getRoot();
|
|
421
|
+
const absPath = path.resolve(root, ...normalizeSegments(relPath));
|
|
422
|
+
if (!isInside(root, absPath)) {
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
try {
|
|
426
|
+
const realRoot = await realpath(root);
|
|
427
|
+
const realPath = await realpath(absPath);
|
|
428
|
+
return isInside(realRoot, realPath) ? realPath : null;
|
|
429
|
+
}
|
|
430
|
+
catch {
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
/**
|
|
435
|
+
* Opens a file for streaming. The handle is opened first and the size read from it, so the
|
|
436
|
+
* Content-Length matches the bytes the stream will deliver even if the agent replaces the
|
|
437
|
+
* file (rename) meanwhile; the stream closes the handle when it ends or fails.
|
|
438
|
+
*/
|
|
439
|
+
this.open = async (relPath) => {
|
|
440
|
+
this.loggerService.log("dataViewService open", { relPath });
|
|
441
|
+
const filePath = await this.resolve(relPath);
|
|
442
|
+
if (!filePath) {
|
|
443
|
+
return null;
|
|
444
|
+
}
|
|
445
|
+
let handle;
|
|
446
|
+
try {
|
|
447
|
+
handle = await open(filePath, "r");
|
|
448
|
+
}
|
|
449
|
+
catch {
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
const fileStat = await handle.stat();
|
|
453
|
+
if (!fileStat.isFile()) {
|
|
454
|
+
await handle.close();
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
return { path: filePath, size: fileStat.size, stream: handle.createReadStream() };
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const DATA_DIR = "data";
|
|
463
|
+
const SLUG = /^[a-z0-9-]+$/;
|
|
464
|
+
const DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
465
|
+
const INDEX_FILE = /^index_(\d+)\.jsonl$/;
|
|
466
|
+
const SCRIPT_FILE = /^([a-z0-9-]+)_(messages|candles|pinescript)\.json$/;
|
|
467
|
+
const CHART_REF = /\[\[(t|range|trade):([^\]]+)\]\]|\]\(ref:(t|range|trade):([^)]+)\)/g;
|
|
468
|
+
const INDEX_LINE = {
|
|
469
|
+
id: { type: "slug", required: true },
|
|
470
|
+
title: { type: "string", required: true },
|
|
471
|
+
kind: { type: "enum", values: ["indicator", "strategy"], required: true },
|
|
472
|
+
subcategory: { type: "string" },
|
|
473
|
+
usable: { type: "boolean" },
|
|
474
|
+
verdict: { type: "enum", values: ["YES", "NO", "UNKNOWN"], required: true },
|
|
475
|
+
main: { type: "number" },
|
|
476
|
+
paper: { type: "number" },
|
|
477
|
+
paperTrades: { type: "integer" },
|
|
478
|
+
expected: { type: "tuple2" },
|
|
479
|
+
iterations: { type: "integer" },
|
|
480
|
+
updated: { type: "date", required: true },
|
|
481
|
+
market: { type: "string" },
|
|
482
|
+
timeframe: { type: "string" },
|
|
483
|
+
license: { type: "string" },
|
|
484
|
+
author: { type: "string" },
|
|
485
|
+
tvUrl: { type: "string" },
|
|
486
|
+
cycleStart: { type: "date" },
|
|
487
|
+
percentile: { type: "number" },
|
|
488
|
+
reason: { type: "string" },
|
|
489
|
+
};
|
|
490
|
+
const MESSAGES_FILE = {
|
|
491
|
+
head: { type: "object" },
|
|
492
|
+
groups: { type: "array" },
|
|
493
|
+
messages: { type: "array", required: true },
|
|
494
|
+
};
|
|
495
|
+
const GROUP = {
|
|
496
|
+
code: { type: "string", required: true },
|
|
497
|
+
title: { type: "string", required: true },
|
|
498
|
+
};
|
|
499
|
+
const MESSAGE = {
|
|
500
|
+
code: { type: "string" },
|
|
501
|
+
group: { type: "string" },
|
|
502
|
+
title: { type: "string", required: true },
|
|
503
|
+
md: { type: "string", required: true },
|
|
504
|
+
iteration: { type: "integer" },
|
|
505
|
+
date: { type: "date" },
|
|
506
|
+
};
|
|
507
|
+
const CANDLES_FILE = {
|
|
508
|
+
timeframe: { type: "string" },
|
|
509
|
+
bars: { type: "array", required: true },
|
|
510
|
+
trades: { type: "array" },
|
|
511
|
+
equity: { type: "array" },
|
|
512
|
+
};
|
|
513
|
+
const BAR = {
|
|
514
|
+
time: { type: "number", required: true },
|
|
515
|
+
open: { type: "number", required: true },
|
|
516
|
+
high: { type: "number", required: true },
|
|
517
|
+
low: { type: "number", required: true },
|
|
518
|
+
close: { type: "number", required: true },
|
|
519
|
+
volume: { type: "number", required: true },
|
|
520
|
+
};
|
|
521
|
+
const TRADE = {
|
|
522
|
+
id: { type: "integer", required: true },
|
|
523
|
+
iteration: { type: "integer" },
|
|
524
|
+
side: { type: "enum", values: ["long", "short"], required: true },
|
|
525
|
+
open_time: { type: "number", required: true },
|
|
526
|
+
open_price: { type: "number", required: true },
|
|
527
|
+
close_time: { type: "number", nullable: true },
|
|
528
|
+
close_price: { type: "number", nullable: true },
|
|
529
|
+
pnl_usd: { type: "number", required: true },
|
|
530
|
+
};
|
|
531
|
+
const EQUITY = {
|
|
532
|
+
time: { type: "number", required: true },
|
|
533
|
+
backtest_usd: { type: "number", nullable: true },
|
|
534
|
+
paper_usd: { type: "number", nullable: true },
|
|
535
|
+
};
|
|
536
|
+
const PINESCRIPT_FILE = {
|
|
537
|
+
source: { type: "string", required: true },
|
|
538
|
+
rule: { type: "string" },
|
|
539
|
+
};
|
|
540
|
+
const isPlainObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
541
|
+
const describe = (v) => {
|
|
542
|
+
if (v === null)
|
|
543
|
+
return "null";
|
|
544
|
+
if (Array.isArray(v))
|
|
545
|
+
return "an array";
|
|
546
|
+
if (typeof v === "string")
|
|
547
|
+
return `"${v.length > 40 ? v.slice(0, 40) + "…" : v}"`;
|
|
548
|
+
return typeof v === "object" ? "an object" : String(v);
|
|
549
|
+
};
|
|
550
|
+
const typeError = (rule, v) => {
|
|
551
|
+
switch (rule.type) {
|
|
552
|
+
case "string": return typeof v === "string" ? null : `must be a string, got ${describe(v)}`;
|
|
553
|
+
case "number": return typeof v === "number" && Number.isFinite(v) ? null : `must be a number, got ${describe(v)}`;
|
|
554
|
+
case "integer": return Number.isInteger(v) ? null : `must be an integer, got ${describe(v)}`;
|
|
555
|
+
case "boolean": return typeof v === "boolean" ? null : `must be true or false, got ${describe(v)}`;
|
|
556
|
+
case "date": return typeof v === "string" && DATE.test(v) && !Number.isNaN(Date.parse(v)) ? null : `must be a date YYYY-MM-DD, got ${describe(v)}`;
|
|
557
|
+
case "slug": return typeof v === "string" && SLUG.test(v) ? null : `must be a slug [a-z0-9-]+, got ${describe(v)}`;
|
|
558
|
+
case "enum": return typeof v === "string" && rule.values.includes(v) ? null : `must be one of ${rule.values.join(", ")}, got ${describe(v)}`;
|
|
559
|
+
case "tuple2": return Array.isArray(v) && v.length === 2 && v.every((x) => typeof x === "number" && Number.isFinite(x)) ? null : `must be [low, high] with two numbers, got ${describe(v)}`;
|
|
560
|
+
case "array": return Array.isArray(v) ? null : `must be an array, got ${describe(v)}`;
|
|
561
|
+
case "object": return isPlainObject(v) ? null : `must be an object, got ${describe(v)}`;
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
/** Key order and formatting do not matter when comparing head with the index line. */
|
|
565
|
+
const canonical = (v) => isPlainObject(v)
|
|
566
|
+
? `{${Object.keys(v).filter((k) => v[k] !== null && v[k] !== undefined).sort().map((k) => `${JSON.stringify(k)}:${canonical(v[k])}`).join(",")}}`
|
|
567
|
+
: JSON.stringify(v);
|
|
568
|
+
const error = (file, message, where = {}) => ({ level: "error", file, message, ...where });
|
|
569
|
+
const warning = (file, message, where = {}) => ({ level: "warning", file, message, ...where });
|
|
570
|
+
/** The shape of a JSON value against a schema; pure and small, so a plain array is returned. */
|
|
571
|
+
const checkObject = (value, schema, where) => {
|
|
572
|
+
const issues = [];
|
|
573
|
+
const at = () => ({ line: where.line, at: where.at });
|
|
574
|
+
if (!isPlainObject(value)) {
|
|
575
|
+
issues.push(error(where.file, `must be an object, got ${describe(value)}`, at()));
|
|
576
|
+
return { ok: false, issues };
|
|
577
|
+
}
|
|
578
|
+
for (const [key, rule] of Object.entries(schema)) {
|
|
579
|
+
const v = value[key];
|
|
580
|
+
if (v === undefined || (v === null && !rule.nullable)) {
|
|
581
|
+
if (rule.required)
|
|
582
|
+
issues.push(error(where.file, v === null ? `"${key}" is required and must not be null` : `"${key}" is required`, at()));
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
if (v === null)
|
|
586
|
+
continue;
|
|
587
|
+
const problem = typeError(rule, v);
|
|
588
|
+
if (problem) {
|
|
589
|
+
issues.push(error(where.file, `"${key}" ${problem}`, at()));
|
|
590
|
+
continue;
|
|
591
|
+
}
|
|
592
|
+
if (rule.required && rule.type === "string" && !v.trim()) {
|
|
593
|
+
issues.push(error(where.file, `"${key}" must not be empty`, at()));
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
const norm = (s) => s.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
597
|
+
for (const key of Object.keys(value)) {
|
|
598
|
+
if (key in schema)
|
|
599
|
+
continue;
|
|
600
|
+
const hit = Object.keys(schema).find((k) => norm(k) === norm(key));
|
|
601
|
+
issues.push(error(where.file, `unknown field "${key}"${hit ? ` (did you mean "${hit}"?)` : ""}`, at()));
|
|
602
|
+
}
|
|
603
|
+
return { ok: !issues.length, issues };
|
|
604
|
+
};
|
|
605
|
+
/** [[t:…]], [[range:…]], [[trade:N]] and [text](ref:…) must point at something the chart can show. */
|
|
606
|
+
function* checkRefs(md, where, tradeIds) {
|
|
607
|
+
for (const m of md.matchAll(CHART_REF)) {
|
|
608
|
+
const kind = m[1] ?? m[3];
|
|
609
|
+
const body = (m[2] ?? m[4]).trim();
|
|
610
|
+
const ref = m[0];
|
|
611
|
+
const at = { at: where.at };
|
|
612
|
+
if (kind === "t") {
|
|
613
|
+
const [ts, price] = body.split("@");
|
|
614
|
+
if (Number.isNaN(Date.parse(ts)))
|
|
615
|
+
yield error(where.file, `${ref}: not a date (ISO 8601 expected)`, at);
|
|
616
|
+
if (price !== undefined && !Number.isFinite(Number(price)))
|
|
617
|
+
yield error(where.file, `${ref}: the price after @ is not a number`, at);
|
|
618
|
+
}
|
|
619
|
+
else if (kind === "range") {
|
|
620
|
+
const [a, b] = body.split("..");
|
|
621
|
+
const from = Date.parse(a), to = Date.parse(b ?? "");
|
|
622
|
+
if (Number.isNaN(from) || Number.isNaN(to))
|
|
623
|
+
yield error(where.file, `${ref}: expected two ISO 8601 dates joined with ".."`, at);
|
|
624
|
+
else if (from >= to)
|
|
625
|
+
yield error(where.file, `${ref}: the range ends before it starts`, at);
|
|
626
|
+
}
|
|
627
|
+
else {
|
|
628
|
+
const n = Number(body);
|
|
629
|
+
if (!Number.isInteger(n))
|
|
630
|
+
yield error(where.file, `${ref}: expected a trade id (integer)`, at);
|
|
631
|
+
else if (tradeIds && !tradeIds.has(n))
|
|
632
|
+
yield error(where.file, `${ref}: no trade with id ${n} in the candles file`, at);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Validates <root>/data against the contract and yields every problem the moment it is found:
|
|
638
|
+
* the index files are read line by line, the files of each script one script at a time, so memory
|
|
639
|
+
* holds one document at once whatever the size of the folder. The generator's return value is
|
|
640
|
+
* the summary (counts) once everything has been yielded.
|
|
641
|
+
*/
|
|
642
|
+
class DataValidationService {
|
|
643
|
+
constructor() {
|
|
644
|
+
this.loggerService = inject(TYPES.loggerService);
|
|
645
|
+
this.validate = async function* (root) {
|
|
646
|
+
this.loggerService.log("dataValidationService validate", { root });
|
|
647
|
+
const dir = path.resolve(root, DATA_DIR);
|
|
648
|
+
const rel = (name) => path.join(DATA_DIR, name);
|
|
649
|
+
const summary = { root, scripts: 0, chunks: 0, errors: 0, warnings: 0 };
|
|
650
|
+
const count = (issue) => {
|
|
651
|
+
if (issue.level === "error")
|
|
652
|
+
summary.errors += 1;
|
|
653
|
+
else
|
|
654
|
+
summary.warnings += 1;
|
|
655
|
+
return issue;
|
|
656
|
+
};
|
|
657
|
+
// the folder
|
|
658
|
+
let dirStat;
|
|
659
|
+
try {
|
|
660
|
+
dirStat = await stat(dir);
|
|
661
|
+
}
|
|
662
|
+
catch {
|
|
663
|
+
yield count(error(DATA_DIR, `folder not found: ${dir}`));
|
|
664
|
+
return summary;
|
|
665
|
+
}
|
|
666
|
+
if (!dirStat.isDirectory()) {
|
|
667
|
+
yield count(error(DATA_DIR, `is not a folder: ${dir}`));
|
|
668
|
+
return summary;
|
|
669
|
+
}
|
|
670
|
+
const names = (await readdir(dir)).filter((n) => !n.startsWith("."));
|
|
671
|
+
const present = new Set(names);
|
|
672
|
+
// index chunks: index_1 … index_N without gaps
|
|
673
|
+
const chunks = names
|
|
674
|
+
.map((n) => ({ name: n, n: Number(INDEX_FILE.exec(n)?.[1]) }))
|
|
675
|
+
.filter((c) => Number.isInteger(c.n) && c.n >= 1)
|
|
676
|
+
.sort((a, b) => a.n - b.n);
|
|
677
|
+
summary.chunks = chunks.length;
|
|
678
|
+
if (!chunks.length) {
|
|
679
|
+
yield count(error(rel("index_1.jsonl"), "not found: the list is read from data/index_1.jsonl (an empty file is fine)"));
|
|
680
|
+
}
|
|
681
|
+
for (let i = 0; i < chunks.length; i += 1) {
|
|
682
|
+
const expected = i + 1;
|
|
683
|
+
if (chunks[i].n !== expected) {
|
|
684
|
+
yield count(error(rel(`index_${expected}.jsonl`), `missing: the site stops at the first missing index file, so index_${chunks[i].n}.jsonl and later are never read`));
|
|
685
|
+
break;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
// index lines, streamed; only the raw text of the last line per id is kept
|
|
689
|
+
const rows = new Map();
|
|
690
|
+
for (const chunk of chunks) {
|
|
691
|
+
const file = rel(chunk.name);
|
|
692
|
+
const lines = createInterface({ input: createReadStream(path.join(dir, chunk.name), "utf8"), crlfDelay: Infinity });
|
|
693
|
+
let line = 0;
|
|
694
|
+
for await (const raw of lines) {
|
|
695
|
+
line += 1;
|
|
696
|
+
const l = raw.trim();
|
|
697
|
+
if (!l)
|
|
698
|
+
continue;
|
|
699
|
+
let obj;
|
|
700
|
+
try {
|
|
701
|
+
obj = JSON.parse(l);
|
|
702
|
+
}
|
|
703
|
+
catch (e) {
|
|
704
|
+
yield count(error(file, `not valid JSON: ${e.message}`, { line }));
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
const check = checkObject(obj, INDEX_LINE, { file, line });
|
|
708
|
+
for (const issue of check.issues)
|
|
709
|
+
yield count(issue);
|
|
710
|
+
if (check.ok)
|
|
711
|
+
rows.set(obj.id, l);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
summary.scripts = rows.size;
|
|
715
|
+
// every script's files, one script at a time
|
|
716
|
+
const known = new Set();
|
|
717
|
+
for (const [id, raw] of rows) {
|
|
718
|
+
const row = JSON.parse(raw);
|
|
719
|
+
const messagesName = `${id}_messages.json`;
|
|
720
|
+
const candlesName = `${id}_candles.json`;
|
|
721
|
+
const pinescriptName = `${id}_pinescript.json`;
|
|
722
|
+
known.add(messagesName);
|
|
723
|
+
known.add(candlesName);
|
|
724
|
+
known.add(pinescriptName);
|
|
725
|
+
let tradeIds = null;
|
|
726
|
+
for await (const item of this.checkCandles(dir, rel(candlesName), present.has(candlesName))) {
|
|
727
|
+
if (item instanceof Set)
|
|
728
|
+
tradeIds = item;
|
|
729
|
+
else
|
|
730
|
+
yield count(item);
|
|
731
|
+
}
|
|
732
|
+
for await (const issue of this.checkMessages(dir, rel(messagesName), present.has(messagesName), id, row, tradeIds))
|
|
733
|
+
yield count(issue);
|
|
734
|
+
if (present.has(pinescriptName)) {
|
|
735
|
+
const doc = await this.readJson(dir, pinescriptName);
|
|
736
|
+
if (doc.issue)
|
|
737
|
+
yield count(doc.issue);
|
|
738
|
+
else
|
|
739
|
+
for (const issue of checkObject(doc.value, PINESCRIPT_FILE, { file: rel(pinescriptName) }).issues)
|
|
740
|
+
yield count(issue);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
// files nobody asked for
|
|
744
|
+
for (const name of names) {
|
|
745
|
+
if (INDEX_FILE.test(name) || known.has(name))
|
|
746
|
+
continue;
|
|
747
|
+
const m = SCRIPT_FILE.exec(name);
|
|
748
|
+
yield count(m
|
|
749
|
+
? warning(rel(name), `no index line with id "${m[1]}": the file is never shown`)
|
|
750
|
+
: warning(rel(name), "unexpected file: the site only reads index_N.jsonl and <id>_messages|candles|pinescript.json"));
|
|
751
|
+
}
|
|
752
|
+
return summary;
|
|
753
|
+
};
|
|
754
|
+
this.readJson = async (dir, name) => {
|
|
755
|
+
const text = await readFile(path.join(dir, name), "utf8");
|
|
756
|
+
try {
|
|
757
|
+
return { value: JSON.parse(text) };
|
|
758
|
+
}
|
|
759
|
+
catch (e) {
|
|
760
|
+
return { issue: error(path.join(DATA_DIR, name), `not valid JSON: ${e.message}`) };
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
/** Yields issues, and the set of trade ids as the last item when the file is usable. */
|
|
764
|
+
this.checkCandles = async function* (dir, file, exists) {
|
|
765
|
+
if (!exists) {
|
|
766
|
+
yield error(file, "not found: every script needs its candles file (bars for the chart)");
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
const doc = await this.readJson(dir, path.basename(file));
|
|
770
|
+
if (doc.issue) {
|
|
771
|
+
yield doc.issue;
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
const top = checkObject(doc.value, CANDLES_FILE, { file });
|
|
775
|
+
yield* top.issues;
|
|
776
|
+
if (!top.ok)
|
|
777
|
+
return;
|
|
778
|
+
const d = doc.value;
|
|
779
|
+
if (!d.bars.length)
|
|
780
|
+
yield error(file, '"bars" must not be empty', { at: "bars" });
|
|
781
|
+
let prev = -Infinity;
|
|
782
|
+
for (let i = 0; i < d.bars.length; i += 1) {
|
|
783
|
+
const check = checkObject(d.bars[i], BAR, { file, at: `bars[${i}]` });
|
|
784
|
+
yield* check.issues;
|
|
785
|
+
if (!check.ok)
|
|
786
|
+
continue;
|
|
787
|
+
const b = d.bars[i];
|
|
788
|
+
if (b.time <= prev)
|
|
789
|
+
yield error(file, `"time" must grow from bar to bar (got ${b.time} after ${prev})`, { at: `bars[${i}]` });
|
|
790
|
+
if (b.high < b.low)
|
|
791
|
+
yield error(file, `"high" is below "low"`, { at: `bars[${i}]` });
|
|
792
|
+
prev = b.time;
|
|
793
|
+
}
|
|
794
|
+
const tradeIds = new Set();
|
|
795
|
+
const trades = d.trades ?? [];
|
|
796
|
+
for (let i = 0; i < trades.length; i += 1) {
|
|
797
|
+
const check = checkObject(trades[i], TRADE, { file, at: `trades[${i}]` });
|
|
798
|
+
yield* check.issues;
|
|
799
|
+
if (!check.ok)
|
|
800
|
+
continue;
|
|
801
|
+
const t = trades[i];
|
|
802
|
+
if (tradeIds.has(t.id))
|
|
803
|
+
yield error(file, `"id" ${t.id} is used twice`, { at: `trades[${i}]` });
|
|
804
|
+
tradeIds.add(t.id);
|
|
805
|
+
if (t.close_time != null && t.close_time < t.open_time)
|
|
806
|
+
yield error(file, `"close_time" is before "open_time"`, { at: `trades[${i}]` });
|
|
807
|
+
}
|
|
808
|
+
const equity = d.equity ?? [];
|
|
809
|
+
for (let i = 0; i < equity.length; i += 1)
|
|
810
|
+
yield* checkObject(equity[i], EQUITY, { file, at: `equity[${i}]` }).issues;
|
|
811
|
+
if (d.equity && d.equity.length === 1)
|
|
812
|
+
yield warning(file, '"equity" has a single point: the P&L line needs at least two', { at: "equity" });
|
|
813
|
+
yield tradeIds;
|
|
814
|
+
};
|
|
815
|
+
this.checkMessages = async function* (dir, file, exists, id, row, tradeIds) {
|
|
816
|
+
if (!exists) {
|
|
817
|
+
yield error(file, "not found: every script needs its messages file (the report text)");
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
const doc = await this.readJson(dir, path.basename(file));
|
|
821
|
+
if (doc.issue) {
|
|
822
|
+
yield doc.issue;
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
const top = checkObject(doc.value, MESSAGES_FILE, { file });
|
|
826
|
+
yield* top.issues;
|
|
827
|
+
if (!top.ok)
|
|
828
|
+
return;
|
|
829
|
+
const d = doc.value;
|
|
830
|
+
if (d.head !== undefined) {
|
|
831
|
+
const check = checkObject(d.head, INDEX_LINE, { file, at: "head" });
|
|
832
|
+
yield* check.issues;
|
|
833
|
+
if (check.ok) {
|
|
834
|
+
if (d.head.id !== id)
|
|
835
|
+
yield error(file, `"id" is "${d.head.id}" but the file belongs to "${id}"`, { at: "head" });
|
|
836
|
+
else if (canonical(d.head) !== canonical(row)) {
|
|
837
|
+
const diff = [...new Set([...Object.keys(d.head), ...Object.keys(row)])].filter((k) => canonical(d.head[k]) !== canonical(row[k]));
|
|
838
|
+
yield error(file, `differs from the index line in ${diff.map((k) => `"${k}"`).join(", ")}: copy the index line as is`, { at: "head" });
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
else {
|
|
843
|
+
yield warning(file, `"head" is missing: a direct link to /run/?id=${id} has to scan the index first`);
|
|
844
|
+
}
|
|
845
|
+
const groups = d.groups ?? [];
|
|
846
|
+
for (let i = 0; i < groups.length; i += 1)
|
|
847
|
+
yield* checkObject(groups[i], GROUP, { file, at: `groups[${i}]` }).issues;
|
|
848
|
+
if (!d.messages.length)
|
|
849
|
+
yield warning(file, '"messages" is empty: the page shows no text', { at: "messages" });
|
|
850
|
+
for (let i = 0; i < d.messages.length; i += 1) {
|
|
851
|
+
const check = checkObject(d.messages[i], MESSAGE, { file, at: `messages[${i}]` });
|
|
852
|
+
yield* check.issues;
|
|
853
|
+
if (!check.ok)
|
|
854
|
+
continue;
|
|
855
|
+
yield* checkRefs(d.messages[i].md, { file, at: `messages[${i}].md` }, tradeIds);
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
{
|
|
862
|
+
provide(TYPES.loggerService, () => new LoggerService());
|
|
863
|
+
}
|
|
864
|
+
{
|
|
865
|
+
provide(TYPES.dataViewService, () => new DataViewService());
|
|
866
|
+
}
|
|
867
|
+
{
|
|
868
|
+
provide(TYPES.dataValidationService, () => new DataValidationService());
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
const baseServices = {
|
|
872
|
+
loggerService: inject(TYPES.loggerService),
|
|
873
|
+
};
|
|
874
|
+
const viewServices = {
|
|
875
|
+
dataViewService: inject(TYPES.dataViewService),
|
|
876
|
+
};
|
|
877
|
+
const validationServices = {
|
|
878
|
+
dataValidationService: inject(TYPES.dataValidationService),
|
|
879
|
+
};
|
|
880
|
+
const ioc = {
|
|
881
|
+
...baseServices,
|
|
882
|
+
...viewServices,
|
|
883
|
+
...validationServices,
|
|
884
|
+
};
|
|
885
|
+
init();
|
|
886
|
+
|
|
887
|
+
const router$2 = Router({
|
|
888
|
+
params: true,
|
|
889
|
+
});
|
|
890
|
+
const DEFAULT_MIME = "application/octet-stream";
|
|
891
|
+
const MIME_OVERRIDES = {
|
|
892
|
+
".json": "application/json; charset=utf-8",
|
|
893
|
+
".jsonl": "application/x-ndjson; charset=utf-8",
|
|
894
|
+
};
|
|
895
|
+
router$2.get("/data/:filePath(*)", async (req, res) => {
|
|
896
|
+
try {
|
|
897
|
+
const { filePath } = req.params;
|
|
898
|
+
const file = await ioc.dataViewService.open(filePath);
|
|
899
|
+
if (!file) {
|
|
900
|
+
ioc.loggerService.log("/data not found", {
|
|
901
|
+
request: { filePath },
|
|
902
|
+
});
|
|
903
|
+
return await micro.send(res, 404, {
|
|
904
|
+
status: "error",
|
|
905
|
+
error: `file not found filePath=${filePath}`,
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
const ext = extname(file.path).toLowerCase();
|
|
909
|
+
const mimeType = MIME_OVERRIDES[ext] ?? (mime.lookup(file.path) || DEFAULT_MIME);
|
|
910
|
+
// A read error (the agent deleted or truncated the file mid-stream) must not become an
|
|
911
|
+
// uncaught exception: answer 500 while the headers are still ours, otherwise cut the connection
|
|
912
|
+
// so the browser sees a failed request instead of a short body.
|
|
913
|
+
file.stream.on("error", (error) => {
|
|
914
|
+
ioc.loggerService.log("/data stream error", {
|
|
915
|
+
request: { filePath },
|
|
916
|
+
error: errorData(error),
|
|
917
|
+
});
|
|
918
|
+
if (res.headersSent) {
|
|
919
|
+
res.destroy(error);
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
micro.send(res, 500, {
|
|
923
|
+
status: "error",
|
|
924
|
+
error: getErrorMessage(error),
|
|
925
|
+
});
|
|
926
|
+
});
|
|
927
|
+
res.on("close", () => {
|
|
928
|
+
file.stream.destroy();
|
|
929
|
+
});
|
|
930
|
+
res.setHeader("Content-Type", mimeType);
|
|
931
|
+
res.setHeader("Content-Length", file.size);
|
|
932
|
+
// the agent rewrites these files between runs; never let the browser keep a stale copy
|
|
933
|
+
res.setHeader("Cache-Control", "no-store");
|
|
934
|
+
ioc.loggerService.log("/data ok", {
|
|
935
|
+
request: { filePath },
|
|
936
|
+
result: { path: file.path, mime: mimeType, size: file.size },
|
|
937
|
+
});
|
|
938
|
+
return await micro.send(res, 200, file.stream);
|
|
939
|
+
}
|
|
940
|
+
catch (error) {
|
|
941
|
+
ioc.loggerService.log("/data error", {
|
|
942
|
+
error: errorData(error),
|
|
943
|
+
});
|
|
944
|
+
return await micro.send(res, 500, {
|
|
945
|
+
status: "error",
|
|
946
|
+
error: getErrorMessage(error),
|
|
947
|
+
});
|
|
948
|
+
}
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
const require = createRequire(import.meta.url);
|
|
952
|
+
/**
|
|
953
|
+
* Static root of the dashboard: <package>/build/modules/frontend/build.
|
|
954
|
+
* Resolved against the installed package (self-reference via package.json "exports"),
|
|
955
|
+
* so it works from any cwd. Override with CC_WWWROOT_PATH or setConfig().
|
|
956
|
+
*/
|
|
957
|
+
function getPublicPath() {
|
|
958
|
+
const modulePath = require.resolve("pine2profit");
|
|
959
|
+
const basePath = dirname(modulePath);
|
|
960
|
+
return join(basePath, "./modules/frontend/build");
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
const router = Router({
|
|
964
|
+
params: true,
|
|
965
|
+
});
|
|
966
|
+
// The agent's files: GET /data/<file> -> <root>/data/<file>. The same relative
|
|
967
|
+
// paths the site requests on GitHub Pages, so nothing differs between the two.
|
|
968
|
+
router.all("/data/*", (req, res) => {
|
|
969
|
+
return router$2(req, res, finalhandler(req, res));
|
|
970
|
+
});
|
|
971
|
+
// Next.js static export (modules/frontend, output: "export", trailingSlash: true):
|
|
972
|
+
// /run/?id=x -> run/index.html, 404.html is picked up by serve-handler.
|
|
973
|
+
router.get("/*", (req, res) => {
|
|
974
|
+
const { CC_WWWROOT_PATH } = getConfig();
|
|
975
|
+
return serveHandler(req, res, {
|
|
976
|
+
public: CC_WWWROOT_PATH || getPublicPath(),
|
|
977
|
+
});
|
|
978
|
+
});
|
|
979
|
+
var router$1 = micro.serve(async (req, res) => {
|
|
980
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
981
|
+
res.setHeader("Access-Control-Allow-Headers", "*");
|
|
982
|
+
res.setHeader("Access-Control-Allow-Credentials", "true");
|
|
983
|
+
res.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
|
|
984
|
+
return router(req, res, finalhandler(req, res));
|
|
985
|
+
});
|
|
986
|
+
|
|
987
|
+
const METHOD_NAME_SERVE = "serve.serve";
|
|
988
|
+
const METHOD_NAME_GET_ROUTER = "serve.getRouter";
|
|
989
|
+
const MAX_CONNECTIONS = 1000;
|
|
990
|
+
const SOCKET_TIMEOUT = 60 * 10 * 1000;
|
|
991
|
+
const serveInternal = singleshot((host = getConfig().CC_WWWROOT_HOST, port = getConfig().CC_WWWROOT_PORT, callback) => {
|
|
992
|
+
const server = new http.Server(router$1);
|
|
993
|
+
server.listen(port, host).addListener("listening", () => {
|
|
994
|
+
console.log(`Listening on http://${host}:${port}`);
|
|
995
|
+
callback && callback();
|
|
996
|
+
});
|
|
997
|
+
server.addListener("error", (err) => {
|
|
998
|
+
console.error("Server error:", err);
|
|
999
|
+
// The server never came up: reset the singleshot, otherwise a retry of serve()
|
|
1000
|
+
// would hand back this dead instance and nobody would try the port again.
|
|
1001
|
+
if (!server.listening) {
|
|
1002
|
+
serveInternal.clear();
|
|
1003
|
+
}
|
|
1004
|
+
callback && callback(err);
|
|
1005
|
+
});
|
|
1006
|
+
server.maxConnections = MAX_CONNECTIONS;
|
|
1007
|
+
server.setTimeout(SOCKET_TIMEOUT);
|
|
1008
|
+
return () => {
|
|
1009
|
+
server.close();
|
|
1010
|
+
serveInternal.clear();
|
|
1011
|
+
};
|
|
1012
|
+
});
|
|
1013
|
+
/**
|
|
1014
|
+
* Starts the server. `cwd` is the project root whose data/ folder is served: the argument wins,
|
|
1015
|
+
* then the CC_DATA_PATH environment variable, then process.cwd().
|
|
1016
|
+
*/
|
|
1017
|
+
function serve(host, port, cwd, callback) {
|
|
1018
|
+
const root = cwd || getConfig().CC_DATA_PATH || process.cwd();
|
|
1019
|
+
ioc.loggerService.log(METHOD_NAME_SERVE, {
|
|
1020
|
+
host,
|
|
1021
|
+
port,
|
|
1022
|
+
root,
|
|
1023
|
+
});
|
|
1024
|
+
serveSubject.next(root);
|
|
1025
|
+
return serveInternal(host, port, callback);
|
|
1026
|
+
}
|
|
1027
|
+
function getRouter() {
|
|
1028
|
+
ioc.loggerService.log(METHOD_NAME_GET_ROUTER);
|
|
1029
|
+
return router$1;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
const METHOD_NAME_VALIDATE = "validate.validate";
|
|
1033
|
+
/**
|
|
1034
|
+
* Checks <root>/data against the contract. Yields each problem as soon as it is found; the
|
|
1035
|
+
* generator's return value is the summary. Consume it with `for await`, or step with next()
|
|
1036
|
+
* when the summary is needed:
|
|
1037
|
+
* const it = validate(root); let r; while (!(r = await it.next()).done) report(r.value); const summary = r.value;
|
|
1038
|
+
*/
|
|
1039
|
+
function validate(root) {
|
|
1040
|
+
ioc.loggerService.log(METHOD_NAME_VALIDATE, { root });
|
|
1041
|
+
return ioc.dataValidationService.validate(root);
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
const MAX_ISSUES_SHOWN = 50;
|
|
1045
|
+
const NO_DIR_TEXT = `
|
|
1046
|
+
Example:
|
|
1047
|
+
|
|
1048
|
+
npx pine2profit --help
|
|
1049
|
+
npx pine2profit <dir>
|
|
1050
|
+
`.trimStart();
|
|
1051
|
+
const formatIssue = (issue) => {
|
|
1052
|
+
const where = `${issue.file}${issue.line ? `:${issue.line}` : ""}${issue.at ? ` ${issue.at}` : ""}`;
|
|
1053
|
+
return `${issue.level.padEnd(8)} ${where}\n ${issue.message}`;
|
|
1054
|
+
};
|
|
1055
|
+
const main = async () => {
|
|
1056
|
+
if (!getEntry(import.meta.url)) {
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
const { values, positionals } = getArgs();
|
|
1060
|
+
if (values.help) {
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
if (values.version) {
|
|
1064
|
+
process.stdout.write(`${"0.1.0"}\n`);
|
|
1065
|
+
process.exit(0);
|
|
1066
|
+
}
|
|
1067
|
+
const [dir = null] = positionals;
|
|
1068
|
+
if (!dir) {
|
|
1069
|
+
process.stdout.write(`pine2profit ${"0.1.0"}\n`);
|
|
1070
|
+
process.stdout.write("\n");
|
|
1071
|
+
process.stdout.write(`The folder that contains data/ is required. Run with --help to see how to build it.\n`);
|
|
1072
|
+
process.stdout.write("\n");
|
|
1073
|
+
process.stdout.write(NO_DIR_TEXT);
|
|
1074
|
+
process.exit(1);
|
|
1075
|
+
}
|
|
1076
|
+
const root = path.resolve(String(dir));
|
|
1077
|
+
setConfig({ CC_DATA_PATH: root });
|
|
1078
|
+
if (values.port) {
|
|
1079
|
+
const port = Number(values.port);
|
|
1080
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
1081
|
+
console.error(`--port must be an integer between 1 and 65535, got "${values.port}"`);
|
|
1082
|
+
process.exit(1);
|
|
1083
|
+
}
|
|
1084
|
+
setConfig({ CC_WWWROOT_PORT: port });
|
|
1085
|
+
}
|
|
1086
|
+
if (values.host) {
|
|
1087
|
+
setConfig({ CC_WWWROOT_HOST: String(values.host) });
|
|
1088
|
+
}
|
|
1089
|
+
// The data folder is checked on every start and a single error refuses to start: the agent
|
|
1090
|
+
// must never be left looking at a page that silently shows something wrong. Problems are
|
|
1091
|
+
// printed as they are found; the folder is read one file at a time.
|
|
1092
|
+
const check = validate(root);
|
|
1093
|
+
let shown = 0;
|
|
1094
|
+
let summary;
|
|
1095
|
+
for (;;) {
|
|
1096
|
+
const step = await check.next();
|
|
1097
|
+
if (step.done) {
|
|
1098
|
+
summary = step.value;
|
|
1099
|
+
break;
|
|
1100
|
+
}
|
|
1101
|
+
const issue = step.value;
|
|
1102
|
+
if (shown < MAX_ISSUES_SHOWN) {
|
|
1103
|
+
(issue.level === "error" ? console.error : console.warn)(formatIssue(issue));
|
|
1104
|
+
}
|
|
1105
|
+
else if (shown === MAX_ISSUES_SHOWN) {
|
|
1106
|
+
console.error("… more problems follow, only the first ones are shown");
|
|
1107
|
+
}
|
|
1108
|
+
shown += 1;
|
|
1109
|
+
}
|
|
1110
|
+
const dataDir = path.join(root, "data");
|
|
1111
|
+
const counts = `${summary.scripts} script${summary.scripts === 1 ? "" : "s"} in ${summary.chunks} index file${summary.chunks === 1 ? "" : "s"}${summary.warnings ? `, ${summary.warnings} warning${summary.warnings === 1 ? "" : "s"}` : ""}`;
|
|
1112
|
+
if (summary.errors) {
|
|
1113
|
+
console.error(`\n${summary.errors} error${summary.errors === 1 ? "" : "s"} in ${dataDir}: ${values.check ? "the folder is not valid" : "refusing to start"}. Run with --help for the data contract.`);
|
|
1114
|
+
process.exit(1);
|
|
1115
|
+
}
|
|
1116
|
+
if (values.check) {
|
|
1117
|
+
// --check: the same validation as before every start, without starting the server
|
|
1118
|
+
console.log(`${dataDir}: no errors, ${counts}`);
|
|
1119
|
+
process.exit(0);
|
|
1120
|
+
}
|
|
1121
|
+
console.log(`data: ${counts}`);
|
|
1122
|
+
const { CC_WWWROOT_HOST, CC_WWWROOT_PORT } = getConfig();
|
|
1123
|
+
const unServer = serve(CC_WWWROOT_HOST, CC_WWWROOT_PORT, root, (error) => {
|
|
1124
|
+
if (error) {
|
|
1125
|
+
process.exit(1);
|
|
1126
|
+
}
|
|
1127
|
+
console.log(`Serving ${path.join(root, "data")} at http://localhost:${CC_WWWROOT_PORT}/data/`);
|
|
1128
|
+
});
|
|
1129
|
+
const beforeExit = () => {
|
|
1130
|
+
process.off("SIGINT", beforeExit);
|
|
1131
|
+
unServer();
|
|
1132
|
+
process.exit(0);
|
|
1133
|
+
};
|
|
1134
|
+
process.on("SIGINT", beforeExit);
|
|
1135
|
+
};
|
|
1136
|
+
main();
|
|
1137
|
+
|
|
1138
|
+
const setLogger = (logger) => {
|
|
1139
|
+
ioc.loggerService.setLogger(logger);
|
|
1140
|
+
};
|
|
1141
|
+
|
|
1142
|
+
export { getConfig, getPublicPath, getRouter, ioc as lib, serve, setConfig, setLogger, validate };
|