@openpkg-ts/extract 0.16.1 → 0.18.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/dist/bin/tspec.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  extract
4
- } from "../shared/chunk-bgnmsw8f.js";
4
+ } from "../shared/chunk-c3pkvc28.js";
5
5
 
6
6
  // src/cli/spec.ts
7
7
  import * as fs from "node:fs";
@@ -1488,7 +1488,7 @@ function summary(options) {
1488
1488
  import { normalize, validateSpec } from "@openpkg-ts/spec";
1489
1489
  import { Command } from "commander";
1490
1490
  function createProgram() {
1491
- const program = new Command("tspec").description("Extract TypeScript package API to OpenPkg spec").argument("[entry]", "Entry point file").option("-o, --output <file>", "Output file", "openpkg.json").option("--max-depth <n>", "Max type depth (default: 4)").option("--skip-resolve", "Skip external type resolution").option("--runtime", "Enable Standard Schema runtime extraction").option("-v, --verbose", "Show detailed output").action(async (entry, options) => {
1491
+ const program = new Command("tspec").description("Extract TypeScript package API to OpenPkg spec").argument("[entry]", "Entry point file").option("-o, --output <file>", "Output file", "openpkg.json").option("--max-depth <n>", "Max type depth (default: 4)").option("--skip-resolve", "Skip external type resolution").option("--runtime", "Enable Standard Schema runtime extraction").option("--only <exports>", "Only extract these exports (comma-separated, supports * wildcards)").option("--ignore <exports>", "Ignore these exports (comma-separated, supports * wildcards)").option("-v, --verbose", "Show detailed output").action(async (entry, options) => {
1492
1492
  let entryFile;
1493
1493
  let fromDts = false;
1494
1494
  if (entry) {
@@ -1513,7 +1513,9 @@ function createProgram() {
1513
1513
  entryFile: path.resolve(entryFile),
1514
1514
  ...options.maxDepth ? { maxTypeDepth: parseInt(options.maxDepth) } : {},
1515
1515
  resolveExternalTypes: !options.skipResolve,
1516
- schemaExtraction: options.runtime ? "hybrid" : "static"
1516
+ schemaExtraction: options.runtime ? "hybrid" : "static",
1517
+ ...options.only ? { only: options.only.split(",").map((s) => s.trim()) } : {},
1518
+ ...options.ignore ? { ignore: options.ignore.split(",").map((s) => s.trim()) } : {}
1517
1519
  });
1518
1520
  const normalized = normalize(result.spec);
1519
1521
  const validation = validateSpec(normalized);
@@ -1538,7 +1540,14 @@ function createProgram() {
1538
1540
  return program;
1539
1541
  }
1540
1542
  function findEntryPoint(cwd) {
1541
- const sourceEntries = ["src/index.ts", "index.ts", "lib/index.ts"];
1543
+ const sourceEntries = [
1544
+ "src/index.ts",
1545
+ "index.ts",
1546
+ "lib/index.ts",
1547
+ "src/index.js",
1548
+ "index.js",
1549
+ "lib/index.js"
1550
+ ];
1542
1551
  for (const entry of sourceEntries) {
1543
1552
  const fullPath = path.join(cwd, entry);
1544
1553
  if (fs.existsSync(fullPath))
@@ -1562,9 +1571,13 @@ function findEntryPoint(cwd) {
1562
1571
  }
1563
1572
  if (pkg.main) {
1564
1573
  const mainTs = pkg.main.replace(/\.js$/, ".ts");
1565
- const fullPath = path.join(cwd, mainTs);
1566
- if (fs.existsSync(fullPath))
1567
- return { path: fullPath, fromDts: false };
1574
+ const tsPath = path.join(cwd, mainTs);
1575
+ if (fs.existsSync(tsPath))
1576
+ return { path: tsPath, fromDts: false };
1577
+ const jsPath = path.join(cwd, pkg.main);
1578
+ if (pkg.main.endsWith(".js") && fs.existsSync(jsPath)) {
1579
+ return { path: jsPath, fromDts: false };
1580
+ }
1568
1581
  }
1569
1582
  } catch {}
1570
1583
  }
@@ -762,6 +762,18 @@ function isSymbolDeprecated(symbol) {
762
762
  // src/compiler/program.ts
763
763
  import * as path from "node:path";
764
764
  import ts4 from "typescript";
765
+ function isJsFile(file) {
766
+ return /\.(js|mjs|cjs|jsx)$/.test(file);
767
+ }
768
+ function getScriptKind(file) {
769
+ if (/\.tsx$/.test(file))
770
+ return ts4.ScriptKind.TSX;
771
+ if (/\.jsx$/.test(file))
772
+ return ts4.ScriptKind.JSX;
773
+ if (/\.(js|mjs|cjs)$/.test(file))
774
+ return ts4.ScriptKind.JS;
775
+ return ts4.ScriptKind.TS;
776
+ }
765
777
  var DEFAULT_COMPILER_OPTIONS = {
766
778
  target: ts4.ScriptTarget.Latest,
767
779
  module: ts4.ModuleKind.CommonJS,
@@ -774,21 +786,33 @@ function createProgram({
774
786
  baseDir = path.dirname(entryFile),
775
787
  content
776
788
  }) {
777
- const configPath = ts4.findConfigFile(baseDir, ts4.sys.fileExists, "tsconfig.json");
789
+ let configPath = ts4.findConfigFile(baseDir, ts4.sys.fileExists, "tsconfig.json");
790
+ if (!configPath) {
791
+ configPath = ts4.findConfigFile(baseDir, ts4.sys.fileExists, "jsconfig.json");
792
+ }
778
793
  let compilerOptions = { ...DEFAULT_COMPILER_OPTIONS };
779
794
  if (configPath) {
780
795
  const configFile = ts4.readConfigFile(configPath, ts4.sys.readFile);
781
796
  const parsedConfig = ts4.parseJsonConfigFileContent(configFile.config, ts4.sys, path.dirname(configPath));
782
797
  compilerOptions = { ...compilerOptions, ...parsedConfig.options };
783
798
  }
784
- const allowJsVal = compilerOptions.allowJs;
785
- if (typeof allowJsVal === "boolean" && allowJsVal) {
786
- compilerOptions = { ...compilerOptions, allowJs: false, checkJs: false };
799
+ if (isJsFile(entryFile)) {
800
+ compilerOptions = {
801
+ ...compilerOptions,
802
+ allowJs: true,
803
+ checkJs: true,
804
+ isolatedDeclarations: false
805
+ };
806
+ } else {
807
+ const allowJsVal = compilerOptions.allowJs;
808
+ if (typeof allowJsVal === "boolean" && allowJsVal) {
809
+ compilerOptions = { ...compilerOptions, allowJs: false, checkJs: false };
810
+ }
787
811
  }
788
812
  const compilerHost = ts4.createCompilerHost(compilerOptions, true);
789
813
  let inMemorySource;
790
814
  if (content !== undefined) {
791
- inMemorySource = ts4.createSourceFile(entryFile, content, ts4.ScriptTarget.Latest, true, ts4.ScriptKind.TS);
815
+ inMemorySource = ts4.createSourceFile(entryFile, content, ts4.ScriptTarget.Latest, true, getScriptKind(entryFile));
792
816
  const originalGetSourceFile = compilerHost.getSourceFile.bind(compilerHost);
793
817
  compilerHost.getSourceFile = (fileName, languageVersion, onError, shouldCreateNewSourceFile) => {
794
818
  if (fileName === entryFile) {
@@ -1562,6 +1586,20 @@ var BUILTIN_TYPES2 = new Set([
1562
1586
  "InstanceType",
1563
1587
  "ThisType"
1564
1588
  ]);
1589
+ function matchesPattern(name, pattern) {
1590
+ if (!pattern.includes("*"))
1591
+ return name === pattern;
1592
+ const regex = new RegExp("^" + pattern.replace(/\*/g, ".*") + "$");
1593
+ return regex.test(name);
1594
+ }
1595
+ function shouldIncludeExport(name, only, ignore) {
1596
+ if (ignore?.some((p) => matchesPattern(name, p)))
1597
+ return false;
1598
+ if (only && only.length > 0) {
1599
+ return only.some((p) => matchesPattern(name, p));
1600
+ }
1601
+ return true;
1602
+ }
1565
1603
  function shouldSkipDanglingRef(name) {
1566
1604
  if (name.startsWith("__"))
1567
1605
  return true;
@@ -1581,7 +1619,9 @@ async function extract(options) {
1581
1619
  maxTypeDepth,
1582
1620
  maxExternalTypeDepth,
1583
1621
  resolveExternalTypes,
1584
- includeSchema
1622
+ includeSchema,
1623
+ only,
1624
+ ignore
1585
1625
  } = options;
1586
1626
  const diagnostics = [];
1587
1627
  const exports = [];
@@ -1614,6 +1654,8 @@ async function extract(options) {
1614
1654
  ctx.exportedIds = exportedIds;
1615
1655
  for (const symbol of exportedSymbols) {
1616
1656
  const exportName = symbol.getName();
1657
+ if (!shouldIncludeExport(exportName, only, ignore))
1658
+ continue;
1617
1659
  try {
1618
1660
  const { declaration, targetSymbol } = resolveExportTarget(symbol, typeChecker);
1619
1661
  if (!declaration)
@@ -75,6 +75,10 @@ interface ExtractOptions {
75
75
  schemaExtraction?: "static" | "hybrid";
76
76
  /** Include $schema URL in output */
77
77
  includeSchema?: boolean;
78
+ /** Only extract these exports (supports * wildcards) */
79
+ only?: string[];
80
+ /** Ignore these exports (supports * wildcards) */
81
+ ignore?: string[];
78
82
  }
79
83
  interface ExtractResult {
80
84
  spec: OpenPkg;
package/dist/src/index.js CHANGED
@@ -26,7 +26,7 @@ import {
26
26
  serializeTypeAlias,
27
27
  serializeVariable,
28
28
  withDescription
29
- } from "../shared/chunk-bgnmsw8f.js";
29
+ } from "../shared/chunk-c3pkvc28.js";
30
30
  // src/schema/registry.ts
31
31
  function isTypeReference(type) {
32
32
  return !!(type.flags & 524288 && type.objectFlags && type.objectFlags & 4);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpkg-ts/extract",
3
- "version": "0.16.1",
3
+ "version": "0.18.0",
4
4
  "description": "TypeScript export extraction to OpenPkg spec",
5
5
  "keywords": [
6
6
  "openpkg",