Skip to content

Commit 198dd96

Browse files
Copilotnvuillam
andauthored
Add TypeScript type definitions (#148)
* Initial plan * Initial plan for TypeScript compatibility Co-authored-by: nvuillam <17500430+nvuillam@users.noreply.github.com> * Add TypeScript type definitions for java-caller Co-authored-by: nvuillam <17500430+nvuillam@users.noreply.github.com> * Add TypeScript usage documentation Co-authored-by: nvuillam <17500430+nvuillam@users.noreply.github.com> * npm audit fix * Fix TS * use javascript megalinter flavor * test typescript integration * changelog * fix ts megalinter * typo * rhaaa * go * rhaaaa --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: nvuillam <17500430+nvuillam@users.noreply.github.com> Co-authored-by: Nicolas Vuillamy <nicolas.vuillamy@gmail.com>
1 parent 19b2784 commit 198dd96

File tree

9 files changed

+416
-5
lines changed

9 files changed

+416
-5
lines changed

.eslintrc.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,22 @@ module.exports = {
2222
parserOptions: {
2323
ecmaVersion: 2018,
2424
requireConfigFile: false,
25-
sourceType: "module"
25+
sourceType: "module",
26+
babelOptions: {
27+
parserOpts: {
28+
plugins: ["typescript"]
29+
}
30+
}
2631
},
32+
overrides: [
33+
{
34+
files: ["**/*.d.ts"],
35+
parser: "@typescript-eslint/parser",
36+
rules: {
37+
"getter-return": "off"
38+
}
39+
}
40+
],
2741
rules: {
2842
"indent": "off" // Managed by prettier
2943
}

.github/workflows/mega-linter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030

3131
# Mega-Linter
3232
- name: Mega-Linter
33-
uses: oxsecurity/megalinter/flavors/cupcake@v9
33+
uses: oxsecurity/megalinter/flavors/javascript@v9
3434
env:
3535
# All available variables are described in documentation
3636
# https://github.com/oxsecurity/megalinter#configuration

.mega-linter.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
DISABLE_LINTERS:
2+
- TYPESCRIPT_STANDARD
3+
- TYPESCRIPT_PRETTIER
4+
TYPESCRIPT_STANDARD_DISABLE_ERRORS: true
15
DISABLE_ERRORS_LINTERS:
26
- ACTION_ACTIONLINT
37
- SPELL_LYCHEE
48
FILTER_REGEX_EXCLUDE: (docs\/github-dependents-info\.md|package-lock\.json)
9+
TYPESCRIPT_DEFAULT_STYLE: prettier

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Unreleased
44

5+
## [4.3.3] 2025-02-03
6+
7+
- Add types definition to make library compliant with typescript usage
8+
- Upgrade dependencies
9+
- CI: Use MegaLinter javascript flavor for better performance
10+
511
## [4.3.2] 2025-01-24
612

713
- Upgrade dependencies

docs/typescript-usage.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# TypeScript Support Example
2+
3+
This example demonstrates how to use java-caller with TypeScript, including strict mode compatibility.
4+
5+
## Setup
6+
7+
```bash
8+
npm install java-caller typescript @types/node
9+
```
10+
11+
## Usage
12+
13+
### Basic TypeScript Example
14+
15+
```typescript
16+
import { JavaCaller, JavaCallerOptions } from "java-caller";
17+
18+
// Define options with full type safety
19+
const options: JavaCallerOptions = {
20+
classPath: 'java/MyApp.jar',
21+
mainClass: 'com.example.MyApp',
22+
minimumJavaVersion: 11,
23+
javaType: "jre"
24+
};
25+
26+
// Create instance with autocomplete support
27+
const java = new JavaCaller(options);
28+
29+
// Run with type-safe result
30+
const result = await java.run(['-arg1', 'value']);
31+
console.log(`Status: ${result.status}`);
32+
console.log(`Output: ${result.stdout}`);
33+
```
34+
35+
### Strict Mode Compatibility
36+
37+
The type definitions are fully compatible with TypeScript's strict mode:
38+
39+
```typescript
40+
// tsconfig.json
41+
{
42+
"compilerOptions": {
43+
"strict": true,
44+
"noImplicitAny": true,
45+
"strictNullChecks": true
46+
}
47+
}
48+
```
49+
50+
### ES Module Import
51+
52+
```typescript
53+
import { JavaCaller, JavaCallerCli } from "java-caller";
54+
```
55+
56+
### CommonJS Require
57+
58+
```typescript
59+
const { JavaCaller, JavaCallerCli } = require("java-caller");
60+
```
61+
62+
Both import styles work with full TypeScript support!

lib/index.d.ts

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
2+
/* eslint-disable no-unused-vars */
3+
/// <reference types="node" />
4+
5+
/**
6+
* Options for JavaCaller constructor
7+
*/
8+
export interface JavaCallerOptions {
9+
/**
10+
* Path to executable jar file
11+
*/
12+
jar?: string;
13+
14+
/**
15+
* If jar parameter is not set, classpath to use.
16+
* Use : as separator (it will be converted if run on Windows), or use a string array.
17+
*/
18+
classPath?: string | string[];
19+
20+
/**
21+
* Set to true if classpaths should not be based on the rootPath
22+
*/
23+
useAbsoluteClassPaths?: boolean;
24+
25+
/**
26+
* If classPath set, main class to call
27+
*/
28+
mainClass?: string;
29+
30+
/**
31+
* Minimum java version to be used to call java command.
32+
* If the java version found on machine is lower, java-caller will try to install and use the appropriate one
33+
* @default 8 (11 on macOS)
34+
*/
35+
minimumJavaVersion?: number;
36+
37+
/**
38+
* Maximum java version to be used to call java command.
39+
* If the java version found on machine is upper, java-caller will try to install and use the appropriate one
40+
*/
41+
maximumJavaVersion?: number;
42+
43+
/**
44+
* jre or jdk (if not defined and installation is required, jre will be installed)
45+
*/
46+
javaType?: "jre" | "jdk";
47+
48+
/**
49+
* If classPath elements are not relative to the current folder, you can define a root path.
50+
* You may use __dirname if you classes / jars are in your module folder
51+
* @default "." (current folder)
52+
*/
53+
rootPath?: string;
54+
55+
/**
56+
* You can force to use a defined java executable, instead of letting java-caller find/install one.
57+
* Can also be defined with env var JAVA_CALLER_JAVA_EXECUTABLE
58+
*/
59+
javaExecutable?: string;
60+
61+
/**
62+
* Additional parameters for JVM that will be added in every JavaCaller instance runs
63+
*/
64+
additionalJavaArgs?: string[];
65+
66+
/**
67+
* Output mode: "none" or "console"
68+
* @default "none"
69+
*/
70+
output?: "none" | "console";
71+
}
72+
73+
/**
74+
* Options for JavaCaller run method
75+
*/
76+
export interface JavaCallerRunOptions {
77+
/**
78+
* If set to true, node will not wait for the java command to be completed.
79+
* In that case, childJavaProcess property will be returned, but stdout and stderr may be empty
80+
* @default false
81+
*/
82+
detached?: boolean;
83+
84+
/**
85+
* Adds control on spawn process stdout
86+
* @default "utf8"
87+
*/
88+
stdoutEncoding?: string;
89+
90+
/**
91+
* If detached is true, number of milliseconds to wait to detect an error before exiting JavaCaller run
92+
* @default 500
93+
*/
94+
waitForErrorMs?: number;
95+
96+
/**
97+
* You can override cwd of spawn called by JavaCaller runner
98+
* @default process.cwd()
99+
*/
100+
cwd?: string;
101+
102+
/**
103+
* List of arguments for JVM only, not the JAR or the class
104+
*/
105+
javaArgs?: string[];
106+
107+
/**
108+
* No quoting or escaping of arguments is done on Windows. Ignored on Unix.
109+
* This is set to true automatically when shell is specified and is CMD.
110+
* @default true
111+
*/
112+
windowsVerbatimArguments?: boolean;
113+
114+
/**
115+
* If windowless is true, JavaCaller calls javaw instead of java to not create any windows,
116+
* useful when using detached on Windows. Ignored on Unix.
117+
* @default false
118+
*/
119+
windowless?: boolean;
120+
}
121+
122+
/**
123+
* Result returned by JavaCaller run method
124+
*/
125+
export interface JavaCallerResult {
126+
/**
127+
* Exit status code of the java command
128+
*/
129+
status: number | null;
130+
131+
/**
132+
* Standard output of the java command
133+
*/
134+
stdout: string;
135+
136+
/**
137+
* Standard error output of the java command
138+
*/
139+
stderr: string;
140+
141+
/**
142+
* Child process object (useful when detached is true)
143+
*/
144+
childJavaProcess?: import('child_process').ChildProcess;
145+
}
146+
147+
/**
148+
* JavaCaller class for calling Java commands from Node.js
149+
*/
150+
export class JavaCaller {
151+
minimumJavaVersion: number;
152+
maximumJavaVersion?: number;
153+
javaType?: "jre" | "jdk";
154+
rootPath: string;
155+
jar?: string;
156+
classPath: string | string[];
157+
useAbsoluteClassPaths: boolean;
158+
mainClass?: string;
159+
output: string;
160+
status: number | null;
161+
javaSupportDir?: string;
162+
javaExecutable: string;
163+
javaExecutableWindowless: string;
164+
additionalJavaArgs: string[];
165+
commandJavaArgs: string[];
166+
javaHome?: string;
167+
javaBin?: string;
168+
javaExecutableFromNodeJavaCaller?: string | null;
169+
prevPath?: string;
170+
prevJavaHome?: string;
171+
172+
/**
173+
* Creates a JavaCaller instance
174+
* @param opts - Run options
175+
*/
176+
constructor(opts: JavaCallerOptions);
177+
178+
/**
179+
* Runs java command of a JavaCaller instance
180+
* @param userArguments - Java command line arguments
181+
* @param runOptions - Run options
182+
* @returns Command result (status, stdout, stderr, childJavaProcess)
183+
*/
184+
run(userArguments?: string[], runOptions?: JavaCallerRunOptions): Promise<JavaCallerResult>;
185+
}
186+
187+
/**
188+
* JavaCallerCli class for using java-caller from command line
189+
*/
190+
export class JavaCallerCli {
191+
javaCallerOptions: JavaCallerOptions;
192+
193+
/**
194+
* Creates a JavaCallerCli instance
195+
* @param baseDir - Base directory containing java-caller-config.json
196+
*/
197+
constructor(baseDir: string);
198+
199+
/**
200+
* Process command line arguments and run java command
201+
*/
202+
process(): Promise<void>;
203+
}

package-lock.json

Lines changed: 36 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)