@openpkg-ts/sdk 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 +127 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +2140 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# OpenPkg SDK
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@openpkg-ts/sdk)
|
|
4
|
+
|
|
5
|
+
TypeScript SDK for generating and post-processing OpenPkg specs directly from your tooling.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# npm
|
|
11
|
+
npm install @openpkg-ts/sdk
|
|
12
|
+
|
|
13
|
+
# bun
|
|
14
|
+
bun add @openpkg-ts/sdk
|
|
15
|
+
|
|
16
|
+
# yarn
|
|
17
|
+
yarn add @openpkg-ts/sdk
|
|
18
|
+
|
|
19
|
+
# pnpm
|
|
20
|
+
pnpm add @openpkg-ts/sdk
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { OpenPkg } from '@openpkg-ts/sdk';
|
|
27
|
+
|
|
28
|
+
const openpkg = new OpenPkg({
|
|
29
|
+
resolveExternalTypes: true,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const spec = await openpkg.analyzeFile('./src/index.ts', {
|
|
33
|
+
filters: {
|
|
34
|
+
include: ['createUser', 'deleteUser'],
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
console.log(`exports: ${spec.exports.length}`);
|
|
39
|
+
console.log(`types: ${spec.types?.length ?? 0}`);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`OpenPkg` automatically resolves local sources, merges in declaration files, and keeps type references intact. Use `filters.include` / `filters.exclude` to narrow the surface area that lands in the final spec.
|
|
43
|
+
|
|
44
|
+
## Filtering Exports
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { analyzeFile } from '@openpkg-ts/sdk';
|
|
48
|
+
|
|
49
|
+
const spec = await analyzeFile('./src/index.ts', {
|
|
50
|
+
filters: {
|
|
51
|
+
include: ['publicFunction'],
|
|
52
|
+
exclude: ['internalHelper'],
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Filtering trims both the `exports` array and orphaned items under `types`. The SDK will surface informational diagnostics whenever an identifier cannot be located or when filtering drops transitive types you may still need.
|
|
58
|
+
|
|
59
|
+
## Diagnostics
|
|
60
|
+
|
|
61
|
+
Use the `analyzeFileWithDiagnostics` or `analyzeWithDiagnostics` helpers when you need visibility into parsing or filtering issues.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { OpenPkg } from '@openpkg-ts/sdk';
|
|
65
|
+
|
|
66
|
+
const openpkg = new OpenPkg();
|
|
67
|
+
const { spec, diagnostics } = await openpkg.analyzeFileWithDiagnostics('./src/index.ts');
|
|
68
|
+
|
|
69
|
+
diagnostics.forEach((diagnostic) => {
|
|
70
|
+
const location = diagnostic.location?.file
|
|
71
|
+
? `${diagnostic.location.file}:${diagnostic.location.line ?? '?'}:${diagnostic.location.column ?? '?'}`
|
|
72
|
+
: '(unknown)';
|
|
73
|
+
console.log(`[${diagnostic.severity}] ${location} ${diagnostic.message}`);
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Diagnostics normalize TypeScript compiler messages into `error`, `warning`, and `info` severity levels so you can decide how to surface them in your own tools.
|
|
78
|
+
|
|
79
|
+
## Programmatic Workflows
|
|
80
|
+
|
|
81
|
+
### Analyze in-memory code
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { analyze } from '@openpkg-ts/sdk';
|
|
85
|
+
|
|
86
|
+
const spec = await analyze(
|
|
87
|
+
`export const sum = (a: number, b: number) => a + b;`,
|
|
88
|
+
{ filters: { include: ['sum'] } },
|
|
89
|
+
);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Batch project analysis
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { OpenPkg } from '@openpkg-ts/sdk';
|
|
96
|
+
import { glob } from 'glob';
|
|
97
|
+
|
|
98
|
+
const openpkg = new OpenPkg();
|
|
99
|
+
const files = await glob('packages/**/src/index.ts');
|
|
100
|
+
const specs = await Promise.all(files.map((file) => openpkg.analyzeFile(file)));
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## API Surface
|
|
104
|
+
|
|
105
|
+
- `new OpenPkg(options?)`
|
|
106
|
+
- `analyze(code, fileName?, options?)`
|
|
107
|
+
- `analyzeFile(filePath, options?)`
|
|
108
|
+
- `analyzeWithDiagnostics(code, fileName?, options?)`
|
|
109
|
+
- `analyzeFileWithDiagnostics(filePath, options?)`
|
|
110
|
+
- `analyze(code, options?)` – convenience wrapper
|
|
111
|
+
- `analyzeFile(filePath, options?)` – convenience wrapper
|
|
112
|
+
- `extractPackageSpec(entry, packageDir, source, options)` – lower-level extractor
|
|
113
|
+
- Types: `OpenPkgSpec`, `FilterOptions`, `AnalyzeOptions`, `AnalysisResult`, `Diagnostic`
|
|
114
|
+
|
|
115
|
+
## Development
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
git clone https://github.com/ryanwaits/openpkg.git
|
|
119
|
+
cd openpkg
|
|
120
|
+
bun install
|
|
121
|
+
bun run build:sdk
|
|
122
|
+
bun test
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
interface OpenPkgOptions {
|
|
2
|
+
includePrivate?: boolean;
|
|
3
|
+
followImports?: boolean;
|
|
4
|
+
maxDepth?: number;
|
|
5
|
+
resolveExternalTypes?: boolean;
|
|
6
|
+
}
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { OpenPkg } from "@openpkg-ts/spec";
|
|
9
|
+
declare const openPkgSchema: z.ZodTypeAny;
|
|
10
|
+
type OpenPkgSpec = OpenPkg;
|
|
11
|
+
declare function extractPackageSpec(entryFile: string, packageDir?: string, content?: string, options?: OpenPkgOptions): Promise<OpenPkgSpec>;
|
|
12
|
+
interface FilterOptions {
|
|
13
|
+
include?: string[];
|
|
14
|
+
exclude?: string[];
|
|
15
|
+
}
|
|
16
|
+
interface Diagnostic {
|
|
17
|
+
message: string;
|
|
18
|
+
severity: "error" | "warning" | "info";
|
|
19
|
+
location?: {
|
|
20
|
+
file: string
|
|
21
|
+
line?: number
|
|
22
|
+
column?: number
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
interface AnalysisResult {
|
|
26
|
+
spec: OpenPkgSpec;
|
|
27
|
+
diagnostics: Diagnostic[];
|
|
28
|
+
metadata: AnalysisMetadata;
|
|
29
|
+
}
|
|
30
|
+
interface AnalysisMetadata {
|
|
31
|
+
baseDir: string;
|
|
32
|
+
configPath?: string;
|
|
33
|
+
packageJsonPath?: string;
|
|
34
|
+
hasNodeModules: boolean;
|
|
35
|
+
resolveExternalTypes: boolean;
|
|
36
|
+
}
|
|
37
|
+
interface AnalyzeOptions {
|
|
38
|
+
filters?: FilterOptions;
|
|
39
|
+
}
|
|
40
|
+
declare class OpenPkg2 {
|
|
41
|
+
private readonly options;
|
|
42
|
+
constructor(options?: OpenPkgOptions);
|
|
43
|
+
analyze(code: string, fileName?: string, analyzeOptions?: AnalyzeOptions): Promise<OpenPkgSpec>;
|
|
44
|
+
analyzeFile(filePath: string, analyzeOptions?: AnalyzeOptions): Promise<OpenPkgSpec>;
|
|
45
|
+
analyzeProject(entryPath: string, analyzeOptions?: AnalyzeOptions): Promise<OpenPkgSpec>;
|
|
46
|
+
analyzeWithDiagnostics(code: string, fileName?: string, analyzeOptions?: AnalyzeOptions): Promise<AnalysisResult>;
|
|
47
|
+
analyzeFileWithDiagnostics(filePath: string, analyzeOptions?: AnalyzeOptions): Promise<AnalysisResult>;
|
|
48
|
+
private normalizeDiagnostic;
|
|
49
|
+
private mapSeverity;
|
|
50
|
+
private normalizeMetadata;
|
|
51
|
+
private applySpecFilters;
|
|
52
|
+
}
|
|
53
|
+
declare function analyze(code: string, options?: AnalyzeOptions): Promise<OpenPkgSpec>;
|
|
54
|
+
declare function analyzeFile(filePath: string, options?: AnalyzeOptions): Promise<OpenPkgSpec>;
|
|
55
|
+
export { openPkgSchema, extractPackageSpec, analyzeFile, analyze, OpenPkgSpec, OpenPkgOptions, OpenPkg2 as OpenPkg, FilterOptions, Diagnostic, AnalyzeOptions, AnalysisResult };
|