UNPKG

eas-cli

Version:
123 lines (122 loc) 5.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ensureBranchExistsAsync = exports.createUpdateBranchOnAppAsync = exports.listAndRenderBranchesOnAppAsync = exports.selectBranchOnAppAsync = exports.BRANCHES_LIMIT = void 0; const tslib_1 = require("tslib"); const chalk_1 = tslib_1.__importDefault(require("chalk")); const graphql_1 = require("graphql"); const graphql_tag_1 = tslib_1.__importDefault(require("graphql-tag")); const utils_1 = require("./utils"); const client_1 = require("../graphql/client"); const BranchQuery_1 = require("../graphql/queries/BranchQuery"); const UpdateBranchBasicInfo_1 = require("../graphql/types/UpdateBranchBasicInfo"); const log_1 = tslib_1.__importDefault(require("../log")); const utils_2 = require("../update/utils"); const json_1 = require("../utils/json"); const queries_1 = require("../utils/queries"); exports.BRANCHES_LIMIT = 50; async function selectBranchOnAppAsync(graphqlClient, { projectId, promptTitle, displayTextForListItem, paginatedQueryOptions, }) { if (paginatedQueryOptions.nonInteractive) { throw new Error('Unable to select a branch in non-interactive mode.'); } const selectedBranch = await (0, queries_1.paginatedQueryWithSelectPromptAsync)({ limit: paginatedQueryOptions.limit ?? exports.BRANCHES_LIMIT, offset: paginatedQueryOptions.offset, queryToPerform: (limit, offset) => queryBranchesOnProjectAsync(graphqlClient, limit, offset, projectId), promptOptions: { title: promptTitle, getIdentifierForQueryItem: updateBranchFragment => updateBranchFragment.id, makePartialChoiceObject: displayTextForListItem, }, }); if (!selectedBranch) { throw new Error(`Could not find any branches for project "${projectId}"`); } return selectedBranch; } exports.selectBranchOnAppAsync = selectBranchOnAppAsync; async function listAndRenderBranchesOnAppAsync(graphqlClient, { projectId, paginatedQueryOptions, }) { if (paginatedQueryOptions.nonInteractive) { const branches = await queryBranchesOnProjectAsync(graphqlClient, paginatedQueryOptions.limit ?? exports.BRANCHES_LIMIT, paginatedQueryOptions.offset, projectId); renderPageOfBranches(branches, paginatedQueryOptions); } else { await (0, queries_1.paginatedQueryWithConfirmPromptAsync)({ limit: paginatedQueryOptions.limit ?? exports.BRANCHES_LIMIT, offset: paginatedQueryOptions.offset, queryToPerform: (limit, offset) => queryBranchesOnProjectAsync(graphqlClient, limit, offset, projectId), promptOptions: { title: 'Load more branches?', renderListItems: branches => { renderPageOfBranches(branches, paginatedQueryOptions); }, }, }); } } exports.listAndRenderBranchesOnAppAsync = listAndRenderBranchesOnAppAsync; async function queryBranchesOnProjectAsync(graphqlClient, limit, offset, projectId) { return await BranchQuery_1.BranchQuery.listBranchesOnAppAsync(graphqlClient, { appId: projectId, limit, offset, }); } function renderPageOfBranches(currentPage, { json }) { if (json) { (0, json_1.printJsonOnlyOutput)(currentPage); } else { log_1.default.addNewLineIfNone(); log_1.default.log(chalk_1.default.bold('Branches:')); log_1.default.addNewLineIfNone(); log_1.default.log(currentPage .map(branch => (0, utils_2.formatBranch)((0, utils_2.getBranchDescription)(branch))) .join(`\n\n${chalk_1.default.dim('———')}\n\n`)); } } async function createUpdateBranchOnAppAsync(graphqlClient, { appId, name }) { const result = await (0, client_1.withErrorHandlingAsync)(graphqlClient .mutation((0, graphql_tag_1.default) ` mutation CreateUpdateBranchForApp($appId: ID!, $name: String!) { updateBranch { createUpdateBranchForApp(appId: $appId, name: $name) { id ...UpdateBranchBasicInfoFragment } } } ${(0, graphql_1.print)(UpdateBranchBasicInfo_1.UpdateBranchBasicInfoFragmentNode)} `, { appId, name, }, { additionalTypenames: ['UpdateBranch'] }) .toPromise()); const newBranch = result.updateBranch.createUpdateBranchForApp; if (!newBranch) { throw new Error(`Could not create branch ${name}.`); } return newBranch; } exports.createUpdateBranchOnAppAsync = createUpdateBranchOnAppAsync; async function ensureBranchExistsAsync(graphqlClient, { appId, branchName, }) { try { const updateBranch = await BranchQuery_1.BranchQuery.getBranchByNameAsync(graphqlClient, { appId, name: branchName, }); return { branch: updateBranch, createdBranch: false }; } catch (error) { if (error instanceof utils_1.BranchNotFoundError) { const newUpdateBranch = await createUpdateBranchOnAppAsync(graphqlClient, { appId, name: branchName, }); return { branch: newUpdateBranch, createdBranch: true }; } else { throw error; } } } exports.ensureBranchExistsAsync = ensureBranchExistsAsync;