UNPKG

inngest-setup-redwoodjs

Version:

Setup Inngest in RedwoodJS

91 lines (90 loc) 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tasks = void 0; const tslib_1 = require("tslib"); const fs_1 = tslib_1.__importDefault(require("fs")); const path_1 = tslib_1.__importDefault(require("path")); const execa_1 = tslib_1.__importDefault(require("execa")); const jscodeshift = tslib_1.__importStar(require("jscodeshift/src/Runner")); const listr2_1 = require("listr2"); const cli_helpers_1 = require("@redwoodjs/cli-helpers"); const project_config_1 = require("@redwoodjs/project-config"); const helpers_1 = require("./helpers"); const tasks = (options) => { const existingFiles = options.force ? 'OVERWRITE' : 'FAIL'; return new listr2_1.Listr([ { title: 'Adding config to redwood.toml...', task: () => { const redwoodTomlPath = (0, project_config_1.getConfigPath)(); const configContent = fs_1.default.readFileSync(redwoodTomlPath, 'utf-8'); const tomlKey = `[experimental.inngest]`; if (!configContent.includes(tomlKey)) { // Use string replace to preserve comments and formatting (0, cli_helpers_1.writeFile)(redwoodTomlPath, configContent.concat(`\n${tomlKey}`), { existingFiles: 'OVERWRITE', // redwood.toml always exists }); } }, }, { title: `Create a ${options.type} Inngest function named ${options.name} ...`, task: () => { const { filename, rendered } = (0, helpers_1.renderFunctionTemplate)(options); (0, helpers_1.writeFunctionFile)(filename, rendered, existingFiles); }, }, { title: `Modify the Inngest handler to register the new ${options.type} Inngest function named ${options.name}...`, task: async () => { const { functionName } = (0, helpers_1.getNamesForFile)(options); const SRC_INNGEST_HANDLER_FILE = path_1.default.join((0, cli_helpers_1.getPaths)().api.functions, 'inngest.ts'); const SRC_INNGEST_CODEMOD_FILE = path_1.default.join(__dirname, '..', 'add-function-to-inngest-handler-codemod.js'); const defaultJscodeshiftOpts = { verbose: 0, dry: false, print: false, babel: false, extensions: 'js', ignorePattern: '**/node_modules/**', ignoreConfig: [], runInBand: false, silent: true, parser: 'ts', parserConfig: {}, failOnError: true, stdin: false, }; const functionImportConfig = [{ import: functionName, from: functionName }]; if (options.type === 'fan-out') { functionImportConfig.push({ import: `${functionName}FanOut`, from: functionName }); } try { await jscodeshift.run(SRC_INNGEST_CODEMOD_FILE, [SRC_INNGEST_HANDLER_FILE], { ...defaultJscodeshiftOpts, functionImportConfig, }); } catch (e) { // eslint-disable-next-line no-console console.error('Failed to modify the GraphQL handler', e.message); } }, }, { title: `Lint and Prettify added files ...`, task: async () => { const SRC_INNGEST_HANDLER_FILE = path_1.default.join((0, cli_helpers_1.getPaths)().api.functions, 'inngest.ts'); execa_1.default.commandSync(`yarn rw lint --fix ${SRC_INNGEST_HANDLER_FILE}`, // eslint-disable-next-line dot-notation process.env['RWJS_CWD'] ? { // eslint-disable-next-line dot-notation cwd: process.env['RWJS_CWD'], } : {}); }, }, ], { rendererOptions: {} }); }; exports.tasks = tasks;