@tradejs/node 1.0.2 → 1.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.
@@ -0,0 +1,246 @@
1
+ import {
2
+ getTradejsProjectCwd,
3
+ importTradejsModule,
4
+ loadTradejsConfig,
5
+ resolvePluginModuleSpecifier
6
+ } from "./chunk-P2ZUWONT.mjs";
7
+
8
+ // src/strategy/manifests.ts
9
+ import {
10
+ registerIndicatorEntries,
11
+ resetIndicatorRegistryCache
12
+ } from "@tradejs/core/indicators";
13
+ import { logger } from "@tradejs/infra/logger";
14
+ var createStrategyRegistryState = () => ({
15
+ strategyCreators: /* @__PURE__ */ new Map(),
16
+ strategyManifestsMap: /* @__PURE__ */ new Map(),
17
+ pluginsLoadPromise: null
18
+ });
19
+ var registryStateByProjectRoot = /* @__PURE__ */ new Map();
20
+ var getStrategyRegistryState = (cwd = getTradejsProjectCwd()) => {
21
+ const projectRoot = getTradejsProjectCwd(cwd);
22
+ let state = registryStateByProjectRoot.get(projectRoot);
23
+ if (!state) {
24
+ state = createStrategyRegistryState();
25
+ registryStateByProjectRoot.set(projectRoot, state);
26
+ }
27
+ return {
28
+ projectRoot,
29
+ state
30
+ };
31
+ };
32
+ var toUniqueModules = (modules = []) => [
33
+ ...new Set(modules.map((moduleName) => moduleName.trim()).filter(Boolean))
34
+ ];
35
+ var getConfiguredPluginModuleNames = async (cwd = getTradejsProjectCwd()) => {
36
+ const config = await loadTradejsConfig(cwd);
37
+ return {
38
+ strategyModules: toUniqueModules(config.strategies),
39
+ indicatorModules: toUniqueModules(config.indicators)
40
+ };
41
+ };
42
+ var extractModuleEntries = (moduleExport, key) => {
43
+ if (!moduleExport || typeof moduleExport !== "object") {
44
+ return null;
45
+ }
46
+ const candidate = moduleExport;
47
+ if (Array.isArray(candidate[key])) {
48
+ return candidate[key];
49
+ }
50
+ const defaultExport = candidate.default;
51
+ if (defaultExport && Array.isArray(defaultExport[key])) {
52
+ return defaultExport[key];
53
+ }
54
+ return null;
55
+ };
56
+ var extractStrategyPluginDefinition = (moduleExport) => {
57
+ const strategyEntries = extractModuleEntries(
58
+ moduleExport,
59
+ "strategyEntries"
60
+ );
61
+ return strategyEntries ? { strategyEntries } : null;
62
+ };
63
+ var extractIndicatorPluginDefinition = (moduleExport) => {
64
+ const indicatorEntries = extractModuleEntries(
65
+ moduleExport,
66
+ "indicatorEntries"
67
+ );
68
+ return indicatorEntries ? { indicatorEntries } : null;
69
+ };
70
+ var registerEntries = (entries, source, state) => {
71
+ for (const entry of entries) {
72
+ const strategyName = entry.manifest?.name;
73
+ if (!strategyName) {
74
+ logger.warn("Skip strategy entry without name from %s", source);
75
+ continue;
76
+ }
77
+ if (state.strategyCreators.has(strategyName)) {
78
+ logger.warn(
79
+ 'Skip duplicate strategy "%s" from %s: already registered',
80
+ strategyName,
81
+ source
82
+ );
83
+ continue;
84
+ }
85
+ state.strategyCreators.set(strategyName, entry.creator);
86
+ state.strategyManifestsMap.set(strategyName, entry.manifest);
87
+ }
88
+ };
89
+ var importStrategyPluginModule = async (moduleName, cwd = getTradejsProjectCwd()) => {
90
+ if (typeof importTradejsModule === "function") {
91
+ return importTradejsModule(moduleName, cwd);
92
+ }
93
+ return import(
94
+ /* webpackIgnore: true */
95
+ moduleName
96
+ );
97
+ };
98
+ var ensureStrategyPluginsLoaded = async (cwd = getTradejsProjectCwd()) => {
99
+ const { projectRoot, state } = getStrategyRegistryState(cwd);
100
+ if (!state.pluginsLoadPromise) {
101
+ resetIndicatorRegistryCache(projectRoot);
102
+ state.pluginsLoadPromise = (async () => {
103
+ const { strategyModules, indicatorModules } = await getConfiguredPluginModuleNames(projectRoot);
104
+ const strategySet = new Set(strategyModules);
105
+ const indicatorSet = new Set(indicatorModules);
106
+ const pluginModuleNames = [
107
+ .../* @__PURE__ */ new Set([...strategyModules, ...indicatorModules])
108
+ ];
109
+ if (!pluginModuleNames.length) {
110
+ return;
111
+ }
112
+ for (const moduleName of pluginModuleNames) {
113
+ try {
114
+ const resolvedModuleName = resolvePluginModuleSpecifier(
115
+ moduleName,
116
+ projectRoot
117
+ );
118
+ const moduleExport = await importStrategyPluginModule(
119
+ resolvedModuleName,
120
+ projectRoot
121
+ );
122
+ if (strategySet.has(moduleName)) {
123
+ const pluginDefinition = extractStrategyPluginDefinition(moduleExport);
124
+ if (!pluginDefinition) {
125
+ logger.warn(
126
+ 'Skip strategy plugin "%s": export { strategyEntries } is missing',
127
+ moduleName
128
+ );
129
+ } else {
130
+ registerEntries(
131
+ pluginDefinition.strategyEntries,
132
+ moduleName,
133
+ state
134
+ );
135
+ }
136
+ }
137
+ if (indicatorSet.has(moduleName)) {
138
+ const indicatorPluginDefinition = extractIndicatorPluginDefinition(moduleExport);
139
+ if (!indicatorPluginDefinition) {
140
+ logger.warn(
141
+ 'Skip indicator plugin "%s": export { indicatorEntries } is missing',
142
+ moduleName
143
+ );
144
+ } else {
145
+ registerIndicatorEntries(
146
+ indicatorPluginDefinition.indicatorEntries,
147
+ moduleName,
148
+ projectRoot
149
+ );
150
+ }
151
+ }
152
+ if (!strategySet.has(moduleName) && !indicatorSet.has(moduleName)) {
153
+ logger.warn(
154
+ 'Skip plugin "%s": no strategy/indicator sections requested in config',
155
+ moduleName
156
+ );
157
+ }
158
+ } catch (error) {
159
+ logger.warn(
160
+ 'Failed to load plugin "%s": %s',
161
+ moduleName,
162
+ String(error)
163
+ );
164
+ }
165
+ }
166
+ })();
167
+ }
168
+ await state.pluginsLoadPromise;
169
+ };
170
+ var ensureIndicatorPluginsLoaded = async (cwd = getTradejsProjectCwd()) => ensureStrategyPluginsLoaded(cwd);
171
+ var getStrategyCreator = async (name, cwd = getTradejsProjectCwd()) => {
172
+ await ensureStrategyPluginsLoaded(cwd);
173
+ const { state } = getStrategyRegistryState(cwd);
174
+ return state.strategyCreators.get(name);
175
+ };
176
+ var getAvailableStrategyNames = async (cwd = getTradejsProjectCwd()) => {
177
+ await ensureStrategyPluginsLoaded(cwd);
178
+ const { state } = getStrategyRegistryState(cwd);
179
+ return [...state.strategyCreators.keys()].sort((a, b) => a.localeCompare(b));
180
+ };
181
+ var getRegisteredStrategies = (cwd = getTradejsProjectCwd()) => {
182
+ const { state } = getStrategyRegistryState(cwd);
183
+ return Object.fromEntries(state.strategyCreators.entries());
184
+ };
185
+ var getRegisteredManifests = (cwd = getTradejsProjectCwd()) => {
186
+ const { state } = getStrategyRegistryState(cwd);
187
+ return [...state.strategyManifestsMap.values()];
188
+ };
189
+ var getStrategyManifest = (name, cwd = getTradejsProjectCwd()) => {
190
+ if (!name) {
191
+ return void 0;
192
+ }
193
+ const { state } = getStrategyRegistryState(cwd);
194
+ return state.strategyManifestsMap.get(name);
195
+ };
196
+ var isKnownStrategy = (name, cwd = getTradejsProjectCwd()) => {
197
+ const { state } = getStrategyRegistryState(cwd);
198
+ return state.strategyCreators.has(name);
199
+ };
200
+ var registerStrategyEntries = (entries, cwd = getTradejsProjectCwd()) => {
201
+ const { state } = getStrategyRegistryState(cwd);
202
+ registerEntries(entries, "runtime", state);
203
+ };
204
+ var resetStrategyRegistryCache = (cwd) => {
205
+ const normalizedCwd = String(cwd ?? "").trim();
206
+ if (!normalizedCwd) {
207
+ registryStateByProjectRoot.clear();
208
+ resetIndicatorRegistryCache();
209
+ return;
210
+ }
211
+ const projectRoot = getTradejsProjectCwd(normalizedCwd);
212
+ registryStateByProjectRoot.delete(projectRoot);
213
+ resetIndicatorRegistryCache(projectRoot);
214
+ };
215
+ var strategies = new Proxy(
216
+ {},
217
+ {
218
+ get: (_target, property) => {
219
+ if (typeof property !== "string") {
220
+ return void 0;
221
+ }
222
+ return getStrategyRegistryState().state.strategyCreators.get(property);
223
+ },
224
+ ownKeys: () => {
225
+ return [...getStrategyRegistryState().state.strategyCreators.keys()];
226
+ },
227
+ getOwnPropertyDescriptor: () => ({
228
+ enumerable: true,
229
+ configurable: true
230
+ })
231
+ }
232
+ );
233
+
234
+ export {
235
+ ensureStrategyPluginsLoaded,
236
+ ensureIndicatorPluginsLoaded,
237
+ getStrategyCreator,
238
+ getAvailableStrategyNames,
239
+ getRegisteredStrategies,
240
+ getRegisteredManifests,
241
+ getStrategyManifest,
242
+ isKnownStrategy,
243
+ registerStrategyEntries,
244
+ resetStrategyRegistryCache,
245
+ strategies
246
+ };