eas-cli 3.17.1 → 3.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +55 -55
  2. package/build/branch/actions/SelectBranch.d.ts +33 -0
  3. package/build/branch/actions/SelectBranch.js +67 -0
  4. package/build/build/android/build.js +1 -2
  5. package/build/build/local.js +1 -1
  6. package/build/build/runBuildAndSubmit.js +2 -0
  7. package/build/channel/actions/SelectChannel.d.ts +33 -0
  8. package/build/channel/actions/SelectChannel.js +67 -0
  9. package/build/channel/branch-mapping.d.ts +20 -0
  10. package/build/channel/branch-mapping.js +75 -5
  11. package/build/channel/queries.d.ts +1 -6
  12. package/build/channel/queries.js +1 -25
  13. package/build/commands/build/version/get.js +1 -0
  14. package/build/commands/build/version/sync.js +1 -0
  15. package/build/commands/channel/rollout-preview.d.ts +32 -0
  16. package/build/commands/channel/rollout-preview.js +109 -0
  17. package/build/commands/metadata/lint.js +1 -0
  18. package/build/commands/metadata/pull.js +1 -0
  19. package/build/commands/metadata/push.js +1 -0
  20. package/build/commands/submit.js +1 -0
  21. package/build/commands/update/republish.js +23 -74
  22. package/build/eas-update/utils.d.ts +15 -0
  23. package/build/eas-update/utils.js +6 -0
  24. package/build/graphql/generated.d.ts +192 -18
  25. package/build/graphql/generated.js +8 -2
  26. package/build/graphql/queries/BranchQuery.d.ts +3 -2
  27. package/build/graphql/queries/BranchQuery.js +43 -1
  28. package/build/graphql/queries/ChannelQuery.d.ts +3 -2
  29. package/build/graphql/queries/ChannelQuery.js +19 -5
  30. package/build/graphql/queries/RuntimeQuery.d.ts +6 -0
  31. package/build/graphql/queries/RuntimeQuery.js +70 -0
  32. package/build/graphql/types/Runtime.d.ts +1 -0
  33. package/build/graphql/types/Runtime.js +11 -0
  34. package/build/graphql/types/UpdateBranch.js +3 -1
  35. package/build/graphql/types/UpdateBranchBasicInfo.d.ts +1 -0
  36. package/build/graphql/types/UpdateBranchBasicInfo.js +11 -0
  37. package/build/project/ios/target.d.ts +1 -1
  38. package/build/project/ios/target.js +4 -4
  39. package/build/rollout/actions/CreateRollout.d.ts +23 -0
  40. package/build/rollout/actions/CreateRollout.js +153 -0
  41. package/build/rollout/actions/EditRollout.d.ts +17 -0
  42. package/build/rollout/actions/EditRollout.js +79 -0
  43. package/build/rollout/actions/EndRollout.d.ts +24 -0
  44. package/build/rollout/actions/EndRollout.js +164 -0
  45. package/build/rollout/actions/ManageRollout.d.ts +24 -0
  46. package/build/rollout/actions/ManageRollout.js +78 -0
  47. package/build/rollout/actions/NonInteractiveRollout.d.ts +18 -0
  48. package/build/rollout/actions/NonInteractiveRollout.js +46 -0
  49. package/build/rollout/actions/RolloutMainMenu.d.ts +28 -0
  50. package/build/rollout/actions/RolloutMainMenu.js +110 -0
  51. package/build/rollout/actions/SelectRollout.d.ts +8 -0
  52. package/build/rollout/actions/SelectRollout.js +33 -0
  53. package/build/rollout/actions/SelectRuntime.d.ts +36 -0
  54. package/build/rollout/actions/SelectRuntime.js +167 -0
  55. package/build/rollout/branch-mapping.d.ts +128 -0
  56. package/build/rollout/branch-mapping.js +260 -0
  57. package/build/rollout/utils.d.ts +10 -53
  58. package/build/rollout/utils.js +78 -94
  59. package/build/update/configure.d.ts +6 -1
  60. package/build/update/configure.js +18 -8
  61. package/build/update/republish.d.ts +26 -0
  62. package/build/update/republish.js +83 -0
  63. package/build/utils/profiles.d.ts +2 -1
  64. package/build/utils/profiles.js +35 -3
  65. package/build/utils/relay.d.ts +80 -15
  66. package/build/utils/relay.js +211 -28
  67. package/oclif.manifest.json +1 -1
  68. package/package.json +5 -5
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.republishAsync = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const assert_1 = tslib_1.__importDefault(require("assert"));
6
+ const url_1 = require("../build/utils/url");
7
+ const PublishMutation_1 = require("../graphql/mutations/PublishMutation");
8
+ const log_1 = tslib_1.__importStar(require("../log"));
9
+ const ora_1 = require("../ora");
10
+ const projectUtils_1 = require("../project/projectUtils");
11
+ const formatFields_1 = tslib_1.__importDefault(require("../utils/formatFields"));
12
+ const json_1 = require("../utils/json");
13
+ /**
14
+ * @param updatesToPublish The update group to republish
15
+ * @param targetBranch The branch to repubish the update group on
16
+ */
17
+ async function republishAsync({ graphqlClient, app, updatesToPublish, targetBranch, updateMessage, json, }) {
18
+ const { branchName: targetBranchName, branchId: targetBranchId } = targetBranch;
19
+ // The update group properties are the same for all updates
20
+ (0, assert_1.default)(updatesToPublish.length > 0, 'Updates to republish must be provided');
21
+ const arbitraryUpdate = updatesToPublish[0];
22
+ const isSameGroup = (update) => update.groupId === arbitraryUpdate.groupId &&
23
+ update.branchId === arbitraryUpdate.branchId &&
24
+ update.branchName === arbitraryUpdate.branchName &&
25
+ update.runtimeVersion === arbitraryUpdate.runtimeVersion;
26
+ (0, assert_1.default)(updatesToPublish.every(isSameGroup), 'All updates must belong to the same update group');
27
+ const { runtimeVersion } = arbitraryUpdate;
28
+ // If codesigning was created for the original update, we need to add it to the republish
29
+ const shouldRepublishWithCodesigning = updatesToPublish.some(update => update.codeSigningInfo);
30
+ if (shouldRepublishWithCodesigning) {
31
+ log_1.default.withTick(`The republished update will be signed with the same codesigning as the original update.`);
32
+ }
33
+ const publishIndicator = (0, ora_1.ora)('Republishing...').start();
34
+ let updatesRepublished;
35
+ try {
36
+ updatesRepublished = await PublishMutation_1.PublishMutation.publishUpdateGroupAsync(graphqlClient, [
37
+ {
38
+ branchId: targetBranchId,
39
+ runtimeVersion,
40
+ message: updateMessage,
41
+ updateInfoGroup: Object.fromEntries(updatesToPublish.map(update => [update.platform, JSON.parse(update.manifestFragment)])),
42
+ gitCommitHash: updatesToPublish[0].gitCommitHash,
43
+ awaitingCodeSigningInfo: shouldRepublishWithCodesigning,
44
+ },
45
+ ]);
46
+ if (shouldRepublishWithCodesigning) {
47
+ const codeSigningByPlatform = Object.fromEntries(updatesToPublish.map(update => [update.platform, update.codeSigningInfo]));
48
+ await Promise.all(updatesRepublished.map(async (update) => {
49
+ const codeSigning = codeSigningByPlatform[update.platform];
50
+ if (codeSigning) {
51
+ await PublishMutation_1.PublishMutation.setCodeSigningInfoAsync(graphqlClient, update.id, codeSigning);
52
+ }
53
+ }));
54
+ }
55
+ publishIndicator.succeed('Republished update');
56
+ }
57
+ catch (error) {
58
+ publishIndicator.fail('Failed to republish update');
59
+ throw error;
60
+ }
61
+ if (json) {
62
+ return (0, json_1.printJsonOnlyOutput)(updatesRepublished);
63
+ }
64
+ const updatesRepublishedByPlatform = Object.fromEntries(updatesRepublished.map(update => [update.platform, update]));
65
+ const arbitraryRepublishedUpdate = updatesRepublished[0];
66
+ const updateGroupUrl = (0, url_1.getUpdateGroupUrl)((await (0, projectUtils_1.getOwnerAccountForProjectIdAsync)(graphqlClient, app.projectId)).name, app.exp.slug, arbitraryRepublishedUpdate.group);
67
+ log_1.default.addNewLineIfNone();
68
+ log_1.default.log((0, formatFields_1.default)([
69
+ { label: 'Branch', value: targetBranchName },
70
+ { label: 'Runtime version', value: arbitraryRepublishedUpdate.runtimeVersion },
71
+ { label: 'Platform', value: updatesRepublished.map(update => update.platform).join(', ') },
72
+ { label: 'Update Group ID', value: arbitraryRepublishedUpdate.group },
73
+ ...(updatesRepublishedByPlatform.android
74
+ ? [{ label: 'Android update ID', value: updatesRepublishedByPlatform.android.id }]
75
+ : []),
76
+ ...(updatesRepublishedByPlatform.ios
77
+ ? [{ label: 'iOS update ID', value: updatesRepublishedByPlatform.ios.id }]
78
+ : []),
79
+ { label: 'Message', value: updateMessage },
80
+ { label: 'Website link', value: (0, log_1.link)(updateGroupUrl, { dim: false }) },
81
+ ]));
82
+ }
83
+ exports.republishAsync = republishAsync;
@@ -6,10 +6,11 @@ export type ProfileData<T extends ProfileType> = {
6
6
  platform: Platform;
7
7
  profileName: string;
8
8
  };
9
- export declare function getProfilesAsync<T extends ProfileType>({ easJsonAccessor, platforms, profileName, type, }: {
9
+ export declare function getProfilesAsync<T extends ProfileType>({ easJsonAccessor, platforms, profileName, type, projectDir, }: {
10
10
  easJsonAccessor: EasJsonAccessor;
11
11
  platforms: Platform[];
12
12
  profileName?: string;
13
+ projectDir: string;
13
14
  type: T;
14
15
  }): Promise<ProfileData<T>[]>;
15
16
  /**
@@ -3,14 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.maybePrintBuildProfileDeprecationWarningsAsync = exports.clearHasPrintedDeprecationWarnings = exports.getProfilesAsync = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const eas_json_1 = require("@expo/eas-json");
6
+ const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
7
+ const path_1 = tslib_1.__importDefault(require("path"));
8
+ const semver_1 = tslib_1.__importDefault(require("semver"));
6
9
  const log_1 = tslib_1.__importStar(require("../log"));
7
- async function getProfilesAsync({ easJsonAccessor, platforms, profileName, type, }) {
10
+ async function getProfilesAsync({ easJsonAccessor, platforms, profileName, type, projectDir, }) {
8
11
  const results = platforms.map(async function (platform) {
9
- const profile = await readProfileAsync({
12
+ const profile = await readProfileWithOverridesAsync({
10
13
  easJsonAccessor,
11
14
  platform,
12
15
  type,
13
16
  profileName,
17
+ projectDir,
14
18
  });
15
19
  return {
16
20
  profile,
@@ -21,10 +25,38 @@ async function getProfilesAsync({ easJsonAccessor, platforms, profileName, type,
21
25
  return await Promise.all(results);
22
26
  }
23
27
  exports.getProfilesAsync = getProfilesAsync;
24
- async function readProfileAsync({ easJsonAccessor, platform, type, profileName, }) {
28
+ async function setNodeVersionFromFileAsync(projectDir, profile) {
29
+ if (profile === null || profile === void 0 ? void 0 : profile.node) {
30
+ return;
31
+ }
32
+ const nodeVersion = await getNodeVersionFromFileAsync(projectDir);
33
+ if (nodeVersion) {
34
+ log_1.default.log(`The EAS build profile does not specify a Node.js version. Using the version specified in .nvmrc: ${nodeVersion} `);
35
+ profile.node = nodeVersion;
36
+ }
37
+ }
38
+ async function getNodeVersionFromFileAsync(projectDir) {
39
+ const nvmrcPath = path_1.default.join(projectDir, '.nvmrc');
40
+ if (!(await fs_extra_1.default.pathExists(nvmrcPath))) {
41
+ return;
42
+ }
43
+ let nodeVersion;
44
+ try {
45
+ nodeVersion = (await fs_extra_1.default.readFile(nvmrcPath, 'utf8')).toString().trim();
46
+ }
47
+ catch {
48
+ return undefined;
49
+ }
50
+ if (!semver_1.default.valid(nodeVersion)) {
51
+ throw new Error(`Invalid node version in .nvmrc: ${nodeVersion}`);
52
+ }
53
+ return nodeVersion;
54
+ }
55
+ async function readProfileWithOverridesAsync({ easJsonAccessor, platform, type, profileName, projectDir, }) {
25
56
  if (type === 'build') {
26
57
  const buildProfile = await eas_json_1.EasJsonUtils.getBuildProfileAsync(easJsonAccessor, platform, profileName);
27
58
  await maybePrintBuildProfileDeprecationWarningsAsync(easJsonAccessor, platform, profileName);
59
+ await setNodeVersionFromFileAsync(projectDir, buildProfile);
28
60
  return buildProfile;
29
61
  }
30
62
  else {
@@ -3,31 +3,96 @@ export type Connection<T> = {
3
3
  edges: Edge<T>[];
4
4
  pageInfo: PageInfo;
5
5
  };
6
- type Edge<T> = {
6
+ export type Edge<T> = {
7
+ cursor: string;
7
8
  node: T;
8
9
  };
9
10
  export type QueryParams = {
10
- first: number;
11
+ first?: number;
11
12
  after?: string;
13
+ last?: number;
14
+ before?: string;
12
15
  };
13
16
  /**
14
- * Fetches dataset in paginated manner (batch by batch) using GraphQL queries.
15
17
  *
18
+ * Pagination that performs client side filtering on the nodes returned from a relay compliant datasource.
19
+ *
20
+ * @param queryParams The query params for the pagination.
16
21
  * @param queryAsync A promise based function for querying.
22
+ * @param filterPredicate A predicate function to filter the node.
23
+ * @param beforeEachQuery Optional. A callback function to be called before each query
24
+ * @param afterEachQuery Optional. A callback function to be called after each query.
25
+ * @param internalBatchSize Optional. The batch size of queryAsync. Defaults to 100.
26
+ * @param maxNodesFetched Optional. The maximum number of nodes to fetch. Defaults to 10_000.
17
27
  * @param beforeEachQuery Optional. A callback function to be called before each query
28
+ * @args externalQueryParams The query params for the pagination.
29
+ * @args totalNodesFetched The total number of nodes fetched so far.
30
+ * @args dataset The dataset so far.
18
31
  * @param afterEachQuery Optional. A callback function to be called after each query.
19
- * @param filterPredicate Optional. A predicate function to filter the node.
20
- * @param batchSize Optional. The batch size of the pagination. Defaults to 100.
32
+ * @args externalQueryParams The query params for the pagination.
33
+ * @args totalNodesFetched The total number of nodes fetched so far.
34
+ * @args dataset The dataset so far.
35
+ * @args willFetchAgain If the query will fetch again to get a complete page.
21
36
  *
22
- * @return {Promise<T[]>} - A promise that resolves to an array (the dataset).
23
37
  * @throws {Error} - If an error occurs during execution of the query or pagination.
24
38
  */
25
- export declare function getPaginatedDatasetAsync<T>({ queryAsync, beforeEachQuery, afterEachQuery, filterPredicate, batchSize, maxNodesFetched, }: {
26
- queryAsync: ({ first, after }: QueryParams) => Promise<Connection<T>>;
27
- beforeEachQuery?: (totalNodesFetched: number, dataset: T[]) => void;
28
- afterEachQuery?: (totalNodesFetched: number, dataset: T[], batch: T[], pageInfo: PageInfo) => void;
29
- filterPredicate?: (node: T) => boolean;
30
- batchSize?: number;
31
- maxNodesFetched?: number;
32
- }): Promise<T[]>;
33
- export {};
39
+ export declare class FilterPagination {
40
+ static getPageAsync<T>({ queryParams, queryAsync, filterPredicate, internalBatchSize, maxNodesFetched, beforeEachQuery, afterEachQuery, }: {
41
+ queryParams: QueryParams;
42
+ queryAsync: (queryParams: QueryParams) => Promise<Connection<T>>;
43
+ filterPredicate: (node: T) => boolean;
44
+ internalBatchSize?: number;
45
+ maxNodesFetched?: number;
46
+ beforeEachQuery?: (externalQueryParams: QueryParams, totalNodesFetched: number, dataset: Edge<T>[]) => void;
47
+ afterEachQuery?: (externalQueryParams: QueryParams, totalNodesFetched: number, dataset: Edge<T>[], willFetchAgain: boolean) => void;
48
+ }): Promise<Connection<T>>;
49
+ static isFirstAfter(connectionArgs: QueryParams): connectionArgs is {
50
+ first: number;
51
+ after?: string;
52
+ };
53
+ static isLastBefore(connectionArgs: {
54
+ first?: number;
55
+ after?: string;
56
+ last?: number;
57
+ before?: string;
58
+ }): connectionArgs is {
59
+ last: number;
60
+ before?: string;
61
+ };
62
+ static getFirstItemsAsync<T>({ first, after }: {
63
+ first: number;
64
+ after?: string;
65
+ }, { internalBatchSize, maxNodesFetched, filterPredicate, queryAsync, beforeEachQuery, afterEachQuery, }: {
66
+ internalBatchSize?: number;
67
+ maxNodesFetched: number;
68
+ filterPredicate: (node: T) => boolean;
69
+ queryAsync: (queryParams: QueryParams) => Promise<Connection<T>>;
70
+ beforeEachQuery?: (externalQueryParams: QueryParams, totalNodesFetched: number, dataset: Edge<T>[]) => void;
71
+ afterEachQuery?: (externalQueryParams: QueryParams, totalNodesFetched: number, dataset: Edge<T>[], willFetchAgain: boolean) => void;
72
+ }): Promise<Connection<T>>;
73
+ static getLastItemsAsync<T>({ last, before }: {
74
+ last: number;
75
+ before?: string;
76
+ }, { internalBatchSize, maxNodesFetched, filterPredicate, queryAsync, beforeEachQuery, afterEachQuery, }: {
77
+ internalBatchSize?: number;
78
+ maxNodesFetched: number;
79
+ filterPredicate: (node: T) => boolean;
80
+ queryAsync: (queryParams: QueryParams) => Promise<Connection<T>>;
81
+ beforeEachQuery?: (externalQueryParams: QueryParams, totalNodesFetched: number, dataset: Edge<T>[]) => void;
82
+ afterEachQuery?: (externalQueryParams: QueryParams, totalNodesFetched: number, dataset: Edge<T>[], willFetchAgain: boolean) => void;
83
+ }): Promise<Connection<T>>;
84
+ }
85
+ export declare function selectPaginatedAsync<T>({ queryAsync, getTitleAsync, printedType, pageSize, }: {
86
+ pageSize: number;
87
+ queryAsync: (queryParams: QueryParams) => Promise<Connection<T>>;
88
+ getTitleAsync: (node: T) => Promise<string>;
89
+ printedType: string;
90
+ }): Promise<T | null>;
91
+ export declare const PREV_PAGE_OPTION: {
92
+ value: symbol;
93
+ title: string;
94
+ };
95
+ export declare const NEXT_PAGE_OPTION: {
96
+ value: symbol;
97
+ title: string;
98
+ };
@@ -1,43 +1,226 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPaginatedDatasetAsync = void 0;
3
+ exports.NEXT_PAGE_OPTION = exports.PREV_PAGE_OPTION = exports.selectPaginatedAsync = exports.FilterPagination = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const assert_1 = tslib_1.__importDefault(require("assert"));
6
+ const prompts_1 = require("../prompts");
4
7
  /**
5
- * Fetches dataset in paginated manner (batch by batch) using GraphQL queries.
6
8
  *
9
+ * Pagination that performs client side filtering on the nodes returned from a relay compliant datasource.
10
+ *
11
+ * @param queryParams The query params for the pagination.
7
12
  * @param queryAsync A promise based function for querying.
13
+ * @param filterPredicate A predicate function to filter the node.
14
+ * @param beforeEachQuery Optional. A callback function to be called before each query
15
+ * @param afterEachQuery Optional. A callback function to be called after each query.
16
+ * @param internalBatchSize Optional. The batch size of queryAsync. Defaults to 100.
17
+ * @param maxNodesFetched Optional. The maximum number of nodes to fetch. Defaults to 10_000.
8
18
  * @param beforeEachQuery Optional. A callback function to be called before each query
19
+ * @args externalQueryParams The query params for the pagination.
20
+ * @args totalNodesFetched The total number of nodes fetched so far.
21
+ * @args dataset The dataset so far.
9
22
  * @param afterEachQuery Optional. A callback function to be called after each query.
10
- * @param filterPredicate Optional. A predicate function to filter the node.
11
- * @param batchSize Optional. The batch size of the pagination. Defaults to 100.
23
+ * @args externalQueryParams The query params for the pagination.
24
+ * @args totalNodesFetched The total number of nodes fetched so far.
25
+ * @args dataset The dataset so far.
26
+ * @args willFetchAgain If the query will fetch again to get a complete page.
12
27
  *
13
- * @return {Promise<T[]>} - A promise that resolves to an array (the dataset).
14
28
  * @throws {Error} - If an error occurs during execution of the query or pagination.
15
29
  */
16
- async function getPaginatedDatasetAsync({ queryAsync, beforeEachQuery, afterEachQuery, filterPredicate, batchSize = 100, maxNodesFetched = 10000, }) {
17
- var _a;
18
- const dataset = [];
19
- let hasMore = true;
20
- let after;
21
- let totalNodesFetched = 0;
22
- while (hasMore) {
23
- if (beforeEachQuery) {
24
- beforeEachQuery(totalNodesFetched, dataset);
30
+ class FilterPagination {
31
+ static async getPageAsync({ queryParams, queryAsync, filterPredicate, internalBatchSize = 100, maxNodesFetched = 10000, beforeEachQuery, afterEachQuery, }) {
32
+ if (this.isFirstAfter(queryParams)) {
33
+ return await this.getFirstItemsAsync(queryParams, {
34
+ queryAsync,
35
+ filterPredicate,
36
+ internalBatchSize,
37
+ maxNodesFetched,
38
+ beforeEachQuery,
39
+ afterEachQuery,
40
+ });
41
+ }
42
+ else if (this.isLastBefore(queryParams)) {
43
+ return await this.getLastItemsAsync(queryParams, {
44
+ queryAsync,
45
+ filterPredicate,
46
+ internalBatchSize,
47
+ maxNodesFetched,
48
+ beforeEachQuery,
49
+ afterEachQuery,
50
+ });
25
51
  }
26
- const result = await queryAsync({ first: batchSize, after });
27
- const { edges, pageInfo } = result;
28
- const nodes = edges.map(edge => edge.node);
29
- const batch = filterPredicate ? nodes.filter(filterPredicate) : nodes;
30
- dataset.push(...batch);
31
- hasMore = pageInfo.hasNextPage;
32
- after = (_a = pageInfo.endCursor) !== null && _a !== void 0 ? _a : undefined;
33
- totalNodesFetched += nodes.length;
34
- if (afterEachQuery) {
35
- afterEachQuery(totalNodesFetched, dataset, batch, pageInfo);
52
+ throw new Error('Invalid query params');
53
+ }
54
+ static isFirstAfter(connectionArgs) {
55
+ return 'first' in connectionArgs;
56
+ }
57
+ static isLastBefore(connectionArgs) {
58
+ return 'last' in connectionArgs;
59
+ }
60
+ static async getFirstItemsAsync({ first, after }, { internalBatchSize, maxNodesFetched, filterPredicate, queryAsync, beforeEachQuery, afterEachQuery, }) {
61
+ var _a, _b, _c, _d, _e;
62
+ const limit = first + 1;
63
+ const dataset = [];
64
+ let hasMore = true;
65
+ let afterInternal = after;
66
+ let totalNodesFetched = 0;
67
+ while (hasMore && dataset.length < limit) {
68
+ if (beforeEachQuery) {
69
+ beforeEachQuery({ first, after }, totalNodesFetched, dataset);
70
+ }
71
+ const result = await queryAsync({ first: internalBatchSize, after: afterInternal });
72
+ const { edges: batchEdges, pageInfo } = result;
73
+ const batch = batchEdges.filter(edge => filterPredicate(edge.node));
74
+ const nodesRemaining = limit - dataset.length;
75
+ dataset.push(...batch.slice(0, nodesRemaining));
76
+ hasMore = pageInfo.hasNextPage;
77
+ afterInternal = (_a = pageInfo.endCursor) !== null && _a !== void 0 ? _a : undefined;
78
+ totalNodesFetched += batchEdges.length;
79
+ if (afterEachQuery) {
80
+ afterEachQuery({ first, after }, totalNodesFetched, dataset, hasMore && dataset.length < limit);
81
+ }
82
+ if (totalNodesFetched >= maxNodesFetched) {
83
+ throw new Error(`Max nodes of ${maxNodesFetched} fetched`);
84
+ }
36
85
  }
37
- if (totalNodesFetched >= maxNodesFetched) {
38
- return dataset;
86
+ const edges = dataset.slice(0, first);
87
+ return {
88
+ edges,
89
+ pageInfo: {
90
+ hasNextPage: dataset.length > first,
91
+ hasPreviousPage: false,
92
+ startCursor: (_c = (_b = edges[0]) === null || _b === void 0 ? void 0 : _b.cursor) !== null && _c !== void 0 ? _c : null,
93
+ endCursor: (_e = (_d = edges[edges.length - 1]) === null || _d === void 0 ? void 0 : _d.cursor) !== null && _e !== void 0 ? _e : null,
94
+ },
95
+ };
96
+ }
97
+ static async getLastItemsAsync({ last, before }, { internalBatchSize, maxNodesFetched, filterPredicate, queryAsync, beforeEachQuery, afterEachQuery, }) {
98
+ var _a, _b, _c, _d, _e;
99
+ const limit = last + 1;
100
+ const dataset = [];
101
+ let hasMore = true;
102
+ let beforeInternal = before;
103
+ let totalNodesFetched = 0;
104
+ while (hasMore && dataset.length < limit) {
105
+ if (beforeEachQuery) {
106
+ beforeEachQuery({ last, before }, totalNodesFetched, dataset);
107
+ }
108
+ const result = await queryAsync({ last: internalBatchSize, before: beforeInternal });
109
+ const { edges: batchEdges, pageInfo } = result;
110
+ const batch = batchEdges.filter(edge => filterPredicate(edge.node));
111
+ const nodesRemaining = limit - dataset.length;
112
+ // relay orders pages from first to last, so we reverse the batch to to choose the last n
113
+ const nodesChosen = batch.reverse().slice(0, nodesRemaining);
114
+ dataset.push(...nodesChosen);
115
+ hasMore = pageInfo.hasPreviousPage;
116
+ beforeInternal = (_a = pageInfo.startCursor) !== null && _a !== void 0 ? _a : undefined;
117
+ totalNodesFetched += batchEdges.length;
118
+ if (afterEachQuery) {
119
+ afterEachQuery({ last, before }, totalNodesFetched, dataset, hasMore && dataset.length < limit);
120
+ }
121
+ if (totalNodesFetched >= maxNodesFetched) {
122
+ throw new Error(`Max nodes of ${maxNodesFetched} fetched`);
123
+ }
39
124
  }
125
+ // we reverse our dataset again to restore the original order of first to last to match relay
126
+ const edges = dataset.slice(0, last).reverse();
127
+ return {
128
+ edges,
129
+ pageInfo: {
130
+ hasNextPage: false,
131
+ hasPreviousPage: dataset.length > last,
132
+ startCursor: (_c = (_b = edges[0]) === null || _b === void 0 ? void 0 : _b.cursor) !== null && _c !== void 0 ? _c : null,
133
+ endCursor: (_e = (_d = edges[edges.length - 1]) === null || _d === void 0 ? void 0 : _d.cursor) !== null && _e !== void 0 ? _e : null,
134
+ },
135
+ };
136
+ }
137
+ }
138
+ exports.FilterPagination = FilterPagination;
139
+ async function selectPaginatedAsync({ queryAsync, getTitleAsync, printedType, pageSize, }) {
140
+ // Dont bother prompting if there are 0 or 1 items
141
+ const connectionPreflight = await queryAsync({ first: pageSize });
142
+ const { edges } = connectionPreflight;
143
+ if (edges.length === 0) {
144
+ return null;
145
+ }
146
+ else if (edges.length === 1) {
147
+ return edges[0].node;
148
+ }
149
+ return await selectPaginatedInternalAsync({
150
+ queryAsync,
151
+ getTitleAsync,
152
+ printedType,
153
+ queryParams: { first: pageSize },
154
+ });
155
+ }
156
+ exports.selectPaginatedAsync = selectPaginatedAsync;
157
+ exports.PREV_PAGE_OPTION = {
158
+ value: Symbol('PREV_PAGE'),
159
+ title: '⬆️ Previous page',
160
+ };
161
+ exports.NEXT_PAGE_OPTION = {
162
+ value: Symbol('NEXT_PAGE'),
163
+ title: '⬇️ Next page',
164
+ };
165
+ async function selectPaginatedInternalAsync({ queryAsync, getTitleAsync, printedType, queryParams, }) {
166
+ var _a;
167
+ const limit = (_a = queryParams.first) !== null && _a !== void 0 ? _a : queryParams.last;
168
+ (0, assert_1.default)(limit, 'queryParams must have either first or last');
169
+ const connection = await queryAsync(queryParams);
170
+ const { edges, pageInfo } = connection;
171
+ /*
172
+ * The Relay spec has a weird definition on hasNextPage and hasPreviousPage:
173
+ * 'If the client is paginating with last/before, then the server must return true if prior edges
174
+ * exist, otherwise false. If the client is paginating with first/after, then the client may
175
+ * return true if edges prior to after exist, if it can do so efficiently, otherwise may return false.'
176
+ *
177
+ * This means if we are paginating with first/after, we can't rely on pageInfo.hasPreviousPage and vice versa.
178
+ */
179
+ const { endCursor, hasNextPage: serverResponseHasNextPage, startCursor, hasPreviousPage: serverResponseHasPreviousPage, } = pageInfo;
180
+ const hasPreviousPage = serverResponseHasPreviousPage || queryParams.after;
181
+ const hasNextPage = serverResponseHasNextPage || queryParams.before;
182
+ const nodes = edges.map(edge => edge.node);
183
+ const options = [];
184
+ if (hasPreviousPage) {
185
+ options.push(exports.PREV_PAGE_OPTION);
186
+ }
187
+ const nodeTitles = await Promise.all(nodes.map(node => getTitleAsync(node)));
188
+ options.push(...nodes.map((node, index) => ({ value: node, title: nodeTitles[index] })));
189
+ if (hasNextPage) {
190
+ options.push(exports.NEXT_PAGE_OPTION);
191
+ }
192
+ const { item: selectedItem } = await (0, prompts_1.promptAsync)({
193
+ type: 'select',
194
+ name: 'item',
195
+ message: `Select a ${printedType}`,
196
+ choices: options.map(option => ({
197
+ value: option.value,
198
+ title: option.title,
199
+ })),
200
+ });
201
+ if (selectedItem === exports.PREV_PAGE_OPTION.value) {
202
+ return await selectPaginatedInternalAsync({
203
+ queryParams: {
204
+ last: limit,
205
+ before: startCursor !== null && startCursor !== void 0 ? startCursor : undefined,
206
+ },
207
+ queryAsync,
208
+ getTitleAsync,
209
+ printedType,
210
+ });
211
+ }
212
+ else if (selectedItem === exports.NEXT_PAGE_OPTION.value) {
213
+ return await selectPaginatedInternalAsync({
214
+ queryParams: {
215
+ first: limit,
216
+ after: endCursor !== null && endCursor !== void 0 ? endCursor : undefined,
217
+ },
218
+ queryAsync,
219
+ getTitleAsync,
220
+ printedType,
221
+ });
222
+ }
223
+ else {
224
+ return selectedItem;
40
225
  }
41
- return dataset;
42
226
  }
43
- exports.getPaginatedDatasetAsync = getPaginatedDatasetAsync;