Skip to content

Instantly share code, notes, and snippets.

@ktcy
Last active August 29, 2015 14:27
Show Gist options
  • Save ktcy/328a344a9668ad003eef to your computer and use it in GitHub Desktop.
Save ktcy/328a344a9668ad003eef to your computer and use it in GitHub Desktop.
$ toremote [--mac] [--windows] [path]
#!/usr/bin/env node
try {
var args = parseArguments();
if (args.help) {
console.log('Usage: toremote [--mac] [--windows] [path]');
process.exit(0);
}
var targetPath = args.path || process.cwd();
var result = toRemote(targetPath);
if (args.mac !== args.windows) {
args.mac && console.log(result.mac);
args.windows && console.log(result.windows);
} else {
console.log([
'',
'For Mac:',
result.mac,
'',
'For Windows:',
result.windows,
''
].join('\n'));
}
} catch (error) {
console.error(error);
process.exit(1);
}
function parseArguments() {
return process.argv.slice(2).reduce(function(result, arg) {
if (arg === '--help' || arg === '-h') {
result.help = true;
} else if (arg === '--mac') {
result.mac = true;
} else if (arg === '--windows') {
result.windows = true;
} else {
result.path = arg;
}
return result;
}, {});
}
function toRemote(targetPath) {
var path = require('path');
var url = require('url');
var info = getMountInfo(targetPath);
if (info.remote.indexOf('//') !== 0) {
throw new Error(targetPath + ' is a local file or directory');
}
var relativePath = path.relative(info.local, targetPath);
var components = url.parse('smb:' + info.remote);
components.auth = null;
components.pathname = path.join(components.pathname, encodeURI(relativePath));
var mac = url.format(components);
var windows = decodeURI(mac.substr(4)).replace(/\//g, '\\');
return {mac: mac, windows: windows};
}
function getMountInfo(targetPath) {
var spawnSync = require('child_process').spawnSync;
var df = spawnSync('df', [targetPath], {encoding: 'utf8'}).stdout;
df = df.trim().split(/\r|\n|\r\n/).pop();
var remote = df.match(/^[^\s]+/)[0];
var local = df.substr(remote.length).match(/\/.*$/)[0];
return {remote: remote, local: local};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment