@tradejs/node 1.0.0 → 1.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/registry.js CHANGED
@@ -248,14 +248,29 @@ var loadTradejsConfig = async (cwd = getTradejsProjectCwd()) => {
248
248
  };
249
249
 
250
250
  // src/strategy/manifests.ts
251
- var strategyCreators = /* @__PURE__ */ new Map();
252
- var strategyManifestsMap = /* @__PURE__ */ new Map();
253
- var pluginsLoadPromise = null;
251
+ var createStrategyRegistryState = () => ({
252
+ strategyCreators: /* @__PURE__ */ new Map(),
253
+ strategyManifestsMap: /* @__PURE__ */ new Map(),
254
+ pluginsLoadPromise: null
255
+ });
256
+ var registryStateByProjectRoot = /* @__PURE__ */ new Map();
257
+ var getStrategyRegistryState = (cwd = getTradejsProjectCwd()) => {
258
+ const projectRoot = getTradejsProjectCwd(cwd);
259
+ let state = registryStateByProjectRoot.get(projectRoot);
260
+ if (!state) {
261
+ state = createStrategyRegistryState();
262
+ registryStateByProjectRoot.set(projectRoot, state);
263
+ }
264
+ return {
265
+ projectRoot,
266
+ state
267
+ };
268
+ };
254
269
  var toUniqueModules = (modules = []) => [
255
270
  ...new Set(modules.map((moduleName) => moduleName.trim()).filter(Boolean))
256
271
  ];
257
- var getConfiguredPluginModuleNames = async () => {
258
- const config = await loadTradejsConfig();
272
+ var getConfiguredPluginModuleNames = async (cwd = getTradejsProjectCwd()) => {
273
+ const config = await loadTradejsConfig(cwd);
259
274
  return {
260
275
  strategyModules: toUniqueModules(config.strategies),
261
276
  indicatorModules: toUniqueModules(config.indicators)
@@ -289,14 +304,14 @@ var extractIndicatorPluginDefinition = (moduleExport) => {
289
304
  );
290
305
  return indicatorEntries ? { indicatorEntries } : null;
291
306
  };
292
- var registerEntries = (entries, source) => {
307
+ var registerEntries = (entries, source, state) => {
293
308
  for (const entry of entries) {
294
309
  const strategyName = entry.manifest?.name;
295
310
  if (!strategyName) {
296
311
  import_logger2.logger.warn("Skip strategy entry without name from %s", source);
297
312
  continue;
298
313
  }
299
- if (strategyCreators.has(strategyName)) {
314
+ if (state.strategyCreators.has(strategyName)) {
300
315
  import_logger2.logger.warn(
301
316
  'Skip duplicate strategy "%s" from %s: already registered',
302
317
  strategyName,
@@ -304,8 +319,8 @@ var registerEntries = (entries, source) => {
304
319
  );
305
320
  continue;
306
321
  }
307
- strategyCreators.set(strategyName, entry.creator);
308
- strategyManifestsMap.set(strategyName, entry.manifest);
322
+ state.strategyCreators.set(strategyName, entry.creator);
323
+ state.strategyManifestsMap.set(strategyName, entry.manifest);
309
324
  }
310
325
  };
311
326
  var importStrategyPluginModule = async (moduleName) => {
@@ -317,93 +332,119 @@ var importStrategyPluginModule = async (moduleName) => {
317
332
  moduleName
318
333
  );
319
334
  };
320
- var ensureStrategyPluginsLoaded = async () => {
321
- if (pluginsLoadPromise) {
322
- return pluginsLoadPromise;
323
- }
324
- pluginsLoadPromise = (async () => {
325
- const { strategyModules, indicatorModules } = await getConfiguredPluginModuleNames();
326
- const strategySet = new Set(strategyModules);
327
- const indicatorSet = new Set(indicatorModules);
328
- const pluginModuleNames = [
329
- .../* @__PURE__ */ new Set([...strategyModules, ...indicatorModules])
330
- ];
331
- if (!pluginModuleNames.length) return;
332
- for (const moduleName of pluginModuleNames) {
333
- try {
334
- const resolvedModuleName = resolvePluginModuleSpecifier(moduleName);
335
- const moduleExport = await importStrategyPluginModule(resolvedModuleName);
336
- if (strategySet.has(moduleName)) {
337
- const pluginDefinition = extractStrategyPluginDefinition(moduleExport);
338
- if (!pluginDefinition) {
339
- import_logger2.logger.warn(
340
- 'Skip strategy plugin "%s": export { strategyEntries } is missing',
341
- moduleName
342
- );
343
- } else {
344
- registerEntries(pluginDefinition.strategyEntries, moduleName);
335
+ var ensureStrategyPluginsLoaded = async (cwd = getTradejsProjectCwd()) => {
336
+ const { projectRoot, state } = getStrategyRegistryState(cwd);
337
+ if (!state.pluginsLoadPromise) {
338
+ (0, import_indicators.resetIndicatorRegistryCache)(projectRoot);
339
+ state.pluginsLoadPromise = (async () => {
340
+ const { strategyModules, indicatorModules } = await getConfiguredPluginModuleNames(projectRoot);
341
+ const strategySet = new Set(strategyModules);
342
+ const indicatorSet = new Set(indicatorModules);
343
+ const pluginModuleNames = [
344
+ .../* @__PURE__ */ new Set([...strategyModules, ...indicatorModules])
345
+ ];
346
+ if (!pluginModuleNames.length) {
347
+ return;
348
+ }
349
+ for (const moduleName of pluginModuleNames) {
350
+ try {
351
+ const resolvedModuleName = resolvePluginModuleSpecifier(
352
+ moduleName,
353
+ projectRoot
354
+ );
355
+ const moduleExport = await importStrategyPluginModule(resolvedModuleName);
356
+ if (strategySet.has(moduleName)) {
357
+ const pluginDefinition = extractStrategyPluginDefinition(moduleExport);
358
+ if (!pluginDefinition) {
359
+ import_logger2.logger.warn(
360
+ 'Skip strategy plugin "%s": export { strategyEntries } is missing',
361
+ moduleName
362
+ );
363
+ } else {
364
+ registerEntries(
365
+ pluginDefinition.strategyEntries,
366
+ moduleName,
367
+ state
368
+ );
369
+ }
345
370
  }
346
- }
347
- if (indicatorSet.has(moduleName)) {
348
- const indicatorPluginDefinition = extractIndicatorPluginDefinition(moduleExport);
349
- if (!indicatorPluginDefinition) {
371
+ if (indicatorSet.has(moduleName)) {
372
+ const indicatorPluginDefinition = extractIndicatorPluginDefinition(moduleExport);
373
+ if (!indicatorPluginDefinition) {
374
+ import_logger2.logger.warn(
375
+ 'Skip indicator plugin "%s": export { indicatorEntries } is missing',
376
+ moduleName
377
+ );
378
+ } else {
379
+ (0, import_indicators.registerIndicatorEntries)(
380
+ indicatorPluginDefinition.indicatorEntries,
381
+ moduleName,
382
+ projectRoot
383
+ );
384
+ }
385
+ }
386
+ if (!strategySet.has(moduleName) && !indicatorSet.has(moduleName)) {
350
387
  import_logger2.logger.warn(
351
- 'Skip indicator plugin "%s": export { indicatorEntries } is missing',
352
- moduleName
353
- );
354
- } else {
355
- (0, import_indicators.registerIndicatorEntries)(
356
- indicatorPluginDefinition.indicatorEntries,
388
+ 'Skip plugin "%s": no strategy/indicator sections requested in config',
357
389
  moduleName
358
390
  );
359
391
  }
360
- }
361
- if (!strategySet.has(moduleName) && !indicatorSet.has(moduleName)) {
392
+ } catch (error) {
362
393
  import_logger2.logger.warn(
363
- 'Skip plugin "%s": no strategy/indicator sections requested in config',
364
- moduleName
394
+ 'Failed to load plugin "%s": %s',
395
+ moduleName,
396
+ String(error)
365
397
  );
366
398
  }
367
- } catch (error) {
368
- import_logger2.logger.warn(
369
- 'Failed to load plugin "%s": %s',
370
- moduleName,
371
- String(error)
372
- );
373
399
  }
374
- }
375
- })();
376
- return pluginsLoadPromise;
400
+ })();
401
+ }
402
+ await state.pluginsLoadPromise;
377
403
  };
378
- var ensureIndicatorPluginsLoaded = ensureStrategyPluginsLoaded;
379
- var getStrategyCreator = async (name) => {
380
- await ensureStrategyPluginsLoaded();
381
- return strategyCreators.get(name);
404
+ var ensureIndicatorPluginsLoaded = async (cwd = getTradejsProjectCwd()) => ensureStrategyPluginsLoaded(cwd);
405
+ var getStrategyCreator = async (name, cwd = getTradejsProjectCwd()) => {
406
+ await ensureStrategyPluginsLoaded(cwd);
407
+ const { state } = getStrategyRegistryState(cwd);
408
+ return state.strategyCreators.get(name);
382
409
  };
383
- var getAvailableStrategyNames = async () => {
384
- await ensureStrategyPluginsLoaded();
385
- return [...strategyCreators.keys()].sort((a, b) => a.localeCompare(b));
410
+ var getAvailableStrategyNames = async (cwd = getTradejsProjectCwd()) => {
411
+ await ensureStrategyPluginsLoaded(cwd);
412
+ const { state } = getStrategyRegistryState(cwd);
413
+ return [...state.strategyCreators.keys()].sort((a, b) => a.localeCompare(b));
386
414
  };
387
- var getRegisteredStrategies = () => {
388
- return Object.fromEntries(strategyCreators.entries());
415
+ var getRegisteredStrategies = (cwd = getTradejsProjectCwd()) => {
416
+ const { state } = getStrategyRegistryState(cwd);
417
+ return Object.fromEntries(state.strategyCreators.entries());
389
418
  };
390
- var getRegisteredManifests = () => {
391
- return [...strategyManifestsMap.values()];
419
+ var getRegisteredManifests = (cwd = getTradejsProjectCwd()) => {
420
+ const { state } = getStrategyRegistryState(cwd);
421
+ return [...state.strategyManifestsMap.values()];
392
422
  };
393
- var getStrategyManifest = (name) => {
394
- return name ? strategyManifestsMap.get(name) : void 0;
423
+ var getStrategyManifest = (name, cwd = getTradejsProjectCwd()) => {
424
+ if (!name) {
425
+ return void 0;
426
+ }
427
+ const { state } = getStrategyRegistryState(cwd);
428
+ return state.strategyManifestsMap.get(name);
395
429
  };
396
- var isKnownStrategy = (name) => {
397
- return strategyCreators.has(name);
430
+ var isKnownStrategy = (name, cwd = getTradejsProjectCwd()) => {
431
+ const { state } = getStrategyRegistryState(cwd);
432
+ return state.strategyCreators.has(name);
398
433
  };
399
- var registerStrategyEntries = (entries) => {
400
- registerEntries(entries, "runtime");
434
+ var registerStrategyEntries = (entries, cwd = getTradejsProjectCwd()) => {
435
+ const { state } = getStrategyRegistryState(cwd);
436
+ registerEntries(entries, "runtime", state);
401
437
  };
402
- var resetStrategyRegistryCache = () => {
403
- strategyCreators.clear();
404
- strategyManifestsMap.clear();
405
- (0, import_indicators.resetIndicatorRegistryCache)();
406
- pluginsLoadPromise = null;
438
+ var resetStrategyRegistryCache = (cwd) => {
439
+ const normalizedCwd = String(cwd ?? "").trim();
440
+ if (!normalizedCwd) {
441
+ registryStateByProjectRoot.clear();
442
+ (0, import_indicators.resetIndicatorRegistryCache)();
443
+ return;
444
+ }
445
+ const projectRoot = getTradejsProjectCwd(normalizedCwd);
446
+ registryStateByProjectRoot.delete(projectRoot);
447
+ (0, import_indicators.resetIndicatorRegistryCache)(projectRoot);
407
448
  };
408
449
  var strategies = new Proxy(
409
450
  {},
@@ -412,10 +453,10 @@ var strategies = new Proxy(
412
453
  if (typeof property !== "string") {
413
454
  return void 0;
414
455
  }
415
- return strategyCreators.get(property);
456
+ return getStrategyRegistryState().state.strategyCreators.get(property);
416
457
  },
417
458
  ownKeys: () => {
418
- return [...strategyCreators.keys()];
459
+ return [...getStrategyRegistryState().state.strategyCreators.keys()];
419
460
  },
420
461
  getOwnPropertyDescriptor: () => ({
421
462
  enumerable: true,
package/dist/registry.mjs CHANGED
@@ -11,8 +11,8 @@ import {
11
11
  registerStrategyEntries,
12
12
  resetStrategyRegistryCache,
13
13
  strategies
14
- } from "./chunk-MHCXPD2B.mjs";
15
- import "./chunk-DE7ADBIR.mjs";
14
+ } from "./chunk-CK2PW4L5.mjs";
15
+ import "./chunk-3C76HVLA.mjs";
16
16
  import "./chunk-6DZX6EAA.mjs";
17
17
  export {
18
18
  ensureIndicatorPluginsLoaded,