Skip to content

Instantly share code, notes, and snippets.

@minodisk
Created June 24, 2014 04:07
Show Gist options
  • Save minodisk/8c14b08999ffe8381e1b to your computer and use it in GitHub Desktop.
Save minodisk/8c14b08999ffe8381e1b to your computer and use it in GitHub Desktop.
再帰的にUTF-8からShift_JISに変換するGruntfile
{ readdirSync, statSync, existsSync, mkdirSync } = require 'fs'
{ inspect } = require 'util'
{ resolve, dirname, filename, extname, sep, relative } = require 'path'
{ exec } = require 'child_process'
firstDirname = (filepath) ->
filepath.split(sep)[0]
mkdirRecursively = (dirStr) ->
cwd = ''
for dir in dirStr.split '/'
cwd = resolve cwd, dir
unless existsSync cwd
mkdirSync cwd
findFilesRecursively = (dir, files = []) ->
for file in readdirSync dir
file = resolve dir, file
stats = statSync file
if stats.isDirectory()
findFilesRecursively file, files
else if stats.isFile()
files.push file
files
iconv = (src, dest, encoding, callback) ->
exec "iconv -f UTF-8 -t #{encoding.toUpperCase()} #{src} > #{dest}", (err, stdout, stderr) ->
if err?
callback err
return
callback null, stdout
module.exports = (grunt) ->
grunt.initConfig
esteWatch:
options:
dirs: [
'iconv/**/'
]
'*': (filepath) ->
switch firstDirname filepath
when 'iconv'
[ "iconv:#{filepath}" ]
iconv:
options:
encoding: 'shift_jis'
src: 'iconv'
dest: 'htdocs'
grunt.registerTask 'iconv', ->
done = @async()
{ src, dest, encoding } = @options encoding: 'shift_jis'
if @args.length is 0
files = findFilesRecursively src
else
files = @args
counter = 0
length = files.length
for arg in files
srcFile = arg
destFile = arg.replace src, dest
mkdirRecursively dirname destFile
iconv srcFile, destFile, encoding, (err, stdout) ->
if err?
grunt.fail.warn err
else
grunt.log.ok "write #{relative __dirname, destFile} as #{encoding}"
if ++counter is length
done()
.
├──htdocs
├──iconv
└──Gruntfile.coffee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment