@chainlink/cre-sdk 1.3.0 → 1.4.0-alpha.1
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 +7 -1
- package/bin/cre-compile.ts +23 -6
- package/package.json +2 -2
- package/scripts/run.ts +5 -1
- package/scripts/src/compile-cli-args.test.ts +32 -0
- package/scripts/src/compile-cli-args.ts +35 -0
- package/scripts/src/compile-to-js.test.ts +90 -0
- package/scripts/src/compile-to-js.ts +46 -7
- package/scripts/src/compile-workflow.ts +51 -12
- package/scripts/src/typecheck-workflow.test.ts +77 -0
- package/scripts/src/typecheck-workflow.ts +96 -0
package/README.md
CHANGED
|
@@ -58,10 +58,16 @@ CRE workflows are compiled to WASM and executed through Javy (QuickJS). This is
|
|
|
58
58
|
|
|
59
59
|
- Node built-ins like `node:fs`, `node:crypto`, `node:http`, `node:net`, etc. are not supported in workflows.
|
|
60
60
|
- Browser globals like `fetch`, `window`, and `setTimeout` are also not available in workflow runtime.
|
|
61
|
-
- `cre compile:workflow` / `cre-compile` now validates workflow source and fails fast when unsupported APIs are used.
|
|
61
|
+
- `cre compile:workflow` / `cre-compile` now typechecks your workflow project first (using your nearest `tsconfig.json`), then validates workflow source and fails fast when unsupported APIs are used.
|
|
62
62
|
|
|
63
63
|
Use CRE capabilities (for example, `cre.capabilities.HTTPClient`) instead of direct Node/browser APIs.
|
|
64
64
|
|
|
65
|
+
If you need to compile despite TypeScript errors, pass `--skip-type-checks`:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
bun x cre-compile src/workflow.ts dist/workflow.wasm --skip-type-checks
|
|
69
|
+
```
|
|
70
|
+
|
|
65
71
|
## Getting Started
|
|
66
72
|
|
|
67
73
|
We recommend you consult the [getting started docs](https://docs.chain.link/cre/getting-started/cli-installation) and install the CRE CLI.
|
package/bin/cre-compile.ts
CHANGED
|
@@ -1,33 +1,50 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
3
|
import { main as compileWorkflow } from "../scripts/src/compile-workflow";
|
|
4
|
+
import { parseCompileCliArgs, skipTypeChecksFlag } from "../scripts/src/compile-cli-args";
|
|
5
|
+
import { WorkflowTypecheckError } from "../scripts/src/typecheck-workflow";
|
|
4
6
|
import { WorkflowRuntimeCompatibilityError } from "../scripts/src/validate-workflow-runtime-compat";
|
|
5
7
|
|
|
6
8
|
const main = async () => {
|
|
7
|
-
|
|
9
|
+
let inputPath: string | undefined;
|
|
10
|
+
let outputPathArg: string | undefined;
|
|
11
|
+
let skipTypeChecks = false;
|
|
8
12
|
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
try {
|
|
14
|
+
const parsed = parseCompileCliArgs(process.argv.slice(2));
|
|
15
|
+
inputPath = parsed.inputPath;
|
|
16
|
+
outputPathArg = parsed.outputPath;
|
|
17
|
+
skipTypeChecks = parsed.skipTypeChecks;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.error(error instanceof Error ? error.message : error);
|
|
20
|
+
console.error(
|
|
21
|
+
`Usage: cre-compile <path/to/workflow.ts> [path/to/output.wasm] [${skipTypeChecksFlag}]`
|
|
22
|
+
);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
11
25
|
|
|
12
26
|
if (!inputPath) {
|
|
13
27
|
console.error(
|
|
14
|
-
|
|
28
|
+
`Usage: cre-compile <path/to/workflow.ts> [path/to/output.wasm] [${skipTypeChecksFlag}]`
|
|
15
29
|
);
|
|
16
30
|
console.error("Examples:");
|
|
17
31
|
console.error(" cre-compile src/standard_tests/secrets/test.ts");
|
|
18
32
|
console.error(
|
|
19
33
|
" cre-compile src/standard_tests/secrets/test.ts .temp/standard_tests/secrets/test.wasm"
|
|
20
34
|
);
|
|
35
|
+
console.error(
|
|
36
|
+
` cre-compile src/standard_tests/secrets/test.ts .temp/standard_tests/secrets/test.wasm ${skipTypeChecksFlag}`
|
|
37
|
+
);
|
|
21
38
|
process.exit(1);
|
|
22
39
|
}
|
|
23
40
|
|
|
24
41
|
// Delegate to the compile-workflow script
|
|
25
|
-
await compileWorkflow(inputPath, outputPathArg);
|
|
42
|
+
await compileWorkflow(inputPath, outputPathArg, { skipTypeChecks });
|
|
26
43
|
};
|
|
27
44
|
|
|
28
45
|
// CLI entry point
|
|
29
46
|
main().catch((e) => {
|
|
30
|
-
if (e instanceof WorkflowRuntimeCompatibilityError) {
|
|
47
|
+
if (e instanceof WorkflowRuntimeCompatibilityError || e instanceof WorkflowTypecheckError) {
|
|
31
48
|
console.error(`\n❌ ${e.message}`);
|
|
32
49
|
} else {
|
|
33
50
|
console.error(e);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainlink/cre-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0-alpha.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"@biomejs/biome": "2.3.14",
|
|
70
70
|
"@bufbuild/buf": "1.56.0",
|
|
71
71
|
"@types/bun": "1.3.8",
|
|
72
|
-
"chain-selectors": "https://github.com/smartcontractkit/chain-selectors.git#
|
|
72
|
+
"chain-selectors": "https://github.com/smartcontractkit/chain-selectors.git#a86ae261a09f805ea37165f58b017b4538e2e3bb",
|
|
73
73
|
"fast-glob": "3.3.3",
|
|
74
74
|
"ts-proto": "2.7.5",
|
|
75
75
|
"typescript": "5.9.3",
|
package/scripts/run.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
|
+
import { WorkflowTypecheckError } from './src/typecheck-workflow'
|
|
3
4
|
import { WorkflowRuntimeCompatibilityError } from './src/validate-workflow-runtime-compat'
|
|
4
5
|
|
|
5
6
|
const availableScripts = [
|
|
@@ -39,7 +40,10 @@ const main = async () => {
|
|
|
39
40
|
process.exit(1)
|
|
40
41
|
}
|
|
41
42
|
} catch (error) {
|
|
42
|
-
if (
|
|
43
|
+
if (
|
|
44
|
+
error instanceof WorkflowRuntimeCompatibilityError ||
|
|
45
|
+
error instanceof WorkflowTypecheckError
|
|
46
|
+
) {
|
|
43
47
|
console.error(`\n❌ ${error.message}`)
|
|
44
48
|
} else {
|
|
45
49
|
console.error(`Failed to run script ${scriptName}:`, error)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { parseCompileCliArgs } from './compile-cli-args'
|
|
3
|
+
|
|
4
|
+
describe('parseCompileCliArgs', () => {
|
|
5
|
+
test('parses positional input and output', () => {
|
|
6
|
+
const parsed = parseCompileCliArgs(['src/workflow.ts', 'dist/workflow.wasm'])
|
|
7
|
+
expect(parsed).toEqual({
|
|
8
|
+
inputPath: 'src/workflow.ts',
|
|
9
|
+
outputPath: 'dist/workflow.wasm',
|
|
10
|
+
skipTypeChecks: false,
|
|
11
|
+
})
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('parses --skip-type-checks flag', () => {
|
|
15
|
+
const parsed = parseCompileCliArgs(['src/workflow.ts', '--skip-type-checks'])
|
|
16
|
+
expect(parsed).toEqual({
|
|
17
|
+
inputPath: 'src/workflow.ts',
|
|
18
|
+
outputPath: undefined,
|
|
19
|
+
skipTypeChecks: true,
|
|
20
|
+
})
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('throws on unknown flags', () => {
|
|
24
|
+
expect(() => parseCompileCliArgs(['src/workflow.ts', '--foo'])).toThrow('Unknown option: --foo')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('throws on too many positional args', () => {
|
|
28
|
+
expect(() => parseCompileCliArgs(['src/workflow.ts', 'dist/workflow.wasm', 'extra'])).toThrow(
|
|
29
|
+
'Too many positional arguments.',
|
|
30
|
+
)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const skipTypeChecksFlag = '--skip-type-checks'
|
|
2
|
+
|
|
3
|
+
export type ParsedCompileArgs = {
|
|
4
|
+
inputPath?: string
|
|
5
|
+
outputPath?: string
|
|
6
|
+
skipTypeChecks: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const parseCompileCliArgs = (args: string[]): ParsedCompileArgs => {
|
|
10
|
+
const positionalArgs: string[] = []
|
|
11
|
+
let skipTypeChecks = false
|
|
12
|
+
|
|
13
|
+
for (const arg of args) {
|
|
14
|
+
if (arg === skipTypeChecksFlag) {
|
|
15
|
+
skipTypeChecks = true
|
|
16
|
+
continue
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (arg.startsWith('-')) {
|
|
20
|
+
throw new Error(`Unknown option: ${arg}`)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
positionalArgs.push(arg)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (positionalArgs.length > 2) {
|
|
27
|
+
throw new Error('Too many positional arguments.')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
inputPath: positionalArgs[0],
|
|
32
|
+
outputPath: positionalArgs[1],
|
|
33
|
+
skipTypeChecks,
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { main as compileToJs } from './compile-to-js'
|
|
5
|
+
import { WorkflowTypecheckError } from './typecheck-workflow'
|
|
6
|
+
|
|
7
|
+
let tempDir: string
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
tempDir = mkdtempSync(path.join(process.cwd(), '.tmp-cre-compile-to-js-test-'))
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
rmSync(tempDir, { recursive: true, force: true })
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const writeTemp = (filename: string, content: string): string => {
|
|
18
|
+
const filePath = path.join(tempDir, filename)
|
|
19
|
+
mkdirSync(path.dirname(filePath), { recursive: true })
|
|
20
|
+
writeFileSync(filePath, content, 'utf-8')
|
|
21
|
+
return filePath
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('compile-to-js typecheck behavior', () => {
|
|
25
|
+
test('fails on type errors by default', async () => {
|
|
26
|
+
writeTemp(
|
|
27
|
+
'tsconfig.json',
|
|
28
|
+
JSON.stringify(
|
|
29
|
+
{
|
|
30
|
+
compilerOptions: {
|
|
31
|
+
target: 'ES2022',
|
|
32
|
+
module: 'ESNext',
|
|
33
|
+
moduleResolution: 'Bundler',
|
|
34
|
+
skipLibCheck: true,
|
|
35
|
+
strict: true,
|
|
36
|
+
types: [],
|
|
37
|
+
lib: ['ESNext'],
|
|
38
|
+
},
|
|
39
|
+
include: ['src/**/*.ts'],
|
|
40
|
+
},
|
|
41
|
+
null,
|
|
42
|
+
2,
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
const entry = writeTemp('src/workflow.ts', "export const shouldBeNumber: number = 'oops'\n")
|
|
46
|
+
const output = path.join(tempDir, 'dist/workflow.js')
|
|
47
|
+
|
|
48
|
+
await expect(compileToJs(entry, output)).rejects.toBeInstanceOf(WorkflowTypecheckError)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('continues when --skip-type-checks is enabled', async () => {
|
|
52
|
+
writeTemp(
|
|
53
|
+
'tsconfig.json',
|
|
54
|
+
JSON.stringify(
|
|
55
|
+
{
|
|
56
|
+
compilerOptions: {
|
|
57
|
+
target: 'ES2022',
|
|
58
|
+
module: 'ESNext',
|
|
59
|
+
moduleResolution: 'Bundler',
|
|
60
|
+
skipLibCheck: true,
|
|
61
|
+
strict: true,
|
|
62
|
+
types: [],
|
|
63
|
+
lib: ['ESNext'],
|
|
64
|
+
},
|
|
65
|
+
include: ['src/**/*.ts'],
|
|
66
|
+
},
|
|
67
|
+
null,
|
|
68
|
+
2,
|
|
69
|
+
),
|
|
70
|
+
)
|
|
71
|
+
const entry = writeTemp('src/workflow.ts', "export const shouldBeNumber: number = 'oops'\n")
|
|
72
|
+
const output = path.join(tempDir, 'dist/workflow.js')
|
|
73
|
+
|
|
74
|
+
let thrownError: unknown
|
|
75
|
+
let result: string | undefined
|
|
76
|
+
try {
|
|
77
|
+
result = await compileToJs(entry, output, {
|
|
78
|
+
skipTypeChecks: true,
|
|
79
|
+
})
|
|
80
|
+
} catch (error) {
|
|
81
|
+
thrownError = error
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
expect(thrownError).not.toBeInstanceOf(WorkflowTypecheckError)
|
|
85
|
+
if (typeof result === 'string') {
|
|
86
|
+
expect(result).toEqual(output)
|
|
87
|
+
expect(existsSync(output)).toBeTrue()
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
})
|
|
@@ -2,24 +2,63 @@ import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
|
2
2
|
import { mkdir } from 'node:fs/promises'
|
|
3
3
|
import path from 'node:path'
|
|
4
4
|
import { $ } from 'bun'
|
|
5
|
+
import { parseCompileCliArgs, skipTypeChecksFlag } from './compile-cli-args'
|
|
6
|
+
import { assertWorkflowTypecheck } from './typecheck-workflow'
|
|
5
7
|
import { assertWorkflowRuntimeCompatibility } from './validate-workflow-runtime-compat'
|
|
6
8
|
import { wrapWorkflowCode } from './workflow-wrapper'
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
type CompileToJsOptions = {
|
|
11
|
+
skipTypeChecks?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const printUsage = () => {
|
|
15
|
+
console.error(`Usage: bun compile:ts-to-js <path-to-file> [output-file] [${skipTypeChecksFlag}]`)
|
|
16
|
+
console.error('Example:')
|
|
17
|
+
console.error(' bun compile:ts-to-js src/tests/foo.ts dist/tests/foo.bundle.js')
|
|
18
|
+
console.error(
|
|
19
|
+
` bun compile:ts-to-js src/tests/foo.ts dist/tests/foo.bundle.js ${skipTypeChecksFlag}`,
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const main = async (
|
|
24
|
+
tsFilePath?: string,
|
|
25
|
+
outputFilePath?: string,
|
|
26
|
+
options?: CompileToJsOptions,
|
|
27
|
+
) => {
|
|
28
|
+
let parsedInputPath: string | undefined
|
|
29
|
+
let parsedOutputPath: string | undefined
|
|
30
|
+
let parsedSkipTypeChecks = false
|
|
31
|
+
|
|
32
|
+
if (tsFilePath != null || outputFilePath != null || options?.skipTypeChecks != null) {
|
|
33
|
+
parsedInputPath = tsFilePath
|
|
34
|
+
parsedOutputPath = outputFilePath
|
|
35
|
+
parsedSkipTypeChecks = options?.skipTypeChecks ?? false
|
|
36
|
+
} else {
|
|
37
|
+
try {
|
|
38
|
+
const parsed = parseCompileCliArgs(process.argv.slice(3))
|
|
39
|
+
parsedInputPath = parsed.inputPath
|
|
40
|
+
parsedOutputPath = parsed.outputPath
|
|
41
|
+
parsedSkipTypeChecks = parsed.skipTypeChecks
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error(error instanceof Error ? error.message : error)
|
|
44
|
+
printUsage()
|
|
45
|
+
process.exit(1)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
10
48
|
|
|
11
49
|
// Prefer function params, fallback to CLI args
|
|
12
|
-
const inputPath =
|
|
13
|
-
const outputPathArg =
|
|
50
|
+
const inputPath = parsedInputPath
|
|
51
|
+
const outputPathArg = parsedOutputPath
|
|
14
52
|
|
|
15
53
|
if (!inputPath) {
|
|
16
|
-
|
|
17
|
-
console.error('Example:')
|
|
18
|
-
console.error(' bun test:standard:compile:js src/tests/foo.ts dist/tests/foo.bundle.js')
|
|
54
|
+
printUsage()
|
|
19
55
|
process.exit(1)
|
|
20
56
|
}
|
|
21
57
|
|
|
22
58
|
const resolvedInput = path.resolve(inputPath)
|
|
59
|
+
if (!parsedSkipTypeChecks) {
|
|
60
|
+
assertWorkflowTypecheck(resolvedInput)
|
|
61
|
+
}
|
|
23
62
|
assertWorkflowRuntimeCompatibility(resolvedInput)
|
|
24
63
|
console.info(`📁 Using input file: ${resolvedInput}`)
|
|
25
64
|
|
|
@@ -1,23 +1,59 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs'
|
|
2
2
|
import { mkdir } from 'node:fs/promises'
|
|
3
3
|
import path from 'node:path'
|
|
4
|
+
import { parseCompileCliArgs, skipTypeChecksFlag } from './compile-cli-args'
|
|
4
5
|
import { main as compileToJs } from './compile-to-js'
|
|
5
6
|
import { main as compileToWasm } from './compile-to-wasm'
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
type CompileWorkflowOptions = {
|
|
9
|
+
skipTypeChecks?: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const printUsage = () => {
|
|
13
|
+
console.error(
|
|
14
|
+
`Usage: bun compile:workflow <path/to/workflow.ts> [path/to/output.wasm] [${skipTypeChecksFlag}]`,
|
|
15
|
+
)
|
|
16
|
+
console.error('Examples:')
|
|
17
|
+
console.error(' bun compile:workflow src/standard_tests/secrets/test.ts')
|
|
18
|
+
console.error(
|
|
19
|
+
' bun compile:workflow src/standard_tests/secrets/test.ts .temp/standard_tests/secrets/test.wasm',
|
|
20
|
+
)
|
|
21
|
+
console.error(
|
|
22
|
+
` bun compile:workflow src/standard_tests/secrets/test.ts .temp/standard_tests/secrets/test.wasm ${skipTypeChecksFlag}`,
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const main = async (
|
|
27
|
+
inputFile?: string,
|
|
28
|
+
outputWasmFile?: string,
|
|
29
|
+
options?: CompileWorkflowOptions,
|
|
30
|
+
) => {
|
|
31
|
+
let parsedInputPath: string | undefined
|
|
32
|
+
let parsedOutputPath: string | undefined
|
|
33
|
+
let parsedSkipTypeChecks = false
|
|
9
34
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
35
|
+
if (inputFile != null || outputWasmFile != null || options?.skipTypeChecks != null) {
|
|
36
|
+
parsedInputPath = inputFile
|
|
37
|
+
parsedOutputPath = outputWasmFile
|
|
38
|
+
parsedSkipTypeChecks = options?.skipTypeChecks ?? false
|
|
39
|
+
} else {
|
|
40
|
+
try {
|
|
41
|
+
const parsed = parseCompileCliArgs(process.argv.slice(3))
|
|
42
|
+
parsedInputPath = parsed.inputPath
|
|
43
|
+
parsedOutputPath = parsed.outputPath
|
|
44
|
+
parsedSkipTypeChecks = parsed.skipTypeChecks
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error(error instanceof Error ? error.message : error)
|
|
47
|
+
printUsage()
|
|
48
|
+
process.exit(1)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const inputPath = parsedInputPath
|
|
53
|
+
const outputPathArg = parsedOutputPath
|
|
13
54
|
|
|
14
55
|
if (!inputPath) {
|
|
15
|
-
|
|
16
|
-
console.error('Examples:')
|
|
17
|
-
console.error(' bun compile:workflow src/standard_tests/secrets/test.ts')
|
|
18
|
-
console.error(
|
|
19
|
-
' bun compile:workflow src/standard_tests/secrets/test.ts .temp/standard_tests/secrets/test.wasm',
|
|
20
|
-
)
|
|
56
|
+
printUsage()
|
|
21
57
|
process.exit(1)
|
|
22
58
|
}
|
|
23
59
|
|
|
@@ -42,10 +78,13 @@ export const main = async (inputFile?: string, outputWasmFile?: string) => {
|
|
|
42
78
|
|
|
43
79
|
console.info(`🚀 Compiling workflow`)
|
|
44
80
|
console.info(`📁 Input: ${resolvedInput}\n`)
|
|
81
|
+
if (parsedSkipTypeChecks) {
|
|
82
|
+
console.info(`⚠️ Skipping TypeScript checks (${skipTypeChecksFlag})`)
|
|
83
|
+
}
|
|
45
84
|
|
|
46
85
|
// Step 1: TS/JS → JS (bundled)
|
|
47
86
|
console.info('📦 Step 1: Compiling JS...')
|
|
48
|
-
await compileToJs(resolvedInput, resolvedJsOutput)
|
|
87
|
+
await compileToJs(resolvedInput, resolvedJsOutput, { skipTypeChecks: parsedSkipTypeChecks })
|
|
49
88
|
|
|
50
89
|
// Step 2: JS → WASM
|
|
51
90
|
console.info('\n🔨 Step 2: Compiling to WASM...')
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { assertWorkflowTypecheck, WorkflowTypecheckError } from './typecheck-workflow'
|
|
6
|
+
|
|
7
|
+
let tempDir: string
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
tempDir = mkdtempSync(path.join(tmpdir(), 'cre-typecheck-test-'))
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
rmSync(tempDir, { recursive: true, force: true })
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const writeTemp = (filename: string, content: string): string => {
|
|
18
|
+
const filePath = path.join(tempDir, filename)
|
|
19
|
+
mkdirSync(path.dirname(filePath), { recursive: true })
|
|
20
|
+
writeFileSync(filePath, content, 'utf-8')
|
|
21
|
+
return filePath
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('assertWorkflowTypecheck', () => {
|
|
25
|
+
test('passes for valid project using nearby tsconfig', () => {
|
|
26
|
+
writeTemp(
|
|
27
|
+
'tsconfig.json',
|
|
28
|
+
JSON.stringify(
|
|
29
|
+
{
|
|
30
|
+
compilerOptions: {
|
|
31
|
+
target: 'ES2022',
|
|
32
|
+
module: 'ESNext',
|
|
33
|
+
moduleResolution: 'Bundler',
|
|
34
|
+
skipLibCheck: true,
|
|
35
|
+
},
|
|
36
|
+
include: ['src/**/*.ts'],
|
|
37
|
+
},
|
|
38
|
+
null,
|
|
39
|
+
2,
|
|
40
|
+
),
|
|
41
|
+
)
|
|
42
|
+
const entry = writeTemp('src/workflow.ts', 'export const value: number = 42\n')
|
|
43
|
+
expect(() => assertWorkflowTypecheck(entry)).not.toThrow()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('fails when tsconfig cannot be found', () => {
|
|
47
|
+
const entry = writeTemp('src/workflow.ts', 'export const value = 1\n')
|
|
48
|
+
expect(() => assertWorkflowTypecheck(entry)).toThrow(WorkflowTypecheckError)
|
|
49
|
+
expect(() => assertWorkflowTypecheck(entry)).toThrow('Could not find tsconfig.json')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('fails on whole-project type errors outside entry file', () => {
|
|
53
|
+
writeTemp(
|
|
54
|
+
'tsconfig.json',
|
|
55
|
+
JSON.stringify(
|
|
56
|
+
{
|
|
57
|
+
compilerOptions: {
|
|
58
|
+
target: 'ES2022',
|
|
59
|
+
module: 'ESNext',
|
|
60
|
+
moduleResolution: 'Bundler',
|
|
61
|
+
skipLibCheck: true,
|
|
62
|
+
strict: true,
|
|
63
|
+
},
|
|
64
|
+
include: ['src/**/*.ts'],
|
|
65
|
+
},
|
|
66
|
+
null,
|
|
67
|
+
2,
|
|
68
|
+
),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
const entry = writeTemp('src/workflow.ts', 'export const value: number = 1\n')
|
|
72
|
+
writeTemp('src/unrelated.ts', "export const shouldBeNumber: number = 'not-a-number'\n")
|
|
73
|
+
|
|
74
|
+
expect(() => assertWorkflowTypecheck(entry)).toThrow(WorkflowTypecheckError)
|
|
75
|
+
expect(() => assertWorkflowTypecheck(entry)).toThrow('unrelated.ts')
|
|
76
|
+
})
|
|
77
|
+
})
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import * as ts from 'typescript'
|
|
3
|
+
import { skipTypeChecksFlag } from './compile-cli-args'
|
|
4
|
+
|
|
5
|
+
const toAbsolutePath = (filePath: string) => path.resolve(filePath)
|
|
6
|
+
|
|
7
|
+
const formatDiagnostic = (diagnostic: ts.Diagnostic): string => {
|
|
8
|
+
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
|
|
9
|
+
if (!diagnostic.file || diagnostic.start == null) {
|
|
10
|
+
return message
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const absoluteFilePath = toAbsolutePath(diagnostic.file.fileName)
|
|
14
|
+
const relativeFilePath = path.relative(process.cwd(), absoluteFilePath)
|
|
15
|
+
const displayPath = relativeFilePath || absoluteFilePath
|
|
16
|
+
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start)
|
|
17
|
+
return `${displayPath}:${line + 1}:${character + 1} ${message}`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class WorkflowTypecheckError extends Error {
|
|
21
|
+
constructor(message: string) {
|
|
22
|
+
super(message)
|
|
23
|
+
this.name = 'WorkflowTypecheckError'
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const findNearestTsconfigPath = (entryFilePath: string): string | null => {
|
|
28
|
+
const configPath = ts.findConfigFile(
|
|
29
|
+
path.dirname(entryFilePath),
|
|
30
|
+
ts.sys.fileExists,
|
|
31
|
+
'tsconfig.json',
|
|
32
|
+
)
|
|
33
|
+
return configPath ?? null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const assertWorkflowTypecheck = (entryFilePath: string) => {
|
|
37
|
+
const rootFile = toAbsolutePath(entryFilePath)
|
|
38
|
+
const configPath = findNearestTsconfigPath(rootFile)
|
|
39
|
+
if (!configPath) {
|
|
40
|
+
throw new WorkflowTypecheckError(
|
|
41
|
+
`TypeScript typecheck failed before workflow compilation.
|
|
42
|
+
Could not find tsconfig.json near: ${rootFile}
|
|
43
|
+
Create a tsconfig.json in your workflow project, or re-run compile with ${skipTypeChecksFlag}.`,
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let unrecoverableDiagnostic: ts.Diagnostic | null = null
|
|
48
|
+
const parsedConfig = ts.getParsedCommandLineOfConfigFile(
|
|
49
|
+
configPath,
|
|
50
|
+
{},
|
|
51
|
+
{
|
|
52
|
+
...ts.sys,
|
|
53
|
+
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
54
|
+
unrecoverableDiagnostic = diagnostic
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
if (!parsedConfig) {
|
|
60
|
+
const details = unrecoverableDiagnostic ? formatDiagnostic(unrecoverableDiagnostic) : ''
|
|
61
|
+
throw new WorkflowTypecheckError(
|
|
62
|
+
`TypeScript typecheck failed before workflow compilation.
|
|
63
|
+
Failed to parse tsconfig.json: ${configPath}
|
|
64
|
+
${details}
|
|
65
|
+
Fix your tsconfig.json, or re-run compile with ${skipTypeChecksFlag}.`,
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const program = ts.createProgram({
|
|
70
|
+
rootNames: parsedConfig.fileNames,
|
|
71
|
+
options: {
|
|
72
|
+
...parsedConfig.options,
|
|
73
|
+
noEmit: true,
|
|
74
|
+
},
|
|
75
|
+
projectReferences: parsedConfig.projectReferences,
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const diagnostics = [...parsedConfig.errors, ...ts.getPreEmitDiagnostics(program)].filter(
|
|
79
|
+
(diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
if (diagnostics.length > 0) {
|
|
83
|
+
const formatted = diagnostics.map(formatDiagnostic).join('\n')
|
|
84
|
+
const relativeConfigPath = path.relative(process.cwd(), toAbsolutePath(configPath))
|
|
85
|
+
const displayConfigPath = relativeConfigPath || toAbsolutePath(configPath)
|
|
86
|
+
throw new WorkflowTypecheckError(
|
|
87
|
+
`TypeScript typecheck failed before workflow compilation.
|
|
88
|
+
Using tsconfig: ${displayConfigPath}
|
|
89
|
+
Fix TypeScript errors, or re-run compile with ${skipTypeChecksFlag}.
|
|
90
|
+
|
|
91
|
+
${formatted}`,
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { WorkflowTypecheckError }
|