mirror of
https://github.com/expo/eas-cli.git
synced 2026-11-15 03:38:08 -10:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
439abcb784 |
No files matched your search
@@ -1,10 +1,15 @@
|
|||||||
|
import { PageInfo } from '../../graphql/generated';
|
||||||
import { promptAsync } from '../../prompts';
|
import { promptAsync } from '../../prompts';
|
||||||
import {
|
import {
|
||||||
Connection,
|
Connection,
|
||||||
|
Edge,
|
||||||
FilterPagination,
|
FilterPagination,
|
||||||
NEXT_PAGE_OPTION,
|
NEXT_PAGE_OPTION,
|
||||||
|
PAGE_SIZE,
|
||||||
PREV_PAGE_OPTION,
|
PREV_PAGE_OPTION,
|
||||||
|
PaginatedGetterAsync,
|
||||||
QueryParams,
|
QueryParams,
|
||||||
|
fetchEntireDatasetAsync,
|
||||||
selectPaginatedAsync,
|
selectPaginatedAsync,
|
||||||
} from '../relay';
|
} from '../relay';
|
||||||
|
|
||||||
@@ -780,3 +785,55 @@ describe(selectPaginatedAsync, () => {
|
|||||||
expect(getTitleAsync).toHaveBeenCalledWith(node3);
|
expect(getTitleAsync).toHaveBeenCalledWith(node3);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe(selectPaginatedAsync, () => {
|
||||||
|
const mockDataset = Array.from({ length: 50 }, (_, idx) => ({ id: idx + 1 }));
|
||||||
|
const testPaginatedGetterAsync: PaginatedGetterAsync<object> = async (
|
||||||
|
relayArgs: QueryParams
|
||||||
|
): Promise<Connection<object>> => {
|
||||||
|
const startIdx = relayArgs.after ? Number(relayArgs.after) : 0;
|
||||||
|
const endIdx = startIdx + (relayArgs.first || PAGE_SIZE);
|
||||||
|
const hasNextPage = endIdx < mockDataset.length;
|
||||||
|
const hasPreviousPage = startIdx > 0;
|
||||||
|
const edges: Edge<object>[] = mockDataset
|
||||||
|
.slice(startIdx, endIdx)
|
||||||
|
.map(node => ({ cursor: String(node.id), node }));
|
||||||
|
|
||||||
|
const pageInfo: PageInfo = {
|
||||||
|
hasNextPage,
|
||||||
|
hasPreviousPage,
|
||||||
|
endCursor: hasNextPage ? String(endIdx) : undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
edges,
|
||||||
|
pageInfo,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getting an entire paginated dataset', async () => {
|
||||||
|
const data = await fetchEntireDatasetAsync({
|
||||||
|
paginatedGetterAsync: testPaginatedGetterAsync,
|
||||||
|
});
|
||||||
|
expect(data).toEqual(mockDataset);
|
||||||
|
});
|
||||||
|
test('getting an empty paginated dataset', async () => {
|
||||||
|
const emptyPaginatedGetterAsync = async (): Promise<Connection<object>> => {
|
||||||
|
return {
|
||||||
|
edges: [],
|
||||||
|
pageInfo: {
|
||||||
|
hasNextPage: false,
|
||||||
|
hasPreviousPage: false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
const data = await fetchEntireDatasetAsync({
|
||||||
|
paginatedGetterAsync: emptyPaginatedGetterAsync,
|
||||||
|
});
|
||||||
|
expect(data).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
|
import cliProgress from 'cli-progress';
|
||||||
|
|
||||||
import { PageInfo } from '../graphql/generated';
|
import { PageInfo } from '../graphql/generated';
|
||||||
import { promptAsync } from '../prompts';
|
import { promptAsync } from '../prompts';
|
||||||
@@ -356,3 +357,51 @@ async function selectPaginatedInternalAsync<T>({
|
|||||||
return selectedItem as T;
|
return selectedItem as T;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type PaginatedGetterAsync<Node> = (relayArgs: QueryParams) => Promise<Connection<Node>>;
|
||||||
|
export const PAGE_SIZE = 20;
|
||||||
|
export async function fetchEntireDatasetAsync<Node>({
|
||||||
|
paginatedGetterAsync,
|
||||||
|
progressBarLabel,
|
||||||
|
}: {
|
||||||
|
paginatedGetterAsync: PaginatedGetterAsync<Node>;
|
||||||
|
progressBarLabel?: string;
|
||||||
|
}): Promise<Node[]> {
|
||||||
|
// No way to know the total count of items beforehand
|
||||||
|
let totalEstimatedWork = 10;
|
||||||
|
const queueProgressBar = new cliProgress.SingleBar(
|
||||||
|
{ format: `|{bar}| ${progressBarLabel}` },
|
||||||
|
cliProgress.Presets.rect
|
||||||
|
);
|
||||||
|
|
||||||
|
const data: Node[] = [];
|
||||||
|
let cursor = undefined;
|
||||||
|
let didStartProgressBar = false;
|
||||||
|
let progress = 0;
|
||||||
|
while (true) {
|
||||||
|
const connection = await paginatedGetterAsync({ first: PAGE_SIZE, after: cursor });
|
||||||
|
const nodes = connection.edges.map(edge => edge.node);
|
||||||
|
const hasNextPage = connection.pageInfo.hasNextPage;
|
||||||
|
data.push(...nodes);
|
||||||
|
if (!hasNextPage) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cursor = connection.pageInfo.endCursor ?? undefined;
|
||||||
|
if (!didStartProgressBar) {
|
||||||
|
// only show the progress bar if user has more than 1 page of items
|
||||||
|
queueProgressBar.start(totalEstimatedWork, 0);
|
||||||
|
didStartProgressBar = true;
|
||||||
|
}
|
||||||
|
progress++;
|
||||||
|
queueProgressBar.update(progress);
|
||||||
|
if (progress >= totalEstimatedWork) {
|
||||||
|
totalEstimatedWork = 8 * totalEstimatedWork;
|
||||||
|
queueProgressBar.setTotal(totalEstimatedWork);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (didStartProgressBar) {
|
||||||
|
queueProgressBar.update(totalEstimatedWork);
|
||||||
|
queueProgressBar.stop();
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user