UNPKG

sicksync

Version:

Don’t accept the available as the preferable. Go extra mile with extra speed.

302 lines (249 loc) 8.78 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.canShellIntoHost = canShellIntoHost; exports.hasSicksyncRemote = hasSicksyncRemote; exports.hasRightSicksyncVerions = hasRightSicksyncVerions; exports.hasConfig = hasConfig; exports.configHasRightShape = configHasRightShape; exports.projectHasRightShape = projectHasRightShape; exports.checkAll = checkAll; var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk); var _fsExtra = require('fs-extra'); var _fsExtra2 = _interopRequireDefault(_fsExtra); var _util = require('./util'); var _package = require('../package.json'); var _package2 = _interopRequireDefault(_package); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const configShape = { version: { type: 'String', func: _lodash2.default.isString }, debug: { type: 'Boolean', func: _lodash2.default.isBoolean }, projects: { type: 'Array', func: _lodash2.default.isArray } }; const projectShape = { hostname: { type: 'String', func: _lodash2.default.isString }, username: { type: 'String', func: _lodash2.default.isString }, sourceLocation: { type: 'String', func: _lodash2.default.isString }, destinationLocation: { type: 'String', func: _lodash2.default.isString }, excludes: { type: 'Array', func: _lodash2.default.isArray }, prefersEncrypted: { type: 'Boolean', func: _lodash2.default.isBoolean }, websocketPort: { type: 'String', func: _lodash2.default.isString }, followSymLinks: { type: 'Boolean', func: _lodash2.default.isBoolean }, project: { type: 'String', func: _lodash2.default.isString } }; function logTimeout(username, host) { console.info(_chalk2.default.red(`Timed out waiting to connect to ${ host } with user ${ username }`)); console.info(`Check to make sure your machine is reachable by by sshing: ${ username }@${ host }`); } function canShellIntoHost(_ref) { let username = _ref.username, hostname = _ref.hostname; return new Promise((resolve, reject) => { const ssh = (0, _util.shellIntoRemote)(`${ username }@${ hostname }`); const timeoutId = setTimeout(() => { logTimeout(username, hostname); ssh.kill(); reject(false); }, 10000); ssh.stdout.on('data', () => { clearTimeout(timeoutId); console.info(_chalk2.default.green(`Successfully connected to ${ hostname } with user ${ username }!`)); ssh.kill(); resolve(true); }); }); } function hasSicksyncRemote(_ref2) { let username = _ref2.username, hostname = _ref2.hostname; return new Promise((resolve, reject) => { const ssh = (0, _util.shellIntoRemote)(`${ username }@${ hostname }`); const timeoutId = setTimeout(() => { clearAndKill(); logTimeout(username, hostname); reject(false); }, 10000); const clearAndKill = () => { ssh.kill(); clearTimeout(timeoutId); }; // See if we can find sicksync ssh.stdin.write('which sicksync\n'); ssh.stdout.on('data', data => { const message = data.toString(); if (_lodash2.default.includes(message, 'no sicksync in')) { clearAndKill(); console.info(_chalk2.default.red(`Couldn't start sicksync on ${ hostname } with user ${ username }!`)); console.info(`Check to ensure it's installed globally on ${ hostname }: 'npm i -g sicksync'`); console.info(`Lastly, check to make sure your .bashrc or .zshrc contains the npm global path.`); reject(false); } if (_lodash2.default.includes(message, '/sicksync')) { clearAndKill(); console.info(_chalk2.default.green(`Successfully found sicksync on host ${ hostname } with user ${ username }!`)); resolve(true); } }); }); } function hasRightSicksyncVerions(_ref3) { let hostname = _ref3.hostname, username = _ref3.username; return new Promise((resolve, reject) => { const ssh = (0, _util.shellIntoRemote)(`${ username }@${ hostname }`); const timeoutId = setTimeout(() => { clearAndKill(); logTimeout(username, hostname); reject(false); }, 10000); const clearAndKill = () => { ssh.kill(); clearTimeout(timeoutId); }; // See if sicksync is at the right version ssh.stdin.write('sicksync -V\n'); ssh.stdout.on('data', data => { const message = data.toString(); if (message.match(/^(\d+\.)?(\d+\.)?(\d+\.*)/g)) { const version = message.trim(); clearAndKill(); if (version !== _package2.default.version) { console.info(_chalk2.default.red(hostname, 'is at version', version, 'but is locally at version', _package2.default.version)); console.info('Please make sure both machines are at the latest verions'); reject(false); } else { console.info(_chalk2.default.green(hostname, 'has same version of sicksync!')); resolve(true); } } }); }); } function hasConfig() { return new Promise((resolve, reject) => { const configPath = (0, _util.getConfigPath)(); _fsExtra2.default.stat(configPath, (err /*, stats */) => { if (err) { console.info(_chalk2.default.red(`sicksync couldn't find a config file!`)); console.info(`Be sure it's located at "~/.sicksync/config.json"`); console.info(`Or add a new project with "sicksync add-project" to create one`); reject(false); } else { console.info(_chalk2.default.green(`Found the sicksync config file!`)); resolve(true); } }); }); } function configHasRightShape(config) { return new Promise((resolve, reject) => { const hasRightShape = _lodash2.default.every(configShape, (_ref4, key) => { let func = _ref4.func, type = _ref4.type; if (_lodash2.default.isUndefined(config[key])) { console.info(_chalk2.default.red(`Config is missing:`, key)); console.info('Please make sure your config has a', key, 'property'); return false; } if (!func(config[key])) { console.info(_chalk2.default.red(`Config`, key, `has the wrong type!`)); console.info('Please make sure your', key, 'has type', type); return false; } return true; }); if (hasRightShape) { console.info(_chalk2.default.green(`sicksync's config has all the right properties and looks good!`)); resolve(true); } else { reject(false); } }); } function projectHasRightShape(project) { return new Promise((resolve, reject) => { const projectName = project.project; const hasRightShape = _lodash2.default.every(projectShape, (_ref5, key) => { let func = _ref5.func, type = _ref5.type; if (_lodash2.default.isUndefined(project[key])) { console.info(_chalk2.default.red(`Project is missing:`, key)); console.info('Please make sure your project has a', key, 'property'); return false; } if (!func(project[key])) { console.info(_chalk2.default.red(projectName, key, `has the wrong type!`)); console.info('Please make sure', projectName, key, 'has type', type); return false; } return true; }); if (hasRightShape) { console.info(_chalk2.default.green(project.project, `has all the right properties and looks good!`)); resolve(true); } else { reject(false); } }); } function checkAll(config) { console.info(_chalk2.default.yellow('* Checking if config is present...')); hasConfig().then(() => { console.info(_chalk2.default.yellow('\n* Checking config file...')); return configHasRightShape(config); }).then(() => { console.info(_chalk2.default.yellow('\n* Checking projects in config file...')); return Promise.all(_lodash2.default.map(config.projects, projectHasRightShape)); }).then(() => { console.info(_chalk2.default.yellow('\n* Checking host for each project')); return Promise.all(_lodash2.default.map(config.projects, canShellIntoHost)); }).then(() => { console.info(_chalk2.default.yellow('\n* Checking sicksync on hosts for each project')); return Promise.all(_lodash2.default.map(config.projects, hasSicksyncRemote)); }).then(() => { console.info(_chalk2.default.yellow('\n* Checking sicksync version on hosts for each project')); return Promise.all(_lodash2.default.map(config.projects, hasRightSicksyncVerions)); }).then(() => { console.info(_chalk2.default.green('\nEverything looks good!')); }); }