@thiagodiogo/psjava 0.0.1-beta.2 → 0.0.1-beta.3
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/dist/cli.js +5 -5
- package/dist/commands/doctor.js +1 -1
- package/dist/commands/run.js +2 -2
- package/dist/core/jdk.d.ts +1 -1
- package/dist/core/jdk.js +2 -2
- package/dist/core/runner.js +12 -2
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -3,16 +3,16 @@ import { runFile } from './commands/run.js';
|
|
|
3
3
|
import { runDoctor } from './commands/doctor.js';
|
|
4
4
|
export function buildProgram() {
|
|
5
5
|
const program = new Command();
|
|
6
|
-
program.name('psjava').description('
|
|
6
|
+
program.name('psjava').description('Run .psjava files').version('0.1.0');
|
|
7
7
|
program
|
|
8
|
-
.argument('[file]', '
|
|
9
|
-
.option('-d, --debug', '
|
|
8
|
+
.argument('[file]', '.psjava file')
|
|
9
|
+
.option('-d, --debug', 'print the elapsed time at the end')
|
|
10
10
|
.action(async (file, opts) => {
|
|
11
11
|
if (!file)
|
|
12
12
|
return program.help();
|
|
13
13
|
await runFile(file, opts.debug);
|
|
14
14
|
});
|
|
15
|
-
program.command('doctor').description('
|
|
15
|
+
program.command('doctor').description('Check the JDK (jshell)').action(runDoctor);
|
|
16
16
|
return program;
|
|
17
17
|
}
|
|
18
18
|
export async function runCli(argv = process.argv) {
|
|
@@ -20,7 +20,7 @@ export async function runCli(argv = process.argv) {
|
|
|
20
20
|
await buildProgram().parseAsync(argv);
|
|
21
21
|
}
|
|
22
22
|
catch (err) {
|
|
23
|
-
console.error(`\
|
|
23
|
+
console.error(`\nError: ${err instanceof Error ? err.message : String(err)}`);
|
|
24
24
|
process.exitCode = 1;
|
|
25
25
|
}
|
|
26
26
|
}
|
package/dist/commands/doctor.js
CHANGED
|
@@ -3,7 +3,7 @@ import { resolveJshell } from '../core/jdk.js';
|
|
|
3
3
|
export async function runDoctor() {
|
|
4
4
|
try {
|
|
5
5
|
await resolveJshell();
|
|
6
|
-
console.log(chalk.green('✓ jshell
|
|
6
|
+
console.log(chalk.green('✓ jshell found — ready to run .psjava'));
|
|
7
7
|
}
|
|
8
8
|
catch (err) {
|
|
9
9
|
console.log(chalk.red(`✗ ${err instanceof Error ? err.message : String(err)}`));
|
package/dist/commands/run.js
CHANGED
|
@@ -13,11 +13,11 @@ export function scriptCandidates(file) {
|
|
|
13
13
|
export async function runFile(file, debug = false) {
|
|
14
14
|
const tried = scriptCandidates(file);
|
|
15
15
|
if (tried.length === 0) {
|
|
16
|
-
throw new Error(`psjava
|
|
16
|
+
throw new Error(`psjava only runs .psjava files (got: ${file})`);
|
|
17
17
|
}
|
|
18
18
|
const found = tried.find((f) => existsSync(f));
|
|
19
19
|
if (!found) {
|
|
20
|
-
throw new Error(`
|
|
20
|
+
throw new Error(`file not found. Tried: ${tried.join(', ')}`);
|
|
21
21
|
}
|
|
22
22
|
const source = await readFile(found, 'utf8');
|
|
23
23
|
const code = await runSource(source, debug);
|
package/dist/core/jdk.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Finds jshell on the PATH and confirms it runs. Throws a friendly error if missing. */
|
|
2
2
|
export declare function resolveJshell(): Promise<string>;
|
package/dist/core/jdk.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { execFile } from 'child_process';
|
|
2
2
|
import { promisify } from 'util';
|
|
3
3
|
const exec = promisify(execFile);
|
|
4
|
-
/**
|
|
4
|
+
/** Finds jshell on the PATH and confirms it runs. Throws a friendly error if missing. */
|
|
5
5
|
export async function resolveJshell() {
|
|
6
6
|
try {
|
|
7
7
|
await exec('jshell', ['--version']);
|
|
8
8
|
return 'jshell';
|
|
9
9
|
}
|
|
10
10
|
catch {
|
|
11
|
-
throw new Error('jshell
|
|
11
|
+
throw new Error('jshell not found. Install a JDK (11+) and make sure jshell is on your PATH.');
|
|
12
12
|
}
|
|
13
13
|
}
|
package/dist/core/runner.js
CHANGED
|
@@ -11,18 +11,28 @@ const PRELUDE = [
|
|
|
11
11
|
export function buildSession(source) {
|
|
12
12
|
return `${PRELUDE}\n${source.replace(/^/, '')}`; // ponytail: BOM é lixo de encoding, não é edição do código
|
|
13
13
|
}
|
|
14
|
+
// Força UTF-8 na entrada e na saída do jshell — sem isso, em locale não-UTF-8 (ex: runner Linux
|
|
15
|
+
// em latin1) o `á` sai como byte solto e vira "ol�". -J = VM do jshell (escreve a saída final);
|
|
16
|
+
// -R = VM de execução (roda o System.out). file.encoding cobre Java 11; stdout/stderr.encoding cobre 18+.
|
|
17
|
+
const UTF8_FLAGS = [
|
|
18
|
+
'-J-Dfile.encoding=UTF-8',
|
|
19
|
+
'-J-Dstdout.encoding=UTF-8',
|
|
20
|
+
'-J-Dstderr.encoding=UTF-8',
|
|
21
|
+
'-R-Dfile.encoding=UTF-8',
|
|
22
|
+
'-R-Dstdout.encoding=UTF-8',
|
|
23
|
+
];
|
|
14
24
|
export async function runSource(source, debug = false) {
|
|
15
25
|
const jshell = await resolveJshell(); // erro amigável se faltar JDK
|
|
16
26
|
const code = buildSession(source);
|
|
17
27
|
const start = performance.now();
|
|
18
28
|
return new Promise((resolve) => {
|
|
19
29
|
// -s: modo script silencioso (sem banner/prompt); lê de stdin e sai sozinho
|
|
20
|
-
const p = spawn(jshell, ['-s', '-'], { stdio: ['pipe', 'inherit', 'inherit'] });
|
|
30
|
+
const p = spawn(jshell, ['-s', ...UTF8_FLAGS, '-'], { stdio: ['pipe', 'inherit', 'inherit'] });
|
|
21
31
|
p.stdin.end(code + '\n');
|
|
22
32
|
p.on('close', (c) => {
|
|
23
33
|
if (debug) {
|
|
24
34
|
const s = ((performance.now() - start) / 1000).toFixed(2);
|
|
25
|
-
console.error(chalk.dim(`[psjava]
|
|
35
|
+
console.error(chalk.dim(`[psjava] done in ${s}s`));
|
|
26
36
|
}
|
|
27
37
|
resolve(c ?? 0);
|
|
28
38
|
});
|