diff --git a/node-print/APKBUILD b/node-print/APKBUILD index d21e33b..86f5a1a 100644 --- a/node-print/APKBUILD +++ b/node-print/APKBUILD @@ -1,7 +1,7 @@ # Contributor: Patrycja Rosa # Maintainer: Patrycja Rosa 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 " diff --git a/node-print/index.js b/node-print/index.js index 7031d5e..2c7f790 100644 --- a/node-print/index.js +++ b/node-print/index.js @@ -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 +}