Skip to content

Commit f256475

Browse files
authoredApr 22, 2024··
Feature | Order list of EVM chains in graph init command (#1634)
* src/command-helpers: Add priority sort function * src/command-helpers: Add priority sort tests * src/commands/init: Sort imports * src/commands/init: sort network choices before showing them to user * remove jsdoc * src/command-helpers: improve priority sort JSDoc * Add changeset file * Fix formatting
1 parent f0c583f commit f256475

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed
 

‎.changeset/early-walls-talk.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
Order list of evm chains in graph init command
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { sortWithPriority } from './sort'; // adjust the import based on your file structure
3+
4+
describe('sortWithPriority', () => {
5+
it('should sort numbers with specific priority elements', () => {
6+
const numbers = [5, 3, 9, 1, 4];
7+
const priorityNumbers = [9, 1];
8+
const result = sortWithPriority(numbers, priorityNumbers);
9+
expect(result).toEqual([1, 9, 3, 4, 5]);
10+
});
11+
12+
it('should default sort numbers if no priority specifier', () => {
13+
const numbers = [5, 3, 9, 1, 4];
14+
const result = sortWithPriority(numbers);
15+
expect(result).toEqual([1, 3, 4, 5, 9]);
16+
});
17+
18+
it('should sort strings with priority determined by a function', () => {
19+
const fruits = ['apple', 'orange', 'banana', 'mango', 'kiwi', 'melon'];
20+
const sortedFruits = sortWithPriority(fruits, fruit => fruit.startsWith('m'));
21+
expect(sortedFruits).toEqual(['mango', 'melon', 'apple', 'banana', 'kiwi', 'orange']);
22+
});
23+
24+
it('should handle an empty array', () => {
25+
const emptyArray: never[] = [];
26+
const result = sortWithPriority(emptyArray, x => x > 3);
27+
expect(result).toEqual([]);
28+
});
29+
30+
it('should sort using a custom compare function', () => {
31+
const numbers = [5, 3, 9, 1, 4];
32+
const priorityNumbers = [9, 1];
33+
const result = sortWithPriority(numbers, priorityNumbers, (a, b) => a - b);
34+
expect(result).toEqual([1, 9, 3, 4, 5]);
35+
});
36+
});
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Sorts an array with specified elements given priority.
3+
* Use a predicate function, or provide an array to prioritise elements.
4+
* If no compare function is provided, default JS sorting (ascending) behaviour prevails.
5+
*
6+
* @param {T[]} array - The array to be sorted.
7+
* @param {((element: T) => boolean) | T[]} prioritySpecifier - A function that returns true if an element should be prioritized, or an array of elements to be prioritized.
8+
* @param {(a: T, b: T) => number} [compareFunction] - An optional comparison function to sort the elements of the array. If omitted, the array is sorted in default order.
9+
* @returns {T[]} The sorted array with priority elements first.
10+
*
11+
* @example
12+
* const numbers = [5, 3, 9, 1, 4];
13+
* sortWithPriority(numbers, x => x > 5); // [9, 1, 3, 4, 5]
14+
* sortWithPriority(numbers, [9, 1]); // [1, 9, 3, 4, 5]
15+
*/
16+
function sortWithPriority<T>(
17+
array: T[],
18+
prioritySpecifier?: ((element: T) => boolean) | T[],
19+
compareFunction?: (a: T, b: T) => number,
20+
): T[] {
21+
// prioritySpecifier can be an array or a function so handle each case
22+
let isPriorityElement: (element: T) => boolean;
23+
24+
if (Array.isArray(prioritySpecifier) || !prioritySpecifier) {
25+
const prioritySet = new Set(prioritySpecifier ?? []);
26+
isPriorityElement = (element: T) => prioritySet.has(element);
27+
} else {
28+
isPriorityElement = prioritySpecifier;
29+
}
30+
31+
const priorityArray = array.filter(isPriorityElement);
32+
const regularArray = array.filter(item => !isPriorityElement(item));
33+
34+
if (compareFunction) {
35+
priorityArray.sort(compareFunction);
36+
regularArray.sort(compareFunction);
37+
} else {
38+
priorityArray.sort();
39+
regularArray.sort();
40+
}
41+
42+
return priorityArray.concat(regularArray);
43+
}
44+
45+
export { sortWithPriority };

‎packages/cli/src/commands/init.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import fs from 'fs';
22
import os from 'os';
33
import path from 'path';
4-
import { filesystem, prompt, system } from 'gluegun';
54
import * as toolbox from 'gluegun';
5+
import { filesystem, prompt, system } from 'gluegun';
66
import { Args, Command, Flags, ux } from '@oclif/core';
77
import {
88
loadAbiFromBlockScout,
@@ -12,6 +12,7 @@ import {
1212
import { initNetworksConfig } from '../command-helpers/network';
1313
import { chooseNodeUrl, SUBGRAPH_STUDIO_URL } from '../command-helpers/node';
1414
import { generateScaffold, writeScaffold } from '../command-helpers/scaffold';
15+
import { sortWithPriority } from '../command-helpers/sort';
1516
import { withSpinner } from '../command-helpers/spinner';
1617
import { getSubgraphBasename, validateSubgraphName } from '../command-helpers/subgraph';
1718
import { GRAPH_CLI_SHARED_HEADERS } from '../constants';
@@ -630,7 +631,7 @@ async function processInitForm(
630631
},
631632
]);
632633

633-
const choices = (await AVAILABLE_NETWORKS())?.[
634+
let choices = (await AVAILABLE_NETWORKS())?.[
634635
product === 'subgraph-studio' ? 'studio' : 'hostedService'
635636
];
636637

@@ -641,6 +642,8 @@ async function processInitForm(
641642
);
642643
}
643644

645+
choices = sortWithPriority(choices, ['mainnet']);
646+
644647
const { network } = await prompt.ask<{ network: string }>([
645648
{
646649
type: 'select',

0 commit comments

Comments
 (0)
Please sign in to comment.