Skip to content

Commit ca0216d

Browse files
PR Feedback:
* Move asHumanfriendlyTokenList to cli-kit/common/array, rename to asHumanFriendlyArray, update asHumanFriendlyArray to accept a generic so the return array can be explicitly typed if needed
1 parent 22d320f commit ca0216d

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

packages/app/src/cli/services/dev.ts

+4-36
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ import {checkPortAvailability, getAvailableTCPPort} from '@shopify/cli-kit/node/
4343
import {TunnelClient} from '@shopify/cli-kit/node/plugins/tunnel'
4444
import {getBackendPort} from '@shopify/cli-kit/node/environment'
4545
import {basename} from '@shopify/cli-kit/node/path'
46-
import {renderWarning, renderInfo, Token} from '@shopify/cli-kit/node/ui'
46+
import {renderWarning, renderInfo} from '@shopify/cli-kit/node/ui'
4747
import {reportAnalyticsEvent} from '@shopify/cli-kit/node/analytics'
4848
import {OutputProcess, formatPackageManagerCommand, outputDebug} from '@shopify/cli-kit/node/output'
4949
import {hashString} from '@shopify/cli-kit/node/crypto'
5050
import {AbortError} from '@shopify/cli-kit/node/error'
51+
import {asHumanFriendlyArray} from '@shopify/cli-kit/common/array'
5152

5253
export interface NoTunnel {
5354
mode: 'use-localhost'
@@ -637,44 +638,11 @@ export function renderPortWarnings(portWarnings: PortWarning[] = []) {
637638
return
638639
}
639640

640-
const formattedWarningTypes = asHumanFriendlyTokenList(portWarnings.map((warning) => warning.type)).join(' ')
641-
const formattedFlags = asHumanFriendlyTokenList(portWarnings.map((warning) => ({command: warning.flag})))
641+
const formattedWarningTypes = asHumanFriendlyArray(portWarnings.map((warning) => warning.type)).join(' ')
642+
const formattedFlags = asHumanFriendlyArray(portWarnings.map((warning) => ({command: warning.flag})))
642643

643644
renderWarning({
644645
headline: [`Random ports will be used for ${formattedWarningTypes} because the requested ports are not available.`],
645646
body: [`If you want to use specific ports, you can choose different ports using the`, ...formattedFlags, `flags.`],
646647
})
647648
}
648-
649-
/**
650-
* Converts an array of Tokens into a human friendly list
651-
*
652-
* Returns a new array that contains the items separated by commas,
653-
* except for the last item, which is seperated by "and".
654-
* This is useful for creating human-friendly sentences.
655-
*
656-
* @example
657-
* ```ts
658-
* const items = ['apple', 'banana', 'cherry'];
659-
* const result = asHumanFriendlyList(items)
660-
*
661-
* //['apple', ',', 'banana', ',', 'and', 'cherry']
662-
* console.log(result);
663-
* ```
664-
*/
665-
666-
function asHumanFriendlyTokenList(items: Token[]): Token[] {
667-
if (items.length < 2) {
668-
return items
669-
}
670-
671-
return items.reduce<Token[]>((acc, item, index) => {
672-
if (index === items.length - 1) {
673-
acc.push('and')
674-
} else if (index !== 0) {
675-
acc.push(', ')
676-
}
677-
acc.push(item)
678-
return acc
679-
}, [])
680-
}

packages/cli-kit/src/public/common/array.ts

+34
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,37 @@ export function uniqBy<T>(array: List<T> | null | undefined, iteratee: ValueIter
6767
export function difference<T>(array: List<T> | null | undefined, ...values: List<T>[]): T[] {
6868
return lodashDifference(array, ...values)
6969
}
70+
71+
/**
72+
* Converts an array of anything into a human friendly list.
73+
*
74+
* Returns a new array that contains the items separated by commas,
75+
* except for the last item, which is separated by "and".
76+
* This is useful for creating human-friendly sentences.
77+
*
78+
* @param items - Token[].
79+
* @returns Token[].
80+
* @example
81+
* ```ts
82+
* const items = ['apple', 'banana', {command: "--flag"}];
83+
* const result = asHumanFriendlyList(items)
84+
*
85+
* // ['apple', ',', 'banana', ',', 'and', {command: "--flag"}]
86+
* console.log(result);
87+
* ```
88+
*/
89+
export function asHumanFriendlyArray<T>(items: T[]): (T | string)[] {
90+
if (items.length < 2) {
91+
return items
92+
}
93+
94+
return items.reduce<(T | string)[]>((acc, item, index) => {
95+
if (index === items.length - 1) {
96+
acc.push('and')
97+
} else if (index !== 0) {
98+
acc.push(', ')
99+
}
100+
acc.push(item)
101+
return acc
102+
}, [])
103+
}

0 commit comments

Comments
 (0)