Table of Contents
Husk
Modular stream transformation system using a plugin architecture.
Designed with command execution and file manipulation at the core it is flexible enough for many common tasks.
Install
npm i husk --save
Usage
var husk = ; ;
Plugin Guide
Plugin functionality is provided by zephyr see the zephyr plugins documentation.
Plugin implementations that encapsulate a stream should export a function that creates a new stream so that the plugin is fully compatible with calling pipe()
and define a plugin
function that defines the plugin functionality, see argv.
For plugins that do not expose a stream they can export the plugin function directly, see core.
The design of the system is such that the plugins and stream implementations are separate modules so that the streams may be used outside of the plugin system if required. The majority of the plugins are thin wrappers for the stream to support chained method calls without always calling pipe()
directly.
Examples
argv
Extract values from program arguments.
ebin/argv index.js package.json
Source.
#!/usr/bin/env node var husk = ; ;
Result.
undefined
[
{
"size": 40
},
{
"size": 1965
}
]
async
Pass data to async functions.
ebin/async
Source.
#!/usr/bin/env node var husk = ; { var chunk = this; if!chunklength return ; { var s = '' + chunk + '\n'; ; } ;} ;
Result.
maerts
nigulp
sj.ksuh
data-write
Pass data to be written on run.
ebin/data-write
Source.
#!/usr/bin/env node var husk = ; ;
Result.
undefined
{
"editor": "vim"
}
exec
Execute an external command with callback and listener.
ebin/exec
Source.
#!/usr/bin/env node var husk = ; ;
Result.
muji
[code: 0, signal: null]
[Process:Transform] whoami
filter
Filter array of lines with accept function.
ebin/filter
Source.
#!/usr/bin/env node var husk = ; objectschema: pid: 0 tt: 1 stat: 2 time: 3 cmd: -4 ;
Result.
fs
Open fd, write close and print file content.
ebin/fs
Source.
#!/usr/bin/env node var path = husk = ; var name = path + '-example.log' content = '[file content]'; // re-read and print file to verify write // clean up file ;
Result.
[file content]
hash
Stream multiple files to multiple hash checksums.
ebin/hash
Source.
#!/usr/bin/env node var husk = ; hash algorithm: 'sha1' 'md5' enc: 'hex' passthrough: true field: 'hash' ;
Result.
undefined
[
{
"file": "lib/plugin/exec/alias.js",
"hash": {
"sha1": "a82a88fb21865c7592ce6237b1c3765be4968126",
"md5": "91706a26c49eb622a3539a3a48f04b37"
}
},
{
"file": "lib/plugin/exec/index.js",
"hash": {
"sha1": "972513e4a08106fdc40b896e5b82d173c961391a",
"md5": "070027151d1a2b74924bb409885a0cbb"
}
}
]
modify-file
Read, parse, modify and write out file.
ebin/modify-file
Source.
#!/usr/bin/env node var husk = ; var input = 'package.json' output = 'dependencies.json'; // parse as json and assign // perform transformation // back to string for write // re-read and print file to verify write // clean up file ;
Result.
undefined
{
"husk-async": "~2.0.0",
"husk-core": "~2.0.0",
"husk-exec": "~2.0.0",
"husk-fs": "~2.0.0",
"zephyr": "~2.0.0"
}
parallel
Execute commands in parallel.
ebin/parallel
Source.
#!/usr/bin/env node var husk = ; ;
Result.
foo bar
baz qux
pluck
Read json from filesystem and pluck field.
ebin/pluck
Source.
#!/usr/bin/env node var husk = ; var path = output = 'target'; ;
Result.
undefined
{
"husk-async": "~1.0.2",
"husk-core": "~1.0.4",
"husk-exec": "~1.0.4",
"husk-fs": "~1.0.2",
"zephyr": "~1.2.6"
}
plugin-events
Listen on streams using plugin chain.
ebin/plugin-events
Source.
#!/usr/bin/env node var husk = ; { console;} ;
Result.
[Process:Transform] find
[Buffer:PassThrough]
[Line:Transform]
[Each:Transform]
[Filter:Transform]
[Transform:Transform]
undefined
[Concat:Transform]
[Stringify:Transform]
[Print:Transform] noop
process-pipe
Pipe stdout of a command to the stdin of the next command.
ebin/process-pipe
Source.
#!/usr/bin/env node var husk = ; // pipe `ls` stdout to `cat` stdin ;
Result.
husk.js
plugin
stream
prompt
Prompt for user input.
ebin/prompt
Source.
#!/usr/bin/env node var husk = ask = message: 'choose directory:' default: 'lib'; // auto fill prompt so it can be automated // show prompt prompt { psprompt ask { ps; ; } } // find files in chosen directory ;
Result.
prompt ⚡ choose directory: (lib) doc
doc/readme
doc/readme/install.md
doc/readme/links.md
doc/readme/introduction.md
doc/readme/usage.md
doc/readme/developer.md
doc/readme/plugins.md
doc/readme/license.md
push
Push multiple chunks.
ebin/push
Source.
#!/usr/bin/env node var path = util = husk = ; ;
Result.
push (431 bytes)
./ebin/push
reject
Filter array of lines with reject function.
ebin/reject
Source.
#!/usr/bin/env node var husk = ; objectschema: pid: 0 tt: 1 stat: 2 time: 3 cmd: -4 ;
Result.
series
Execute commands in series.
ebin/series
Source.
#!/usr/bin/env node var husk = ; ;
Result.
1 2 3
foo bar
stdin
Pipe stdin to various plugins to produce json.
who | ebin/stdin
Source.
#!/usr/bin/env node var husk = ; objectschema: user: 0 line: 1 when: -2 ;
Result.
undefined
{
"user": "muji",
"line": ":0",
"when": "2016-01-27 11:32 (:0)"
}
stream-events
Bypass chained method calls and listen on streams.
ebin/stream-events
Source.
#!/usr/bin/env node var husk = exec = each = print = concat = buffer = lines = filter = transform = stringify = ; { console;} var h = ;h ; h;
Result.
[Process:Transform]
[Buffer:PassThrough]
[Line:Transform]
[Each:Transform]
[Filter:Transform]
[Transform:Transform]
undefined
[Concat:Transform]
[Stringify:Transform]
[Print:Transform] noop
transform
Find files, filter and transform to a json array.
ebin/transform
Source.
#!/usr/bin/env node var husk = ; ;
Result.
undefined
[
"./stream/argv/README.md",
"./stream/async/README.md",
"./stream/assert/README.md",
"./plugin/argv/README.md",
"./plugin/async/README.md",
"./plugin/assert/README.md"
]
url
Parse URL arguments.
ebin/url https://example.com:443/?var=foo
Source.
#!/usr/bin/env node var husk = ; ;
Result.
undefined
[
{
"var": "foo"
}
]
zlib
Compress files and print compressed ratio.
ebin/zlib
Source.
#!/usr/bin/env node var husk = zlib = huskzlib; ;
Result.
undefined
[
{
"source": "lib/plugin/exec/alias.js",
"file": "lib/plugin/exec/alias.js.gz",
"ratio": 0.3165910563836682,
"percent": "32%"
},
{
"source": "lib/plugin/exec/index.js",
"file": "lib/plugin/exec/index.js.gz",
"ratio": 0.3460193652501345,
"percent": "35%"
}
]
Developer
Whilst the design is modular the repository is monolithic to reduce maintenance, all the modules in plugin and stream should be linked and it is easiest to resolve all dependencies at the top-level during development.
To get up and running:
npm i -dd && npm run ln
Install
Runs npm install
on all modules:
npm run installify
Link
Runs npm link
on all modules:
npm run linkify
Example
To view the output from all examples in ebin (also included in the readme build):
sbin/ebin
Test
Tests are not included in the package, clone the repository:
npm test
Cover
To generate code coverage run:
npm run cover
Documentation
To generate all documentation:
npm run docs
Readme
To build the readme file from the partial definitions (requires mdp):
npm run readme
License
Everything is MIT. Read the license if you feel inclined.
Generated by mdp(1).