|
| 1 | +import { readFile } from 'fs' |
| 2 | +import { IResolvers } from 'graphql-tools' |
| 3 | +import { join } from 'path' |
| 4 | +import { compose, curry, mergeDeepRight } from 'ramda' |
| 5 | +import readdir from 'readdir-enhanced' |
| 6 | + |
| 7 | +const deep = true |
| 8 | + |
| 9 | +const makePath = curry((path: string, file: string): string => join(path, file)) |
| 10 | + |
| 11 | +const readFiles = (path: string, filter: string): Promise<Array<string>> => readdir(path, { deep, filter }) |
| 12 | + |
| 13 | +const loadContent = (path: string): Promise<string> => { |
| 14 | + return new Promise((resolve, reject) => { |
| 15 | + readFile(path, 'utf8', (err, data) => { |
| 16 | + if (err) return reject(err) |
| 17 | + resolve(data) |
| 18 | + }) |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +const readContentFiles = async (path: string, files: Array<string>): Promise<string[]> => { |
| 23 | + return Promise.all(files.map(compose(loadContent, makePath(path)))) |
| 24 | +} |
| 25 | + |
| 26 | +const requireFiles = async (path: string, files: Array<string>): Promise<IResolvers> => { |
| 27 | + return files.map(makePath(path)).reduce((prev, curr) => { |
| 28 | + /* eslint @typescript-eslint/no-var-requires: "Off" */ |
| 29 | + const cxt = require(`${curr}`) |
| 30 | + return mergeDeepRight(prev, cxt) |
| 31 | + }, {}) |
| 32 | +} |
| 33 | + |
| 34 | +export const loadSchemas = async (path: string, filter: string): Promise<string> => { |
| 35 | + const files = await readFiles(path, filter) |
| 36 | + const list = await readContentFiles(path, files) |
| 37 | + return list.join('\n') |
| 38 | +} |
| 39 | + |
| 40 | +export const loadContext = async (dir: string, filter: string): Promise<IResolvers> => { |
| 41 | + const files = await readFiles(dir, filter) |
| 42 | + return requireFiles(dir, files) |
| 43 | +} |
0 commit comments