Last active
August 29, 2015 14:27
-
-
Save ktcy/f34659df083204408f12 to your computer and use it in GitHub Desktop.
$ invpath <path>
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 | |
getOptions(function(options) { | |
if (!options.path) { | |
console.log('Usage: invpath <path>'); | |
process.exit(0); | |
} | |
var path = options.path.trim(); | |
var type = guess(path); | |
var result = null; | |
switch (type) { | |
case 'mac': result = toWindows(path); break; | |
case 'windows': result = toMac(path); break; | |
} | |
if (result) { | |
console.log(result); | |
process.exit(0); | |
} else { | |
console.error('Unknown format: "' + path + '"'); | |
process.exit(1); | |
} | |
}); | |
function getOptions(callback) { | |
var options = parseArguments(); | |
if (options.path) { | |
callback(options); | |
} else { | |
process.stdin.setEncoding('utf8'); | |
process.stdin.once('readable', function() { | |
options.path = process.stdin.read() || ''; | |
callback(options); | |
}); | |
} | |
} | |
function parseArguments() { | |
return process.argv.slice(2).reduce(function(options, arg) { | |
if (arg.indexOf('-') !== 0) { | |
options.path = arg; | |
} | |
return options; | |
}, {}); | |
} | |
function guess(string) { | |
var type = 'unknown'; | |
if (require('url').parse(string).protocol === 'smb:') { | |
type = 'mac'; | |
} else if (string.match(/^\\\\|^¥¥/)) { | |
type = 'windows'; | |
} | |
return type; | |
} | |
function toMac(string) { | |
return 'smb:' + string.replace(/\\|¥/g, '/'); | |
} | |
function toWindows(string) { | |
return string.substr(4).replace(/\//g, '\\'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment