venom-generator
Version:
In the way of code generation, complete the storage model->computation model (business API) transformation to achieve the goal of design and implementation
74 lines • 3.02 kB
JavaScript
;
// 程序服务模块,简化index文件
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path_1 = require("path");
const Resolver = require("./resolver");
// 存储模型->计算模型(业务 API)转换
function resolvers(config) {
const server = config.server ? config.server : '';
const output = config.output ? config.output : '';
const yml = config.yml ? config.yml : './prisma';
const dev = config.dev ? config.dev : false;
const resolversPath = `${output}/resolvers`;
// 判断文件夹是否存在
if (!fs.existsSync(output))
fs.mkdirSync(output);
if (!fs.existsSync(resolversPath))
fs.mkdirSync(resolversPath);
const common = /common/.test(resolversPath);
// 生成Query.ts文件
const qeruy = path_1.join(resolversPath, 'Query.ts');
const qData = Resolver.querys(server, yml, output, common, dev);
fs.writeFileSync(qeruy, qData.apiData);
// 生成Mutation.ts文件
const mData = Resolver.mutations(server, yml, output, common, dev).apiData;
if (mData !== '') {
const mutation = path_1.join(resolversPath, 'Mutation.ts');
fs.writeFileSync(mutation, mData);
}
// 是否需要写入自定义方法文件[指定路径无文件时再次获取./prisma文件夹下是否存在]
let custom = `${yml}/${server}/${server}.ts`;
if (!fs.existsSync(custom))
custom = `./prisma/${server}/${server}.ts`;
if (fs.existsSync(custom)) {
let data = '';
if (common) {
if (qData.againCommon) {
data = fs
.readFileSync(custom)
.toString()
.replace(/..\/..\/src\/generated\/common\/poclicy/g, '../poclicy')
.replace(/..\/..\/src\/generated/g, '..');
}
else {
data = fs
.readFileSync(custom)
.toString()
.replace(/..\/..\/src\/generated\/common\/poclicy/g, '..')
.replace(/..\/..\/src\/generated/g, '..');
}
}
else {
data = fs
.readFileSync(custom)
.toString()
.replace(/..\/..\/src\/generated/g, '..');
}
const customData = getPrismaClient(data.replace(getCaption(data), ''));
const customfile = path_1.join(resolversPath, `${server}.ts`);
fs.writeFileSync(customfile, `import { Context } from '../context'\n${customData}`);
}
}
exports.resolvers = resolvers;
// 获取需要忽略的代码块
function getCaption(str) {
const index = str.indexOf('@ignore#');
str = str.substring(index - 3, str.length);
return str;
}
// 自定义文件忽略导入的测试模块
function getPrismaClient(str) {
return str.replace(/import { Prisma(.*)prisma-client'/g, '');
}
//# sourceMappingURL=service.js.map