@helios-lang/contract-utils 0.1.23 → 0.1.25

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.
@@ -2,4 +2,12 @@
2
2
 
3
3
  Run `npx hl2ts` to generate Javascript/Typescript equivalents of the `spending.hl` and `minting.hl` validators.
4
4
 
5
- Then have a look at `contract.js` to see how these validators can be used within Javascript/Typescript in a type-safe way (ideally using VSCode so you can hover over the types).
5
+ Then have a look at `contract.js` to see how these validators can be used within Javascript/Typescript in a type-safe way (ideally using VSCode so you can hover over the types).
6
+
7
+ # Installing compilers
8
+
9
+ If you want to use an older version of Helios (under the @hyperionbt org), you can use the following command.
10
+
11
+ ```
12
+ npm install --save-exact @helios-lang/compiler:npm@hyperionbt/helios@<version>
13
+ ```
@@ -1,21 +1,28 @@
1
1
  import { ContractContextBuilder } from "@helios-lang/contract-utils"
2
- import { Address, TxBuilder } from "@helios-lang/ledger"
2
+ import { Address, AssetClass, TxBuilder } from "@helios-lang/ledger"
3
3
  import { match_string, match_string_policy } from "./index.js"
4
4
 
5
- const context = ContractContextBuilder.new()
6
- .with(match_string)
7
- .with(match_string_policy)
8
- .build({
9
- match_string_policy:
10
- "f7c8a87011f1aec5967dd3cd68b7a2c26135d750867bfd957b4d8967" // expected hash for match_string_policy
11
- })
5
+ export function createApp() {
6
+ const context = ContractContextBuilder.new()
7
+ .with(match_string)
8
+ .with(match_string_policy)
9
+ .build({
10
+ match_string_policy:
11
+ "f7c8a87011f1aec5967dd3cd68b7a2c26135d750867bfd957b4d8967" // expected hash for match_string_policy
12
+ })
12
13
 
13
- console.log(context.match_string_policy.toHex())
14
+ console.log(context.match_string_policy.toHex())
14
15
 
15
- const address = Address.fromHash(context.match_string)
16
+ const assetClass = new AssetClass(context.match_string_policy, [])
16
17
 
17
- const b = new TxBuilder()
18
- .mint(context.match_string_policy, [["", 10]], "hello")
19
- .pay(address, 1_000_000n, { hash: { One: { message: "hello" } } })
20
- // Types from Helios are propagated into Typescript, allowing typesafe conversion of Datums
21
- .pay(address, 2_000_000n, { hash: { Two: { message: "hello" } } })
18
+ const address = Address.fromHash(context.match_string)
19
+
20
+ let b = new TxBuilder().mint(assetClass, 10, true)
21
+
22
+ let c = b
23
+ .pay(address, 1_000_000n, { hash: { One: { message: "hello" } } })
24
+ // Types from Helios are propagated into Typescript, allowing typesafe conversion of Datums
25
+ .pay(address, 2_000_000n, { hash: { Two: { code: "hello" } } })
26
+ }
27
+
28
+ createApp()
@@ -1,5 +1,5 @@
1
1
  import type {UplcData} from "@helios-lang/ledger";
2
- import { Address, AssetClass, Credential, DatumHash, MintingPolicyHash, PubKeyHash, StakingCredential, StakingHash, StakingValidatorHash, TimeRange, TxId, TxOutputDatum, ValidatorHash, Value} from "@helios-lang/ledger";
2
+ import { Address, AssetClass, DatumHash, MintingPolicyHash, PubKey, PubKeyHash, SpendingCredential, StakingCredential, StakingHash, StakingValidatorHash, TimeRange, TxId, TxOutputDatum, ValidatorHash, Value } from "@helios-lang/ledger";
3
3
  import { Cast } from "@helios-lang/contract-utils";
4
4
  export const utils: {
5
5
  $name: string
@@ -14,7 +14,7 @@ export const match_string_policy: {
14
14
  $dependencies: []
15
15
  $hashDependencies: []
16
16
  $dependsOnOwnHash: boolean
17
- $Redeemer: Cast<string, string>
17
+ $Redeemer: Cast<boolean, boolean>
18
18
 
19
19
  }
20
20
  export const match_string: {
@@ -2,23 +2,23 @@ import { Cast } from "@helios-lang/contract-utils";
2
2
  export const utils = {
3
3
  $name: "utils",
4
4
  $purpose: "module",
5
- $sourceCode: "module utils\n\nfunc compare(a: String, b: String) -> Bool {\n a == b\n}",
5
+ $sourceCode: "module utils\n\nconst my_assetclass: AssetClass = AssetClass::new(Scripts::match_string_policy, #)\n\nconst my_hash: ValidatorHash = Scripts::match_string\n\nfunc compare(a: String, b: String) -> Bool {\n a == b\n}",
6
6
  $dependencies: [],
7
7
  }
8
8
  export const match_string_policy = {
9
9
  $name: "match_string_policy",
10
10
  $purpose: "minting",
11
- $sourceCode: "minting match_string_policy\n\nfunc main(redeemer: String, _) -> Bool {\n redeemer == \"Hello\"\n}",
11
+ $sourceCode: "minting match_string_policy\n\nfunc main(redeemer: Bool, _) -> Bool {\n redeemer\n}",
12
12
  $dependencies: [],
13
13
  $hashDependencies: [],
14
14
  $dependsOnOwnHash: false,
15
- $Redeemer: new Cast({"primitiveType":"String"}),
15
+ $Redeemer: new Cast({"primitiveType":"Bool"}),
16
16
 
17
17
  }
18
18
  export const match_string = {
19
19
  $name: "match_string",
20
20
  $purpose: "spending",
21
- $sourceCode: "spending match_string\n\nimport { compare } from utils\n\nenum Datum {\n One {\n message: String\n }\n\n Two {\n code: Int\n }\n}\n\nfunc main(datum: Datum, redeemer: String, ctx: ScriptContext) -> Bool {\n compare(datum.switch{d: One => d.message, d: Two => d.code.show()}, redeemer) &&\n ctx.tx.minted.get(AssetClass::new(Scripts::match_string_policy, #)) > 0\n}",
21
+ $sourceCode: "spending match_string\n\nimport { compare, my_assetclass } from utils\n\nenum Datum {\n One {\n message: String\n }\n\n Two {\n code: Int\n }\n}\n\nfunc main(datum: Datum, redeemer: String, ctx: ScriptContext) -> Bool {\n compare(datum.switch{d: One => d.message, d: Two => d.code.show()}, redeemer) &&\n ctx.tx.minted.get(my_assetclass) > 0\n}",
22
22
  $dependencies: [utils],
23
23
  $hashDependencies: [match_string_policy],
24
24
  $dependsOnOwnHash: true,
@@ -1,5 +1,5 @@
1
1
  minting match_string_policy
2
2
 
3
- func main(redeemer: String, _) -> Bool {
4
- redeemer == "Hello"
3
+ func main(redeemer: Bool, _) -> Bool {
4
+ redeemer
5
5
  }
@@ -6,7 +6,6 @@
6
6
  "": {
7
7
  "name": "spending_example",
8
8
  "dependencies": {
9
- "@helios-lang/compiler": "0.16.7-1",
10
9
  "@helios-lang/contract-utils": "file:../..",
11
10
  "@hyperionbt/helios": "^0.16.7"
12
11
  },
@@ -16,14 +15,14 @@
16
15
  },
17
16
  "../..": {
18
17
  "name": "@helios-lang/contract-utils",
19
- "version": "0.1.7",
18
+ "version": "0.1.23",
20
19
  "license": "BSD-3-Clause",
21
20
  "dependencies": {
22
- "@helios-lang/codec-utils": "^0.1.26",
23
- "@helios-lang/compiler-utils": "^0.1.20",
24
- "@helios-lang/ledger": "^0.1.10",
21
+ "@helios-lang/codec-utils": "^0.1.27",
22
+ "@helios-lang/compiler-utils": "^0.1.21",
23
+ "@helios-lang/ledger": "^0.1.21",
25
24
  "@helios-lang/type-utils": "^0.1.8",
26
- "@helios-lang/uplc": "^0.1.21"
25
+ "@helios-lang/uplc": "^0.1.22"
27
26
  },
28
27
  "bin": {
29
28
  "hl2ts": "src/hl2ts.js"
@@ -34,15 +33,7 @@
34
33
  "typescript": "^5.3.2"
35
34
  },
36
35
  "peerDependencies": {
37
- "@helios-lang/compiler": "^0.16.7"
38
- }
39
- },
40
- "node_modules/@helios-lang/compiler": {
41
- "version": "0.16.7-1",
42
- "resolved": "https://registry.npmjs.org/@helios-lang/compiler/-/compiler-0.16.7-1.tgz",
43
- "integrity": "sha512-M6x99ApFUNtNrPucx5IrcTbqHP1yyVQXeaKuQWOICgioSbWMKeogN7Hn2INk6f//DzGaqFZ7Cd1JOtt8zXHE/Q==",
44
- "dependencies": {
45
- "@hyperionbt/helios": "0.16.7"
36
+ "@helios-lang/compiler": "*"
46
37
  }
47
38
  },
48
39
  "node_modules/@helios-lang/contract-utils": {
@@ -2,7 +2,6 @@
2
2
  "name": "spending_example",
3
3
  "type": "module",
4
4
  "dependencies": {
5
- "@helios-lang/compiler": "0.16.7-1",
6
5
  "@helios-lang/contract-utils": "file:../..",
7
6
  "@hyperionbt/helios": "^0.16.7"
8
7
  },
@@ -1,6 +1,6 @@
1
1
  spending match_string
2
2
 
3
- import { compare } from "./utils.hl"
3
+ import { compare, my_assetclass } from "./utils.hl"
4
4
 
5
5
  enum Datum {
6
6
  One {
@@ -14,5 +14,5 @@ enum Datum {
14
14
 
15
15
  func main(datum: Datum, redeemer: String, ctx: ScriptContext) -> Bool {
16
16
  compare(datum.switch{d: One => d.message, d: Two => d.code.show()}, redeemer) &&
17
- ctx.tx.minted.get(AssetClass::new(Scripts::match_string_policy, #)) > 0
17
+ ctx.tx.minted.get(my_assetclass) > 0
18
18
  }
@@ -1,5 +1,9 @@
1
1
  module utils
2
2
 
3
+ const my_assetclass: AssetClass = AssetClass::new(Scripts::match_string_policy, #)
4
+
5
+ const my_hash: ValidatorHash = Scripts::match_string
6
+
3
7
  func compare(a: String, b: String) -> Bool {
4
8
  a == b
5
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helios-lang/contract-utils",
3
- "version": "0.1.23",
3
+ "version": "0.1.25",
4
4
  "description": "Convenience and type-safety utilities for using Helios validators from within Typescript",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
@@ -9,9 +9,9 @@
9
9
  "hl2ts": "src/hl2ts.js"
10
10
  },
11
11
  "scripts": {
12
- "build": "npm run build:types",
12
+ "build": "npm run prettify && npm run build:types && npm run test:suite",
13
13
  "build:types": "npx tsc -p jsconfig.json --noEmit false --emitDeclarationOnly",
14
- "prepack": "npm run test:pretty && npm run test:suite && npm run build",
14
+ "prepack": "npm run build",
15
15
  "prettify": "npx prettier . --write",
16
16
  "test": "npm run test:pretty && npm run test:types && npm run test:suite",
17
17
  "test:pretty": "npx prettier . --check",
@@ -35,6 +35,7 @@
35
35
  "@helios-lang/compiler": "*"
36
36
  },
37
37
  "dependencies": {
38
+ "@helios-lang/cli-utils": "^0.1.2",
38
39
  "@helios-lang/codec-utils": "^0.1.27",
39
40
  "@helios-lang/compiler-utils": "^0.1.21",
40
41
  "@helios-lang/ledger": "^0.1.21",
@@ -13,6 +13,7 @@ export class LoadedScriptsWriter {
13
13
  constructor() {
14
14
  this.definition = new StringWriter()
15
15
  this.declaration = new StringWriter()
16
+ this.combined = new StringWriter()
16
17
  }
17
18
 
18
19
  /**
@@ -102,10 +103,14 @@ export class LoadedScriptsWriter {
102
103
  }
103
104
 
104
105
  /**
105
- * @returns {[string, string]}
106
+ * @returns {[string, string, string]}
106
107
  */
107
108
  finalize() {
108
- return [this.definition.finalize(), this.declaration.finalize()]
109
+ return [
110
+ this.definition.finalize(),
111
+ this.declaration.finalize(),
112
+ this.combined.finalize()
113
+ ]
109
114
  }
110
115
 
111
116
  /**
@@ -128,16 +133,13 @@ export class LoadedScriptsWriter {
128
133
  this.definition.writeLine(
129
134
  'import { Cast } from "@helios-lang/contract-utils";'
130
135
  )
131
-
132
- this.declaration.writeLine(
133
- 'import type {UplcData} from "@helios-lang/ledger";'
134
- )
135
- this.declaration.writeLine(
136
- 'import { Address, AssetClass, Credential, DatumHash, MintingPolicyHash, PubKeyHash, StakingCredential, StakingHash, StakingValidatorHash, TimeRange, TxId, TxOutputDatum, ValidatorHash, Value} from "@helios-lang/ledger";'
137
- )
138
- this.declaration.writeLine(
139
- 'import { Cast } from "@helios-lang/contract-utils";'
140
- )
136
+ ;[this.declaration, this.combined].forEach((w) => {
137
+ w.writeLine('import type {UplcData} from "@helios-lang/ledger";')
138
+ w.writeLine(
139
+ 'import { Address, AssetClass, DatumHash, MintingPolicyHash, PubKey, PubKeyHash, SpendingCredential, StakingCredential, StakingHash, StakingValidatorHash, TimeRange, TxId, TxOutputDatum, ValidatorHash, Value } from "@helios-lang/ledger";'
140
+ )
141
+ w.writeLine('import { Cast } from "@helios-lang/contract-utils";')
142
+ })
141
143
  }
142
144
 
143
145
  /**
@@ -145,20 +147,33 @@ export class LoadedScriptsWriter {
145
147
  * @param {TypeCheckedModule} m
146
148
  */
147
149
  writeModule(m) {
148
- this.write(
150
+ this.definition.write(
149
151
  `export const ${m.name} = {
150
152
  $name: "${m.name}",
151
153
  $purpose: "${m.purpose}",
152
154
  $sourceCode: ${JSON.stringify(m.sourceCode)},
153
155
  $dependencies: [${m.moduleDepedencies.join(", ")}],
154
156
  }
155
- `,
157
+ `
158
+ )
159
+
160
+ this.declaration.write(
156
161
  `export const ${m.name}: {
157
162
  $name: string
158
163
  $purpose: string
159
164
  $sourceCode: string
160
165
  $dependencies: [${m.moduleDepedencies.map((d) => `typeof ${d}`).join(", ")}]
161
166
  }
167
+ `
168
+ )
169
+
170
+ this.combined.write(
171
+ `export const ${m.name} = {
172
+ $name: "${m.name}" as "${m.name}",
173
+ $purpose: "${m.purpose}" as "${m.purpose}",
174
+ $sourceCode: ${JSON.stringify(m.sourceCode)} as string,
175
+ $dependencies: [${m.moduleDepedencies.join(", ")}],
176
+ }
162
177
  `
163
178
  )
164
179
  }
@@ -171,7 +186,7 @@ export class LoadedScriptsWriter {
171
186
  const redeemerTypes = genTypes(v.Redeemer)
172
187
  const datumTypes = v.Datum ? genTypes(v.Datum) : undefined
173
188
 
174
- this.write(
189
+ this.definition.write(
175
190
  `export const ${v.name} = {
176
191
  $name: "${v.name}",
177
192
  $purpose: "${v.purpose}",
@@ -182,7 +197,9 @@ export class LoadedScriptsWriter {
182
197
  $Redeemer: new Cast(${JSON.stringify(v.Redeemer)}),
183
198
  ${datumTypes ? `$Datum: new Cast(${JSON.stringify(v.Datum)})` : ""}
184
199
  }
185
- `,
200
+ `
201
+ )
202
+ this.declaration.write(
186
203
  `export const ${v.name}: {
187
204
  $name: "${v.name}"
188
205
  $purpose: "${v.purpose}"
@@ -196,6 +213,20 @@ export class LoadedScriptsWriter {
196
213
  $Redeemer: Cast<${redeemerTypes[0]}, ${redeemerTypes[1]}>
197
214
  ${datumTypes ? `$Datum: Cast<${datumTypes[0]}, ${datumTypes[1]}>` : ""}
198
215
  }
216
+ `
217
+ )
218
+
219
+ this.combined.write(
220
+ `export const ${v.name} = {
221
+ $name: "${v.name}" as "${v.name}",
222
+ $purpose: "${v.purpose}" as "${v.purpose}",
223
+ $sourceCode: ${JSON.stringify(v.sourceCode)} as string,
224
+ $dependencies: [${v.moduleDepedencies.join(", ")}],
225
+ $hashDependencies: [${v.hashDependencies.filter((d) => d != v.name).join(", ")}],
226
+ $dependsOnOwnHash: ${v.hashDependencies.some((d) => d == v.name)} as boolean,
227
+ $Redeemer: new Cast<${redeemerTypes[0]}, ${redeemerTypes[1]}>(${JSON.stringify(v.Redeemer)}),
228
+ ${datumTypes ? `$Datum: new Cast<${datumTypes[0]}, ${datumTypes[1]}>(${JSON.stringify(v.Datum)})` : ""}
229
+ }
199
230
  `
200
231
  )
201
232
  }
@@ -62,11 +62,11 @@ export function genTypes(schema) {
62
62
  "MintingPolicyHash | string | number[]"
63
63
  ]
64
64
  case "PubKey":
65
- return ["string", "string | number[]"]
65
+ return ["PubKey", "PubKey | string | number[]"]
66
66
  case "PubKeyHash":
67
67
  return ["PubKeyHash", "PubKeyHash | string | number[]"]
68
68
  case "Real":
69
- return ["number", "numer"]
69
+ return ["number", "number"]
70
70
  case "StakingCredential":
71
71
  return [
72
72
  "StakingCredential",
@@ -4,7 +4,6 @@ import { readHeader } from "@helios-lang/compiler-utils"
4
4
  /**
5
5
  * @typedef {import("../codegen/index.js").TypeCheckedModule} TypeCheckedModule
6
6
  * @typedef {import("../codegen/index.js").TypeCheckedValidator} TypeCheckedValidator
7
-
8
7
  * @typedef {import("../codegen/index.js").TypeSchema} InternalTypeDetails
9
8
  * @typedef {import("./CompilerLib.js").CompileOptions} CompileOptions
10
9
  * @typedef {import("./CompilerLib.js").CompileOutput} CompileOutput
@@ -239,7 +238,7 @@ export class CompilerLib_v0_16 {
239
238
  moduleDepedencies: m
240
239
  .filterDependencies(allModules)
241
240
  .map((m) => m.name.value),
242
- types: {} // not yet exported
241
+ types: {}
243
242
  }
244
243
  }
245
244
  }
@@ -459,6 +458,8 @@ export class CompilerLib_v0_16 {
459
458
  }
460
459
 
461
460
  /**
461
+ * Tries to convert a compiler type into TypeSchema
462
+ * TODO: make sure it works for all structs and enums
462
463
  * @private
463
464
  * @param {DataType} dt
464
465
  * @returns {InternalTypeDetails}
package/src/hl2ts.js CHANGED
@@ -1,11 +1,18 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { promises, readFileSync, writeFileSync } from "node:fs"
3
+ import {
4
+ existsSync,
5
+ fstat,
6
+ promises,
7
+ readFileSync,
8
+ writeFileSync
9
+ } from "node:fs"
4
10
  import { dirname, join, resolve } from "node:path"
5
- import { readHeader, translateImportPaths } from "@helios-lang/compiler-utils"
11
+ import { Cli, Command, EnumOpt, StringOpt } from "@helios-lang/cli-utils"
6
12
  import { LoadedScriptsWriter } from "./codegen/index.js"
7
13
  import { loadCompilerLib } from "./compiler/index.js"
8
14
  import { typeCheckFiles, typeCheckScripts } from "./compiler/ops.js"
15
+ import { None } from "@helios-lang/type-utils"
9
16
 
10
17
  /**
11
18
  * @typedef {import("./compiler/CompilerLib.js").CompilerLib} CompilerLib
@@ -21,10 +28,54 @@ import { typeCheckFiles, typeCheckScripts } from "./compiler/ops.js"
21
28
  * }} SourceDetails
22
29
  */
23
30
 
31
+ /**
32
+ * @typedef {"javascript" | "typescript"} FormatKind
33
+ */
34
+
24
35
  async function main() {
25
- const lib = loadCompilerLib()
36
+ const cli = new Cli({
37
+ minArgs: 0,
38
+ maxArgs: 0,
39
+ options: {
40
+ outDir: new StringOpt({
41
+ long: "--out-dir",
42
+ short: "-o",
43
+ default: () => process.cwd()
44
+ }),
45
+ format: new EnumOpt({
46
+ long: "--format",
47
+ short: "-f",
48
+ variants: /** @type {FormatKind[]} */ ([
49
+ "javascript",
50
+ "typescript"
51
+ ]),
52
+ default: () => {
53
+ const path = findPackageJson()
54
+
55
+ if (
56
+ path &&
57
+ existsSync(join(dirname(path), "tsconfig.json"))
58
+ ) {
59
+ return "typescript"
60
+ } else {
61
+ return "javascript"
62
+ }
63
+ }
64
+ })
65
+ },
66
+ action: mainInternal
67
+ })
26
68
 
27
- const { outDir } = parseArgs(process.argv)
69
+ await cli.run()
70
+ }
71
+
72
+ /**
73
+ * @param {string[]} _args
74
+ * @param {{outDir: string, format: FormatKind}} options
75
+ * @returns {Promise<void>}
76
+ */
77
+ async function mainInternal(_args, { outDir, format }) {
78
+ const lib = loadCompilerLib()
28
79
 
29
80
  const filePaths = await listFiles(process.cwd(), ".hl")
30
81
  const files = readFiles(filePaths)
@@ -37,38 +88,49 @@ async function main() {
37
88
  resolve(join(dirname(current), rel))
38
89
  )
39
90
 
40
- const [js, dts] = LoadedScriptsWriter.new()
91
+ const [js, dts, ts] = LoadedScriptsWriter.new()
41
92
  .writeModules(modules)
42
93
  .writeValidators(validators)
43
94
  .finalize()
44
95
 
45
- const jsPath = join(outDir, "index.js")
46
- const dtsPath = join(outDir, "index.d.ts")
96
+ if (format == "typescript") {
97
+ const tsPath = join(outDir, "index.ts")
98
+
99
+ writeFileSync(tsPath, ts)
100
+
101
+ console.log(` Output:`)
102
+ console.log(` ${tsPath}`)
103
+ } else {
104
+ const jsPath = join(outDir, "index.js")
105
+ const dtsPath = join(outDir, "index.d.ts")
47
106
 
48
- writeFileSync(jsPath, js)
49
- writeFileSync(dtsPath, dts)
107
+ writeFileSync(jsPath, js)
108
+ writeFileSync(dtsPath, dts)
50
109
 
51
- console.log(` Output:`)
52
- console.log(` ${jsPath}`)
53
- console.log(` ${dtsPath}`)
110
+ console.log(` Output:`)
111
+ console.log(` ${jsPath}`)
112
+ console.log(` ${dtsPath}`)
113
+ }
54
114
  }
55
115
 
56
116
  main()
57
117
 
58
118
  /**
59
119
  * @param {string[]} args
60
- * @returns {{outDir: string}}
120
+ * @returns {{outDir: string, isTypescript: boolean}}
61
121
  */
62
122
  function parseArgs(args) {
123
+ const isTypescript = args.indexOf("--ts") != -1
124
+
63
125
  let i = args.indexOf("-o")
64
126
  if (i != -1) {
65
- if (i == args.length - 1) {
127
+ if (i == args.length - 1 || args[i + 1].startsWith("-")) {
66
128
  throw new Error("expected argument after -o")
67
129
  }
68
130
 
69
- return { outDir: resolve(args[i + 1]) }
131
+ return { outDir: resolve(args[i + 1]), isTypescript }
70
132
  } else {
71
- return { outDir: process.cwd() }
133
+ return { outDir: process.cwd(), isTypescript }
72
134
  }
73
135
  }
74
136
 
@@ -112,46 +174,24 @@ function readFiles(filePaths) {
112
174
  }
113
175
 
114
176
  /**
115
- *
116
- * @param {{[path: string]: string}} files
117
- * @returns {string[]}
177
+ * @param {string} fileName
178
+ * @returns {Option<string>}
118
179
  */
119
- function preparseFiles(files) {
120
- const partialSources = {}
180
+ function findPackageJson(fileName = "package.json") {
181
+ let dir = process.cwd()
121
182
 
122
- for (let path in files) {
123
- const [purpose, name] = readHeader(files[path])
183
+ let path = join(dir, fileName)
184
+ let found = existsSync(path)
124
185
 
125
- partialSources[path] = {
126
- name: name,
127
- purpose: purpose,
128
- sourceCode: files[path]
129
- }
186
+ while (!found) {
187
+ dir = dirname(dir)
188
+ path = join(dir, fileName)
189
+ found = existsSync(path)
130
190
  }
131
191
 
132
- /**
133
- * @type {{[name: string]: SourceDetails}}
134
- */
135
- const sources = {}
136
-
137
- for (let path in files) {
138
- const sourceCode = translateImportPaths(files[path], (relPath) => {
139
- const absPath = resolve(join(dirname(path), relPath))
140
-
141
- const d = partialSources[absPath] ?? partialSources[absPath + ".hl"]
142
-
143
- if (!d) {
144
- throw new Error(`'${relPath}' not found`)
145
- }
146
-
147
- return d.name
148
- })
149
-
150
- sources[partialSources[path].name] = {
151
- ...partialSources[path],
152
- sourceCode: sourceCode
153
- }
192
+ if (found) {
193
+ return path
194
+ } else {
195
+ return None
154
196
  }
155
-
156
- return Object.values(sources).map((s) => s.sourceCode)
157
197
  }
@@ -10,6 +10,7 @@ export class LoadedScriptsWriter {
10
10
  static "new"(): LoadedScriptsWriter;
11
11
  definition: StringWriter;
12
12
  declaration: StringWriter;
13
+ combined: StringWriter;
13
14
  /**
14
15
  * @param {{[name: string]: TypeCheckedModule}} ms
15
16
  * @returns {LoadedScriptsWriter}
@@ -25,9 +26,9 @@ export class LoadedScriptsWriter {
25
26
  [name: string]: import("./TypeCheckedValidator.js").TypeCheckedValidator;
26
27
  }): LoadedScriptsWriter;
27
28
  /**
28
- * @returns {[string, string]}
29
+ * @returns {[string, string, string]}
29
30
  */
30
- finalize(): [string, string];
31
+ finalize(): [string, string, string];
31
32
  /**
32
33
  * @private
33
34
  * @param {string} def
@@ -1 +1 @@
1
- {"version":3,"file":"LoadedScriptsWriter.d.ts","sourceRoot":"","sources":["../../src/codegen/LoadedScriptsWriter.js"],"names":[],"mappings":"AAGA;;;GAGG;AAEH;IASI;;;OAGG;IACH,gBAFa,mBAAmB,CAQ/B;IAdG,yBAAoC;IACpC,0BAAqC;IAezC;;;OAGG;IACH;;QAFa,mBAAmB,CAgC/B;IAED;;;OAGG;IACH;;QAFa,mBAAmB,CAkC/B;IAED;;OAEG;IACH,YAFa,CAAC,MAAM,EAAE,MAAM,CAAC,CAI5B;IAED;;;;;OAKG;IACH,cAKC;IAED;;OAEG;IACH,qBAcC;IAED;;;OAGG;IACH,oBAiBC;IAED;;;OAGG;IACH,uBA+BC;CACJ;gCArMY,OAAO,wBAAwB,EAAE,iBAAiB;mCAClD,OAAO,2BAA2B,EAAE,oBAAoB;6BALxC,0BAA0B"}
1
+ {"version":3,"file":"LoadedScriptsWriter.d.ts","sourceRoot":"","sources":["../../src/codegen/LoadedScriptsWriter.js"],"names":[],"mappings":"AAGA;;;GAGG;AAEH;IAUI;;;OAGG;IACH,gBAFa,mBAAmB,CAQ/B;IAfG,yBAAoC;IACpC,0BAAqC;IACrC,uBAAkC;IAetC;;;OAGG;IACH;;QAFa,mBAAmB,CAgC/B;IAED;;;OAGG;IACH;;QAFa,mBAAmB,CAkC/B;IAED;;OAEG;IACH,YAFa,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAQpC;IAED;;;;;OAKG;IACH,cAKC;IAED;;OAEG;IACH,qBAWC;IAED;;;OAGG;IACH,oBA8BC;IAED;;;OAGG;IACH,uBA+CC;CACJ;gCApOY,OAAO,wBAAwB,EAAE,iBAAiB;mCAClD,OAAO,2BAA2B,EAAE,oBAAoB;6BALxC,0BAA0B"}
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * @typedef {import("../codegen/index.js").TypeCheckedModule} TypeCheckedModule
3
3
  * @typedef {import("../codegen/index.js").TypeCheckedValidator} TypeCheckedValidator
4
-
5
4
  * @typedef {import("../codegen/index.js").TypeSchema} InternalTypeDetails
6
5
  * @typedef {import("./CompilerLib.js").CompileOptions} CompileOptions
7
6
  * @typedef {import("./CompilerLib.js").CompileOutput} CompileOutput
@@ -182,6 +181,8 @@ export class CompilerLib_v0_16 implements CompilerLib {
182
181
  */
183
182
  private convertInternalType;
184
183
  /**
184
+ * Tries to convert a compiler type into TypeSchema
185
+ * TODO: make sure it works for all structs and enums
185
186
  * @private
186
187
  * @param {DataType} dt
187
188
  * @returns {InternalTypeDetails}
@@ -1 +1 @@
1
- {"version":3,"file":"CompilerLib_v0_16.d.ts","sourceRoot":"","sources":["../../src/compiler/CompilerLib_v0_16.js"],"names":[],"mappings":"AAGA;;;;;;;;;;GAUG;AAEH;;;;GAIG;AAEH;;;GAGG;AAEH;;;;GAIG;AAEH;;;;GAIG;AAEH;;;;GAIG;AAEH;;GAEG;AAEH;;;;;;GAMG;AAEH;;;;;GAKG;AAEH;;;;;;GAMG;AACH;;;;;;;;;;GAUG;AAEH;;;;GAIG;AAEH;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;;GAIG;AAEH;;;;;;GAMG;AAEH;;;GAGG;AAEH;;GAEG;AACH;IACI;;OAEG;IACH,iBAFW,GAAG,EAIb;IADG,SAAc;IAGlB;;OAEG;IACH,sBAEC;IAED;;;OAGG;IACH,2BAHW,MAAM,GACJ,cAAc,CAa1B;IAED;;;;OAIG;IACH,sBAJW,MAAM,EAAE,WACR,MAAM,EAAE,GACN,eAAe,CAmF3B;IAED;;;;;;;;;;;OAWG;IACH,mCAoDC;IAED;;;;;OAKG;IACH,cALW,MAAM,WACN,MAAM,EAAE,WACR,cAAc,GACZ,aAAa,CAyCzB;IAED;;;;;;OAMG;IACH,uBAOC;IAED;;;;;OAKG;IACH,iBAWC;IAED;;;;;OAKG;IACH,6BAeC;IAED;;;;OAIG;IACH,4BA8BC;IAED;;;;OAIG;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rB,GAAG;IACN,IAAQ,EAAQ,QAAQ,CAAA;IACxB,UAAc,EAAE,eAAe,EAAE,CAAA;CAC9B,GAAG;IACN,IAAQ,EAAU,MAAM,CAAA;IACxB,YAAgB,EAAE;QAAC,MAAM,MAAM,CAAC;QAAC,UAAU,EAAE,eAAe,EAAE,CAAA;KAAC,EAAE,CAAA;CAC9D;8BAIS;IACZ,MAAU,MAAM,CAAA;CACb,GAAG,UAAU;0BAIJ;IACZ,SAAa,EAAE,MAAM,CAAA;IACrB,UAAc,EAAE,MAAM,CAAA;IACtB,YAAgB,EAAE,UAAU,CAAA;CACzB;wBAIS,EACT"}
package/types/hl2ts.d.ts CHANGED
@@ -7,4 +7,5 @@ export type SourceDetails = {
7
7
  purpose: string;
8
8
  sourceCode: string;
9
9
  };
10
+ export type FormatKind = "javascript" | "typescript";
10
11
  //# sourceMappingURL=hl2ts.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"hl2ts.d.ts","sourceRoot":"","sources":["../src/hl2ts.js"],"names":[],"mappings":";0BAUa,OAAO,2BAA2B,EAAE,WAAW;4BAC/C,OAAO,2BAA2B,EAAE,iBAAiB;+BACrD,OAAO,2BAA2B,EAAE,oBAAoB;4BAIxD;IACZ,MAAU,MAAM,CAAA;IAChB,OAAW,EAAE,MAAM,CAAA;IACnB,UAAc,EAAE,MAAM,CAAA;CACnB"}
1
+ {"version":3,"file":"hl2ts.d.ts","sourceRoot":"","sources":["../src/hl2ts.js"],"names":[],"mappings":";0BAiBa,OAAO,2BAA2B,EAAE,WAAW;4BAC/C,OAAO,2BAA2B,EAAE,iBAAiB;+BACrD,OAAO,2BAA2B,EAAE,oBAAoB;4BAIxD;IACZ,MAAU,MAAM,CAAA;IAChB,OAAW,EAAE,MAAM,CAAA;IACnB,UAAc,EAAE,MAAM,CAAA;CACnB;yBAIS,YAAY,GAAG,YAAY"}