Skip to content

Instantly share code, notes, and snippets.

@takuya
Created November 13, 2013 11:26
Show Gist options
  • Save takuya/7447546 to your computer and use it in GitHub Desktop.
Save takuya/7447546 to your computer and use it in GitHub Desktop.
nodeの関数をbashシェルで使えるようにする。 ref: http://qiita.com/takuya_1st/items/11b9b79d16ccbb3aed40
bash> echo 'aaa=1&bb=あああ' | encodeURIComponent| decodeURIComponent | querystring.parse
{ aaa: '1', bb: 'あああ' }
bash> echo 'aaa=1&bb=あああ' | encodeURIComponent| decodeURIComponent | querystring.parse
{ aaa: '1', bb: 'あああ' }
#!/usr/bin/env node
/***
* author takuya_1st
* Released under GPL Licenses.
* Date: 2013-11-21 2:06
*/
var fs = require('fs')
var path = require('path');
var name = process.argv[1];
var str = process.argv[2];
//モジュールの処理
if( name.match(/\./) ){
module_name = path.basename( name.split(/\./).shift()) ;
name = name.split(/\./).pop();
if( global[module_name] ){
module = global[module_name];
}else{
module = require(module_name);
}
}else {
name = path.basename(name);
module = global;
}
//コマンドシェルで実行したい関数
var func = module[name];
if(str) {
if(fs.existsSync(str)){
str = fs.readFileSync(str);
}
str = func(str);
console.log(str);
}else{
process.stdin.resume();
process.stdin.setEncoding('utf8');
var lines = []
// 標準入力がくると発生するイベント
process.stdin.on('data', function (chunk) {
lines.push(chunk.trim())
});
// EOFがくると発生するイベント
process.stdin.on('end', function () {
lines = lines.join("\n")
console.log( func(lines) )
});
}
bash> git clone https://gist.github.com/f5a6fb560dc357835122.git
bash> chmod +x node2bash.js
bash> ln -s node2bash.js /usr/local/bin/encodeURIComponent
bash> ln -s node2bash.js /usr/local/bin/decodeURIComponent
bash> ln -s node2bash.js /usr/local/bin/querystring.parse
bash> encodeURIComponent 'aaa=1&bb=あああ'
aaa%3D1%26bb%3D%E3%81%82%E3%81%82%E3%81%82
bash > encodeURIComponent 'aaa=1&bb=あああ' | decodeURIComponent
aaa=1&bb=あああ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment