UNPKG

inngest-setup-redwoodjs

Version:

Setup Inngest in RedwoodJS

178 lines (177 loc) 8.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateTomlConfig = exports.modifyGraphQLHandlerTask = exports.addInngestHelloWorldExampleTask = exports.addInngestGraphQLPluginTask = exports.configureInngestTask = exports.addScriptToPackageJsonTask = exports.tasks = void 0; const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const execa_1 = tslib_1.__importDefault(require("execa")); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); 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 tasks = (options) => { const PACKAGE_JSON_PATH = path_1.default.join((0, cli_helpers_1.getPaths)().base, 'package.json'); const SRC_INNGEST_PATH = path_1.default.join((0, cli_helpers_1.getPaths)().api.src, 'jobs', 'inngest'); const SRC_LIB_PATH = path_1.default.join((0, cli_helpers_1.getPaths)().api.lib); const SRC_PLUGINS_PATH = path_1.default.join((0, cli_helpers_1.getPaths)().api.src, 'plugins'); const SRC_GRAPHQL_FUNCTION_FILE = path_1.default.join((0, cli_helpers_1.getPaths)().api.functions, 'graphql.ts'); const SRC_INNGEST_CODEMOD_FILE = path_1.default.join(__dirname, '..', 'use-inngest-codemod.js'); const commandPaths = { PACKAGE_JSON_PATH, SRC_GRAPHQL_FUNCTION_FILE, SRC_INNGEST_CODEMOD_FILE, SRC_INNGEST_PATH, SRC_LIB_PATH, SRC_PLUGINS_PATH, }; const existingFiles = options.force ? 'OVERWRITE' : 'FAIL'; return new listr2_1.Listr([ { title: 'Install inngest packages ...', task: () => { execa_1.default.commandSync('yarn workspace api add envelop-plugin-inngest', // eslint-disable-next-line dot-notation process.env['RWJS_CWD'] ? { // eslint-disable-next-line dot-notation cwd: process.env['RWJS_CWD'], } : {}); }, }, { title: 'Configure Inngest ...', task: () => { (0, exports.configureInngestTask)({ commandPaths, existingFiles }); }, }, { title: 'Add the Inngest GraphQL plugin ...', task: () => { (0, exports.addInngestGraphQLPluginTask)({ commandPaths, existingFiles }); }, }, { title: 'Add Inngest helloWorld example ...', task: () => { (0, exports.addInngestHelloWorldExampleTask)({ commandPaths, existingFiles }); }, }, { title: 'Modify the GraphQL handler to the useInngest plugin ...', task: async () => { (0, exports.modifyGraphQLHandlerTask)({ commandPaths }); }, }, { title: 'Add inngest dev script to package.json', task: async () => { (0, exports.addScriptToPackageJsonTask)({ commandPaths }); }, }, { title: 'Adding config to redwood.toml...', task: () => { (0, exports.updateTomlConfig)(); }, }, { title: `Lint and Prettify added files ...`, task: async () => { const SRC_GRAPHQL_FUNCTION_FILE = path_1.default.join((0, cli_helpers_1.getPaths)().api.functions, 'graphql.ts'); execa_1.default.commandSync(`yarn rw lint --fix ${SRC_GRAPHQL_FUNCTION_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; const addScriptToPackageJsonTask = ({ commandPaths, }) => { const packageJson = JSON.parse(fs_extra_1.default.readFileSync(commandPaths.PACKAGE_JSON_PATH, 'utf-8')); if (!packageJson.scripts) { packageJson.scripts = {}; } packageJson.scripts['inngest:dev'] = 'npx inngest-cli@latest dev -u http://localhost:8911/inngest'; (0, cli_helpers_1.writeFile)(commandPaths.PACKAGE_JSON_PATH, JSON.stringify(packageJson, null, 2), { existingFiles: 'OVERWRITE', }); }; exports.addScriptToPackageJsonTask = addScriptToPackageJsonTask; const configureInngestTask = ({ commandPaths, existingFiles, }) => { // save inngest handler function in api functions const inngestServerFunctionTemplate = fs_extra_1.default.readFileSync(path_1.default.resolve(__dirname, '..', '..', 'templates', 'plugin', 'inngest.ts.template'), 'utf-8'); (0, cli_helpers_1.writeFile)(path_1.default.join((0, cli_helpers_1.getPaths)().api.functions, 'inngest.ts'), inngestServerFunctionTemplate, { existingFiles, }); // save inngest client to a api lib fs_extra_1.default.ensureDirSync(commandPaths.SRC_LIB_PATH); const inngestClientTemplate = fs_extra_1.default.readFileSync(path_1.default.resolve(__dirname, '..', '..', 'templates', 'plugin', 'client.ts.template'), 'utf-8'); (0, cli_helpers_1.writeFile)(path_1.default.join(commandPaths.SRC_LIB_PATH, 'inngest.ts'), inngestClientTemplate, { existingFiles: 'OVERWRITE', }); }; exports.configureInngestTask = configureInngestTask; const addInngestGraphQLPluginTask = ({ commandPaths, existingFiles, }) => { // save inngest plugin to a new plugins folder fs_extra_1.default.ensureDirSync(commandPaths.SRC_PLUGINS_PATH); const inngestPluginTemplate = fs_extra_1.default.readFileSync(path_1.default.resolve(__dirname, '..', '..', 'templates', 'plugin', 'plugin.ts.template'), 'utf-8'); (0, cli_helpers_1.writeFile)(path_1.default.join(commandPaths.SRC_PLUGINS_PATH, 'useInngest.ts'), inngestPluginTemplate, { existingFiles, }); }; exports.addInngestGraphQLPluginTask = addInngestGraphQLPluginTask; const addInngestHelloWorldExampleTask = ({ commandPaths, existingFiles, }) => { // save example inngest functions in the inngest folder fs_extra_1.default.ensureDirSync(commandPaths.SRC_INNGEST_PATH); const inngestHelloWorldTemplate = fs_extra_1.default.readFileSync(path_1.default.resolve(__dirname, '..', '..', 'templates', 'plugin', 'helloWorld.ts.template'), 'utf-8'); return (0, cli_helpers_1.writeFile)(path_1.default.join(commandPaths.SRC_INNGEST_PATH, 'helloWorld.ts'), inngestHelloWorldTemplate, { existingFiles, }); }; exports.addInngestHelloWorldExampleTask = addInngestHelloWorldExampleTask; const modifyGraphQLHandlerTask = async ({ commandPaths, dry = 0, }) => { const defaultJscodeshiftOpts = { verbose: 0, dry, print: false, babel: false, extensions: 'js', ignorePattern: '**/node_modules/**', ignoreConfig: [], runInBand: false, silent: true, parser: 'ts', parserConfig: {}, failOnError: true, stdin: false, }; try { await jscodeshift.run(commandPaths.SRC_INNGEST_CODEMOD_FILE, [commandPaths.SRC_GRAPHQL_FUNCTION_FILE], { ...defaultJscodeshiftOpts, }); } catch (e) { // eslint-disable-next-line no-console console.error('Failed to modify the GraphQL handler', e.message); } }; exports.modifyGraphQLHandlerTask = modifyGraphQLHandlerTask; const updateTomlConfig = () => { const redwoodTomlPath = (0, project_config_1.getConfigPath)(); const configContent = fs_extra_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 }); } }; exports.updateTomlConfig = updateTomlConfig;