Skip to content

Commit

Permalink
basic outline
Browse files Browse the repository at this point in the history
  • Loading branch information
Moshe Kolodny committed May 5, 2015
1 parent e7e7ed9 commit fe499a1
Show file tree
Hide file tree
Showing 9 changed files with 540 additions and 337 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
@@ -0,0 +1,12 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules/
18 changes: 8 additions & 10 deletions README.md
Expand Up @@ -2,26 +2,25 @@
`nip` is a command line utility for performing any type of processing to and from files and pipes

# Install
With [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):

npm install nip -g

If you omit the `-g` then make sure to add the local npm module path to your dafault path
If you omit the `-g` then make sure to add the local npm module path to your default path

You should now be able to call `nip` from the command line.

### Usage: `nip js-function [options] [files]`

The js-function can be one of three syntaxes:

1. `function(line, index, lines, cols) { /* code here */ return value; }`
2. `return line.substr(0, 10) + index`
1. `return line.substr(0, 10) + index`
2. `function(line, index, lines, cols) { /* code here */ return value; }`
3. `/* code */ return function(line, i, lines) { /* ... */ return value; }`

The names `line`, `index`, `lines`, and `cols` can be changed in the first and third style syntaxes
The names `line`, `index`, `lines`, and `cols` can be changed in the second and third style syntaxes

If the return value is `false` or `undefined` nothing is sent to output stream
If the return value is `true` then the line will be sent to the output stream
If the return value is `false` nothing is sent to output stream
If the return value is not a string or number then the line will be sent to the output stream
else the return value will be sent to the output stream (including an empty string)

### options
Expand Down Expand Up @@ -107,14 +106,13 @@ find the biggest number from all files in a directory:

---

By default there are `start`, `end`, `fileStart`, and `fileEnd` events you can register, you can also register in
`this.onend = function() {/* code */}` format
By default there are `start`, `end`, `fileStart`, and `fileEnd` events you can register by doing `this.on('end', function() {})`

The context inside the main function can be used as a global store, and has a `filename` property

---

### Why
This is for people who aren't "devops" and who can't crank out a fancy piped shell script using `awk`, `sed,` and `grep`. Also most programmers who have node installed can write a quick javascript one+ liner to do what the oldschoolers would make a shell script out of.
This is for people who aren't "devops" and who can't crank out a fancy piped shell script using `awk` and `sed`. Also most programmers who have node installed can write a quick javascript one+ liner to do what the oldschoolers would make a shell script out of.

https://github.com/kolodny/nip
55 changes: 55 additions & 0 deletions bin/nip.js
@@ -0,0 +1,55 @@
#!/usr/bin/env node

var nip = require('..');
var fs = require('fs');
var path = require('path');
var argv = require('minimist')(process.argv.slice(2));

var jsFile = argv.f || argv.file

if (argv.h || argv.help) {
console.log(fs.readFileSync(__dirname + '/usage.txt').toString());
return;
}

if (!argv._.length && !jsFile) {
console.error('You muse specify a js-exec or a --js-file\nsee nip --help for more info');
return;
}



var callbackStr;
if (jsFile) {
callbackStr = fs.readFileSync(jsFile).toString();
} else {
callbackStr = argv._.shift();
}

var files = argv._;

var callback;
try {
if (/^\s*return\b/.test(callbackStr)) {
callback = Function('return function(line, index) {' + callbackStr + '}')();
} else if (/\s*function\b/.test(callbackStr)) {
callback = Function('return ' + callbackStr)();
} else {
callback = Function(callbackStr)();
}
} catch (e) {
console.error("coudn't build callback function", e);
return;
}

files = files.map(function(file) {
return (file === '-') ? nip.STDIN : file;
});

console.log(files)

nip({
files: files,
callback: callback
});

21 changes: 21 additions & 0 deletions bin/usage.txt
@@ -0,0 +1,21 @@
Usage: nip [js-executor] {OPTIONS} [files, ...]

The js-executor can either be a js function-like string like this:
'return line.toUpperCase()'
or can be a file which contains the js function-like string

A js function-like string can be one of these three formats:
'return line.toUpperCase()'
'function(line, lineIndex) { return line.toUpperCase(); }'
'var setup = 123; return function(line) { return line; }'
If used like in the first example, the variables line and index
are available.
You can return a transformed line or false to omit the line

You must supply either a js function-like string or a --file

Standard Options:

--file, -f The file that contains the js-executor

--help, -h Show this message

0 comments on commit fe499a1

Please sign in to comment.