forked from castframework/cast1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenSDLForModules.ts
117 lines (96 loc) · 3.2 KB
/
genSDLForModules.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
import { find, ls } from 'shelljs';
import {
GraphQLSchemaBuilderModule,
GraphQLSchemaFactory,
} from '@nestjs/graphql';
import { NestFactory } from '@nestjs/core';
import { printSchema } from 'graphql';
import * as fs from 'fs';
import * as minimist from 'minimist';
import * as chalk from 'chalk';
import { Module } from '@nestjs/common';
const args = minimist(process.argv.slice(2));
if (!(args.o && args.i) || args.h) {
showHelp();
process.exit(-1);
}
const outSchemaDir = args.o;
const modulesPath = args.i;
ls(modulesPath).forEach((module) =>
buildModuleFromPath(module, `${modulesPath}/${module}`)
.then((mod) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
if (mod.resolvers.length > 0) generateSchema(mod, outSchemaDir);
else logWarn(`🤔 Module ${mod.name} has no resolver. Skipping...`);
})
.catch((err) => {
logError(`💥 Error while generating schema for module : ${module}`);
console.error(module, err);
}),
);
interface Module {
name: string;
// eslint-disable-next-line @typescript-eslint/ban-types
resolvers: Function[];
}
interface FunctionMap {
// eslint-disable-next-line @typescript-eslint/ban-types
[key: string]: Function;
}
async function buildModuleFromPath(
moduleName: string,
modulePath: string,
): Promise<Module> {
log(`🔎 Module ${moduleName} found`);
const resolvers = find(modulePath)
.filter((file) => file.match(/\.resolver\.ts$/))
.map((file) => import(`./${file}`));
const fxoRegex = /^f[^x]o$/; // Will match fro, fio, fso but not fxo
if (moduleName.match(fxoRegex)) {
log(`🔎 Adding fxo, swift and position resolver for ${moduleName}`);
resolvers.push(import('./src/modules/fxo/fxo.resolver'));
resolvers.push(import('./src/modules/fxo/position/position.resolver'));
}
return Promise.all(resolvers).then((res) => ({
name: moduleName,
resolvers: flatImport(res),
}));
}
async function generateSchema(module: Module, outDir: string): Promise<void> {
const outFilePath = `${outDir}/${module.name}.graphql`;
log(`🛠 Generating SDL for module ${module.name} in file "${outFilePath}"`);
const app = await NestFactory.create(GraphQLSchemaBuilderModule, {
logger: false,
});
await app.init();
try {
const gqlSchemaFactory = app.get(GraphQLSchemaFactory);
const schema = await gqlSchemaFactory.create(module.resolvers);
const schemaString = printSchema(schema);
fs.writeFileSync(outFilePath, schemaString);
log(`🎊 Module ${module.name} done !`);
} catch (err) {
logError(`💥 Error in module ${module.name}`);
console.error(err);
}
}
// eslint-disable-next-line @typescript-eslint/ban-types
function flatImport(res: FunctionMap[]): Function[] {
return res.reduce((acc, val) => [...acc, ...Object.values(val)], []);
}
function showHelp(): void {
console.log(`Generate GraphQL SDL from nestJS resolver
-i : path to modules folder
-o : output folder
-h : show this help
`);
}
function log(message: string): void {
console.log(chalk.green(message));
}
function logWarn(message: string): void {
console.log(chalk.yellow(message));
}
function logError(message: string): void {
console.log(chalk.red(message));
}