From 031634a7aa637baf252e8e97ada4eaad5b9885b8 Mon Sep 17 00:00:00 2001 From: Jason Gwartz Date: Tue, 6 Aug 2024 11:31:51 +0100 Subject: [PATCH 1/3] Export generic load() function, and generate type-only .d.ts files --- .../output/01_primitive_types.pkl.d.ts | 26 ++++++++++++ .../output/01_primitive_types.pkl.ts | 40 ------------------- ...ections.pkl.ts => 02_collections.pkl.d.ts} | 14 ------- .../output/03_nullables.pkl.d.ts | 16 ++++++++ .../snippet-tests/output/03_nullables.pkl.ts | 30 -------------- ...th_class.pkl.ts => 04_with_class.pkl.d.ts} | 14 ------- .../output/05_with_pair.pkl.d.ts | 10 +++++ .../snippet-tests/output/05_with_pair.pkl.ts | 24 ----------- ...ias.pkl.ts => 06_with_type_alias.pkl.d.ts} | 14 ------- .../output/07_literal_types.pkl.d.ts | 12 ++++++ .../output/07_literal_types.pkl.ts | 26 ------------ ...th_union.pkl.ts => 08_with_union.pkl.d.ts} | 14 ------- .../output/09_custom_types.pkl.d.ts | 24 +++++++++++ .../output/09_custom_types.pkl.ts | 38 ------------------ .../output/11_with_import.pkl.d.ts | 21 ++++++++++ .../output/11_with_import.pkl.ts | 35 ---------------- ...pkl_typescript_tests_named_module.pkl.d.ts | 10 +++++ ...0_pkl_typescript_tests_named_module.pkl.ts | 24 ----------- ...script_tests_module_name_override.pkl.d.ts | 20 ++++++++++ ...pescript_tests_module_name_override.pkl.ts | 34 ---------------- codegen/src/internal/TypescriptModule.pkl | 21 +--------- src/index.ts | 35 +++++++++++++--- 22 files changed, 169 insertions(+), 333 deletions(-) create mode 100644 codegen/snippet-tests/output/01_primitive_types.pkl.d.ts delete mode 100644 codegen/snippet-tests/output/01_primitive_types.pkl.ts rename codegen/snippet-tests/output/{02_collections.pkl.ts => 02_collections.pkl.d.ts} (56%) create mode 100644 codegen/snippet-tests/output/03_nullables.pkl.d.ts delete mode 100644 codegen/snippet-tests/output/03_nullables.pkl.ts rename codegen/snippet-tests/output/{04_with_class.pkl.ts => 04_with_class.pkl.d.ts} (68%) create mode 100644 codegen/snippet-tests/output/05_with_pair.pkl.d.ts delete mode 100644 codegen/snippet-tests/output/05_with_pair.pkl.ts rename codegen/snippet-tests/output/{06_with_type_alias.pkl.ts => 06_with_type_alias.pkl.d.ts} (50%) create mode 100644 codegen/snippet-tests/output/07_literal_types.pkl.d.ts delete mode 100644 codegen/snippet-tests/output/07_literal_types.pkl.ts rename codegen/snippet-tests/output/{08_with_union.pkl.ts => 08_with_union.pkl.d.ts} (65%) create mode 100644 codegen/snippet-tests/output/09_custom_types.pkl.d.ts delete mode 100644 codegen/snippet-tests/output/09_custom_types.pkl.ts create mode 100644 codegen/snippet-tests/output/11_with_import.pkl.d.ts delete mode 100644 codegen/snippet-tests/output/11_with_import.pkl.ts create mode 100644 codegen/snippet-tests/output/n10_pkl_typescript_tests_named_module.pkl.d.ts delete mode 100644 codegen/snippet-tests/output/n10_pkl_typescript_tests_named_module.pkl.ts create mode 100644 codegen/snippet-tests/output/n12_pkl_typescript_tests_module_name_override.pkl.d.ts delete mode 100644 codegen/snippet-tests/output/n12_pkl_typescript_tests_module_name_override.pkl.ts diff --git a/codegen/snippet-tests/output/01_primitive_types.pkl.d.ts b/codegen/snippet-tests/output/01_primitive_types.pkl.d.ts new file mode 100644 index 0000000..ee0fa5e --- /dev/null +++ b/codegen/snippet-tests/output/01_primitive_types.pkl.d.ts @@ -0,0 +1,26 @@ +/* This file was generated by `pkl-typescript` from Pkl module `01-primitiveTypes`. */ +/* DO NOT EDIT! */ +/* istanbul ignore file */ +/* eslint-disable */ +import * as pklTypescript from "@pkl-community/pkl-typescript" + +// Ref: Module root. +export interface N01PrimitiveTypes { + str: string + + int: number + + int8: number + + uint: number + + float: number + + bool: boolean + + nullType: null + + anyType: pklTypescript.Any + + nothingType: never +} diff --git a/codegen/snippet-tests/output/01_primitive_types.pkl.ts b/codegen/snippet-tests/output/01_primitive_types.pkl.ts deleted file mode 100644 index abb010b..0000000 --- a/codegen/snippet-tests/output/01_primitive_types.pkl.ts +++ /dev/null @@ -1,40 +0,0 @@ -/* This file was generated by `pkl-typescript` from Pkl module `01-primitiveTypes`. */ -/* DO NOT EDIT! */ -/* istanbul ignore file */ -/* eslint-disable */ -import * as pklTypescript from "@pkl-community/pkl-typescript" - -// Ref: Module root. -export interface N01PrimitiveTypes { - str: string - - int: number - - int8: number - - uint: number - - float: number - - bool: boolean - - nullType: null - - anyType: pklTypescript.Any - - nothingType: never -} - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N01PrimitiveTypes -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/02_collections.pkl.ts b/codegen/snippet-tests/output/02_collections.pkl.d.ts similarity index 56% rename from codegen/snippet-tests/output/02_collections.pkl.ts rename to codegen/snippet-tests/output/02_collections.pkl.d.ts index 6d782e3..894ed5e 100644 --- a/codegen/snippet-tests/output/02_collections.pkl.ts +++ b/codegen/snippet-tests/output/02_collections.pkl.d.ts @@ -32,17 +32,3 @@ export interface N02Collections { setInt: Set } - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N02Collections -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/03_nullables.pkl.d.ts b/codegen/snippet-tests/output/03_nullables.pkl.d.ts new file mode 100644 index 0000000..fc2dcc0 --- /dev/null +++ b/codegen/snippet-tests/output/03_nullables.pkl.d.ts @@ -0,0 +1,16 @@ +/* This file was generated by `pkl-typescript` from Pkl module `03-nullables`. */ +/* DO NOT EDIT! */ +/* istanbul ignore file */ +/* eslint-disable */ +import * as pklTypescript from "@pkl-community/pkl-typescript" + +// Ref: Module root. +export interface N03Nullables { + nullableString: string|null + + nullableInt: number|null + + nullableListingOfStrings: Array|null + + listingOfNullableStrings: Array +} diff --git a/codegen/snippet-tests/output/03_nullables.pkl.ts b/codegen/snippet-tests/output/03_nullables.pkl.ts deleted file mode 100644 index 33d086b..0000000 --- a/codegen/snippet-tests/output/03_nullables.pkl.ts +++ /dev/null @@ -1,30 +0,0 @@ -/* This file was generated by `pkl-typescript` from Pkl module `03-nullables`. */ -/* DO NOT EDIT! */ -/* istanbul ignore file */ -/* eslint-disable */ -import * as pklTypescript from "@pkl-community/pkl-typescript" - -// Ref: Module root. -export interface N03Nullables { - nullableString: string|null - - nullableInt: number|null - - nullableListingOfStrings: Array|null - - listingOfNullableStrings: Array -} - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N03Nullables -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/04_with_class.pkl.ts b/codegen/snippet-tests/output/04_with_class.pkl.d.ts similarity index 68% rename from codegen/snippet-tests/output/04_with_class.pkl.ts rename to codegen/snippet-tests/output/04_with_class.pkl.d.ts index b52c22a..252a130 100644 --- a/codegen/snippet-tests/output/04_with_class.pkl.ts +++ b/codegen/snippet-tests/output/04_with_class.pkl.d.ts @@ -53,17 +53,3 @@ export interface MyConcreteClass extends MyAbstractClass { overridableUnion2: string } - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N04WithClass -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/05_with_pair.pkl.d.ts b/codegen/snippet-tests/output/05_with_pair.pkl.d.ts new file mode 100644 index 0000000..9e3354f --- /dev/null +++ b/codegen/snippet-tests/output/05_with_pair.pkl.d.ts @@ -0,0 +1,10 @@ +/* This file was generated by `pkl-typescript` from Pkl module `05-withPair`. */ +/* DO NOT EDIT! */ +/* istanbul ignore file */ +/* eslint-disable */ +import * as pklTypescript from "@pkl-community/pkl-typescript" + +// Ref: Module root. +export interface N05WithPair { + x: pklTypescript.Pair +} diff --git a/codegen/snippet-tests/output/05_with_pair.pkl.ts b/codegen/snippet-tests/output/05_with_pair.pkl.ts deleted file mode 100644 index 2f5dcb4..0000000 --- a/codegen/snippet-tests/output/05_with_pair.pkl.ts +++ /dev/null @@ -1,24 +0,0 @@ -/* This file was generated by `pkl-typescript` from Pkl module `05-withPair`. */ -/* DO NOT EDIT! */ -/* istanbul ignore file */ -/* eslint-disable */ -import * as pklTypescript from "@pkl-community/pkl-typescript" - -// Ref: Module root. -export interface N05WithPair { - x: pklTypescript.Pair -} - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N05WithPair -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/06_with_type_alias.pkl.ts b/codegen/snippet-tests/output/06_with_type_alias.pkl.d.ts similarity index 50% rename from codegen/snippet-tests/output/06_with_type_alias.pkl.ts rename to codegen/snippet-tests/output/06_with_type_alias.pkl.d.ts index f513f6a..6e4ff7a 100644 --- a/codegen/snippet-tests/output/06_with_type_alias.pkl.ts +++ b/codegen/snippet-tests/output/06_with_type_alias.pkl.d.ts @@ -23,17 +23,3 @@ type MyStringAlias = string // Ref: Pkl type `06-withTypeAlias.MyAliasedClass`. type MyAliasedClass = MyClassToBeAliased - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N06WithTypeAlias -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/07_literal_types.pkl.d.ts b/codegen/snippet-tests/output/07_literal_types.pkl.d.ts new file mode 100644 index 0000000..fcdbee7 --- /dev/null +++ b/codegen/snippet-tests/output/07_literal_types.pkl.d.ts @@ -0,0 +1,12 @@ +/* This file was generated by `pkl-typescript` from Pkl module `07-literalTypes`. */ +/* DO NOT EDIT! */ +/* istanbul ignore file */ +/* eslint-disable */ +import * as pklTypescript from "@pkl-community/pkl-typescript" + +// Ref: Module root. +export interface N07LiteralTypes { + x: "one" + + y: "two" +} diff --git a/codegen/snippet-tests/output/07_literal_types.pkl.ts b/codegen/snippet-tests/output/07_literal_types.pkl.ts deleted file mode 100644 index 273948b..0000000 --- a/codegen/snippet-tests/output/07_literal_types.pkl.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* This file was generated by `pkl-typescript` from Pkl module `07-literalTypes`. */ -/* DO NOT EDIT! */ -/* istanbul ignore file */ -/* eslint-disable */ -import * as pklTypescript from "@pkl-community/pkl-typescript" - -// Ref: Module root. -export interface N07LiteralTypes { - x: "one" - - y: "two" -} - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N07LiteralTypes -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/08_with_union.pkl.ts b/codegen/snippet-tests/output/08_with_union.pkl.d.ts similarity index 65% rename from codegen/snippet-tests/output/08_with_union.pkl.ts rename to codegen/snippet-tests/output/08_with_union.pkl.d.ts index 853837b..b3ca86a 100644 --- a/codegen/snippet-tests/output/08_with_union.pkl.ts +++ b/codegen/snippet-tests/output/08_with_union.pkl.d.ts @@ -54,17 +54,3 @@ type PersonName = string // Ref: Pkl type `08-withUnion.PersonAge`. type PersonAge = number - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N08WithUnion -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/09_custom_types.pkl.d.ts b/codegen/snippet-tests/output/09_custom_types.pkl.d.ts new file mode 100644 index 0000000..54cbaeb --- /dev/null +++ b/codegen/snippet-tests/output/09_custom_types.pkl.d.ts @@ -0,0 +1,24 @@ +/* This file was generated by `pkl-typescript` from Pkl module `09-customTypes`. */ +/* DO NOT EDIT! */ +/* istanbul ignore file */ +/* eslint-disable */ +import * as pklTypescript from "@pkl-community/pkl-typescript" + +// Ref: Module root. +export interface N09CustomTypes { + varAny: pklTypescript.Any + + varDymaic: pklTypescript.Dynamic + + varDataSizeUnit: pklTypescript.DataSizeUnit + + varDataSize: pklTypescript.DataSize + + varDuration: pklTypescript.Duration + + varIntSeq: pklTypescript.IntSeq + + varRegex: pklTypescript.Regex + + varPair: pklTypescript.Pair +} diff --git a/codegen/snippet-tests/output/09_custom_types.pkl.ts b/codegen/snippet-tests/output/09_custom_types.pkl.ts deleted file mode 100644 index c5d056d..0000000 --- a/codegen/snippet-tests/output/09_custom_types.pkl.ts +++ /dev/null @@ -1,38 +0,0 @@ -/* This file was generated by `pkl-typescript` from Pkl module `09-customTypes`. */ -/* DO NOT EDIT! */ -/* istanbul ignore file */ -/* eslint-disable */ -import * as pklTypescript from "@pkl-community/pkl-typescript" - -// Ref: Module root. -export interface N09CustomTypes { - varAny: pklTypescript.Any - - varDymaic: pklTypescript.Dynamic - - varDataSizeUnit: pklTypescript.DataSizeUnit - - varDataSize: pklTypescript.DataSize - - varDuration: pklTypescript.Duration - - varIntSeq: pklTypescript.IntSeq - - varRegex: pklTypescript.Regex - - varPair: pklTypescript.Pair -} - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N09CustomTypes -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/11_with_import.pkl.d.ts b/codegen/snippet-tests/output/11_with_import.pkl.d.ts new file mode 100644 index 0000000..4872922 --- /dev/null +++ b/codegen/snippet-tests/output/11_with_import.pkl.d.ts @@ -0,0 +1,21 @@ +/* This file was generated by `pkl-typescript` from Pkl module `11-withImport`. */ +/* DO NOT EDIT! */ +/* istanbul ignore file */ +/* eslint-disable */ +import * as pklTypescript from "@pkl-community/pkl-typescript" + +// Ref: Module root. +export interface N11WithImport { + value: ExampleClass +} + +// Ref: Pkl class `moduleWithClass.ExampleClass`. +export interface ExampleClass { + x: string + + y: number +} + +// Ref: Module root. +export interface ModuleWithClass { +} diff --git a/codegen/snippet-tests/output/11_with_import.pkl.ts b/codegen/snippet-tests/output/11_with_import.pkl.ts deleted file mode 100644 index fba56fd..0000000 --- a/codegen/snippet-tests/output/11_with_import.pkl.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* This file was generated by `pkl-typescript` from Pkl module `11-withImport`. */ -/* DO NOT EDIT! */ -/* istanbul ignore file */ -/* eslint-disable */ -import * as pklTypescript from "@pkl-community/pkl-typescript" - -// Ref: Module root. -export interface N11WithImport { - value: ExampleClass -} - -// Ref: Pkl class `moduleWithClass.ExampleClass`. -export interface ExampleClass { - x: string - - y: number -} - -// Ref: Module root. -export interface ModuleWithClass { -} - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N11WithImport -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/n10_pkl_typescript_tests_named_module.pkl.d.ts b/codegen/snippet-tests/output/n10_pkl_typescript_tests_named_module.pkl.d.ts new file mode 100644 index 0000000..44cd690 --- /dev/null +++ b/codegen/snippet-tests/output/n10_pkl_typescript_tests_named_module.pkl.d.ts @@ -0,0 +1,10 @@ +/* This file was generated by `pkl-typescript` from Pkl module `n10.pkl.typescript.tests.namedModule`. */ +/* DO NOT EDIT! */ +/* istanbul ignore file */ +/* eslint-disable */ +import * as pklTypescript from "@pkl-community/pkl-typescript" + +// Ref: Module root. +export interface NamedModule { + x: string +} diff --git a/codegen/snippet-tests/output/n10_pkl_typescript_tests_named_module.pkl.ts b/codegen/snippet-tests/output/n10_pkl_typescript_tests_named_module.pkl.ts deleted file mode 100644 index ddc1a8e..0000000 --- a/codegen/snippet-tests/output/n10_pkl_typescript_tests_named_module.pkl.ts +++ /dev/null @@ -1,24 +0,0 @@ -/* This file was generated by `pkl-typescript` from Pkl module `n10.pkl.typescript.tests.namedModule`. */ -/* DO NOT EDIT! */ -/* istanbul ignore file */ -/* eslint-disable */ -import * as pklTypescript from "@pkl-community/pkl-typescript" - -// Ref: Module root. -export interface NamedModule { - x: string -} - -// LoadFromPath loads the pkl module at the given path and evaluates it into a NamedModule -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/snippet-tests/output/n12_pkl_typescript_tests_module_name_override.pkl.d.ts b/codegen/snippet-tests/output/n12_pkl_typescript_tests_module_name_override.pkl.d.ts new file mode 100644 index 0000000..6d3629b --- /dev/null +++ b/codegen/snippet-tests/output/n12_pkl_typescript_tests_module_name_override.pkl.d.ts @@ -0,0 +1,20 @@ +/* This file was generated by `pkl-typescript` from Pkl module `n12.pkl.typescript.tests.moduleNameOverride`. */ +/* DO NOT EDIT! */ +/* istanbul ignore file */ +/* eslint-disable */ +import * as pklTypescript from "@pkl-community/pkl-typescript" + +// Ref: Module root. +export interface N12TypescriptGeneratedInterface { + classTypeProperty: MyCustomClassName + + typeAliasProperty: ThisTypeAlias +} + +// Ref: Pkl class `n12.pkl.typescript.tests.moduleNameOverride.CustomClassImpl`. +export interface MyCustomClassName { + name: string +} + +// Ref: Pkl type `n12.pkl.typescript.tests.moduleNameOverride.AliasName`. +type ThisTypeAlias = "x" | "y" | MyCustomClassName diff --git a/codegen/snippet-tests/output/n12_pkl_typescript_tests_module_name_override.pkl.ts b/codegen/snippet-tests/output/n12_pkl_typescript_tests_module_name_override.pkl.ts deleted file mode 100644 index 5104593..0000000 --- a/codegen/snippet-tests/output/n12_pkl_typescript_tests_module_name_override.pkl.ts +++ /dev/null @@ -1,34 +0,0 @@ -/* This file was generated by `pkl-typescript` from Pkl module `n12.pkl.typescript.tests.moduleNameOverride`. */ -/* DO NOT EDIT! */ -/* istanbul ignore file */ -/* eslint-disable */ -import * as pklTypescript from "@pkl-community/pkl-typescript" - -// Ref: Module root. -export interface N12TypescriptGeneratedInterface { - classTypeProperty: MyCustomClassName - - typeAliasProperty: ThisTypeAlias -} - -// Ref: Pkl class `n12.pkl.typescript.tests.moduleNameOverride.CustomClassImpl`. -export interface MyCustomClassName { - name: string -} - -// Ref: Pkl type `n12.pkl.typescript.tests.moduleNameOverride.AliasName`. -type ThisTypeAlias = "x" | "y" | MyCustomClassName - -// LoadFromPath loads the pkl module at the given path and evaluates it into a N12TypescriptGeneratedInterface -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/codegen/src/internal/TypescriptModule.pkl b/codegen/src/internal/TypescriptModule.pkl index 02442bb..bcc21ed 100644 --- a/codegen/src/internal/TypescriptModule.pkl +++ b/codegen/src/internal/TypescriptModule.pkl @@ -82,25 +82,6 @@ contents: String = new Listing { gen.contents "" } - - when (!`module`.modifiers.contains("open") && !`module`.modifiers.contains("abstract")) { - """ - // LoadFromPath loads the pkl module at the given path and evaluates it into a \(moduleClass.name) - export const loadFromPath = async (path: string): Promise<\(moduleClass.type.render(moduleClass.typescriptModule))> => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } - }; - - export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise<\(moduleClass.type.render(moduleClass.typescriptModule))> => - evaluator.evaluateModule(source) as Promise<\(moduleClass.type.render(moduleClass.typescriptModule))>; - - """ - } }.join("\n") @@ -109,7 +90,7 @@ output { // Snake-case the generated filenames, as per the Google TypeScript styleguide: // https://google.github.io/styleguide/tsguide.html#identifiers-imports // > "Module namespace imports are lowerCamelCase while files are snake_case" - ["\(utils.snakeCase(`module`.name)).pkl.ts"] { + ["\(utils.snakeCase(`module`.name)).pkl.d.ts"] { text = contents } } diff --git a/src/index.ts b/src/index.ts index 39affa9..9c15a28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,32 @@ -export {PreconfiguredOptions, EvaluatorOptions} from "./evaluator/evaluator_options"; -export {FileSource, TextSource, UriSource, ModuleSource} from "./evaluator/module_source"; +export { PreconfiguredOptions, EvaluatorOptions } from "./evaluator/evaluator_options"; +export { FileSource, TextSource, UriSource, ModuleSource } from "./evaluator/module_source"; export { - newEvaluator, newEvaluatorWithCommand, newProjectEvaluator, newProjectEvaluatorWithCommand + newEvaluator, + newEvaluatorWithCommand, + newProjectEvaluator, + newProjectEvaluatorWithCommand, } from "./evaluator/evaluator_exec"; -export {newEvaluatorManager, newEvaluatorManagerWithCommand} from "./evaluator/evaluator_manager"; -export type {Evaluator} from "./evaluator/evaluator"; -export * from "./types/pkl" +export { newEvaluatorManager, newEvaluatorManagerWithCommand } from "./evaluator/evaluator_manager"; +export type { Evaluator } from "./evaluator/evaluator"; +export * from "./types/pkl"; + +import type { Evaluator } from "./evaluator/evaluator"; +import { newEvaluator } from "./evaluator/evaluator_exec"; +import { PreconfiguredOptions } from "./evaluator/evaluator_options"; +import type { ModuleSource } from "./evaluator/module_source"; +import { FileSource } from "./evaluator/module_source"; + +// LoadFromPath loads the pkl module at the given path and evaluates it into a N01PrimitiveTypes +export const loadFromPath = async (path: string): Promise => { + const evaluator = await newEvaluator(PreconfiguredOptions); + try { + const result = await load(evaluator, FileSource(path)); + return result; + } finally { + evaluator.close(); + } +}; + +export const load = (evaluator: Evaluator, source: ModuleSource): Promise => + evaluator.evaluateModule(source) as Promise; From d94924300f1a19794c11b11e3490e5eb5dfc49ee Mon Sep 17 00:00:00 2001 From: Jason Gwartz Date: Tue, 6 Aug 2024 11:52:36 +0100 Subject: [PATCH 2/3] Update test to use library loadFromPath --- e2e/only_primitives/primitives.test.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/e2e/only_primitives/primitives.test.ts b/e2e/only_primitives/primitives.test.ts index 28aac7e..7b8b106 100644 --- a/e2e/only_primitives/primitives.test.ts +++ b/e2e/only_primitives/primitives.test.ts @@ -1,13 +1,13 @@ import { describe, expect, it } from "@jest/globals"; +import { loadFromPath } from "@pkl-community/pkl-typescript"; +import { join } from "path"; import pklGenTypescript from "../../pkl-gen-typescript/main"; -import { join } from "path"; describe("E2E of config with only primitive values", () => { it("can generate TypeScript sources and load valid values", async () => { await pklGenTypescript([join(__dirname, "schema.pkl")]); - const configPkl = await import(join(__dirname, "../../.out/schema.pkl.ts")); - const config = await configPkl.loadFromPath(join(__dirname, "correct.pkl")); + const config = await loadFromPath(join(__dirname, "correct.pkl")); expect(config).toStrictEqual({ addr: "localhost", port: 3000, @@ -22,12 +22,7 @@ describe("E2E of config with only primitive values", () => { "can generate TypeScript sources but error on evaluating invalid values: %s", async (_, fileBase) => { await pklGenTypescript([join(__dirname, "schema.pkl")]); - const configPkl = await import( - join(__dirname, "../../.out/schema.pkl.ts") - ); - await expect( - configPkl.loadFromPath(join(__dirname, `${fileBase}.pkl`)) - ).rejects.toThrowError(); - } + await expect(loadFromPath(join(__dirname, `${fileBase}.pkl`))).rejects.toThrowError(); + }, ); }); From 47814b0a0c0ae1ce89e1eaeda4cbab857bcc5744 Mon Sep 17 00:00:00 2001 From: Jason Gwartz Date: Tue, 6 Aug 2024 11:56:09 +0100 Subject: [PATCH 3/3] Update example usage in express server --- .../generated/config_schema.pkl.d.ts | 12 ++++++++++ .../generated/config_schema.pkl.ts | 24 ------------------- examples/express-server/index.ts | 20 ++++++++++------ 3 files changed, 25 insertions(+), 31 deletions(-) create mode 100644 examples/express-server/generated/config_schema.pkl.d.ts delete mode 100644 examples/express-server/generated/config_schema.pkl.ts diff --git a/examples/express-server/generated/config_schema.pkl.d.ts b/examples/express-server/generated/config_schema.pkl.d.ts new file mode 100644 index 0000000..1bc9906 --- /dev/null +++ b/examples/express-server/generated/config_schema.pkl.d.ts @@ -0,0 +1,12 @@ +/* This file was generated by `pkl-typescript` from Pkl module `ConfigSchema`. */ +/* DO NOT EDIT! */ +/* istanbul ignore file */ +/* eslint-disable */ +import * as pklTypescript from "@pkl-community/pkl-typescript" + +// Ref: Module root. +export interface ConfigSchema { + address: string + + port: number +} diff --git a/examples/express-server/generated/config_schema.pkl.ts b/examples/express-server/generated/config_schema.pkl.ts deleted file mode 100644 index dcab113..0000000 --- a/examples/express-server/generated/config_schema.pkl.ts +++ /dev/null @@ -1,24 +0,0 @@ -// This file was generated by `pkl-typescript` from Pkl module `ConfigSchema`. -// DO NOT EDIT. -import * as pklTypescript from "@pkl-community/pkl-typescript" - -// Ref: Module root. -export interface ConfigSchema { - address: string - - port: number -} - -// LoadFromPath loads the pkl module at the given path and evaluates it into a ConfigSchema -export const loadFromPath = async (path: string): Promise => { - const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions); - try { - const result = await load(evaluator, pklTypescript.FileSource(path)); - return result - } finally { - evaluator.close() - } -}; - -export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise => - evaluator.evaluateModule(source) as Promise; diff --git a/examples/express-server/index.ts b/examples/express-server/index.ts index 1492413..c54274a 100644 --- a/examples/express-server/index.ts +++ b/examples/express-server/index.ts @@ -1,5 +1,7 @@ +import { loadFromPath } from "@pkl-community/pkl-typescript"; import express from "express"; -import { loadFromPath } from "./generated/config_schema.pkl"; + +import type { ConfigSchema } from "./generated/config_schema.pkl"; const app = express(); @@ -11,11 +13,15 @@ app.get("/", (_, res) => { const configFile = `config.${process.env.NODE_ENV ?? "dev"}.pkl`; // Use pkl-typescript to load and evaluate the selected Pkl file -loadFromPath(configFile).then((config) => { - console.log("Loaded config values from Pkl:", JSON.stringify(config)); +loadFromPath(configFile) + .then((config) => { + console.log("Loaded config values from Pkl:", JSON.stringify(config)); - // `config` is a typed object, of the schema given in ConfigSchema.pkl - app.listen(config.port, config.address, () => { - console.log("Server started"); + // `config` is a typed object, of the schema given in ConfigSchema.pkl + app.listen(config.port, config.address, () => { + console.log("Server started"); + }); + }) + .catch((err: unknown) => { + console.error(err); }); -});