java-bridge 2.1.3 → 2.1.5-beta.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.
@@ -1,148 +0,0 @@
1
- import yargs from 'yargs';
2
- import { performance } from 'perf_hooks';
3
- import path from 'path';
4
- import java, {
5
- ensureJvm,
6
- getJavaInstance,
7
- TypescriptDefinitionGenerator,
8
- } from '../.';
9
- import { version } from '../../package.json';
10
- import type { Ora } from 'ora';
11
-
12
- interface Args {
13
- classnames: string[];
14
- output: string;
15
- classpath?: string | string[];
16
- }
17
-
18
- type YargsHandler<T> = (args: yargs.ArgumentsCamelCase<T>) => Promise<void>;
19
-
20
- const importOra = (): Promise<typeof import('ora').default> =>
21
- eval("import('ora').then(ora => ora.default)");
22
- const importChalk = (): Promise<typeof import('chalk').default> =>
23
- eval("import('chalk').then(chalk => chalk.default)");
24
-
25
- const builder: yargs.BuilderCallback<{}, Args> = (command) => {
26
- command.positional('classnames', {
27
- describe: 'The fully qualified class name(s) to convert',
28
- type: 'string',
29
- });
30
-
31
- command.positional('output', {
32
- describe: 'The output file',
33
- type: 'string',
34
- });
35
-
36
- command.option('classpath', {
37
- alias: 'cp',
38
- type: 'string',
39
- describe: 'The classpath to use',
40
- });
41
- };
42
-
43
- const handler: YargsHandler<Args> = async ({
44
- classnames,
45
- output,
46
- classpath,
47
- }) => {
48
- let spinner: Ora | null = null;
49
- try {
50
- const startTime = performance.now();
51
- ensureJvm({
52
- useDaemonThreads: true,
53
- });
54
-
55
- if (classpath) {
56
- java.classpath.append(classpath);
57
- }
58
-
59
- const chalk = await importChalk();
60
- const ora = await importOra();
61
-
62
- console.log(
63
- `Starting ${chalk.cyanBright('java-bridge')} ${chalk.greenBright(
64
- 'v' + version
65
- )} Java definition generator`
66
- );
67
-
68
- const javaInstance = getJavaInstance()!;
69
- const loadedJars = java.classpath.get();
70
- if (loadedJars.length > 0) {
71
- console.log(
72
- `Started JVM with version ${chalk.cyanBright(
73
- javaInstance.version
74
- )} and classpath '${loadedJars
75
- .map((j) => chalk.cyanBright(j))
76
- .join(';')}'`
77
- );
78
- }
79
-
80
- console.log(
81
- `Converting classes ${classnames
82
- .map((c) => chalk.magentaBright(c))
83
- .join(
84
- ', '
85
- )} to typescript and saving result to ${chalk.cyanBright(
86
- path.normalize(output)
87
- )}`
88
- );
89
-
90
- spinner = ora().start();
91
-
92
- const resolvedImports: string[] = [];
93
- let resolvedCounter: number = 0;
94
- let numResolved: number = 0;
95
-
96
- let approximateTimeElapsed: number = 0;
97
- let lastClassResolved: string = '';
98
- const timeElapsedInterval = setInterval(() => {
99
- approximateTimeElapsed += 1;
100
- setText();
101
- }, 1000);
102
-
103
- const setText = () => {
104
- spinner!.text = chalk.gray(
105
- `Elapsed time: ${chalk.yellow(
106
- approximateTimeElapsed
107
- )} seconds ${chalk.white('|')} Converted ${chalk.cyanBright(
108
- resolvedCounter
109
- )} classes ${chalk.white(
110
- '|'
111
- )} Converting class ${chalk.magentaBright(lastClassResolved)}`
112
- );
113
- };
114
-
115
- for (const classname of classnames) {
116
- const generator = new TypescriptDefinitionGenerator(
117
- classname,
118
- (name) => {
119
- lastClassResolved = name;
120
- resolvedCounter++;
121
- setText();
122
- },
123
- resolvedImports
124
- );
125
- const generated = await generator.generate();
126
- numResolved += generated.length;
127
-
128
- spinner!.text = 'saving results';
129
- await TypescriptDefinitionGenerator.save(generated, output);
130
- }
131
-
132
- clearInterval(timeElapsedInterval);
133
- const timeElapsed = ((performance.now() - startTime) / 1000).toFixed(1);
134
- spinner!.succeed(
135
- `Success - Converted ${chalk.blueBright(
136
- numResolved
137
- )} classes in ${chalk.blueBright(timeElapsed)} seconds`
138
- );
139
- } catch (e) {
140
- spinner?.fail('Failed to convert classes');
141
- console.error(e);
142
- process.exit(1);
143
- }
144
- };
145
-
146
- yargs
147
- .command<Args>('* <output> <classnames..>', false, builder, handler)
148
- .parse();