UNPKG

chimp

Version:

Your development companion for doing quality, faster.

72 lines (71 loc) 3.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const core_1 = require("@oclif/core"); const generate_module_1 = require("../generate/generate-module"); const execQuietly_1 = require("../generate/helpers/execQuietly"); const path = tslib_1.__importStar(require("node:path")); const fs = tslib_1.__importStar(require("node:fs")); const findProjectMainPath_1 = require("../generate/helpers/findProjectMainPath"); const ListrHelper_1 = require("../generate/helpers/ListrHelper"); const runTypeGen = async (projectMainPath, appPrefix) => { const customCodegenConfig = path.join(projectMainPath, './codegen.js'); const codegenConfigPath = fs.existsSync(customCodegenConfig) ? customCodegenConfig : path.join(__dirname, '../generate/runtime-config-helpers/codegen.js'); await (0, execQuietly_1.execQuietly)(`PROJECT_PATH=${projectMainPath} APP_PREFIX=${appPrefix} npx graphql-codegen --config ${codegenConfigPath}`, { cwd: path.join(__dirname, '../../'), }); }; const fixGenerated = async (projectMainPath) => { const fixGeneratedPath = path.join(__dirname, '../generate/runtime-config-helpers/fix-generated.js'); await (0, execQuietly_1.execQuietly)(`node ${fixGeneratedPath}`, {}); const customFixGenerated = path.join(projectMainPath, 'fix-generated.js'); const fixCustomGeneratedPath = fs.existsSync(customFixGenerated) ? customFixGenerated : null; if (fixCustomGeneratedPath) { await (0, execQuietly_1.execQuietly)(`node ${fixCustomGeneratedPath}`, { cwd: projectMainPath }); } }; const prettifyGenerated = async (projectMainPath, modulesPath = 'src') => { await (0, execQuietly_1.execQuietly)(`npx prettier --ignore-path /dev/null --write "${modulesPath}/**/*.ts" "generated/**/*.ts" --log-level error`, { cwd: projectMainPath, }); }; class Generate extends core_1.Command { async run() { const { flags } = await this.parse(Generate); const projectMainPath = (0, findProjectMainPath_1.findProjectMainPath)(); const tasks = (0, ListrHelper_1.setupListr)([ (0, ListrHelper_1.newTask)('Generating code', async () => (0, generate_module_1.executeGeneration)(flags.appPrefix, flags.generatedPrefix, flags.modulesPath)), (0, ListrHelper_1.newTask)('Generating types', async () => runTypeGen(projectMainPath, flags.appPrefix)), (0, ListrHelper_1.newTask)('Tweak the generated types', async () => fixGenerated(projectMainPath)), (0, ListrHelper_1.newTask)('Prettify the generated code', async () => prettifyGenerated(projectMainPath, flags.modulesPath)), ]); try { await tasks.run(); } catch (error) { console.error(error); } } } Generate.description = 'generate GraphQL code'; Generate.examples = ['$ chimp generate', '$ chimp generate -a ~src -g ~chimp-helpers']; Generate.flags = { help: core_1.Flags.help({ char: 'h' }), appPrefix: core_1.Flags.string({ char: 'a', description: 'prefix that points to the sourcecode of your app', default: '~app', }), generatedPrefix: core_1.Flags.string({ char: 'g', description: 'prefix that points to the generated by chimp helper code', default: '~generated', }), modulesPath: core_1.Flags.string({ char: 'p', description: 'path to the graphQL modules, only use if you are migrating an existing Apollo App and you want to use chimp only for a part of it', }), }; exports.default = Generate;