UNPKG

@nx/react

Version:

The React plugin for Nx contains executors and generators for managing React applications and libraries within an Nx workspace. It provides: - Integration with libraries such as Jest, Vitest, Playwright, Cypress, and Storybook. - Generators for applica

84 lines (83 loc) 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hookGenerator = hookGenerator; const internal_1 = require("@nx/devkit/internal"); const assert_supported_react_version_1 = require("../../utils/assert-supported-react-version"); // TODO(jack): Remove inline renderHook function when RTL releases with its own version const devkit_1 = require("@nx/devkit"); const internal_2 = require("@nx/js/internal"); const path_1 = require("path"); const ast_utils_1 = require("../../utils/ast-utils"); async function hookGenerator(host, schema) { (0, assert_supported_react_version_1.assertSupportedReactVersion)(host); const options = await normalizeOptions(host, schema); createFiles(host, options); addExportsToBarrel(host, options); return await (0, devkit_1.formatFiles)(host); } function createFiles(host, options) { const specExt = options.fileExtensionType === 'ts' ? 'tsx' : 'js'; (0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, './files'), options.directory, { ...options, ext: options.fileExtension, specExt, isTs: options.fileExtensionType === 'ts', }); if (options.skipTests) { host.delete((0, devkit_1.joinPathFragments)(options.directory, `${options.fileName}.spec.${specExt}`)); } } let tsModule; function addExportsToBarrel(host, options) { if (!tsModule) { tsModule = (0, internal_2.ensureTypescript)(); } const workspace = (0, devkit_1.getProjects)(host); const isApp = workspace.get(options.projectName).projectType === 'application'; if (options.export && !isApp) { const indexFilePath = (0, devkit_1.joinPathFragments)(options.projectSourceRoot, options.fileExtensionType === 'js' ? 'index.js' : 'index.ts'); if (!host.exists(indexFilePath)) { return; } const indexSource = host.read(indexFilePath, 'utf-8'); const indexSourceFile = tsModule.createSourceFile(indexFilePath, indexSource, tsModule.ScriptTarget.Latest, true); const changes = (0, devkit_1.applyChangesToString)(indexSource, (0, ast_utils_1.addImport)(indexSourceFile, `export * from './${options.directory}/${options.fileName}';`)); host.write(indexFilePath, changes); } } async function normalizeOptions(host, options) { const { artifactName, directory, fileName: hookFilename, fileExtension, fileExtensionType, project: projectName, } = await (0, internal_1.determineArtifactNameAndDirectoryOptions)(host, { path: options.path, name: options.name, allowedFileExtensions: ['js', 'ts'], fileExtension: options.js ? 'js' : 'ts', js: options.js, }); const { className } = (0, devkit_1.names)(hookFilename); // if name is provided, use it as is for the hook name, otherwise prepend // `use` to the pascal-cased file name if it doesn't already start with `use` const hookName = options.name ? artifactName : className.toLocaleLowerCase().startsWith('use') ? className : `use${className}`; const hookTypeName = (0, devkit_1.names)(hookName).className; const project = (0, devkit_1.getProjects)(host).get(projectName); const { root, sourceRoot: projectSourceRoot, projectType } = project; if (options.export && (0, internal_2.getProjectType)(host, root, projectType) === 'application') { devkit_1.logger.warn(`The "--export" option should not be used with applications and will do nothing.`); } return { ...options, directory, hookName, hookTypeName, fileName: hookFilename, fileExtension, fileExtensionType, projectSourceRoot, projectName, }; } exports.default = hookGenerator;