UNPKG

ogit

Version:

A lazy developer's Git CLI made simple. Makes using git on cloud IDEs (i.e. C9) a walk in the park.

84 lines (83 loc) 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const command_1 = require("@oclif/command"); const inquirer = require("inquirer"); const git_1 = require("../wrapper/git"); class CloneRepoCommand extends command_1.Command { run() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { flags } = this.parse(CloneRepoCommand); if (flags.search) { inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt')); } let firstSetOfAnswers = {}; let answers = yield inquirer .prompt([ { message: 'Please enter the URL of the repo to clone', type: 'input', name: 'url' }, { message: 'Please enter the name of the directory to clone into (optional)', type: 'input', name: 'dirName' } ]) .then((answers) => tslib_1.__awaiter(this, void 0, void 0, function* () { firstSetOfAnswers = Object.assign({}, answers); if (flags.search || flags.list) { this.references = yield git_1.GitFacade.listRemoteReferences(answers.url); // console.log(references); return inquirer.prompt({ message: flags.search ? 'Name of the target branch/tag to clone' : 'Select of the target branch/tag to clone', source: (answers, input) => { const searchResults = this.references.filter(branch => branch.name.indexOf(input) > 0); return Promise.resolve(searchResults ? searchResults : answers); }, choices: () => { return this.references.map(ref => ref.name); }, type: flags.search ? 'autocomplete' : 'list', name: 'targetBranch', validate(choices) { return choices.length > 0; } }); } })); if (answers) { answers = Object.assign(answers, firstSetOfAnswers); } else { answers = firstSetOfAnswers; } // console.log(answers); if (!answers.dirName) { answers.dirName = answers.url.substring(answers.url.lastIndexOf('/') + 1, answers.url.lastIndexOf('.git')); } if (answers.targetBranch) { answers.targetBranch = answers.targetBranch.substring(answers.targetBranch.indexOf('/') + 1); } // console.log(answers); yield git_1.GitFacade.cloneRepo(answers.url, answers.dirName, answers.targetBranch); }); } } CloneRepoCommand.description = 'Clones a remote repo'; CloneRepoCommand.flags = { // can search --search or -s search: command_1.flags.boolean({ char: 's', description: 'Search through branches and tags' }), // list --list or -l list: command_1.flags.boolean({ char: 'l', description: 'List branches and tags' }) }; exports.CloneRepoCommand = CloneRepoCommand;