Skip to content

SDE Market Group Tree ​

Needs local SDE data

Walks the market group hierarchy recursively to display the full market category tree, optionally listing types in each leaf group.

Run it ​

bash
git clone https://github.com/lgriffin/ESI.ts.git && cd ESI.ts
npm ci
npx ts-node scripts/sde/sde-ingest.ts --output sde-data
npm run example:sde-market-tree

examples/sde-market-tree.ts on GitHub. The imports below are written as a consumer of the package writes them; the file in the repository imports from ../src.

Source ​

ts
import { SdeDataProvider } from '@lgriffin/esi.ts/sde';
import type { MarketGroup } from '@lgriffin/esi.ts/sde';
import { createConsoleLogger } from '@lgriffin/esi.ts';

// The program's own output. The client logs through the same console sink
// at ESI_LOG_LEVEL (default warn), so its diagnostics stay out of the way.
const log = createConsoleLogger('info');
const esiLog = createConsoleLogger();

function printTree(
  sde: ReturnType<typeof SdeDataProvider.fromDirectory>,
  group: MarketGroup,
  depth: number = 0,
  maxDepth: number = 3,
) {
  const indent = '  '.repeat(depth);
  const typeCount = group.hasTypes
    ? ` [${sde.getTypesByMarketGroup(group.marketGroupId).length} types]`
    : '';
  log.info(`${indent}- ${group.name} (${group.marketGroupId})${typeCount}`);

  if (depth >= maxDepth) return;

  const children = sde.getMarketGroupsByParent(group.marketGroupId);
  for (const child of children) {
    printTree(sde, child, depth + 1, maxDepth);
  }
}

function main() {
  const sde = SdeDataProvider.fromDirectory(
    process.env.SDE_DATA_PATH || './sde-data',
  );

  try {
    const roots = sde.getRootMarketGroups();
    log.info(`=== Market Group Tree (${roots.length} root categories) ===\n`);

    for (const root of roots) {
      printTree(sde, root);
    }

    // Search example
    log.info('\n--- Search: "Drone" market groups ---');
    const droneGroups = sde.searchMarketGroupsByName('Drone', 10);
    for (const g of droneGroups) {
      const parent = g.parentGroupId
        ? sde.getMarketGroup(g.parentGroupId)
        : null;
      log.info(`  ${g.name} (parent: ${parent?.name ?? 'root'})`);
    }
  } finally {
    sde.close();
  }
}

main();

Released under the GPL-3.0-or-later license.