All files / lib cli.js

53.33% Statements 176/330
94.87% Branches 37/39
61.53% Functions 8/13
53.33% Lines 176/330

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 3311x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x     4x 4x 1x 1x 1x 1x 4x 3x 3x 3x 2x 3x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 4x 4x 4x 1x                                                                                             1x                                                             1x                       1x                                                           1x 4x 4x 4x 4x 4x 4x 3x 3x 3x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 1x                                                 1x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 1x 3x 2x 2x 2x 3x 3x 5x 5x 5x 4x 3x 3x 3x 3x 3x 4x 1x 1x 1x 5x 1x 1x 5x 5x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 11x 11x 11x 11x 6x 11x 11x 11x 11x 11x                         11x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x 1x 1x  
const _ = require('lodash');
const bag = require('bagofcli');
const fs = require('fs');
const p = require('path');
const prompt = require('prompt');
const prompts = require('prompts');
const Repoman = require('./repoman');
const GithubAuth = require('./auth/github');
 
/**
 * Repoman CLI module.
 * @module cli
 */
 
function _config(args) {
  const opts = {};
 
  if (args.removeExtraneous) {
    opts.removeExtraneous = true;
  }
 
  if (args.bitbucketAuthUser || args.bitbucketAuthPass) {
    opts.bitbucket = {
      authUser: args.bitbucketAuthUser,
      authPass: args.bitbucketAuthPass
    };
  } else if (
    args.githubUser ||
    args.githubOrg ||
    args.githubAuthUser ||
    args.githubAuthPass
  ) {
    opts.github = {
      user: args.githubUser,
      org: args.githubOrg,
      authUser: args.githubAuthUser,
      authPass: args.githubAuthPass,
      useSsh: args.githubUseSsh
    };
  } else if (args.local) {
    opts.local = {
      dir: process.cwd()
    };
  }
 
  new Repoman().config(opts, bag.exit);
}
 
function _add(args) {
  let type = args.type;
  let name = args.repositoryName || args.name;
  if (typeof name === 'function') {
    name = null;
  }
  const url = args.url;

  if (!!type && type !== 'git' && type !== 'svn') {
    console.error(`Unknown --type "${type}".`);
    process.exit(1);
  }
  if (!type) {
    console.log('No --type specified, will assume "git".');
    type = 'git';
  }
  if (!url) {
    console.error('You need to provide the mandatory parameter --url.');
    process.exit(1);
  }
  if (!name && type === 'git') {
    const nameMatch = /^.*\/(.*)\.git/.exec(url);
    if (nameMatch) {
      name = nameMatch[1];
      console.log(
        `You have given no --repository-name (or --name), I have tried to figure it out from the given --url and arrived at "${name}".`
      );
    }
  }
  if (!name) {
    console.error(
      'You have given no --repository-name (or --name) and I have not been able to figure it out from the given --url. Please provide the name explicitly.'
    );
    process.exit(1);
  }

  const opts = { verbose: args.parent.verbose };
  createRepomanInstance(args).add(type, name, url, opts, err => {
    if (err) {
      return bag.exit(err);
    } else {
      console.log(`${name}: ${url} has been added.`);
      return bag.exit(err);
    }
  });
}
 
function _remove(args) {
  let name = args.repositoryName || args.name;
  if (typeof name === 'function') {
    name = null;
  }

  if (!name) {
    console.error(
      'You need to provide the mandatory parameter --name (or --repository-name).'
    );
    process.exit(1);
  }

  if (args.force) {
    _reallyRemove(args, name);
  } else {
    prompts({
      type: 'confirm',
      name: 'confirmed',
      message: `This will delete the folder ${name} from your disk, are you sure you want to do this?`,
      initial: true
    }).then(response => {
      if (response && response.confirmed) {
        _reallyRemove(args, name);
      } else {
        process.exit(0);
      }
    });
  }
}
 
function _reallyRemove(args, name) {
  const opts = { verbose: args.parent.verbose };
  createRepomanInstance(args).remove(name, opts, err => {
    if (err) {
      return bag.exit(err);
    } else {
      console.log(`${name} has been removed.`);
      return bag.exit(err);
    }
  });
}
 
function _signin() {
  prompt.start();
  const authOpts = {};
  prompt.get(['Enter your Github username'], (err, result) => {
    authOpts.username = result['Enter your Github username'];
    prompt.get(['Enter your Github password'], (err, result) => {
      authOpts.password = result['Enter your Github password'];
      const ghAuth = new GithubAuth();
      ghAuth.authBasic(authOpts.username, authOpts.password);
      ghAuth.fetchAndStoreAuthToken(authOpts.twoFactor).catch(e => {
        if (
          e &&
          e.code === 401 &&
          e.message &&
          e.message.includes('two-factor')
        ) {
          prompt.get(
            ['Enter your Github two factor auth code'],
            (err, result) => {
              ghAuth.fetchAndStoreAuthToken(
                result['Enter your Github two factor auth code']
              );
            }
          );
        }
      });
    });
  });
}
 
function _exec(command, args) {
  const opts = {};
 
  // repoman exec provides command and args as function arguments
  // other 'exec' commands (e.g. init, get) only provides args as the only function argument
  if (!args) {
    args = command;
    command = args._name;
  }
 
  opts.failFast = args.parent.failFast;
  opts.verbose = args.parent.verbose;
  opts.regex = args.parent.regex;
  if (args.parent.tags) {
    opts.tags = args.parent.tags.split(',');
  }
 
  createRepomanInstance(args).exec(command, opts, bag.exit);
}
 
function _report(command, args) {
  const opts = {};

  if (!args) {
    args = command;
    command = args._name;
  }

  opts.verbose = args.parent.verbose;
  opts.regex = args.parent.regex;
  if (args.parent.tags) {
    opts.tags = args.parent.tags.split(',');
  }

  createRepomanInstance(args).report(opts, (err, result) => {
    if (err) {
      return bag.exit(err);
    } else {
      // write formatted report to stdout
      console.log(result);
      return bag.exit();
    }
  });
}
 
function _list(args) {
  const opts = {};
  opts.regex = args.parent.regex;
  if (args.parent.tags) {
    opts.tags = args.parent.tags.split(',');
  }
 
  const config = loadConfigFile(args);
  const repoman = new Repoman(config);
 
  repoman.list(
    opts,
    bag.exitCb(null, result => {
      result.forEach(name => {
        console.log(name);
      });
    })
  );
}
 
function _clean(args) {
  const config = loadConfigFile(args);
  const repoman = new Repoman(config);
 
  function _confirm() {
    prompt.start();
    prompt.get(['Are you sure? (Y/N)'], (err, result) => {
      let answer = result['Are you sure? (Y/N)'];
      answer = answer ? answer.toUpperCase() : 'N';
      if (answer === 'Y') {
        repoman.clean(false, bag.exit);
      } else {
        console.log('Nothing is deleted');
        process.exit(0);
      }
    });
  }
 
  repoman.clean(true, (err, result) => {
    if (!err) {
      if (!_.isEmpty(result)) {
        console.log(
          'The following files/directories will be deleted: %s',
          result.join(', ')
        );
        _confirm();
      } else {
        console.log('Nothing to delete');
        process.exit(0);
      }
    } else {
      bag.exit(err, result);
    }
  });
}
 
function createRepomanInstance(args) {
  const platform = args.parent.platform || process.platform;
  const scmsFile = platform === 'win32' ? 'scms-win32.json' : 'scms.json';
  const scms = JSON.parse(
    fs.readFileSync(p.join(__dirname, `../conf/${scmsFile}`))
  );
  const config = loadConfigFile(args);
  return new Repoman(config, scms);
}
 
function loadConfigFile(args) {
  /* jshint ignore:start */
  const configFile =
    args && args.parent && args.parent.configFile
      ? args.parent.configFile
      : '.repoman.json';
  /* jshint ignore:end */
  try {
    return JSON.parse(bag.lookupFile(configFile));
  } catch (e) {
    if (
      e.name === 'Error' &&
      e.message &&
      e.message.includes('Unable to lookup file in ')
    ) {
      console.error('Config file .repoman.json does not exist.');
      console.error(e.message);
      process.exit(1);
    } else {
      throw e;
    }
  }
}
 
function exec() {
  const actions = {
    commands: {
      config: { action: _config },
      add: { action: _add },
      remove: { action: _remove },
      delete: { action: _exec },
      init: { action: _exec },
      get: { action: _exec },
      changes: { action: _exec },
      report: { action: _report },
      save: { action: _exec },
      undo: { action: _exec },
      exec: { action: _exec },
      list: { action: _list },
      clean: { action: _clean },
      signin: { action: _signin }
    }
  };
 
  bag.command(__dirname, actions);
}
 
/**
 * Execute a Repoman command line command.
 */
exports.exec = exec;