Skip to content

Instantly share code, notes, and snippets.

@snowyu
Last active September 5, 2015 23:00
Show Gist options
  • Save snowyu/bdc39a93d25503333991 to your computer and use it in GitHub Desktop.
Save snowyu/bdc39a93d25503333991 to your computer and use it in GitHub Desktop.
nodejs check whether the file name is case-insensitive and get the real path for the case-insensitive file-system across os.
fs = require 'fs'
path= require 'path'
tmp = require 'tmp'
tmp.setGracefulCleanup()
folderIsInsensitive = (aFolder)->
aFolder ?= '.'
t = tmp.fileSync template:path.join aFolder, '_tmp-XXXXXXXXX'
result = fs.existsSync t.name.toUpperCase()
t.removeCallback()
result
getRealPathForInsensitive = (aPath)->
result = ''
while aPath and aPath != path.sep
basename = path.basename(aPath).toLowerCase()
aPath = path.dirname aPath
dirs = fs.readdirSync aPath
for dir in dirs
if dir.toLowerCase() is basename
result = path.join dir, result
break
path.join aPath, result
getRealPath = (aPath)->
if folderIsInsensitive aPath
getRealPathForInsensitive(aPath)
else
aPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment