fauxnix-cli 1.0.0 → 2.1.0
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/README.md +61 -8
- package/dist/cli.js +30 -7
- package/dist/commands/archive.js +135 -103
- package/dist/commands/files.d.ts +2 -1
- package/dist/commands/files.js +140 -50
- package/dist/commands/gzip-node.d.ts +17 -0
- package/dist/commands/gzip-node.js +136 -0
- package/dist/commands/move-native.d.ts +1 -0
- package/dist/commands/move-native.js +82 -0
- package/dist/commands/net.js +26 -34
- package/dist/commands/sysinfo.js +7 -3
- package/dist/commands/text-filters.js +60 -123
- package/dist/commands/text-io.js +106 -92
- package/dist/commands/text-output.d.ts +14 -0
- package/dist/commands/text-output.js +29 -0
- package/dist/doctor.js +18 -12
- package/dist/encoding.d.ts +5 -0
- package/dist/encoding.js +40 -0
- package/dist/executor.js +1 -15
- package/dist/facade-launch.d.ts +29 -0
- package/dist/facade-launch.js +197 -0
- package/dist/facade-main.d.ts +1 -0
- package/dist/facade-main.js +11 -0
- package/dist/facade-sea.cjs +17800 -0
- package/dist/facade.js +137 -8
- package/dist/index.js +11 -5
- package/dist/mcp.d.ts +1 -1
- package/dist/mcp.js +162 -141
- package/dist/parser.js +42 -33
- package/dist/ps-host.js +34 -3
- package/dist/registry.d.ts +5 -0
- package/dist/registry.js +10 -5
- package/dist/runtime-support.d.ts +3 -0
- package/dist/runtime-support.js +31 -0
- package/dist/translator.d.ts +3 -1
- package/dist/translator.js +155 -56
- package/dist/version.js +12 -5
- package/package.json +8 -4
package/dist/commands/text-io.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { wordToString } from '../ast.js';
|
|
2
2
|
import { lookup, parseWords, psStr } from '../registry.js';
|
|
3
3
|
import { argListExpr, exprOfWord, operandExpr } from '../translator.js';
|
|
4
|
+
import { PS_WRITE_FN, fxTermLine } from './text-output.js';
|
|
4
5
|
/* ------------------------------------------------------------------ */
|
|
5
6
|
/* Shared PS snippets (same shape as files.ts / text-filters.ts) */
|
|
6
7
|
/* ------------------------------------------------------------------ */
|
|
@@ -66,35 +67,6 @@ const PS_UNESQ_FN = [
|
|
|
66
67
|
' $sb.ToString()',
|
|
67
68
|
'}',
|
|
68
69
|
].join('\n');
|
|
69
|
-
/**
|
|
70
|
-
* Terminal-aware write. When this block's output goes straight to the console
|
|
71
|
-
* (a lone `& { }` scriptblock — $MyInvocation name is empty — or the last
|
|
72
|
-
* stage of a pipeline):
|
|
73
|
-
* - a string ending in \n is emitted as line items (the console formatter
|
|
74
|
-
* appends the final newline; still correct inside $(...) substitution);
|
|
75
|
-
* - a string without a trailing newline is written with the exact bytes so
|
|
76
|
-
* `echo -n`, `printf 'x'`, `base64 -w0`, `head -c N` stay GNU-exact.
|
|
77
|
-
* Inside a pipeline stage emit one string item instead (line semantics
|
|
78
|
-
* downstream; embedded \n is preserved for consumers that re-split).
|
|
79
|
-
*/
|
|
80
|
-
const PS_WRITE_FN = [
|
|
81
|
-
'function fx-write($s, $term) {',
|
|
82
|
-
" if ($s -eq '') { return }",
|
|
83
|
-
// Inside quoted/assignment $(...) the collector wants one string object
|
|
84
|
-
// so interior newlines survive (PS would otherwise join lines with spaces).
|
|
85
|
-
' if ($script:fx_csub) { $s; return }',
|
|
86
|
-
' if (-not $term) { $s; return }',
|
|
87
|
-
' if (-not $s.EndsWith([string][char]10)) { [Console]::Out.Write($s); return }',
|
|
88
|
-
' $t = $s.Substring(0, $s.Length - 1)',
|
|
89
|
-
' foreach ($fx_l in $t.Split([char]10)) { $fx_l }',
|
|
90
|
-
'}',
|
|
91
|
-
].join('\n');
|
|
92
|
-
/** $fx_term: is this block's output console-terminal? (see PS_WRITE_FN) */
|
|
93
|
-
function fxTermLine(position) {
|
|
94
|
-
return ("$fx_term = (($MyInvocation.MyCommand.Name -eq '') -or " +
|
|
95
|
-
(position === 'last' ? '$true' : '$false') +
|
|
96
|
-
')');
|
|
97
|
-
}
|
|
98
70
|
/** PS boolean literal. */
|
|
99
71
|
function pb(v) {
|
|
100
72
|
return v ? '$true' : '$false';
|
|
@@ -175,12 +147,12 @@ const echo = (args, ctx) => {
|
|
|
175
147
|
else if (t === '-E')
|
|
176
148
|
esc = false;
|
|
177
149
|
else if (/^-[neE]{2,}$/.test(t)) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
150
|
+
for (const flag of t.slice(1)) {
|
|
151
|
+
if (flag === 'n')
|
|
152
|
+
noNewline = true;
|
|
153
|
+
else
|
|
154
|
+
esc = flag === 'e';
|
|
155
|
+
}
|
|
184
156
|
}
|
|
185
157
|
else
|
|
186
158
|
break;
|
|
@@ -331,12 +303,7 @@ const PS_PRINTF_FNS = [
|
|
|
331
303
|
].join('\n');
|
|
332
304
|
const printf = (args, ctx) => {
|
|
333
305
|
// no options; skip a single leading `--` separator
|
|
334
|
-
const ops = [];
|
|
335
|
-
for (const w of args) {
|
|
336
|
-
if (ops.length === 0 && wordToString(w) === '--')
|
|
337
|
-
continue;
|
|
338
|
-
ops.push(w);
|
|
339
|
-
}
|
|
306
|
+
const ops = args.length > 0 && wordToString(args[0]) === '--' ? args.slice(1) : args;
|
|
340
307
|
if (ops.length === 0) {
|
|
341
308
|
return psErrExpr(psStr('printf: usage: printf format [arguments]'), '2');
|
|
342
309
|
}
|
|
@@ -346,6 +313,9 @@ const printf = (args, ctx) => {
|
|
|
346
313
|
fxTermLine(ctx.position),
|
|
347
314
|
'$fx_fmt = ' + exprOfWord(ops[0]),
|
|
348
315
|
'$fx_av = ' + psArray(ops.slice(1), exprOfWord),
|
|
316
|
+
// A substitution can fail while producing an argument. printf's status
|
|
317
|
+
// belongs to printf itself; later formatting/write exceptions still fail.
|
|
318
|
+
'$script:fx_exit = 0',
|
|
349
319
|
'fx-write (fx-printf $fx_fmt $fx_av) $fx_term',
|
|
350
320
|
].join('\n');
|
|
351
321
|
};
|
|
@@ -400,9 +370,27 @@ const head = (args, ctx) => {
|
|
|
400
370
|
// option scan (legacy `head -N` supported; -n/-c take values, incl. negative)
|
|
401
371
|
let nLines = null;
|
|
402
372
|
let nBytes = null;
|
|
373
|
+
let dropLast = false;
|
|
403
374
|
const operandWords = [];
|
|
404
375
|
let quiet = false;
|
|
405
376
|
let verbose = false;
|
|
377
|
+
const setCount = (kind, raw) => {
|
|
378
|
+
const literal = countMagnitudeLiteral(raw);
|
|
379
|
+
if (literal === null) {
|
|
380
|
+
return psErrExpr(psStr("head: invalid number of " + kind + ": '" + raw + "'"));
|
|
381
|
+
}
|
|
382
|
+
dropLast = raw.startsWith('-');
|
|
383
|
+
const signedLiteral = dropLast && literal !== '0' ? '-' + literal : literal;
|
|
384
|
+
if (kind === 'bytes') {
|
|
385
|
+
nBytes = signedLiteral;
|
|
386
|
+
nLines = null;
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
nLines = signedLiteral;
|
|
390
|
+
nBytes = null;
|
|
391
|
+
}
|
|
392
|
+
return null;
|
|
393
|
+
};
|
|
406
394
|
{
|
|
407
395
|
let i = 0;
|
|
408
396
|
let onlyOps = false;
|
|
@@ -434,19 +422,20 @@ const head = (args, ctx) => {
|
|
|
434
422
|
else {
|
|
435
423
|
i++;
|
|
436
424
|
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
nLines = val;
|
|
425
|
+
const err = setCount(name === '--bytes' ? 'bytes' : 'lines', val);
|
|
426
|
+
if (err !== null)
|
|
427
|
+
return err;
|
|
441
428
|
continue;
|
|
442
429
|
}
|
|
443
430
|
if (name === '--quiet' || name === '--silent') {
|
|
444
431
|
quiet = true;
|
|
432
|
+
verbose = false;
|
|
445
433
|
i++;
|
|
446
434
|
continue;
|
|
447
435
|
}
|
|
448
436
|
if (name === '--verbose') {
|
|
449
437
|
verbose = true;
|
|
438
|
+
quiet = false;
|
|
450
439
|
i++;
|
|
451
440
|
continue;
|
|
452
441
|
}
|
|
@@ -454,39 +443,42 @@ const head = (args, ctx) => {
|
|
|
454
443
|
continue;
|
|
455
444
|
}
|
|
456
445
|
let m;
|
|
457
|
-
if (t === '-n' || t === '-c') {
|
|
458
|
-
const val = i + 1 < args.length ? wordToString(args[i + 1]) : null;
|
|
459
|
-
if (val === null) {
|
|
460
|
-
return psErrExpr(psStr('head: option requires an argument -- ' + t.slice(1)));
|
|
461
|
-
}
|
|
462
|
-
if (t === '-c')
|
|
463
|
-
nBytes = val;
|
|
464
|
-
else
|
|
465
|
-
nLines = val;
|
|
466
|
-
i += 2;
|
|
467
|
-
continue;
|
|
468
|
-
}
|
|
469
|
-
if ((m = t.match(/^-[nc](.+)$/)) !== null) {
|
|
470
|
-
if (t[1] === 'c')
|
|
471
|
-
nBytes = m[1];
|
|
472
|
-
else
|
|
473
|
-
nLines = m[1];
|
|
474
|
-
i++;
|
|
475
|
-
continue;
|
|
476
|
-
}
|
|
477
446
|
if ((m = t.match(/^-(\d+)$/)) !== null) {
|
|
478
|
-
|
|
447
|
+
const err = setCount('lines', m[1]);
|
|
448
|
+
if (err !== null)
|
|
449
|
+
return err;
|
|
479
450
|
i++;
|
|
480
451
|
continue;
|
|
481
452
|
}
|
|
482
453
|
if (t.startsWith('-') && t.length > 1) {
|
|
483
|
-
|
|
484
|
-
|
|
454
|
+
const body = t.slice(1);
|
|
455
|
+
let usedNext = false;
|
|
456
|
+
for (let c = 0; c < body.length; c++) {
|
|
457
|
+
const ch = body[c];
|
|
458
|
+
if (ch === 'q') {
|
|
485
459
|
quiet = true;
|
|
486
|
-
|
|
460
|
+
verbose = false;
|
|
461
|
+
}
|
|
462
|
+
else if (ch === 'v') {
|
|
487
463
|
verbose = true;
|
|
464
|
+
quiet = false;
|
|
465
|
+
}
|
|
466
|
+
else if (ch === 'n' || ch === 'c') {
|
|
467
|
+
let val = body.slice(c + 1);
|
|
468
|
+
if (val === '') {
|
|
469
|
+
if (i + 1 >= args.length) {
|
|
470
|
+
return psErrExpr(psStr('head: option requires an argument -- ' + ch));
|
|
471
|
+
}
|
|
472
|
+
val = wordToString(args[i + 1]);
|
|
473
|
+
usedNext = true;
|
|
474
|
+
}
|
|
475
|
+
const err = setCount(ch === 'c' ? 'bytes' : 'lines', val);
|
|
476
|
+
if (err !== null)
|
|
477
|
+
return err;
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
488
480
|
}
|
|
489
|
-
i
|
|
481
|
+
i += usedNext ? 2 : 1;
|
|
490
482
|
continue;
|
|
491
483
|
}
|
|
492
484
|
operandWords.push(args[i]);
|
|
@@ -495,9 +487,6 @@ const head = (args, ctx) => {
|
|
|
495
487
|
}
|
|
496
488
|
const bytesMode = nBytes !== null;
|
|
497
489
|
const countLit = bytesMode ? nBytes : nLines !== null ? nLines : '10';
|
|
498
|
-
if (!/^[+-]?\d+$/.test(countLit)) {
|
|
499
|
-
return psErrExpr(psStr("head: invalid number of " + (bytesMode ? 'bytes' : 'lines') + ": '" + countLit + "'"));
|
|
500
|
-
}
|
|
501
490
|
const lines = [
|
|
502
491
|
PS_GLOB_FN,
|
|
503
492
|
PS_READTEXT_FN,
|
|
@@ -509,22 +498,22 @@ const head = (args, ctx) => {
|
|
|
509
498
|
lines.push(STDIN_ITEMS, PS_STDINRAW_FN);
|
|
510
499
|
else
|
|
511
500
|
lines.push(STDIN_INLINES);
|
|
512
|
-
lines.push(...psCollectFiles(operandWords, (g) => qErr('head', g, 'No such file or directory'), (g) => qErr('head', g, 'Is a directory', 'error reading ')), '$fx_count = [int](' + countLit + ')', '$fx_hdr = ((($fx_srcs.Count -gt 1) -and ' + pb(!quiet) + ') -or ' + pb(verbose) + ')', '$fx_first = $true');
|
|
501
|
+
lines.push(...psCollectFiles(operandWords, (g) => qErr('head', g, 'No such file or directory'), (g) => qErr('head', g, 'Is a directory', 'error reading ')), '$fx_count = [int](' + countLit + ')', '$fx_dropLast = ' + pb(dropLast), '$fx_hdr = ((($fx_srcs.Count -gt 1) -and ' + pb(!quiet) + ') -or ' + pb(verbose) + ')', '$fx_first = $true');
|
|
513
502
|
if (bytesMode) {
|
|
514
503
|
lines.push('$fx_out = New-Object System.Text.StringBuilder', 'for ($fx_k = 0; $fx_k -lt $fx_srcs.Count; $fx_k++) {', ' $fx_g = $fx_srcs[$fx_k]', " if ($fx_g -eq '-') { $fx_txt = (fx-stdinraw $fx_items); $fx_disp = 'standard input' }", ' else { $fx_txt = fx-read $fx_g; $fx_disp = $fx_names[$fx_k] }', ' if ($fx_hdr) {', ' if (-not $fx_first) { [void]$fx_out.Append([string][char]10) }', " [void]$fx_out.Append('==> ' + $fx_disp + ' <==' + [string][char]10)", ' }', ' $fx_first = $false',
|
|
515
|
-
// GNU: --bytes=-N prints all but last N
|
|
516
|
-
' if ($fx_count -lt 0) { $fx_len = [math]::Max(0, $fx_txt.Length + $fx_count) }', ' else { $fx_len = [math]::Min($fx_count, $fx_txt.Length) }', ' if ($fx_len -gt 0) { [void]$fx_out.Append($fx_txt.Substring(0, $fx_len)) }', '}', 'fx-write $fx_out.ToString() $fx_term');
|
|
504
|
+
// GNU: --bytes=-N prints all but last N, including the whole input for -0.
|
|
505
|
+
' if ($fx_count -lt 0) { $fx_len = [math]::Max(0, $fx_txt.Length + $fx_count) }', ' elseif ($fx_dropLast) { $fx_len = $fx_txt.Length }', ' else { $fx_len = [math]::Min($fx_count, $fx_txt.Length) }', ' if ($fx_len -gt 0) { [void]$fx_out.Append($fx_txt.Substring(0, $fx_len)) }', '}', 'fx-write $fx_out.ToString() $fx_term');
|
|
517
506
|
}
|
|
518
507
|
else {
|
|
519
|
-
lines.push('for ($fx_k = 0; $fx_k -lt $fx_srcs.Count; $fx_k++) {', ' $fx_g = $fx_srcs[$fx_k]', " if ($fx_g -eq '-') { $fx_ls = @($fx_in); $fx_disp = 'standard input' }", ' else { $fx_ls = @(fx-splitlines (fx-read $fx_g)); $fx_disp = $fx_names[$fx_k] }', ' if ($fx_hdr) {', " if (-not $fx_first) { '' }", " '==> ' + $fx_disp + ' <=='", ' }', ' $fx_first = $false', ' $fx_lim = $fx_ls.Count', ' if ($fx_count -lt 0) { $fx_lim = $fx_ls.Count + $fx_count }', ' elseif ($fx_lim -gt $fx_count) { $fx_lim = $fx_count }', ' if ($fx_lim -lt 0) { $fx_lim = 0 }', ' for ($fx_i = 0; $fx_i -lt $fx_lim; $fx_i++) { $fx_ls[$fx_i] }', '}');
|
|
508
|
+
lines.push('for ($fx_k = 0; $fx_k -lt $fx_srcs.Count; $fx_k++) {', ' $fx_g = $fx_srcs[$fx_k]', " if ($fx_g -eq '-') { $fx_ls = @($fx_in); $fx_disp = 'standard input' }", ' else { $fx_ls = @(fx-splitlines (fx-read $fx_g)); $fx_disp = $fx_names[$fx_k] }', ' if ($fx_hdr) {', " if (-not $fx_first) { '' }", " '==> ' + $fx_disp + ' <=='", ' }', ' $fx_first = $false', ' $fx_lim = $fx_ls.Count', ' if ($fx_count -lt 0) { $fx_lim = $fx_ls.Count + $fx_count }', ' elseif ($fx_dropLast) { $fx_lim = $fx_ls.Count }', ' elseif ($fx_lim -gt $fx_count) { $fx_lim = $fx_count }', ' if ($fx_lim -lt 0) { $fx_lim = 0 }', ' for ($fx_i = 0; $fx_i -lt $fx_lim; $fx_i++) { $fx_ls[$fx_i] }', '}');
|
|
520
509
|
}
|
|
521
510
|
return lines.join('\n');
|
|
522
511
|
};
|
|
523
512
|
/* ------------------------------------------------------------------ */
|
|
524
513
|
/* tail */
|
|
525
514
|
/* ------------------------------------------------------------------ */
|
|
526
|
-
/** A count embedded in generated PS must be a canonical Int32 literal. */
|
|
527
|
-
function
|
|
515
|
+
/** A count magnitude embedded in generated PS must be a canonical Int32 literal. */
|
|
516
|
+
function countMagnitudeLiteral(raw) {
|
|
528
517
|
if (!/^[+-]?\d+$/.test(raw))
|
|
529
518
|
return null;
|
|
530
519
|
try {
|
|
@@ -552,7 +541,7 @@ const tail = (args, ctx) => {
|
|
|
552
541
|
let quiet = false;
|
|
553
542
|
let verbose = false;
|
|
554
543
|
const setCount = (kind, raw) => {
|
|
555
|
-
const literal =
|
|
544
|
+
const literal = countMagnitudeLiteral(raw);
|
|
556
545
|
if (literal === null) {
|
|
557
546
|
return psErrExpr(psStr("tail: invalid number of " + kind + ": '" + raw + "'"));
|
|
558
547
|
}
|
|
@@ -614,11 +603,13 @@ const tail = (args, ctx) => {
|
|
|
614
603
|
}
|
|
615
604
|
if (name === '--quiet' || name === '--silent') {
|
|
616
605
|
quiet = true;
|
|
606
|
+
verbose = false;
|
|
617
607
|
i++;
|
|
618
608
|
continue;
|
|
619
609
|
}
|
|
620
610
|
if (name === '--verbose') {
|
|
621
611
|
verbose = true;
|
|
612
|
+
quiet = false;
|
|
622
613
|
i++;
|
|
623
614
|
continue;
|
|
624
615
|
}
|
|
@@ -645,10 +636,14 @@ const tail = (args, ctx) => {
|
|
|
645
636
|
let usedNext = false;
|
|
646
637
|
for (let c = 0; c < body.length; c++) {
|
|
647
638
|
const ch = body[c];
|
|
648
|
-
if (ch === 'q')
|
|
639
|
+
if (ch === 'q') {
|
|
649
640
|
quiet = true;
|
|
650
|
-
|
|
641
|
+
verbose = false;
|
|
642
|
+
}
|
|
643
|
+
else if (ch === 'v') {
|
|
651
644
|
verbose = true;
|
|
645
|
+
quiet = false;
|
|
646
|
+
}
|
|
652
647
|
else if (ch === 'n' || ch === 'c') {
|
|
653
648
|
let val = body.slice(c + 1);
|
|
654
649
|
if (val === '') {
|
|
@@ -712,12 +707,23 @@ const wc = (args) => {
|
|
|
712
707
|
// GNU: stdin (implicit or `-`) prints the classic 7-wide columns; real
|
|
713
708
|
// file operands use dynamic widths; `-` displays the name '-'.
|
|
714
709
|
const fromFiles = operandWords.some((w) => wordToString(w) !== '-');
|
|
710
|
+
const charCount = wantM ? '(fx-charcount $fx_txt)' : '$fx_txt.Length';
|
|
715
711
|
return [
|
|
716
712
|
PS_GLOB_FN,
|
|
717
713
|
PS_READTEXT_FN,
|
|
718
714
|
PS_SPLITLINES_FN,
|
|
719
715
|
STDIN_ITEMS,
|
|
720
716
|
PS_STDINRAW_FN,
|
|
717
|
+
...(wantM ? [
|
|
718
|
+
'function fx-charcount($t) {',
|
|
719
|
+
' $n = 0',
|
|
720
|
+
' for ($i = 0; $i -lt $t.Length; $i++) {',
|
|
721
|
+
' $n++',
|
|
722
|
+
' if ([char]::IsHighSurrogate($t[$i]) -and ($i + 1) -lt $t.Length -and [char]::IsLowSurrogate($t[$i + 1])) { $i++ }',
|
|
723
|
+
' }',
|
|
724
|
+
' return $n',
|
|
725
|
+
'}',
|
|
726
|
+
] : []),
|
|
721
727
|
...psCollectFiles(operandWords, (g) => sErr('wc', g, 'No such file or directory'), (g) => sErr('wc', g, 'Is a directory')),
|
|
722
728
|
'function fx-wdcount($ls) {',
|
|
723
729
|
' $t = 0',
|
|
@@ -740,14 +746,14 @@ const wc = (args) => {
|
|
|
740
746
|
" if ($fx_txt -ne '' -and -not $fx_txt.EndsWith([string][char]10)) { $fx_nl = $fx_nl + 1 }",
|
|
741
747
|
' $fx_ww = fx-wdcount @(fx-splitlines $fx_txt)',
|
|
742
748
|
' $fx_bc = [System.Text.Encoding]::UTF8.GetByteCount($fx_txt)',
|
|
743
|
-
|
|
749
|
+
' $fx_rows += ,@($fx_nl, $fx_ww, $fx_bc, ' + charCount + ', $fx_names[$fx_k])',
|
|
744
750
|
' } else {',
|
|
745
751
|
' $fx_txt = fx-read $fx_g',
|
|
746
752
|
' $fx_txt2 = $fx_txt.Replace([string][char]13 + [string][char]10, [string][char]10)',
|
|
747
753
|
" $fx_nl = [regex]::Matches($fx_txt2, '\\n').Count",
|
|
748
754
|
' $fx_bytes = [IO.File]::ReadAllBytes($fx_g).Length',
|
|
749
755
|
' $fx_ww = fx-wdcount @(fx-splitlines $fx_txt)',
|
|
750
|
-
' $fx_rows += ,@($fx_nl, $fx_ww, $fx_bytes,
|
|
756
|
+
' $fx_rows += ,@($fx_nl, $fx_ww, $fx_bytes, ' + charCount + ', $fx_names[$fx_k])',
|
|
751
757
|
' }',
|
|
752
758
|
'}',
|
|
753
759
|
'$fx_tot = @(0, 0, 0, 0)',
|
|
@@ -853,12 +859,11 @@ const tac = (args) => {
|
|
|
853
859
|
PS_SPLITLINES_FN,
|
|
854
860
|
STDIN_INLINES,
|
|
855
861
|
...psCollectFiles(operandWords, (g) => qErr('tac', g, 'No such file or directory', 'failed to open '), (g) => sErr('tac', g, 'read error: Is a directory')),
|
|
856
|
-
'$fx_all = @()',
|
|
857
862
|
'foreach ($fx_g in $fx_srcs) {',
|
|
858
|
-
" if ($fx_g -eq '-') { $
|
|
859
|
-
' else { $
|
|
863
|
+
" if ($fx_g -eq '-') { $fx_ls = @($fx_in) }",
|
|
864
|
+
' else { $fx_ls = @(fx-splitlines (fx-read $fx_g)) }',
|
|
865
|
+
' for ($fx_i = $fx_ls.Count - 1; $fx_i -ge 0; $fx_i--) { $fx_ls[$fx_i] }',
|
|
860
866
|
'}',
|
|
861
|
-
'for ($fx_i = $fx_all.Count - 1; $fx_i -ge 0; $fx_i--) { $fx_all[$fx_i] }',
|
|
862
867
|
].join('\n');
|
|
863
868
|
};
|
|
864
869
|
/* ------------------------------------------------------------------ */
|
|
@@ -960,12 +965,21 @@ const sha256sum = hashSum('sha256sum', '[System.Security.Cryptography.SHA256]::C
|
|
|
960
965
|
/* base64 */
|
|
961
966
|
/* ------------------------------------------------------------------ */
|
|
962
967
|
const base64 = (args, ctx) => {
|
|
963
|
-
const { flags, longs,
|
|
968
|
+
const { flags, longs, valueEntries, missingValue, operandWords } = parseWords(args, ['w'], ['--wrap']);
|
|
964
969
|
const decode = flags.has('d') || longs.has('--decode');
|
|
970
|
+
if (missingValue.length > 0) {
|
|
971
|
+
return psErrExpr(psStr('base64: option requires an argument -- ' + missingValue[0]));
|
|
972
|
+
}
|
|
965
973
|
let wrap = 76;
|
|
966
|
-
const
|
|
967
|
-
|
|
968
|
-
|
|
974
|
+
for (const { name, value } of valueEntries) {
|
|
975
|
+
if (name !== '-w' && name !== '--wrap')
|
|
976
|
+
continue;
|
|
977
|
+
const literal = /^\d+$/.test(value) ? countMagnitudeLiteral(value) : null;
|
|
978
|
+
if (literal === null) {
|
|
979
|
+
return psErrExpr(psStr("base64: invalid wrap size: '" + value + "'"));
|
|
980
|
+
}
|
|
981
|
+
wrap = Number(literal);
|
|
982
|
+
}
|
|
969
983
|
const pre = [
|
|
970
984
|
PS_GLOB_FN,
|
|
971
985
|
PS_READTEXT_FN,
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal-aware write. When this block's output goes straight to the console
|
|
3
|
+
* (a lone `& { }` scriptblock — $MyInvocation name is empty — or the last
|
|
4
|
+
* stage of a pipeline):
|
|
5
|
+
* - a string ending in \n is emitted as line items (the console formatter
|
|
6
|
+
* appends the final newline; still correct inside $(...) substitution);
|
|
7
|
+
* - a string without a trailing newline is written with the exact bytes so
|
|
8
|
+
* `echo -n`, `printf 'x'`, `base64 -w0`, `head -c N` stay GNU-exact.
|
|
9
|
+
* Inside a pipeline stage emit one string item instead (line semantics
|
|
10
|
+
* downstream; embedded \n is preserved for consumers that re-split).
|
|
11
|
+
*/
|
|
12
|
+
export declare const PS_WRITE_FN: string;
|
|
13
|
+
/** $fx_term: is this block's output console-terminal? (see PS_WRITE_FN) */
|
|
14
|
+
export declare function fxTermLine(position: 'first' | 'middle' | 'last'): string;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal-aware write. When this block's output goes straight to the console
|
|
3
|
+
* (a lone `& { }` scriptblock — $MyInvocation name is empty — or the last
|
|
4
|
+
* stage of a pipeline):
|
|
5
|
+
* - a string ending in \n is emitted as line items (the console formatter
|
|
6
|
+
* appends the final newline; still correct inside $(...) substitution);
|
|
7
|
+
* - a string without a trailing newline is written with the exact bytes so
|
|
8
|
+
* `echo -n`, `printf 'x'`, `base64 -w0`, `head -c N` stay GNU-exact.
|
|
9
|
+
* Inside a pipeline stage emit one string item instead (line semantics
|
|
10
|
+
* downstream; embedded \n is preserved for consumers that re-split).
|
|
11
|
+
*/
|
|
12
|
+
export const PS_WRITE_FN = [
|
|
13
|
+
'function fx-write($s, $term) {',
|
|
14
|
+
" if ($s -eq '') { return }",
|
|
15
|
+
// Inside quoted/assignment $(...) the collector wants one string object
|
|
16
|
+
// so interior newlines survive (PS would otherwise join lines with spaces).
|
|
17
|
+
' if ($script:fx_csub) { $s; return }',
|
|
18
|
+
' if (-not $term) { $s; return }',
|
|
19
|
+
' if (-not $s.EndsWith([string][char]10)) { [Console]::Out.Write($s); return }',
|
|
20
|
+
' $t = $s.Substring(0, $s.Length - 1)',
|
|
21
|
+
' foreach ($fx_l in $t.Split([char]10)) { $fx_l }',
|
|
22
|
+
'}',
|
|
23
|
+
].join('\n');
|
|
24
|
+
/** $fx_term: is this block's output console-terminal? (see PS_WRITE_FN) */
|
|
25
|
+
export function fxTermLine(position) {
|
|
26
|
+
return ("$fx_term = (($MyInvocation.MyCommand.Name -eq '') -or " +
|
|
27
|
+
(position === 'last' ? '$true' : '$false') +
|
|
28
|
+
')');
|
|
29
|
+
}
|
package/dist/doctor.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { nodeEngineRange, supportsNode } from './runtime-support.js';
|
|
2
3
|
import { homedir } from 'node:os';
|
|
3
4
|
import { join } from 'node:path';
|
|
4
5
|
import { resolveQwenLaunchTuple, sameQwenLaunchTuple } from './qwen-launch.js';
|
|
6
|
+
import { launcherStatus } from './facade-launch.js';
|
|
5
7
|
const VALUE_INDENT = ' ';
|
|
6
8
|
export async function collectDoctorReport(opts = {}) {
|
|
7
9
|
const home = opts.home ?? homedir();
|
|
@@ -15,6 +17,7 @@ export async function collectDoctorReport(opts = {}) {
|
|
|
15
17
|
lines.push(field('codex', detectCodex(home, env)));
|
|
16
18
|
lines.push(field('opencode', detectOpenCode(home, env)));
|
|
17
19
|
lines.push(field('qwen', detectQwen(home)));
|
|
20
|
+
lines.push(field('facade', detectFacade(env)));
|
|
18
21
|
lines.push('');
|
|
19
22
|
const mcp = await mcpLines(nodeVersion, opts.loadMcp);
|
|
20
23
|
lines.push(...mcp.lines);
|
|
@@ -23,6 +26,13 @@ export async function collectDoctorReport(opts = {}) {
|
|
|
23
26
|
function field(label, value) {
|
|
24
27
|
return `${label.padEnd(10)} : ${value}`;
|
|
25
28
|
}
|
|
29
|
+
function detectFacade(env) {
|
|
30
|
+
const status = launcherStatus(env);
|
|
31
|
+
const pointed = env['CLAUDE_CODE_GIT_BASH_PATH'] === status.exePath ? ', CLAUDE_CODE_GIT_BASH_PATH points at it' : '';
|
|
32
|
+
if (!status.installed)
|
|
33
|
+
return 'not installed (experimental: fauxnix install --claude-shell)';
|
|
34
|
+
return `${status.upToDate ? 'up to date' : 'stale'} at ${status.exePath}${pointed}`;
|
|
35
|
+
}
|
|
26
36
|
function encodingLines(env) {
|
|
27
37
|
const raw = env.FAUXNIX_NATIVE_ENCODING;
|
|
28
38
|
const current = raw === undefined || raw === ''
|
|
@@ -121,14 +131,15 @@ function detectCodex(home, env) {
|
|
|
121
131
|
return `found ${path}, fauxnix MCP not listed — run: codex mcp add fauxnix -- fauxnix mcp`;
|
|
122
132
|
}
|
|
123
133
|
export function hasCodexFauxnix(text) {
|
|
124
|
-
if (/^\s*\[mcp_servers\.(?:fauxnix|"fauxnix"|'fauxnix')\]/im.test(text))
|
|
125
|
-
return true;
|
|
126
134
|
const tables = text.split(/^\s*\[/m);
|
|
127
135
|
for (const table of tables) {
|
|
128
|
-
if (!/^mcp_servers\./i.test(table))
|
|
129
|
-
continue;
|
|
130
136
|
const header = (table.split(/[\]\r\n]/, 1)[0] ?? '').trim();
|
|
131
|
-
|
|
137
|
+
// TOML allows quoted key components and whitespace around dots and brackets.
|
|
138
|
+
// Recognize those equivalent spellings before install appends a duplicate table.
|
|
139
|
+
const server = /^(?:mcp_servers|"mcp_servers"|'mcp_servers')\s*\.\s*(.+)$/i.exec(header);
|
|
140
|
+
if (!server)
|
|
141
|
+
continue;
|
|
142
|
+
if (/^(?:fauxnix|"fauxnix"|'fauxnix')$/i.test(server[1]))
|
|
132
143
|
return true;
|
|
133
144
|
const cmd = /^\s*command\s*=\s*(?:"([^"]*)"|'([^']*)')/im.exec(table);
|
|
134
145
|
const command = cmd?.[1] ?? cmd?.[2];
|
|
@@ -251,8 +262,7 @@ function isFauxnixExecutable(s) {
|
|
|
251
262
|
return /^fauxnix(-cli)?(\.cmd|\.exe)?$/i.test(base);
|
|
252
263
|
}
|
|
253
264
|
async function mcpLines(nodeVersion, loadMcp) {
|
|
254
|
-
const
|
|
255
|
-
const nodeOk = major >= 18;
|
|
265
|
+
const nodeOk = supportsNode(nodeVersion);
|
|
256
266
|
let moduleOk = false;
|
|
257
267
|
let moduleDetail = '';
|
|
258
268
|
try {
|
|
@@ -266,7 +276,7 @@ async function mcpLines(nodeVersion, loadMcp) {
|
|
|
266
276
|
moduleDetail = e instanceof Error ? e.message : String(e);
|
|
267
277
|
}
|
|
268
278
|
const lines = [
|
|
269
|
-
field('node', `${nodeVersion.startsWith('v') ? nodeVersion : 'v' + nodeVersion}${nodeOk ?
|
|
279
|
+
field('node', `${nodeVersion.startsWith('v') ? nodeVersion : 'v' + nodeVersion}${nodeOk ? ` (${nodeEngineRange} required)` : ` FAILED (requires ${nodeEngineRange})`}`),
|
|
270
280
|
field('mcp', moduleOk ? 'module loads' : `FAILED to load${moduleDetail ? ': ' + moduleDetail : ''}`),
|
|
271
281
|
VALUE_INDENT + 'start with: fauxnix mcp',
|
|
272
282
|
];
|
|
@@ -275,10 +285,6 @@ async function mcpLines(nodeVersion, loadMcp) {
|
|
|
275
285
|
async function defaultLoadMcp() {
|
|
276
286
|
return import('./mcp.js');
|
|
277
287
|
}
|
|
278
|
-
function nodeMajor(version) {
|
|
279
|
-
const m = /^v?(\d+)/.exec(version);
|
|
280
|
-
return m ? Number(m[1]) : 0;
|
|
281
|
-
}
|
|
282
288
|
function stripBom(text) {
|
|
283
289
|
return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
|
|
284
290
|
}
|
package/dist/encoding.d.ts
CHANGED
|
@@ -25,3 +25,8 @@ export declare function decodeOutput(buf: Buffer, prefer?: NativeEncodingPref):
|
|
|
25
25
|
export declare function normalizeHostNewlines(s: string): string;
|
|
26
26
|
/** Encode a PowerShell script for -EncodedCommand (UTF-16LE base64). */
|
|
27
27
|
export declare function encodeCommand(script: string): string;
|
|
28
|
+
/** Keep the longest whole-codepoint prefix within a caller's UTF-8 byte budget. */
|
|
29
|
+
export declare function clipUtf8(text: string, limit: number): {
|
|
30
|
+
text: string;
|
|
31
|
+
truncated: boolean;
|
|
32
|
+
};
|
package/dist/encoding.js
CHANGED
|
@@ -56,3 +56,43 @@ export function normalizeHostNewlines(s) {
|
|
|
56
56
|
export function encodeCommand(script) {
|
|
57
57
|
return Buffer.from(script, 'utf16le').toString('base64');
|
|
58
58
|
}
|
|
59
|
+
/** Keep the longest whole-codepoint prefix within a caller's UTF-8 byte budget. */
|
|
60
|
+
export function clipUtf8(text, limit) {
|
|
61
|
+
const total = Buffer.byteLength(text, 'utf8');
|
|
62
|
+
if (total <= limit)
|
|
63
|
+
return { text, truncated: false };
|
|
64
|
+
// Captures normally exceed the caller budget by at most one codepoint.
|
|
65
|
+
// Trim that small suffix rather than walking megabytes of retained output.
|
|
66
|
+
// For a small budget, use the forward path instead; neither path allocates
|
|
67
|
+
// another full UTF-8 buffer or splits a surrogate pair.
|
|
68
|
+
if (total - limit <= limit) {
|
|
69
|
+
let used = total;
|
|
70
|
+
let end = text.length;
|
|
71
|
+
while (used > limit && end > 0) {
|
|
72
|
+
const last = text.charCodeAt(end - 1);
|
|
73
|
+
const previous = end > 1 ? text.charCodeAt(end - 2) : 0;
|
|
74
|
+
if (last >= 0xdc00 && last <= 0xdfff && previous >= 0xd800 && previous <= 0xdbff) {
|
|
75
|
+
used -= 4;
|
|
76
|
+
end -= 2;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
// Lone surrogates encode as a three-byte replacement character,
|
|
80
|
+
// but retain their original JS spelling just as the forward path does.
|
|
81
|
+
used -= last < 0x80 ? 1 : last < 0x800 ? 2 : 3;
|
|
82
|
+
end--;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return { text: text.slice(0, end), truncated: true };
|
|
86
|
+
}
|
|
87
|
+
let used = 0;
|
|
88
|
+
let end = 0;
|
|
89
|
+
for (const codepoint of text) {
|
|
90
|
+
const value = codepoint.codePointAt(0);
|
|
91
|
+
const size = value < 0x80 ? 1 : value < 0x800 ? 2 : value < 0x10000 ? 3 : 4;
|
|
92
|
+
if (used + size > limit)
|
|
93
|
+
break;
|
|
94
|
+
used += size;
|
|
95
|
+
end += codepoint.length;
|
|
96
|
+
}
|
|
97
|
+
return { text: text.slice(0, end), truncated: true };
|
|
98
|
+
}
|
package/dist/executor.js
CHANGED
|
@@ -3,7 +3,7 @@ import { promises as fs, readFileSync, existsSync, openSync, closeSync, readSync
|
|
|
3
3
|
import os from 'node:os';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { normalizeLiteralPath, wrapScript } from './translator.js';
|
|
6
|
-
import { decodeOutput, resolveNativePref } from './encoding.js';
|
|
6
|
+
import { clipUtf8, decodeOutput, resolveNativePref } from './encoding.js';
|
|
7
7
|
import { normalizeStderr } from './errors.js';
|
|
8
8
|
import { DEFAULT_STDERR_LIMIT, DEFAULT_STDOUT_LIMIT, PowerShellHost, } from './ps-host.js';
|
|
9
9
|
import { resolvePowerShell } from './powershell.js';
|
|
@@ -727,20 +727,6 @@ async function runPlans(plans, session, opts, afterSegment, ensureHost) {
|
|
|
727
727
|
spawnError,
|
|
728
728
|
};
|
|
729
729
|
}
|
|
730
|
-
function clipUtf8(text, limit) {
|
|
731
|
-
if (Buffer.byteLength(text, 'utf8') <= limit)
|
|
732
|
-
return { text, truncated: false };
|
|
733
|
-
let used = 0;
|
|
734
|
-
let end = 0;
|
|
735
|
-
for (const codepoint of text) {
|
|
736
|
-
const size = Buffer.byteLength(codepoint, 'utf8');
|
|
737
|
-
if (used + size > limit)
|
|
738
|
-
break;
|
|
739
|
-
used += size;
|
|
740
|
-
end += codepoint.length;
|
|
741
|
-
}
|
|
742
|
-
return { text: text.slice(0, end), truncated: true };
|
|
743
|
-
}
|
|
744
730
|
/** Copy a host spool into an already-open redirect fd with bounded memory. */
|
|
745
731
|
function writeSpoolToPrepFd(fds, file, target) {
|
|
746
732
|
const outFd = fds.get(target);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Experimental bash.exe launcher (RFC: docs/rfc-bash-facade.md).
|
|
3
|
+
*
|
|
4
|
+
* Harnesses point CLAUDE_CODE_GIT_BASH_PATH at an executable whose basename
|
|
5
|
+
* is exactly bash.exe, so the launcher is a copy of the active node.exe with
|
|
6
|
+
* the prebuilt facade bundle (dist/facade-sea.cjs) injected via Node SEA +
|
|
7
|
+
* postject. Roughly 85 MB per install — the documented experimental-phase
|
|
8
|
+
* trade; the GA plan is a small prebuilt stub.
|
|
9
|
+
*/
|
|
10
|
+
export interface LauncherStatus {
|
|
11
|
+
/** Directory that holds (or would hold) bash.exe. */
|
|
12
|
+
dir: string;
|
|
13
|
+
exePath: string;
|
|
14
|
+
installed: boolean;
|
|
15
|
+
upToDate: boolean;
|
|
16
|
+
packageVersion: string;
|
|
17
|
+
nodeVersion: string;
|
|
18
|
+
}
|
|
19
|
+
export declare function launcherDir(env?: NodeJS.ProcessEnv): string;
|
|
20
|
+
export declare function launcherStatus(env?: NodeJS.ProcessEnv): LauncherStatus;
|
|
21
|
+
/** Build (or rebuild when stale) the launcher; returns the bash.exe path. */
|
|
22
|
+
export declare function buildLauncher(env?: NodeJS.ProcessEnv): Promise<string>;
|
|
23
|
+
/** User-level CLAUDE_CODE_GIT_BASH_PATH management, with prior-value restore. */
|
|
24
|
+
export declare function setUserEnv(name: string, value: string | null): Promise<void>;
|
|
25
|
+
export declare function getUserEnv(name: string): Promise<string | null>;
|
|
26
|
+
/** `fauxnix install --claude-shell` — build the launcher and point Claude Code at it. */
|
|
27
|
+
export declare function installClaudeShell(env?: NodeJS.ProcessEnv): Promise<string[]>;
|
|
28
|
+
/** `fauxnix install --claude-shell-off` — restore the previous value and remove the launcher. */
|
|
29
|
+
export declare function uninstallClaudeShell(env?: NodeJS.ProcessEnv): Promise<string[]>;
|