Created
November 13, 2013 11:26
-
-
Save takuya/7447546 to your computer and use it in GitHub Desktop.
nodeの関数をbashシェルで使えるようにする。 ref: http://qiita.com/takuya_1st/items/11b9b79d16ccbb3aed40
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bash> echo 'aaa=1&bb=あああ' | encodeURIComponent| decodeURIComponent | querystring.parse | |
{ aaa: '1', bb: 'あああ' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bash> echo 'aaa=1&bb=あああ' | encodeURIComponent| decodeURIComponent | querystring.parse | |
{ aaa: '1', bb: 'あああ' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) ) | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bash> encodeURIComponent 'aaa=1&bb=あああ' | |
aaa%3D1%26bb%3D%E3%81%82%E3%81%82%E3%81%82 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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