|
| 1 | +#! /usr/bin/env node |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +const fs = require("fs"); |
| 5 | +const os = require("os"); |
| 6 | +const path = require("path"); |
| 7 | +const ts = require("typescript"); |
| 8 | +const { beforeEachTestCase } = require("./helpers/common"); |
| 9 | + |
| 10 | +// This test ensures the published TypeScript declarations remain valid for a consumer project. |
| 11 | +describe("TypeScript usage", () => { |
| 12 | + beforeEach(beforeEachTestCase); |
| 13 | + |
| 14 | + it("type-checks a sample consumer", () => { |
| 15 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "java-caller-ts-")); |
| 16 | + const sourcePath = path.join(tempDir, "example.ts"); |
| 17 | + |
| 18 | + const config = { |
| 19 | + compilerOptions: { |
| 20 | + target: "ES2019", |
| 21 | + module: "CommonJS", |
| 22 | + moduleResolution: "Node", |
| 23 | + strict: true, |
| 24 | + esModuleInterop: true, |
| 25 | + allowSyntheticDefaultImports: true, |
| 26 | + baseUrl: process.cwd(), |
| 27 | + paths: { |
| 28 | + "java-caller": ["lib/index.d.ts"] |
| 29 | + }, |
| 30 | + types: ["node"] |
| 31 | + }, |
| 32 | + include: ["example.ts"] |
| 33 | + }; |
| 34 | + |
| 35 | + fs.writeFileSync(sourcePath, `import { JavaCaller, JavaCallerOptions, JavaCallerResult } from "java-caller"; |
| 36 | +
|
| 37 | +const options: JavaCallerOptions = { |
| 38 | + classPath: "test/java/dist", |
| 39 | + mainClass: "com.nvuillam.javacaller.JavaCallerTester", |
| 40 | + minimumJavaVersion: 8, |
| 41 | + javaType: "jre" |
| 42 | +}; |
| 43 | +
|
| 44 | +async function runExample(): Promise<JavaCallerResult> { |
| 45 | + const java = new JavaCaller(options); |
| 46 | + const result = await java.run(["--sleep"], { detached: true, stdoutEncoding: "utf8" }); |
| 47 | + if (result.childJavaProcess) { |
| 48 | + result.childJavaProcess.kill("SIGINT"); |
| 49 | + } |
| 50 | + return result; |
| 51 | +} |
| 52 | +
|
| 53 | +async function run(): Promise<void> { |
| 54 | + const result = await runExample(); |
| 55 | + const statusText: string = result.status === 0 ? "ok" : "ko"; |
| 56 | + console.log(statusText, result.stdout, result.stderr); |
| 57 | +} |
| 58 | +
|
| 59 | +run(); |
| 60 | +`); |
| 61 | + |
| 62 | + try { |
| 63 | + const parsed = ts.parseJsonConfigFileContent(config, ts.sys, tempDir); |
| 64 | + const program = ts.createProgram({ rootNames: parsed.fileNames, options: parsed.options }); |
| 65 | + const diagnostics = ts.getPreEmitDiagnostics(program); |
| 66 | + |
| 67 | + if (diagnostics.length) { |
| 68 | + const formatted = diagnostics |
| 69 | + .map(diag => { |
| 70 | + if (diag.file && typeof diag.start === "number") { |
| 71 | + const { line, character } = diag.file.getLineAndCharacterOfPosition(diag.start); |
| 72 | + const message = ts.flattenDiagnosticMessageText(diag.messageText, "\n"); |
| 73 | + return `${diag.file.fileName} (${line + 1},${character + 1}): ${message}`; |
| 74 | + } |
| 75 | + return ts.flattenDiagnosticMessageText(diag.messageText, "\n"); |
| 76 | + }) |
| 77 | + .join("\n"); |
| 78 | + throw new Error(`TypeScript compilation failed:\n${formatted}`); |
| 79 | + } |
| 80 | + } finally { |
| 81 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 82 | + } |
| 83 | + }); |
| 84 | +}); |
0 commit comments