UNPKG

@nx/storybook

Version:

The Nx Plugin for Storybook contains executors and generators for allowing your workspace to use the powerful Storybook integration testing & documenting capabilities.

72 lines (71 loc) 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.interactionTestsDependencies = interactionTestsDependencies; exports.addInteractionsInAddons = addInteractionsInAddons; const devkit_1 = require("@nx/devkit"); const tsquery_1 = require("@phenomnomnominal/tsquery"); const versions_1 = require("../../../utils/versions"); function interactionTestsDependencies() { return { '@storybook/test-runner': versions_1.storybookTestRunnerVersion, '@storybook/addon-interactions': versions_1.storybookVersion, '@storybook/testing-library': versions_1.storybookTestingLibraryVersion, '@storybook/jest': versions_1.storybookJestVersion, }; } function addInteractionsInAddons(tree, projectConfig) { const mainJsTsPath = getMainTsJsPath(tree, projectConfig); if (!mainJsTsPath) return; let mainJsTs = tree.read(mainJsTsPath, 'utf-8'); if (!mainJsTs) return; // Find addons array const addonsArray = tsquery_1.tsquery.query(mainJsTs, `PropertyAssignment:has(Identifier:has([name="addons"]))`)?.[0]; // if there's no addons array don't do anything // they may be setting up storybook in another project if (!addonsArray) return; // Check if addons array already has addon-interactions const addonsArrayHasAddonInteractions = tsquery_1.tsquery.query(addonsArray, `StringLiteral:has([text="@storybook/addon-interactions"])`)?.[0]; if (addonsArrayHasAddonInteractions) return; // get the array of the addons const arrayLiteralExpression = tsquery_1.tsquery.query(addonsArray, `ArrayLiteralExpression`)?.[0]; if (!arrayLiteralExpression) return; mainJsTs = (0, devkit_1.applyChangesToString)(mainJsTs, [ { type: devkit_1.ChangeType.Insert, index: arrayLiteralExpression.getStart() + 1, text: `'@storybook/addon-interactions', `, }, ]); tree.write(mainJsTsPath, mainJsTs); } function getMainTsJsPath(host, projectConfig) { // Inferred targets from `@nx/storybook/plugin` are inferred from `.storybook/main.{js,ts,mjs,mts,cjs,cts}` so we can assume the directory. if (!projectConfig.targets) { const exts = ['js', 'ts', 'mjs', 'mts', 'cjs', 'cts']; for (const ext of exts) { const candidate = `${projectConfig.root}/.storybook/main.${ext}`; if (host.exists(candidate)) return candidate; } throw new Error(`Cannot find main Storybook file. Does this file exist? e.g. ${projectConfig.root}/.storybook/main.ts`); } let mainJsTsPath = undefined; Object.entries(projectConfig.targets).forEach(([_targetName, targetConfig]) => { if (targetConfig.executor === '@nx/storybook:storybook' || targetConfig.executor === '@storybook/angular:start-storybook') { const configDir = targetConfig.options?.configDir; if (host.exists(`${configDir}/main.js`)) { mainJsTsPath = `${configDir}/main.js`; } if (host.exists(`${configDir}/main.ts`)) { mainJsTsPath = `${configDir}/main.ts`; } } }); return mainJsTsPath; }