Milk+ea

Weblog Is My Hobby.

Rakefileのshでarg有りのコマンドを実行した時に文字化け(コマンドプロンプト)

こんな感じのファイルを用意。

Rakefile

task :sh do
  sh "node sh.js ん゛ぁ゛ぁ゛あ゛あ゛"
end

sh.js

console.log(process.argv[2]);

rake shを実行すると文字化けが起きる。

>rake sh
node sh.js ん゛ぁ゛ぁ゛あ゛あ゛
繧薙・縺√・縺√・縺ゅ・縺ゅ・

.encode(Encoding::Windows_31J)を付けてあげる

Rakefileを変更。

task :sh do
  sh "node sh.js や゛っ゛た゛ぁ゛あ゛あ゛!゛".encode(Encoding::Windows_31J)
end
>rake sh
node sh.js や゛っ゛た゛ぁ゛あ゛あ゛!゛
や゛っ゛た゛ぁ゛あ゛あ゛!゛