Skip to content

Commit

Permalink
Let's get jawkward, y'all
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMarnell committed Feb 16, 2022
1 parent 3299b43 commit fbb6fed
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
54 changes: 54 additions & 0 deletions jawk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env node

/**
* To Do features:
* ✅ Emulate begin and end
* ✅ Support different field separators
* ✅ Handle basic print line use case
* ✅ Add NR, NF
* ✅ Add lib access like moment
* ⬜️ Support '/regEx/ {}' skyntax
* ⬜️ Support '/fromRegEx/,/toRegEx/ {}' skyntax
*/

const moment = require('moment')
const readline = require('readline').createInterface({input:process.stdin})

const argv = Object.assign({
fs: '\\s+'
}, require('minimist')(process.argv.slice(2)))
const ctx = {}

if (argv.begin) {
eval(`${argv.begin}`)
}

let lineHandlerParsed
if (argv._.length >= 1) {
let src = argv._[argv._.length - 1]
src = src.replace(/\$(\d+)/ig, "__$[$1]")
lineHandlerParsed = eval(`
function lineHandler(__$, NR, NF, ctx, moment) {
return ( ${src} );
};
lineHandler
`)
argv.fs = new RegExp(argv.fs)
let NR = 0
readline.on('line', line => {
const __$ = [line, ...(line.split(argv.fs))]
const NF = __$.length
NR++
const result = lineHandlerParsed(__$, NR, NF, ctx, moment)
console.log(Array.isArray(result) ? result.join(' ') : result)
})
}

readline.on('close', () => {
if (argv.end) {
eval(`${argv.end}`)
}
if (argv.begin || argv.end) {
console.log(ctx)
}
})
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "j5-scripts",
"version": "0.0.1",
"version": "0.0.2",
"description": "Johnny Marnell's scripts",
"keywords": [
"Johnny",
Expand All @@ -21,9 +21,12 @@
},
"type": "commonjs",
"dependencies": {
"zx": "^5.0.0"
"zx": "^5.0.0",
"minimist": "^1.2.5",
"moment": "^2.29.1"
},
"bin": {
"jawk": "./jawk.js",
"j5-ffmpeg": "./ffmpeg.mjs"
},
"bundleDependencies": [],
Expand Down

0 comments on commit fbb6fed

Please sign in to comment.