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

70 lines (69 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createComponentStoriesFile = createComponentStoriesFile; exports.findPropsAndGenerateFile = findPropsAndGenerateFile; exports.componentStoryGenerator = componentStoryGenerator; const devkit_1 = require("@nx/devkit"); const ast_utils_1 = require("../../utils/ast-utils"); const component_props_1 = require("../../utils/component-props"); const ensure_typescript_1 = require("@nx/js/src/utils/typescript/ensure-typescript"); let tsModule; function createComponentStoriesFile(host, { project, componentPath, interactionTests }) { if (!tsModule) { tsModule = (0, ensure_typescript_1.ensureTypescript)(); } const proj = (0, devkit_1.getProjects)(host).get(project); const componentFilePath = (0, devkit_1.joinPathFragments)(proj.sourceRoot ?? proj.root, componentPath); const componentDirectory = componentFilePath.replace(componentFilePath.slice(componentFilePath.lastIndexOf('/')), ''); const isPlainJs = componentFilePath.endsWith('.jsx') || componentFilePath.endsWith('.js'); const componentFileName = componentFilePath .slice(componentFilePath.lastIndexOf('/') + 1) .replace('.tsx', '') .replace('.jsx', '') .replace('.js', ''); const name = componentFileName; const contents = host.read(componentFilePath, 'utf-8'); if (contents === null) { throw new Error(`Failed to read ${componentFilePath}`); } const sourceFile = tsModule.createSourceFile(componentFilePath, contents, tsModule.ScriptTarget.Latest, true); const cmpDeclaration = (0, ast_utils_1.getComponentNode)(sourceFile); if (!cmpDeclaration) { const componentNodes = (0, ast_utils_1.findExportDeclarationsForJsx)(sourceFile); if (componentNodes?.length) { componentNodes.forEach((declaration) => { findPropsAndGenerateFile(host, sourceFile, declaration, componentDirectory, name, interactionTests, isPlainJs, componentNodes.length > 1); }); } else { throw new Error(`Could not find any React component in file ${componentFilePath}`); } } else { findPropsAndGenerateFile(host, sourceFile, cmpDeclaration, componentDirectory, name, interactionTests, isPlainJs); } } function findPropsAndGenerateFile(host, sourceFile, cmpDeclaration, componentDirectory, name, interactionTests, isPlainJs, fromNodeArray) { const { props, argTypes } = (0, component_props_1.getComponentPropDefaults)(sourceFile, cmpDeclaration); (0, devkit_1.generateFiles)(host, (0, devkit_1.joinPathFragments)(__dirname, `./files${isPlainJs ? '/jsx' : '/tsx'}`), (0, devkit_1.normalizePath)(componentDirectory), { tmpl: '', componentFileName: fromNodeArray ? `${name}--${cmpDeclaration.name.text}` : name, componentImportFileName: name, props, argTypes, componentName: cmpDeclaration.name.text, interactionTests, }); } async function componentStoryGenerator(host, schema) { createComponentStoriesFile(host, { ...schema, interactionTests: schema.interactionTests ?? true, }); if (!schema.skipFormat) { await (0, devkit_1.formatFiles)(host); } } exports.default = componentStoryGenerator;