UNPKG

tyr-cli

Version:

A command line interface for hammer-io.

149 lines (126 loc) 10.9 kB
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.generateGithubFiles = exports.createGitHubRepository = exports.getUserRepositories = exports.isValidCredentials = undefined; /** * Checks if the user's github credentials are valid by requesting account information. * @param username the username * @param password the password * @returns {Boolean} true if valid, false if invalid, throws error if something went wrong * connecting to the api */let isValidCredentials = exports.isValidCredentials = (() => {var _ref = _asyncToGenerator( function* (username, password) { try { yield githubClient.getCurrentUser(username, password); return true; } catch (error) { if (error.status !== 401) { throw new Error('Something went wrong contacting the GitHub API!'); } else { return false; } } });return function isValidCredentials(_x, _x2) {return _ref.apply(this, arguments);};})(); /** * Gets the repositories for the given user * @param username the username of the user * @param password the password for the user * @returns {Promise<>} */let getUserRepositories = exports.getUserRepositories = (() => {var _ref2 = _asyncToGenerator( function* (username, password) { let repos = []; let pageNumber = 1; let done = false; while (!done) { const githubRepos = yield githubClient.getRepositories(username, password, pageNumber); if (githubRepos.length === 0) { done = true; } else { repos = repos.concat(githubRepos); pageNumber += 1; } } return repos; });return function getUserRepositories(_x3, _x4) {return _ref2.apply(this, arguments);};})(); /** * Checks if the given repository name is a valid name. A repository name is valid if the name * does not already exist as a repository for the user on github. * @param repositoryName the repository name to check * @param repositories the github repositories for the user * @returns {Boolean} true if the name does not exist as a repository, false if it does. */ /** * Creates a github repository * @param repositoryName the name of the repository to create * @param repositoryDescription the description of the repository * @param credentials the password, username, and/or token of the user * @param isPrivate flag if the project should be private or not * @returns {Promise<void>} */let createGitHubRepository = exports.createGitHubRepository = (() => {var _ref3 = _asyncToGenerator( function* ( repositoryName, repositoryDescription, credentials, isPrivate) { log.verbose(`Creating GitHub repository ${credentials.username}/${repositoryName}.`); try { yield githubClient.createRepository( repositoryName, repositoryDescription, credentials, isPrivate); log.info(`Successfully created GitHub repository ${credentials.username}/${repositoryName}.`); } catch (error) { const errorMessage = 'Failed to create GitHub Repository!'; log.debug(errorMessage, { status: error.status, message: error.message }); throw new Error(errorMessage); } });return function createGitHubRepository(_x5, _x6, _x7, _x8) {return _ref3.apply(this, arguments);};})(); /** * Generates the necessary git files, including .gitignore * @param projectPath the newly created project's file path * @returns {Promise<void>} */let generateGithubFiles = exports.generateGithubFiles = (() => {var _ref4 = _asyncToGenerator( function* (projectPath) { log.verbose('Generating files for GitHub.'); const path = `${projectPath}/.gitignore`; const contents = file.loadTemplate('./../../templates/git/gitignore'); file.writeFile(path, contents); log.info(`Successfully generated file: ${path}`); });return function generateGithubFiles(_x9) {return _ref4.apply(this, arguments);};})(); /** * Init the git repository, add all the files, make the first commit, * add the remote origin, and push origin to master. * * @param credentials the github credentials object * @param projectName the project's name * @param filePath the filePath where the project files are */exports.isValidGithubRepositoryName = isValidGithubRepositoryName;exports. initAddCommitAndPush = initAddCommitAndPush;var _simpleGit = require('simple-git');var _simpleGit2 = _interopRequireDefault(_simpleGit);var _githubClient = require('../clients/github-client');var githubClient = _interopRequireWildcard(_githubClient);var _file = require('../utils/file');var file = _interopRequireWildcard(_file);var _winston = require('../utils/winston');function _interopRequireWildcard(obj) {if (obj && obj.__esModule) {return obj;} else {var newObj = {};if (obj != null) {for (var key in obj) {if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key];}}newObj.default = obj;return newObj;}}function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _asyncToGenerator(fn) {return function () {var gen = fn.apply(this, arguments);return new Promise(function (resolve, reject) {function step(key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {return Promise.resolve(value).then(function (value) {step("next", value);}, function (err) {step("throw", err);});}}return step("next");});};} /* eslint-disable import/prefer-default-export,no-await-in-loop,prefer-destructuring */const log = (0, _winston.getActiveLogger)();function isValidGithubRepositoryName(repositoryName, repositories) {return repositories.filter(repo => repo.name === repositoryName).length === 0;}function initAddCommitAndPush(credentials, projectName, filePath) { log.verbose('Github Service - initAddCommitAndPush()'); const username = credentials.username; let secret = credentials.token; if (!secret) { if (!credentials.password || credentials.password.includes('/')) { throw new Error('Github password cannot contain backward slashes'); } secret = credentials.password; } const uri = `https://${username}:${secret}@github.com/${username}/${projectName}.git`; return new Promise(resolve => { (0, _simpleGit2.default)(`${filePath}/${projectName}`). init(). silent(). addConfig('user.name', 'hammer-io'). addConfig('user.email', 'hammer.io.team@gmail.com'). add('.gitignore'). add('./*'). commit('Initial Commit w/ :heart: by @hammer-io.'). addRemote('origin', uri). push('origin', 'master'). exec(() => { log.warn('Please wait while files are pushed to GitHub...'); setTimeout(() => { resolve(); }, 10000); // TODO: Find a better way to do this than a timeout }); }); }