sicksync
Version:
Don’t accept the available as the preferable. Go extra mile with extra speed.
251 lines (190 loc) • 6.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getSicksyncDir = getSicksyncDir;
exports.getUpdatePath = getUpdatePath;
exports.getConfigPath = getConfigPath;
exports.getConfig = getConfig;
exports.getId = getId;
exports.writeConfig = writeConfig;
exports.isExcluded = isExcluded;
exports.generateLog = generateLog;
exports.rebounce = rebounce;
exports.ensureTrailingSlash = ensureTrailingSlash;
exports.open = open;
exports.toBoolean = toBoolean;
exports.setupPrompter = setupPrompter;
exports.shellIntoRemote = shellIntoRemote;
exports.printLogo = printLogo;
exports.uniqInstance = uniqInstance;
exports.getProjectFromCwd = getProjectFromCwd;
exports.getProjectsFromConfig = getProjectsFromConfig;
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _fsExtra = require('fs-extra');
var _fsExtra2 = _interopRequireDefault(_fsExtra);
var _child_process = require('child_process');
var _anymatch = require('anymatch');
var _anymatch2 = _interopRequireDefault(_anymatch);
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _untildify = require('untildify');
var _untildify2 = _interopRequireDefault(_untildify);
var _constants = require('../conf/constants');
var _constants2 = _interopRequireDefault(_constants);
var _text = require('../conf/text');
var _text2 = _interopRequireDefault(_text);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Returns the path to the sicksync dir
function getSicksyncDir() {
return (0, _untildify2.default)(_constants2.default.SICKSYNC_DIR);
}
// Return the path to the update json
function getUpdatePath() {
return getSicksyncDir() + '/' + _constants2.default.UPDATE_FILE;
}
// Returns the path the the config file
function getConfigPath() {
return getSicksyncDir() + '/' + _constants2.default.CONFIG_FILE;
}
// Returns the config object if it exists, if not an empty object. Loads/saves from cache where possible
// TODO: `existsSync` is going to be deprecated soon...
function getConfig() {
const configPath = getConfigPath();
let config = {};
if (_fsExtra2.default.existsSync(configPath)) {
config = require(configPath);
}
// Return a cloned copy of config to avoid indirect changes
return _lodash2.default.cloneDeep(config);
}
// Randomly generate a unique ID
function getId() {
return Math.random().toString(36).substr(2, 9) + Date.now();
}
// Write out the config file with a provided <obj>
function writeConfig(configFile) {
const configPath = getConfigPath();
_fsExtra2.default.outputFileSync(configPath, JSON.stringify(configFile, null, 4));
console.info(_text2.default.CONFIG_SAVED);
}
// Given a file path, check to see if it's in the excludes array
function isExcluded(filepath, excludes) {
return (0, _anymatch2.default)(excludes, filepath);
}
// Log messages with Hostname prepended
function generateLog(projectName, hostname) {
const args = _lodash2.default.slice(arguments);
// If only one argument it's the hostname
if (args.length === 1) {
projectName = null;
hostname = args[0];
}
return function log() {
const args = [projectName ? _chalk2.default.blue('[' + projectName + ']') : '', hostname ? _chalk2.default.green('[' + hostname + ']') : ''].concat([].slice.call(arguments));
console.info.apply(console, args);
};
}
//
// Rebounce
//
// Takes a desired function, a fallback function, the number of times
// called, and a cool down. Returns a `rebounced` function.
//
// The basic idea is that we want to limit calls to function to a
// certain # of times in a given time-frame. If it breaks over that,
// then we fallback to another function, and halt previous desired calls
function rebounce(primaryFn, secondaryFn, fallOverAmount, coolDown) {
const timeOutIds = [];
let timesCalled = 0;
let fallbackCalled = false;
return function rebounced() {
const args = arguments;
timesCalled++;
timeOutIds.push(setTimeout(function () {
timesCalled = 0;
primaryFn.apply(null, args);
}, coolDown));
if (timesCalled >= fallOverAmount) {
timeOutIds.forEach(clearTimeout);
/* istanbul ignore else */
if (!fallbackCalled) {
timesCalled = 0;
fallbackCalled = true;
secondaryFn.apply(null, arguments);
setTimeout(function () {
fallbackCalled = false;
}, coolDown);
}
}
};
}
function ensureTrailingSlash(path) {
return path.substring(path.length - 1) === '/' ? path : path + '/';
}
function open(parameter) {
return (0, _child_process.exec)('open ' + parameter);
}
function toBoolean(param) {
const lowerParam = param.toLowerCase();
if (lowerParam.indexOf('y') > -1) {
return true;
}
if (lowerParam.indexOf('n') > -1) {
return false;
}
if (lowerParam === 'true' || lowerParam === 'false') {
return JSON.parse(lowerParam);
}
return false;
}
function setupPrompter(prompt) {
prompt.message = '';
prompt.delimiter = '';
prompt.start();
return prompt;
}
function shellIntoRemote(remote) {
return (0, _child_process.spawn)('ssh', ['-tt', remote]);
}
function printLogo() {
console.info(_chalk2.default.blue(_fsExtra2.default.readFileSync(_path2.default.resolve(__dirname, '../conf/logo.txt')).toString()));
}
function uniqInstance(tokenPath, Constructor) {
const instances = {};
return function (args) {
const token = _lodash2.default.get(args, tokenPath, null);
if (_lodash2.default.get(instances, token, null)) {
return instances[token];
}
if (!_lodash2.default.isNull(token)) {
instances[token] = new Constructor(args);
return instances[token];
}
return new Constructor(args);
};
}
function getProjectFromCwd(config) {
return _lodash2.default.chain(config.projects).filter(project => {
return _lodash2.default.isEqual(ensureTrailingSlash((0, _untildify2.default)(project.sourceLocation)), ensureTrailingSlash(process.cwd()));
}).value();
}
function getProjectsFromConfig(config, projects) {
let foundProjects = [];
if (_lodash2.default.isEmpty(projects)) {
const cwdProject = getProjectFromCwd(config);
if (cwdProject) {
foundProjects = cwdProject;
}
}
_lodash2.default.each(projects, project => {
const projectConf = _lodash2.default.find(config.projects, { project: project });
if (!_lodash2.default.isEmpty(projectConf)) {
foundProjects.push(projectConf);
}
});
return foundProjects;
}
;