tyr-cli
Version:
A command line interface for hammer-io.
192 lines (141 loc) • 11.3 kB
JavaScript
;Object.defineProperty(exports, "__esModule", { value: true });exports.run = exports.validateProjectNameAgainstGithubRepositories = exports.signInToGithub = exports.signInToHeroku = undefined;
/**
* Gets the users project configurations by triggering the prompt
* @returns {Object} the user's project configurations
*/let getProjectConfigurations = (() => {var _ref = _asyncToGenerator(
function* () {
const configs = yield prompt.promptForProjectConfigurations();
return configs;
});return function getProjectConfigurations() {return _ref.apply(this, arguments);};})();
/**
* Gets the users tooling configurations by triggering the prompt
* @returns {Object} the user's tooling configurations
*/let getToolingConfigurations = (() => {var _ref2 = _asyncToGenerator(
function* () {
const configs = yield prompt.promptForToolingConfigurations();
return configs;
});return function getToolingConfigurations() {return _ref2.apply(this, arguments);};})();
/**
* Function which facilitates signing in to heroku. Gets the credentials, validates the
* credentials, and if they are invalid, prompts for new credentials.
* @returns {Object} the heroku credentials as a email and password key/value pair
*/let signInToHeroku = exports.signInToHeroku = (() => {var _ref3 = _asyncToGenerator(
function* () {
const credentials = yield prompt.promptForHerokuCredentials();
const isValid = yield herokuService.isValidCredentials(credentials.email, credentials.password);
if (!isValid) {
log.error('Incorrect Email and/or Password!');
yield signInToHeroku();
}
return credentials;
});return function signInToHeroku() {return _ref3.apply(this, arguments);};})();
/**
* Function which facilitates signing in to github. Gets the credentials, validates the
* credentials, and if they are invalid, prompts for new credentials.
* @returns {Object} the github credentials as a username and password key/value pair
*/let signInToGithub = exports.signInToGithub = (() => {var _ref4 = _asyncToGenerator(
function* () {
let credentials = yield prompt.promptForGithubCredentials();
const isValid = yield githubService.isValidCredentials(
credentials.username,
credentials.password);
if (!isValid) {
log.error('Incorrect Username and/or Password!');
credentials = yield signInToGithub();
}
return credentials;
});return function signInToGithub() {return _ref4.apply(this, arguments);};})();
// If the user needs to sign in to third party tools, then a key/value pair should go in this
// constant. The key should be the tool's name (all lowercase) and the value should be a function
// which should handle the sign in process.
/**
* Function which facilitates signing in to third party tools
* @param toolingConfigs the tooling configurations
* @returns {Object} the credentials for the third party tools
*/let signInToThirdPartyTools = (() => {var _ref5 = _asyncToGenerator(
function* (toolingConfigs) {
const credentials = {};
for (const key of Object.keys(toolingConfigs)) {
const tool = toolingConfigs[key];
if (thirdPartyTools[tool.toLowerCase()]) {
credentials[tool.toLowerCase()] = yield thirdPartyTools[tool.toLowerCase()]();
}
}
return credentials;
});return function signInToThirdPartyTools(_x) {return _ref5.apply(this, arguments);};})();
/**
* Get the configurations from a file
* @param configFile the path to the configuration file
* @returns {Object} the configurations
*/
/**
* Get the configuration from a prompt
* @returns {Object} the project configurations
*/let getConfigurationsFromPrompt = (() => {var _ref6 = _asyncToGenerator(
function* () {
const configurations = {};
const projectConfigurations = yield getProjectConfigurations();
const toolingConfigurations = yield getToolingConfigurations();
configurations.projectConfigurations = projectConfigurations;
configurations.toolingConfigurations = toolingConfigurations;
return configurations;
});return function getConfigurationsFromPrompt() {return _ref6.apply(this, arguments);};})();let validateProjectNameAgainstGithubRepositories = exports.validateProjectNameAgainstGithubRepositories = (() => {var _ref7 = _asyncToGenerator(
function* (configurations) {
// validate github project name to make sure it doesn't exist
const repositories = yield githubService.getUserRepositories(
configurations.credentials.github.username,
configurations.credentials.github.password);
const isValid = githubService.isValidGithubRepositoryName(
configurations.projectConfigurations.projectName,
repositories);
// if the name is not valid, then reprompt for a new name.
if (!isValid) {
log.error('GitHub repository with this name already exists.');
const newName = yield prompt.repromptForProjectName(repositories);
return newName;
}
return configurations.projectConfigurations.projectName;
});return function validateProjectNameAgainstGithubRepositories(_x2) {return _ref7.apply(this, arguments);};})();
/**
* The main run function
* @param configFile path to the configuration file
* @param logFile path to the logfile
*/let run = exports.run = (() => {var _ref8 = _asyncToGenerator(
function* (configFile, logFile) {
if (logFile) {
(0, _winston.enableLogFile)(logFile);
}
let configurations = {};
// parse from the config file that was passed in
try {
if (configFile) {
configurations = getConfigurationsFromFile(configFile);
// if something goes wrong, get configs from the prompt
if (!configurations) {
configurations = yield getConfigurationsFromPrompt();
}
// no config file, that means we should get configs from the prompt
} else {
configurations = yield getConfigurationsFromPrompt();
}
} catch (error) {
log.error('Unable to get configurations. Exiting tyr.');
return;
}
// sign in to third party tools
try {
const credentials = yield signInToThirdPartyTools(configurations.toolingConfigurations);
configurations.credentials = credentials;
} catch (error) {
log.error(`${error.message}. Exiting tyr.`);
return;
}
if (configurations.toolingConfigurations.sourceControl === 'GitHub') {
configurations.projectConfigurations.projectName =
yield validateProjectNameAgainstGithubRepositories(configurations);
}
yield tyr.generateProject(configurations);
// npm install
yield nodeService.npmInstall(configurations.projectConfigurations.projectName);
log.info('>>> Finished!');
});return function run(_x3, _x4) {return _ref8.apply(this, arguments);};})();var _prompt = require('./prompt/prompt');var prompt = _interopRequireWildcard(_prompt);var _winston = require('./utils/winston');var _tyr = require('./tyr');var tyr = _interopRequireWildcard(_tyr);var _projectConfigurationService = require('./services/project-configuration-service');var _githubService = require('./services/github-service');var githubService = _interopRequireWildcard(_githubService);var _herokuService = require('./services/heroku-service');var herokuService = _interopRequireWildcard(_herokuService);var _nodeService = require('./services/node-service');var nodeService = _interopRequireWildcard(_nodeService);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 _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 no-await-in-loop,no-restricted-syntax */const log = (0, _winston.getActiveLogger)();const thirdPartyTools = { heroku: signInToHeroku, github: signInToGithub };function getConfigurationsFromFile(configFile) {let configurations = {};try {configurations = (0, _projectConfigurationService.parseConfigsFromFile)(configFile);return configurations;} catch (error) {log.error('Failed to parse from configuration file', error);}}