Skip to content

Commit 3d38913

Browse files
authored
Merge pull request #60 from kcl-lang/nodejs-load-package-api
feat: add load package API in node.js
2 parents e637cf4 + 76338e4 commit 3d38913

File tree

6 files changed

+913
-3
lines changed

6 files changed

+913
-3
lines changed

nodejs/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ fmt:
1111

1212
test:
1313
pnpm test
14+
15+
check:
16+
cargo check
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import test from 'ava'
2+
3+
import { loadPackage, LoadPackageArgs } from '../index.js'
4+
5+
test('loadPackage', (t) => {
6+
const result = loadPackage(LoadPackageArgs(['__test__/test_data/schema.k'], [], true))
7+
t.deepEqual(result.parseErrors, [])
8+
t.deepEqual(result.typeErrors, [])
9+
})

nodejs/index.d.ts

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,318 @@
33

44
/* auto-generated by NAPI-RS */
55

6+
export interface CmdExternalPkgSpec {
7+
pkgName: string
8+
pkgPath: string
9+
}
10+
/** kcl main.k -D name=value */
11+
export interface CmdArgSpec {
12+
name: string
13+
value: string
14+
}
15+
export interface CmdOverrideSpec {
16+
pkgpath: string
17+
fieldPath: string
18+
fieldValue: string
19+
action: string
20+
}
21+
export interface Error {
22+
level: string
23+
code: string
24+
messages: Array<Message>
25+
}
26+
export interface Message {
27+
msg: string
28+
pos?: Position
29+
}
30+
export interface PingArgs {
31+
value: string
32+
}
33+
export interface PingResult {
34+
value: string
35+
}
36+
/** empty */
37+
export interface ListMethodArgs {}
38+
export interface ListMethodResult {
39+
methodNameList: Array<string>
40+
}
41+
export interface ParseFileArgs {
42+
path: string
43+
source: string
44+
externalPkgs: Array<CmdExternalPkgSpec>
45+
}
46+
export interface ParseFileResult {
47+
/** JSON string value */
48+
astJson: string
49+
/** file dependency paths */
50+
deps: Array<string>
51+
/** Parse errors */
52+
errors: Array<Error>
53+
}
54+
export interface ParseProgramArgs {
55+
paths: Array<string>
56+
sources: Array<string>
57+
/** External packages path */
58+
externalPkgs: Array<CmdExternalPkgSpec>
59+
}
60+
export interface ParseProgramResult {
61+
/** JSON string value */
62+
astJson: string
63+
/** Returns the files in the order they should be compiled */
64+
paths: Array<string>
65+
/** Parse errors */
66+
errors: Array<Error>
67+
}
68+
export interface LoadPackageResult {
69+
/** JSON string value */
70+
program: string
71+
/** Returns the files in the order they should be compiled */
72+
paths: Array<string>
73+
/** Parse errors */
74+
parseErrors: Array<Error>
75+
/** Type errors */
76+
typeErrors: Array<Error>
77+
/** Map key is the ScopeIndex json string. */
78+
scopes: Record<string, Scope>
79+
/** Map key is the SymbolIndex json string. */
80+
symbols: Record<string, symbol>
81+
/** Map key is the AST index UUID string. */
82+
nodeSymbolMap: Record<string, SymbolIndex>
83+
/** Map key is the SymbolIndex json string. */
84+
symbolNodeMap: Record<string, string>
85+
/** Map key is the fully_qualified_name e.g. `pkg.Name` */
86+
fullyQualifiedNameMap: Record<string, SymbolIndex>
87+
/** Map key is the package path. */
88+
pkgScopeMap: Record<string, ScopeIndex>
89+
}
90+
export interface ListOptionsResult {
91+
/** Returns the files in the order they should be compiled */
92+
options: Array<OptionHelp>
93+
}
94+
export interface OptionHelp {
95+
name: string
96+
type: string
97+
required: boolean
98+
defaultValue: string
99+
help: string
100+
}
101+
export interface Symbol {
102+
ty?: string
103+
name: string
104+
owner?: SymbolIndex
105+
def?: SymbolIndex
106+
attrs: Array<SymbolIndex>
107+
isGlobal: boolean
108+
}
109+
export interface Scope {
110+
kind: string
111+
parent?: ScopeIndex
112+
owner?: SymbolIndex
113+
children: Array<ScopeIndex>
114+
defs: Array<SymbolIndex>
115+
}
116+
export interface SymbolIndex {
117+
i: number
118+
g: number
119+
kind: string
120+
}
121+
export interface ScopeIndex {
122+
i: number
123+
g: number
124+
kind: string
125+
}
126+
export interface ExecProgramResult {
127+
jsonResult: string
128+
yamlResult: string
129+
logMessage: string
130+
errMessage: string
131+
}
132+
export interface BuildProgramResult {
133+
path: string
134+
}
135+
export interface ResetPluginArgs {
136+
pluginRoot: string
137+
}
138+
/** empty */
139+
export interface ResetPluginResult {}
140+
export interface FormatCodeArgs {
141+
source: string
142+
}
143+
export interface FormatCodeResult {
144+
formatted: Array<number>
145+
}
146+
export interface FormatPathArgs {
147+
path: string
148+
}
149+
export interface FormatPathResult {
150+
changedPaths: Array<string>
151+
}
152+
export interface LintPathArgs {
153+
paths: Array<string>
154+
}
155+
export interface LintPathResult {
156+
results: Array<string>
157+
}
158+
export interface OverrideFileArgs {
159+
file: string
160+
specs: Array<string>
161+
importPaths: Array<string>
162+
}
163+
export interface OverrideFileResult {
164+
result: boolean
165+
}
166+
export interface ListVariablesResult {
167+
variables: Record<string, Variable>
168+
unsupportedCodes: Array<string>
169+
}
170+
export interface Variable {
171+
value: string
172+
}
173+
export interface GetSchemaTypeResult {
174+
schemaTypeList: Array<KclType>
175+
}
176+
export interface GetSchemaTypeMappingArgs {
177+
file: string
178+
code: string
179+
schemaName: string
180+
}
181+
export interface GetSchemaTypeMappingResult {
182+
schemaTypeMapping: Record<string, KclType>
183+
}
184+
export interface ValidateCodeArgs {
185+
datafile: string
186+
data: string
187+
file: string
188+
code: string
189+
schema: string
190+
attributeName: string
191+
format: string
192+
}
193+
export interface ValidateCodeResult {
194+
success: boolean
195+
errMessage: string
196+
}
197+
export interface Position {
198+
line: number
199+
column: number
200+
filename: string
201+
}
202+
export interface ListDepFilesArgs {
203+
workDir: string
204+
useAbsPath: boolean
205+
includeAll: boolean
206+
useFastParser: boolean
207+
}
208+
export interface ListDepFilesResult {
209+
pkgroot: string
210+
pkgpath: string
211+
files: Array<string>
212+
}
213+
export interface LoadSettingsFilesArgs {
214+
workDir: string
215+
files: Array<string>
216+
}
217+
export interface LoadSettingsFilesResult {
218+
kclCliConfigs?: CliConfig
219+
kclOptions: Array<KeyValuePair>
220+
}
221+
export interface CliConfig {
222+
files: Array<string>
223+
output: string
224+
overrides: Array<string>
225+
pathSelector: Array<string>
226+
strictRangeCheck: boolean
227+
disableNone: boolean
228+
verbose: number
229+
debug: boolean
230+
sortKeys: boolean
231+
showHidden: boolean
232+
includeSchemaTypePath: boolean
233+
fastEval: boolean
234+
}
235+
export interface KeyValuePair {
236+
key: string
237+
value: string
238+
}
239+
export interface RenameArgs {
240+
/** the file path to the package root */
241+
packageRoot: string
242+
/** the path to the target symbol to be renamed. The symbol path should conform to format: `<pkgpath>:<field_path>` When the pkgpath is '__main__', `<pkgpath>:` can be omitted. */
243+
symbolPath: string
244+
/** the paths to the source code files */
245+
filePaths: Array<string>
246+
/** the new name of the symbol */
247+
newName: string
248+
}
249+
export interface RenameResult {
250+
/** the file paths got changed */
251+
changedFiles: Array<string>
252+
}
253+
export interface RenameCodeArgs {
254+
/** the file path to the package root */
255+
packageRoot: string
256+
/** the path to the target symbol to be renamed. The symbol path should conform to format: `<pkgpath>:<field_path>` When the pkgpath is '__main__', `<pkgpath>:` can be omitted. */
257+
symbolPath: string
258+
/** the source code. a <filename>:<code> map */
259+
sourceCodes: Record<string, string>
260+
/** the new name of the symbol */
261+
newName: string
262+
}
263+
export interface RenameCodeResult {
264+
/** the changed code. a <filename>:<code> map */
265+
changedCodes: Record<string, string>
266+
}
267+
export interface TestResult {
268+
info: Array<TestCaseInfo>
269+
}
270+
export interface TestCaseInfo {
271+
/** Test case name */
272+
name: string
273+
error: string
274+
logMessage: string
275+
}
276+
export interface KclType {
277+
/** schema, dict, list, str, int, float, bool, any, union, number_multiplier */
278+
type: string
279+
/** union types */
280+
unionTypes: Array<KclType>
281+
/** default value */
282+
default: string
283+
/** schema name */
284+
schemaName: string
285+
/** schema doc */
286+
schemaDoc: string
287+
/** schema properties */
288+
properties: Record<string, KclType>
289+
/** required schema properties, [property_name1, property_name2] */
290+
required: Array<string>
291+
/** dict key type */
292+
key: KclType
293+
/** dict/list item type */
294+
item: KclType
295+
line: number
296+
/** schema decorators */
297+
decorators: Array<Decorator>
298+
/** `filename` represents the absolute path of the file name where the attribute is located. */
299+
filename: string
300+
/** `pkg_path` represents the path name of the package where the attribute is located. */
301+
pkgPath: string
302+
/** `description` represents the document of the attribute. */
303+
description: string
304+
/** A map object to hold examples, the key is the example name. */
305+
examples: Record<string, Example>
306+
}
307+
export interface Decorator {
308+
name: string
309+
arguments: Array<string>
310+
keywords: Record<string, string>
311+
}
312+
export interface Example {
313+
summary: string
314+
description: string
315+
value: string
316+
}
317+
export function loadPackage(args: LoadPackageArgs): LoadPackageResult
6318
export interface ExecProgramResult {
7319
jsonResult: string
8320
yamlResult: string
@@ -18,6 +330,15 @@ export interface Variable {
18330
value: string
19331
}
20332
export function listVariables(args: ListVariablesArgs): ListVariablesResult
333+
export class LoadPackageArgs {
334+
constructor(
335+
paths: Array<string>,
336+
sources: Array<string>,
337+
resolveAst?: boolean | undefined | null,
338+
loadBuiltin?: boolean | undefined | null,
339+
withAstIndex?: boolean | undefined | null,
340+
)
341+
}
21342
export class ExecProgramArgs {
22343
constructor(paths: Array<string>, workDir?: string | undefined | null)
23344
}

nodejs/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,10 @@ if (!nativeBinding) {
284284
throw new Error(`Failed to load native binding`)
285285
}
286286

287-
const { ExecProgramArgs, execProgram, ListVariablesArgs, listVariables } = nativeBinding
287+
const { LoadPackageArgs, loadPackage, ExecProgramArgs, execProgram, ListVariablesArgs, listVariables } = nativeBinding
288288

289+
module.exports.LoadPackageArgs = LoadPackageArgs
290+
module.exports.loadPackage = loadPackage
289291
module.exports.ExecProgramArgs = ExecProgramArgs
290292
module.exports.execProgram = execProgram
291293
module.exports.ListVariablesArgs = ListVariablesArgs

0 commit comments

Comments
 (0)