-
Notifications
You must be signed in to change notification settings - Fork 267
/
util.ts
844 lines (740 loc) · 24.2 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { asyncExitHook, gracefulExit } from "@scg82/exit-hook"
import { execSync } from "child_process"
import _spawn from "cross-spawn"
import { createHash } from "crypto"
import { readFile } from "fs-extra"
import { load } from "js-yaml"
import {
difference,
find,
fromPairs,
groupBy,
isArray,
isPlainObject,
mapValues,
memoize,
omit,
pick,
range,
some,
truncate,
uniqBy,
} from "lodash"
import pMap from "p-map"
import pProps from "p-props"
import { isAbsolute, relative } from "path"
import { Readable, Writable } from "stream"
import type { PrimitiveMap } from "../config/common"
import { DEFAULT_GARDEN_CLOUD_DOMAIN, DOCS_BASE_URL, gardenEnv } from "../constants"
import {
ChildProcessError,
InternalError,
ParameterError,
RuntimeError,
TimeoutError,
isErrnoException,
} from "../exceptions"
import type { Log } from "../logger/log-entry"
import { getDefaultProfiler } from "./profiling"
import { dedent, naturalList, tailString } from "./string"
import split2 = require("split2")
import execa = require("execa")
export { apply as jsonMerge } from "json-merge-patch"
export type HookCallback = (signal: number) => void | Promise<void>
const exitHookNames: string[] = [] // For debugging/testing/inspection purposes
// For creating a subset of a union type, see: https://stackoverflow.com/a/53637746
export type PickFromUnion<T, U extends T> = U
export type ValueOf<T> = T[keyof T]
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
export type Diff<T, U> = T extends U ? never : T
export type Mutable<T> = { -readonly [K in keyof T]: T[K] }
export type Nullable<T> = { [P in keyof T]: T[P] | null }
export type MaybeUndefined<T> = T | undefined
// From: https://stackoverflow.com/a/49936686/5629940
export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Array<infer U>
? Array<DeepPartial<U>>
: T[P] extends ReadonlyArray<infer V>
? ReadonlyArray<DeepPartial<V>>
: DeepPartial<T[P]>
}
export type Unpacked<T> = T extends (infer U)[]
? U
: T extends (...args: any[]) => infer V
? V
: T extends Promise<infer W>
? W
: T
export type ExcludesFalsy = <T>(x: T | false | null | undefined) => x is T
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
const MAX_BUFFER_SIZE = 1024 * 1024
// Used to control process-level operations during testing
export const testFlags = {
expandErrors: false,
disableShutdown: false,
}
export async function shutdown(code?: number) {
// This is a good place to log exitHookNames if needed.
if (!testFlags.disableShutdown) {
if (gardenEnv.GARDEN_ENABLE_PROFILING) {
// eslint-disable-next-line no-console
console.log(getDefaultProfiler().report())
}
gracefulExit(code)
}
}
export function registerCleanupFunction(name: string, func: HookCallback) {
exitHookNames.push(name)
const callbackFunc = (signal: number) => {
return Promise.resolve(func(signal))
}
asyncExitHook(callbackFunc, {
minimumWait: 10000,
})
}
export function getPackageVersion(): string {
const version = require("../../../package.json").version
return version
}
type CloudDistroName = "Cloud Dashboard" | "Garden Enterprise" | "Garden Cloud"
/**
* Returns "Garden Cloud" if domain matches https://<some-subdomain>.app.garden,
* otherwise "Garden Enterprise".
*
* TODO: Return the distribution type from the API and store on the CloudApi class.
*/
export function getCloudDistributionName(domain: string): CloudDistroName {
if (domain === DEFAULT_GARDEN_CLOUD_DOMAIN) {
return "Cloud Dashboard"
}
if (!domain.match(/^https:\/\/.+\.app\.garden$/i)) {
return "Garden Enterprise"
}
return "Garden Cloud"
}
export function getCloudLogSectionName(distroName: CloudDistroName): string {
if (distroName === "Cloud Dashboard") {
return "cloud-dashboard"
} else if (distroName === "Garden Cloud") {
return "garden-cloud"
} else {
return "garden-enterprise"
}
}
export async function sleep(msec: number) {
return new Promise((resolve) => setTimeout(resolve, msec))
}
/**
* Returns a promise that can be resolved/rejected by calling resolve/reject.
*/
export type Deferred<T> = {
resolve: (value: T) => void
reject: (reason?: any) => void
promise: Promise<T>
}
export function defer<T>(): Deferred<T> {
let resolve: (value: T) => void
let reject: (reason?: any) => void
const promise = new Promise((pResolve, pReject) => {
resolve = pResolve
reject = pReject
}) as Promise<T>
return {
resolve: resolve!,
reject: reject!,
promise,
}
}
/**
* Creates an output stream that logs the message.
*/
export function createOutputStream(log: Log, origin?: string) {
const outputStream = split2()
const streamLog = log.createLog({ origin })
outputStream.on("error", () => {})
outputStream.on("data", (line: Buffer) => {
streamLog.info({ msg: line.toString() })
})
return outputStream
}
export interface ExecOpts extends execa.Options {
stdout?: Writable
stderr?: Writable
}
/**
* A wrapper function around execa that standardises error messages.
* Enforces `buffer: true` (which is the default execa behavior).
*
* Also adds the ability to pipe stdout|stderr to an output stream.
*
* @throws RuntimeError on EMFILE (Too many open files)
* @throws ChildProcessError on any other error condition
*/
export async function exec(cmd: string, args: string[], opts: ExecOpts = {}) {
// Ensure buffer is always set to true so that we can read the error output
// Defaulting cwd to process.cwd() to avoid defaulting to a virtual path after packaging with pkg
opts = { cwd: process.cwd(), windowsHide: true, ...opts, buffer: true, all: true }
const proc = execa(cmd, args, omit(opts, ["stdout", "stderr"]))
opts.stdout && proc.stdout && proc.stdout.pipe(opts.stdout)
opts.stderr && proc.stderr && proc.stderr.pipe(opts.stderr)
try {
const res = await proc
return res
} catch (err) {
if (isErrnoException(err) && err.code === "EMFILE") {
throw new RuntimeError({
message: dedent`
Received EMFILE (Too many open files) error when running ${cmd}.
This may mean there are too many files in the project, and that you need to exclude large dependency directories. Please see ${DOCS_BASE_URL}/using-garden/configuration-overview#including-excluding-files-and-directories for information on how to do that.
This can also be due to limits on open file descriptors being too low. Here is one guide on how to configure those limits for different platforms: https://docs.riak.com/riak/kv/latest/using/performance/open-files-limit/index.html
`,
})
} else if (isErrnoException(err)) {
throw new RuntimeError({
message: `Failed to run ${cmd}: ${err}`,
code: err.code,
})
}
const error = <execa.ExecaError>err
throw new ChildProcessError({
cmd,
args,
code: error.exitCode,
output: error.all || error.stdout || error.stderr || "",
stderr: error.stderr || "",
stdout: error.stdout || "",
})
}
}
export interface SpawnOpts {
timeoutSec?: number
cwd?: string
data?: Buffer
ignoreError?: boolean
env?: { [key: string]: string | undefined }
rawMode?: boolean // Only used if tty = true. See also: https://nodejs.org/api/tty.html#tty_readstream_setrawmode_mode
stdout?: Writable
stderr?: Writable
tty?: boolean
wait?: boolean
}
export interface SpawnOutput {
code: number
all: string
stdout: string
stderr: string
proc: any
}
// TODO Dump output to a log file if it exceeds the MAX_BUFFER_SIZE
/**
* Note: For line-by-line stdout/stderr streaming, both `opts.stdout` and `opts.stderr` must be defined. This is a
* result of how Node's child process spawning works (which this function wraps).
*
* @throws RuntimeError on ENOENT (command not found)
* @throws ChildProcessError on any other error condition
*/
export function spawn(cmd: string, args: string[], opts: SpawnOpts = {}) {
const {
timeoutSec: timeout = 0,
cwd = process.cwd(), // This is to avoid running from a virtual path after packaging in pkg
data,
ignoreError = false,
env,
rawMode = true,
stdout,
stderr,
tty,
wait = true,
} = opts
const stdio = tty ? "inherit" : "pipe"
const proc = _spawn(cmd, args, { cwd, env, stdio, windowsHide: true })
const result: SpawnOutput = {
code: 0,
all: "",
stdout: "",
stderr: "",
proc,
}
let _process = <any>process
if (tty) {
if (data) {
throw new InternalError({
message: `Cannot pipe to stdin when tty=true. (spawn(${JSON.stringify(cmd)}, ${JSON.stringify(args)})`,
})
}
_process.stdin.setEncoding("utf8")
// raw mode is not available if we're running without a TTY
if (rawMode) {
_process.stdin.setRawMode && _process.stdin.setRawMode(true)
}
}
// We ensure the output strings never exceed the MAX_BUFFER_SIZE
proc.stdout?.on("data", (s) => {
result.all = tailString(result.all + s, MAX_BUFFER_SIZE, true)
result.stdout! = tailString(result.stdout! + s, MAX_BUFFER_SIZE, true)
})
proc.stderr?.on("data", (s) => {
result.all = tailString(result.all + s, MAX_BUFFER_SIZE, true)
result.stderr! = tailString(result.stderr! + s, MAX_BUFFER_SIZE, true)
})
stdout && proc.stdout?.pipe(stdout)
stderr && proc.stderr?.pipe(stderr)
if (data) {
// This may happen if the spawned process errors while we're still writing data.
proc.stdin?.on("error", () => {})
proc.stdin?.end(data)
}
return new Promise<SpawnOutput>((resolve, reject) => {
let _timeout: NodeJS.Timeout
if (!wait) {
resolve(result)
return
}
if (timeout > 0) {
_timeout = setTimeout(() => {
proc.kill("SIGKILL")
reject(new TimeoutError({ message: `${cmd} timed out after ${timeout} seconds.` }))
}, timeout * 1000)
}
proc.on("error", (err) => {
let msg = `An error occurred while trying to run '${cmd}' (${err.message}).`
if ((<any>err).code === "ENOENT") {
msg = `${msg} Please make sure '${cmd}' is installed and in the $PATH.`
cwd && (msg = `${msg} Please make sure '${cwd}' exists and is a valid directory path.`)
}
reject(new RuntimeError({ message: msg }))
})
proc.on("close", (code) => {
_timeout && clearTimeout(_timeout)
result.code = code!
if (code === 0 || ignoreError) {
resolve(result)
} else {
reject(
new ChildProcessError({
cmd,
args,
code: result.code,
opts,
output: result.all,
stderr: result.stderr,
stdout: result.stdout,
})
)
}
})
})
}
/**
* Recursively resolves all promises in the given input,
* walking through all object keys and array items.
*/
type Resolvable<R> = R | PromiseLike<R>
type ResolvableProps<T> = object & { [K in keyof T]: Resolvable<T[K]> }
export async function deepResolve<T>(
value: T | Iterable<T> | Iterable<PromiseLike<T>> | ResolvableProps<T>
): Promise<T | Iterable<T> | { [K in keyof T]: T[K] }> {
if (isArray(value)) {
return await pMap(value, deepResolve)
} else if (isPlainObject(value)) {
return await pProps(<ResolvableProps<T>>mapValues(<ResolvableProps<T>>value, deepResolve))
} else {
return Promise.resolve(<T>value)
}
}
type DeepMappable = any | ArrayLike<DeepMappable> | { [k: string]: DeepMappable }
/**
* Recursively maps over all keys in the input and resolves the resulting promises,
* walking through all object keys and array items.
*/
export async function asyncDeepMap<T extends DeepMappable>(
obj: T,
mapper: (value) => Promise<any>,
options?: pMap.Options
): Promise<T> {
if (isArray(obj)) {
return <any>pMap(obj, (v) => asyncDeepMap(v, mapper, options), options)
} else if (isPlainObject(obj)) {
return <T>(
fromPairs(
await pMap(
Object.entries(obj as { [k: string]: DeepMappable }),
async ([key, value]) => [key, await asyncDeepMap(value, mapper, options)],
options
)
)
)
} else {
return mapper(obj)
}
}
export function getEnumKeys(Enum) {
return Object.values(Enum).filter((k) => typeof k === "string") as string[]
}
export async function loadYamlFile(path: string): Promise<any> {
const fileData = await readFile(path)
return load(fileData.toString())
}
export interface ObjectWithName {
name: string
[key: string]: any
}
export function getNames<T extends ObjectWithName>(array: T[]) {
return array.map((v) => v.name)
}
export function findByName<T extends ObjectWithName>(array: T[], name: string): T | undefined {
return find(array, ["name", name])
}
export function uniqByName<T extends ObjectWithName>(array: T[]): T[] {
return uniqBy(array, (item) => item.name)
}
export function isDisjoint<T>(set1: Set<T>, set2: Set<T>): boolean {
return !some([...set1], (element) => set2.has(element))
}
/**
* Returns an array of arrays, where the elements of a given array are the elements of items for which
* isRelated returns true for one or more elements of its class.
*
* I.e. an element is related to at least one element of its class, transitively.
*/
export function relationshipClasses<I>(items: I[], isRelated: (item1: I, item2: I) => boolean): I[][] {
// We start with each item in its own class.
//
// We then keep looking for relationships/connections between classes and merging them when one is found,
// until no two classes have elements that are related to each other.
const classes: I[][] = items.map((i) => [i])
let didMerge = false
do {
didMerge = false
for (const classIndex1 of range(0, classes.length)) {
for (const classIndex2 of range(0, classes.length)) {
if (classIndex1 === classIndex2) {
continue
}
const c1 = classes[classIndex1]
const c2 = classes[classIndex2]
if (some(c1, (i1) => some(c2, (i2) => isRelated(i1, i2)))) {
// Then we merge c1 and c2.
didMerge = true
classes.splice(classIndex2, 1)
classes[classIndex1] = [...c1, ...c2]
break
}
}
if (didMerge) {
break
}
}
} while (didMerge)
// Once this point is reached, no two classes are related to each other, so we can return them.
return classes
}
/**
* Converts a string identifier to the appropriate casing and style for use in environment variable names.
* (e.g. "my-service" -> "MY_SERVICE")
*/
export function getEnvVarName(identifier: string) {
return identifier.replace(/-/g, "_").toUpperCase()
}
/**
* Picks the specified keys from the given object, and throws an error if one or more keys are not found.
*/
export function pickKeys<T extends object, U extends keyof T>(obj: T, keys: U[], description = "key"): Pick<T, U> {
const picked = pick(obj, ...keys)
const missing = difference(<string[]>keys, Object.keys(picked))
if (missing.length) {
throw new ParameterError({
message: `Could not find ${description}(s): ${missing.map((k, _) => k).join(", ")}. Available: ${naturalList(
Object.keys(obj)
)}`,
})
}
return picked
}
export function findByNames<T extends ObjectWithName>({
names,
entries,
description,
allowMissing,
}: {
names: string[]
entries: T[]
description: string
allowMissing: boolean
}) {
const available = getNames(entries)
const missing = difference(names, available)
if (missing.length && !allowMissing) {
throw new ParameterError({
message: `Could not find ${description}(s): ${missing.join(", ")}. Available: ${naturalList(available)}`,
})
}
return entries.filter(({ name }) => names.includes(name))
}
export function hashString(s: string, length?: number): string {
const urlHash = createHash("sha256")
urlHash.update(s)
const str = urlHash.digest("hex")
return length ? str.slice(0, length) : str
}
/**
* Ensures that `obj` has an array at `key`, creating it if necessary, and then pushes `value` on that array.
*/
export function pushToKey(obj: object, key: string, value: any) {
if (obj[key]) {
if (!isArray(obj[key])) {
throw new RuntimeError({
message: `Value at '${key}' is not an array. Got ${typeof obj[key]}`,
})
}
obj[key].push(value)
} else {
obj[key] = [value]
}
}
/**
* A type guard that's useful e.g. when filtering an array which may have blank entries.
*/
export function isTruthy<T>(value: T | undefined | null | false | 0 | ""): value is T {
return !!value
}
/**
* Returns `true` if `path` is a subdirectory of `ofPath`. Returns `false` otherwise.
*/
export function isSubdir(path: string, ofPath: string): boolean {
const rel = relative(path, ofPath)
return !!(rel && !rel.startsWith("..") && !isAbsolute(rel))
}
// Used to make the platforms more consistent with other tools
const platformMap = {
win32: "windows" as const,
}
const archMap = {
x32: "386" as const,
x64: "amd64" as const,
}
export type Architecture = Exclude<NodeJS.Architecture, keyof typeof archMap> | (typeof archMap)[keyof typeof archMap]
export type Platform =
| Exclude<NodeJS.Platform, keyof typeof platformMap>
| (typeof platformMap)[keyof typeof platformMap]
export function getPlatform(): Platform {
return platformMap[process.platform] || process.platform
}
export function getArchitecture(): Architecture {
// Note: When node is running a x64 build,
// process.arch is always x64 even though the underlying CPU architecture may be arm64
// To check if we are running under Rosetta,
// use the `isDarwinARM` function below
const arch = process.arch
return archMap[arch] || arch
}
export const isDarwinARM = memoize(() => {
if (process.platform !== "darwin") {
return false
}
if (process.arch === "arm64") {
return true
} else if (process.arch === "x64") {
// detect rosetta on Apple M cpu family macs
// see also https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment
// We use execSync here, because this function is called in a constructor
// otherwise we'd make the function async and call `spawn`
try {
execSync("sysctl -n -q sysctl.proc_translated", { encoding: "utf-8" })
} catch (err) {
return false
}
return true
}
return false
})
export function getDurationMsec(start: Date, end: Date): number {
return Math.round(end.getTime() - start.getTime())
}
export async function runScript({
log,
cwd,
script,
envVars,
}: {
log: Log
cwd: string
script: string
envVars?: PrimitiveMap
}) {
const env = toEnvVars(envVars || {})
const outputStream = split2()
outputStream.on("error", (line: Buffer) => {
log.error(line.toString())
})
outputStream.on("data", (line: Buffer) => {
log.info(line.toString())
})
const errorStream = split2()
errorStream.on("error", (line: Buffer) => {
log.error(line.toString())
})
errorStream.on("data", (line: Buffer) => {
// NOTE: We're intentionally logging stderr streams at the "info" level
// because some tools will write to stderr even if it's not an actual error.
// So rendering it as such will look confusing to the user.
// An example of this is the gcloud CLI tool. If run from e.g. the exec
// provider init script, Garden would log those lines as errors if we don't
// use the info level here.
// Actual error are handled specifically.
log.info(line.toString())
})
// Workaround for https://github.com/vercel/pkg/issues/897
env.PKG_EXECPATH = ""
// script can be either a command or path to an executable
// shell script since we use the shell option.
const result = await exec(script, [], {
shell: true,
cwd,
env,
stdout: outputStream,
stderr: errorStream,
})
return result
}
export async function streamToString(stream: Readable) {
const chunks: Buffer[] = []
return new Promise((resolve, reject) => {
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)))
stream.on("error", reject)
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")))
})
}
/**
* A writable stream that collects all data written, and features a `getString()` method.
*/
export class StringCollector extends Writable {
private chunks: Buffer[]
private error?: Error
constructor() {
super()
this.chunks = []
this.on("data", (chunk) => this.chunks.push(Buffer.from(chunk)))
this.on("error", (err) => {
this.error = err
})
}
override _write(chunk: Buffer, _: string, callback: () => void) {
this.chunks.push(Buffer.from(chunk))
callback()
}
getString() {
if (this.error) {
throw this.error
}
return Buffer.concat(this.chunks).toString("utf8")
}
}
export function toEnvVars(vars: PrimitiveMap): { [key: string]: string | undefined } {
return mapValues(vars, (v) => (v === undefined ? undefined : "" + v))
}
/**
* Given a list of `items`, group them by `key` and return a list of `{ value, duplicateItems }` objects, where
* `value` is the value of item[key] and `duplicateItems` are the items that share the value. If the list is empty,
* no items have a duplicate value for the `key`.
*
* @example
* const items = [{ a: 1, b: 1 }, { a: 1, b: 2 }, { a: 2, b: 2 }]
* // returns [{ value: 1, duplicateItems: [{ a: 1, b: 1 }, { a: 1, b: 2 }] }]
* duplicateKeys(items, "a")
* // returns [{ value: 2, duplicateItems: [{ a: 1, b: 2 }, { a: 2, b: 2 }] }]
* duplicateKeys(items, "b")
*/
export function duplicatesByKey(items: any[], key: string) {
const grouped = groupBy(items, key)
return Object.entries(grouped)
.map(([value, duplicateItems]) => ({ value, duplicateItems }))
.filter(({ duplicateItems }) => duplicateItems.length > 1)
}
export function isNotNull<T>(v: T | null): v is T {
return v !== null
}
/**
* Find and return the index of the given `slice` within `array`. Returns -1 if the slice is not found.
*
* Adapted from https://stackoverflow.com/posts/29426078/revisions
*
* @param array
* @param slice
*/
export function findSlice(array: any[], slice: any[], fromIndex = 0) {
let i = fromIndex
const sliceLength = slice.length
const l = array.length + 1 - sliceLength
loop: for (; i < l; i++) {
for (let j = 0; j < sliceLength; j++) {
if (array[i + j] !== slice[j]) {
continue loop
}
}
return i
}
return -1
}
/**
* Returns a copy of the given array, with the first instance (if any) of the given slice removed.
*/
export function removeSlice(array: any[], slice: any[]) {
const out = [...array]
const index = findSlice(array, slice)
if (index > -1) {
out.splice(index, slice.length)
}
return out
}
/**
* Prompt the user for input, using inquirer.
*
* Note: Wrapping inquirer here and requiring inline because it is surprisingly slow to import on load.
*/
export function userPrompt(params: {
name: string
message: string
type: "confirm" | "list" | "input"
default?: any
choices?: string[]
pageSize?: number
}): Promise<any> {
return require("inquirer").prompt(params)
}
export function getGitHubIssueLink(title: string, type: "bug" | "crash" | "feature-request") {
try {
title = encodeURIComponent(
truncate(title, {
length: 80,
omission: encodeURIComponent("..."),
})
).replaceAll("'", "%27")
} catch (e) {
// encodeURIComponent might throw URIError with malformed unicode strings.
// The title is not that important, we can also leave it empty in that case.
title = ""
}
switch (type) {
case "feature-request":
return `https://github.com/garden-io/garden/issues/new?labels=feature+request&template=FEATURE_REQUEST.md&title=%5BFEATURE%5D%3A+${title}`
case "bug":
return `https://github.com/garden-io/garden/issues/new?labels=bug&template=BUG_REPORT.md&title=${title}`
case "crash":
return `https://github.com/garden-io/garden/issues/new?labels=bug,crash&template=CRASH.md&title=${title}`
}
}
/**
* Check if a given date instance is valid.
*/
export function isValidDateInstance(d: any) {
return !isNaN(d) && d instanceof Date
}