node-print: upgrade to 4

This commit is contained in:
ptrcnull 2022-10-11 13:16:01 +02:00
parent 1dcddc7196
commit f1fcd00c98
2 changed files with 43 additions and 8 deletions

View file

@ -1,7 +1,7 @@
# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=node-print
pkgver=3
pkgver=4
pkgrel=0
pkgdesc="another terrible javascript engine wrapper"
url="https://git.ddd.rip/ptrcnull/ptrcports"
@ -19,7 +19,7 @@ check() {
local out
out="$(echo test | node -r . -p "stdin().substring(2).replace('t', 'rc').replace('s', 'pt')")"
_assert "$out" "ptrc"
out="$(echo '{"test":"property"}' | node -r . -p "stdin().test")"
_assert "$out" "property"
@ -28,6 +28,12 @@ check() {
out="$(echo crtp | node -r . -p "stdin().lines()[0].reverse()")"
_assert "$out" "ptrc"
out="$(node -r . -p "exec('echo', ['1']).stdout.toString()")"
_assert "$out" "1"
out="$(node -r . -p "exec('echo', ['[]']).stdout.length")"
_assert "$out" "0"
}
package() {
@ -37,5 +43,5 @@ package() {
sha512sums="
b3dfdeb49637be33d2e2718c5abcf35a87dd55023918c99341273c3b38bd6685189d1f786451a742c47c5f3bc3b58555decb58e2a3a018c9b9ee92043f8fac03 np
7a1622dcfcdea192368329c962a25b42ca919895669f1e646e6958ef770c8c6a90cdeb88acea368fb40d5e956afff4baec0310dc06c78e6aa092947f9fc70fc7 index.js
0262bc1b6b15180ef715e0be3cb2e3d8a83ed532c6282a5b4466c16251f0f29b2d870dbac5795a4fb6a0b1e917b362ebd18d5fa6d6402209ca4e35409eec59e6 index.js
"

View file

@ -1,11 +1,7 @@
global.stdin = () => {
const fs = require('fs')
const out = fs.readFileSync(0).toString()
if (out.at(0) === '{' || out.at(0) === '[') {
return JSON.parse(out)
} else {
return out
}
return out.tryParseJSON()
}
Object.prototype.map = function(cb) {
@ -20,3 +16,36 @@ String.prototype.lines = function() {
String.prototype.reverse = function() {
return this.split('').reverse().join('')
}
String.prototype.tryParseJSON = function() {
const str = this.trim()
if (
(str.at(0) === '{' && str.at(-1) === '}') ||
(str.at(0) === '[' && str.at(-1) === ']')
) {
try {
return JSON.parse(str)
} catch (err) {
return this.toString()
}
}
return this.toString()
}
Buffer.prototype.tryParseJSON = function() {
const out = this.toString().tryParseJSON()
if (typeof out === 'string') {
return this
} else {
return out
}
}
global.exec = (command, args, options) => {
const child_process = require('child_process')
const res = child_process.spawnSync(command, args, options)
res.stdout = res.stdout.tryParseJSON()
res.stderr = res.stderr.tryParseJSON()
return res
}