telebit.js/lib/cli-common.js

81 lines
2.3 KiB
JavaScript

'use strict';
var common = module.exports;
var path = require('path');
var mkdirp = require('mkdirp');
var os = require('os');
var homedir = os.homedir();
var localshare = '.local/share/telebit';
var localconf = '.config/telebit';
common.pipename = function (config, newApi) {
var _ipc = {
path: (config.sock || common.DEFAULT_SOCK_NAME)
, comment: (/^win/i.test(os.platform()) ? 'windows pipe' : 'unix socket')
, type: (/^win/i.test(os.platform()) ? 'pipe' : 'socket')
};
if ('pipe' === _ipc.type) {
_ipc.path = '\\\\?\\pipe' + _ipc.path.replace(/\//, '\\');
}
if (newApi) {
return _ipc;
}
return _ipc.path;
};
common.DEFAULT_SOCK_NAME = path.join(homedir, localshare, 'var', 'run', 'telebit.sock');
common.parseUrl = function (hostname) {
var url = require('url');
var location = url.parse(hostname);
if (!location.protocol || /\./.test(location.protocol)) {
hostname = 'https://' + hostname;
location = url.parse(hostname);
}
hostname = location.hostname + (location.port ? ':' + location.port : '');
hostname = location.protocol.replace(/https?/, 'https') + '//' + hostname + location.pathname;
return hostname;
};
common.parseHostname = function (hostname) {
var url = require('url');
var location = url.parse(hostname);
if (!location.protocol || /\./.test(location.protocol)) {
hostname = 'https://' + hostname;
location = url.parse(hostname);
}
//hostname = location.hostname + (location.port ? ':' + location.port : '');
//hostname = location.protocol.replace(/https?/, 'https') + '//' + hostname + location.pathname;
return location.hostname;
};
common.apiDirectory = '_apis/telebit.cloud/index.json';
function leftpad(i, n, c) {
i = i.toString();
while (i.length < (n || 4)) {
i = (c || '0') + i;
}
return i;
}
common.otp = function getOtp() {
return leftpad(Math.round(Math.random() * 9999), 4, '0');
};
try {
mkdirp.sync(path.join(__dirname, '..', 'var', 'log'));
mkdirp.sync(path.join(__dirname, '..', 'var', 'run'));
mkdirp.sync(path.join(__dirname, '..', 'etc'));
} catch(e) {
console.error(e);
}
try {
mkdirp.sync(path.join(homedir, localshare, 'var', 'log'));
mkdirp.sync(path.join(homedir, localshare, 'var', 'run'));
//mkdirp.sync(path.join(homedir, localshare, 'etc'));
mkdirp.sync(path.join(homedir, localconf));
} catch(e) {
console.error(e);
}