core utils updates
This commit is contained in:
+39
-3
@@ -60,12 +60,42 @@ function barePosixBlocks(size) {
|
||||
return Math.ceil(Number(size) / 512) || 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Raw stdout for NUL/binary when **`process.stdout.write`** is missing.
|
||||
* If **`ctx.bareOsBinWrite(Uint8Array|string)`** is set (tests / host), use it.
|
||||
* @param {Record<string, unknown>} ctx
|
||||
* @param {string | Uint8Array} chunk
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function bareOsEmitRaw(ctx, chunk) {
|
||||
if (typeof ctx.bareOsBinWrite === 'function') {
|
||||
const b4 = ctx.b4a
|
||||
const u8 =
|
||||
typeof chunk === 'string'
|
||||
? b4 && typeof b4.from === 'function'
|
||||
? b4.from(chunk)
|
||||
: new TextEncoder().encode(chunk)
|
||||
: chunk
|
||||
ctx.bareOsBinWrite(u8 instanceof Uint8Array ? u8 : new Uint8Array(u8))
|
||||
return true
|
||||
}
|
||||
const w = globalThis.process?.stdout?.write
|
||||
if (typeof w === 'function') {
|
||||
w.call(globalThis.process.stdout, chunk)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
async function run(ctx, argv) {
|
||||
let append = false
|
||||
let unbuf = false
|
||||
const files = []
|
||||
for (let i = 1; i < argv.length; i++) {
|
||||
if (argv[i] === '-a' || argv[i] === '--append') append = true
|
||||
else if (argv[i] !== '--') files.push(argv[i])
|
||||
const a = argv[i]
|
||||
if (a === '-a' || a === '--append') append = true
|
||||
else if (a === '-u') unbuf = true
|
||||
else if (a !== '--') files.push(a)
|
||||
}
|
||||
const text = bareStdin(ctx)
|
||||
const data = ctx.b4a.from(text)
|
||||
@@ -83,5 +113,11 @@ async function run(ctx, argv) {
|
||||
ctx.exitCode = 1
|
||||
}
|
||||
}
|
||||
ctx.console.log(text.replace(/\n$/, ''))
|
||||
const out = text.endsWith('\n') ? text : text + '\n'
|
||||
const pw = globalThis.process?.stdout?.write
|
||||
if (unbuf && typeof pw === 'function') {
|
||||
pw.call(globalThis.process.stdout, out)
|
||||
} else {
|
||||
ctx.console.log(text.replace(/\n$/, ''))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user