From 5013e1c904cc0bbd102f2c85f35dd29c9489cfaf Mon Sep 17 00:00:00 2001 From: Moshe Kolodny Date: Sun, 28 Sep 2014 22:04:12 -0400 Subject: [PATCH] moving over to module system --- .gitignore | 1 + bin/index.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++ lib/index.js | 6 +++++ lib/read-writer.js | 31 ++++++++++++++++++++++++ package.json | 12 ++++++---- 5 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 bin/index.js create mode 100644 lib/index.js create mode 100644 lib/read-writer.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/bin/index.js b/bin/index.js new file mode 100644 index 0000000..9d6e2a6 --- /dev/null +++ b/bin/index.js @@ -0,0 +1,59 @@ +#!/usr/bin/env node + +var program = require('commander'); +var fs = require('fs'); +var nip = require('../lib'); + +program.name = 'nip'; + +program + .version('0.0.1') + .usage('[js-function] [options] [files]') + .option('-1, --first-line-only', 'only execute once per file, not for each line') + .option('-f, --file [js-file]', 'use the js-file as the executer on the input') + .option('-n, --line-splitter [regex]', 'the line separator, gets sent to RegExp', '\\r?\\n') + +program.on('--help', function() { + console.log([ + "", + " Here are some {} clones of well known cli tools", + "", + " cat : {} 'return line' file.txt", + " cat : {} 'return true' file.txt", + " head: {} 'return index <= 10' file.txt", + " head: {} 'function(line, index) { return index <= 10; }' file.txt", + " tail: {} --buffer 'return lines.slice(-10).join(\"\\n\")' file.txt", + " grep: {} 'return /node/.test(line)' file.txt", + " sed : {} 'return line.replace(/hello/g, \"goodbye\")' file.txt", + "", + " this tool takes a list of files like other cli tools to work on", + " you can pass in - for stdin so you can do something like this", + " `echo appended | nip 'return true' file.txt - > newfile.txt`", + "", + ].join('\n').replace(/\{\}/g, program.name)); +}); + +program.parse(process.argv); + +if (!program.file && !/[^a-z0-9$_]/i.test(program.args[0])) { + program.help(); +} + +if (program.file && /[^a-z0-9$_]/i.test(program.args[0])) { + console.log("You can use either the --file option or the [js-function], not both"); + process.exit() +} + +var js; +if (program.file) { + js = fs.readFileSync(program.file) +} else { + js = program.args[0]; + program.args.splice(0, 1); +} + +if (program.args.length === 0) { + program.args.push('-'); +} + +nip(program); \ No newline at end of file diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..d1047eb --- /dev/null +++ b/lib/index.js @@ -0,0 +1,6 @@ +function compileJs(js, ctx) { + if (/^return\b/.test(js)) { + return js = 'return function(line, index, lines) {\n' + js + '\n}' + } + return new Function(js)().bind(ctx); +} diff --git a/lib/read-writer.js b/lib/read-writer.js new file mode 100644 index 0000000..a36a57d --- /dev/null +++ b/lib/read-writer.js @@ -0,0 +1,31 @@ +var fs = require('fs'); +var _ = require('lodash'); + +module.exports = function readWriter(filename, options) { + options = _.extend(options || {}, { + chunksize: 4096, + splitter: '\\r?\\n', + }); + + var bytesRead; + var readOffset = 0; + var file = fs.openSync(filename, 'r+'); + var buffer = new Buffer(options.chunksize); + var splitterRegex = new RegExp(options.splitter, 'g'); + var lines; + + while (true) { + bytesRead = fs.readSync(file, buffer, 0, options.chunksize, readOffset); + if (bytesRead === 0) break; + lines = buffer + .slice(readOffset, bytesRead) + .toString('utf8') + .split(splitterRegex); + console.log(lines); + readOffset += bytesRead; + } + + console.log(options); +} + +module.exports( './lib/read-writer.js', {}) \ No newline at end of file diff --git a/package.json b/package.json index 3450080..bb3d979 100644 --- a/package.json +++ b/package.json @@ -8,17 +8,19 @@ "url": "http://github.com/kolodny/nip.git" }, "dependencies": { - "ccolors": "0.3.x" + "ccolors": "0.3.x", + "commander": "^2.3.0", + "lodash": "^2.4.1" }, "bin": { "nip": "./index.js" }, "keywords": [ - "cli", - "unix", - "utility", + "cli", + "unix", + "utility", "sed", "grep", "awk" ] -} \ No newline at end of file +}