java-bridge 2.1.1 → 2.1.4

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,86 +0,0 @@
1
- import path from 'path';
2
- import fs, { readFileSync } from 'fs';
3
- import glob from 'glob';
4
-
5
- const { platform, arch } = process;
6
-
7
- function getModule(base: string): string {
8
- const local = path.join(__dirname, base + '.node');
9
-
10
- if (fs.existsSync(local)) {
11
- return local;
12
- } else {
13
- const module = base.replaceAll('.', '-').replace('java', 'java-bridge');
14
- // @ts-ignore
15
- if (__non_webpack_require__ && __non_webpack_require__.resolve) {
16
- // @ts-ignore
17
- return __non_webpack_require__.resolve(module);
18
- } else {
19
- return require.resolve(module);
20
- }
21
- }
22
- }
23
-
24
- function UnsupportedPlatform(): Error {
25
- return new Error(`Unsupported platform: ${platform} ${arch}`);
26
- }
27
-
28
- function isMusl() {
29
- // For Node 10
30
- if (!process.report || typeof process.report.getReport !== 'function') {
31
- try {
32
- return readFileSync('/usr/bin/ldd', 'utf8').includes('musl');
33
- } catch (e) {
34
- return true;
35
- }
36
- } else {
37
- const { glibcVersionRuntime } = (process.report.getReport() as any)
38
- .header;
39
- return !glibcVersionRuntime;
40
- }
41
- }
42
-
43
- export function getNativeLibPath(): string {
44
- switch (platform) {
45
- case 'android':
46
- switch (arch) {
47
- case 'arm64':
48
- return getModule('java.android-arm64');
49
- case 'arm':
50
- return getModule('java.android-arm-eabi');
51
- default:
52
- throw UnsupportedPlatform();
53
- }
54
- case 'win32':
55
- return getModule(`java.win32-${arch}-msvc`);
56
- case 'darwin':
57
- return getModule(`java.darwin-${arch}`);
58
- case 'freebsd':
59
- return getModule(`java.freebsd-${arch}`);
60
- case 'linux':
61
- switch (arch) {
62
- case 'x64':
63
- case 'arm64':
64
- return getModule(
65
- `java.linux-${arch}-${isMusl() ? 'musl' : 'gnu'}`
66
- );
67
- case 'arm':
68
- return getModule('java.linux-arm-gnueabihf');
69
- default:
70
- throw UnsupportedPlatform();
71
- }
72
- default:
73
- throw UnsupportedPlatform();
74
- }
75
- }
76
-
77
- export function getJavaLibPath(): string {
78
- const dir = path.join(__dirname, '..', 'java-src', 'build', 'libs');
79
- let files = glob.sync('*.jar', { cwd: dir });
80
-
81
- if (files.length === 0) {
82
- throw new Error(`No java lib found in ${dir}`);
83
- } else {
84
- return path.join(dir, files[0]);
85
- }
86
- }
@@ -1,142 +0,0 @@
1
- import yargs from 'yargs';
2
- import { performance } from 'perf_hooks';
3
- import path from 'path';
4
- import java from '..';
5
- import { version } from '../../package.json';
6
-
7
- interface Args {
8
- classnames: string[];
9
- output: string;
10
- classpath?: string | string[];
11
- }
12
-
13
- type YargsHandler<T> = (args: yargs.ArgumentsCamelCase<T>) => Promise<void>;
14
-
15
- const importOra = (): Promise<typeof import('ora').default> =>
16
- eval("import('ora').then(ora => ora.default)");
17
- const importChalk = (): Promise<typeof import('chalk').default> =>
18
- eval("import('chalk').then(chalk => chalk.default)");
19
-
20
- const builder: yargs.BuilderCallback<{}, Args> = (command) => {
21
- command.positional('classnames', {
22
- describe: 'The fully qualified class name(s) to convert',
23
- type: 'string',
24
- });
25
-
26
- command.positional('output', {
27
- describe: 'The output file',
28
- type: 'string',
29
- });
30
-
31
- command.option('classpath', {
32
- alias: 'cp',
33
- type: 'string',
34
- describe: 'The classpath to use',
35
- });
36
- };
37
-
38
- const handler: YargsHandler<Args> = async ({
39
- classnames,
40
- output,
41
- classpath,
42
- }) => {
43
- try {
44
- const startTime = performance.now();
45
-
46
- if (classpath) {
47
- java.classpath.append(classpath);
48
- }
49
-
50
- const chalk = await importChalk();
51
- const ora = await importOra();
52
-
53
- console.log(
54
- `Starting ${chalk.cyanBright('java-bridge')} ${chalk.greenBright(
55
- 'v' + version
56
- )} Java definition generator`
57
- );
58
-
59
- const javaInstance = java.getJavaInstance()!;
60
- const loadedJars = java.classpath.get();
61
- if (loadedJars.length > 0) {
62
- console.log(
63
- `Started JVM with version ${chalk.cyanBright(
64
- javaInstance.version
65
- )} and classpath '${loadedJars
66
- .map((j) => chalk.cyanBright(j))
67
- .join(';')}'`
68
- );
69
- }
70
-
71
- console.log(
72
- `Converting classes ${classnames
73
- .map((c) => chalk.magentaBright(c))
74
- .join(
75
- ', '
76
- )} to typescript and saving result to ${chalk.cyanBright(
77
- path.normalize(output)
78
- )}`
79
- );
80
-
81
- const spinner = ora().start();
82
-
83
- const resolvedImports: string[] = [];
84
- let resolvedCounter: number = 0;
85
- let numResolved: number = 0;
86
-
87
- let approximateTimeElapsed: number = 0;
88
- let lastClassResolved: string = '';
89
- const timeElapsedInterval = setInterval(() => {
90
- approximateTimeElapsed += 1;
91
- setText();
92
- }, 1000);
93
-
94
- const setText = () => {
95
- spinner.text = chalk.gray(
96
- `Elapsed time: ${chalk.yellow(
97
- approximateTimeElapsed
98
- )} seconds ${chalk.white('|')} Converted ${chalk.cyanBright(
99
- resolvedCounter
100
- )} classes ${chalk.white(
101
- '|'
102
- )} Converting class ${chalk.magentaBright(lastClassResolved)}`
103
- );
104
- };
105
-
106
- const TypescriptDefinitionGenerator = (
107
- await import('../TypescriptDefinitionGenerator')
108
- ).default;
109
-
110
- for (const classname of classnames) {
111
- const generator = new TypescriptDefinitionGenerator(
112
- classname,
113
- (name) => {
114
- lastClassResolved = name;
115
- resolvedCounter++;
116
- setText();
117
- },
118
- resolvedImports
119
- );
120
- const generated = await generator.generate();
121
- numResolved += generated.length;
122
-
123
- spinner.text = 'saving results';
124
- await TypescriptDefinitionGenerator.save(generated, output);
125
- }
126
-
127
- clearInterval(timeElapsedInterval);
128
- const timeElapsed = ((performance.now() - startTime) / 1000).toFixed(1);
129
- spinner.succeed(
130
- `Success - Converted ${chalk.blueBright(
131
- numResolved
132
- )} classes in ${chalk.blueBright(timeElapsed)} seconds`
133
- );
134
- } catch (e) {
135
- console.error(e);
136
- process.exit(1);
137
- }
138
- };
139
-
140
- yargs
141
- .command<Args>('* <output> <classnames..>', false, builder, handler)
142
- .parse();