squint-cli
Version:
Squint makes visual reviews of web app releases easy
65 lines (55 loc) • 1.5 kB
text/typescript
import fs from 'fs'
import _ from 'lodash'
// https://github.com/sindresorhus/p-each-series/blob/main/license
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
const PROMISE_EACH_SERIES_STOP = Symbol('promiseEachSeries.stop')
export async function promiseEachSeries<T>(
iterable: Iterable<T>,
iterator: (
value: T,
index: number
) => Promise<void | typeof PROMISE_EACH_SERIES_STOP>
) {
let index = 0
for (const value of iterable) {
// eslint-disable-next-line no-await-in-loop
const returnValue = await iterator(await value, index++)
if (returnValue === PROMISE_EACH_SERIES_STOP) {
break
}
}
return iterable
}
// lodash is in scope for evaluated code
export function evalJs(code: string, ...args: any[]): any {
let result: any
const codeToEval = `(() => { return ${code}; })()`
try {
const afterEval = eval(codeToEval)
if (_.isFunction(afterEval)) {
result = afterEval(...args)
} else {
result = afterEval
}
} catch (e) {
console.error(`Unable to \`eval\` code: ${e.message}`)
console.error('Evaluated code:')
console.error(codeToEval)
throw e
}
return result
}
export async function mkdirp(dir: string) {
try {
await fs.promises.mkdir(dir, { recursive: true })
} catch (e) {
// ignore
}
}
export function union<T>(setA: Set<T>, setB: Set<T>) {
const _union = new Set(setA)
for (const elem of setB) {
_union.add(elem)
}
return _union
}