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.

150 lines (147 loc) • 7.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.STORYBOOK_PROP_MAPPINGS = void 0; exports.getConfigFilePath = getConfigFilePath; exports.addConfigValuesToConfigFile = addConfigValuesToConfigFile; exports.ensureViteConfigPathIsRelative = ensureViteConfigPathIsRelative; exports.getInstalledPackageVersion = getInstalledPackageVersion; exports.getInstalledPackageVersionInfo = getInstalledPackageVersionInfo; const tsquery_1 = require("@phenomnomnominal/tsquery"); const devkit_1 = require("@nx/devkit"); const plugin_migration_utils_1 = require("@nx/devkit/src/generators/plugin-migrations/plugin-migration-utils"); const semver_1 = require("semver"); function getConfigFilePath(tree, configDir) { return [ (0, devkit_1.joinPathFragments)(configDir, `main.ts`), (0, devkit_1.joinPathFragments)(configDir, `main.cts`), (0, devkit_1.joinPathFragments)(configDir, `main.mts`), (0, devkit_1.joinPathFragments)(configDir, `main.js`), (0, devkit_1.joinPathFragments)(configDir, `main.cjs`), (0, devkit_1.joinPathFragments)(configDir, `main.mjs`), ].find((f) => tree.exists(f)); } function addConfigValuesToConfigFile(tree, configFile, configValues) { const IMPORT_PROPERTY_SELECTOR = 'ImportDeclaration'; const configFileContents = tree.read(configFile, 'utf-8'); const ast = tsquery_1.tsquery.ast(configFileContents); // AST TO GET SECTION TO APPEND TO const importNodes = (0, tsquery_1.tsquery)(ast, IMPORT_PROPERTY_SELECTOR, { visitAllChildren: true, }); let startPosition = 0; if (importNodes.length !== 0) { const lastImportNode = importNodes[importNodes.length - 1]; startPosition = lastImportNode.getEnd(); } const configValuesString = ` // These options were migrated by @nx/storybook:convert-to-inferred from the project.json file. const configValues = ${JSON.stringify(configValues)}; // Determine the correct configValue to use based on the configuration const nxConfiguration = process.env.NX_TASK_TARGET_CONFIGURATION ?? 'default'; const options = { ...configValues.default, ...(configValues[nxConfiguration] ?? {}) }`; tree.write(configFile, `${configFileContents.slice(0, startPosition)} ${configValuesString} ${configFileContents.slice(startPosition)}`); } exports.STORYBOOK_PROP_MAPPINGS = { v7: { port: 'port', previewUrl: 'preview-url', host: 'host', docs: 'docs', configDir: 'config-dir', logLevel: 'loglevel', quiet: 'quiet', webpackStatsJson: 'webpack-stats-json', debugWebpack: 'debug-webpack', disableTelemetry: 'disable-telemetry', https: 'https', sslCa: 'ssl-ca', sslCert: 'ssl-cert', sslKey: 'ssl-key', smokeTest: 'smoke-test', noOpen: 'no-open', outputDir: 'output-dir', }, v8: { port: 'port', previewUrl: 'preview-url', host: 'host', docs: 'docs', configDir: 'config-dir', logLevel: 'loglevel', quiet: 'quiet', webpackStatsJson: 'stats-json', debugWebpack: 'debug-webpack', disableTelemetry: 'disable-telemetry', https: 'https', sslCa: 'ssl-ca', sslCert: 'ssl-cert', sslKey: 'ssl-key', smokeTest: 'smoke-test', noOpen: 'no-open', outputDir: 'output-dir', }, }; function ensureViteConfigPathIsRelative(tree, configPath, projectName, projectRoot, executorName, migrationLogs) { const configFileContents = tree.read(configPath, 'utf-8'); if (configFileContents.includes('viteFinal:')) { return; } const ast = tsquery_1.tsquery.ast(configFileContents); const REACT_FRAMEWORK_SELECTOR_IDENTIFIERS = 'PropertyAssignment:has(Identifier[name=framework]) PropertyAssignment:has(Identifier[name=name]) StringLiteral[value=@storybook/react-vite]'; const REACT_FRAMEWORK_SELECTOR_STRING_LITERALS = 'PropertyAssignment:has(StringLiteral[value=framework]) PropertyAssignment:has(StringLiteral[value=name]) StringLiteral[value=@storybook/react-vite]'; const VUE_FRAMEWORK_SELECTOR_IDENTIFIERS = 'PropertyAssignment:has(Identifier[name=framework]) PropertyAssignment:has(Identifier[name=name]) StringLiteral[value=@storybook/vue3-vite]'; const VUE_FRAMEWORK_SELECTOR_STRING_LITERALS = 'PropertyAssignment:has(StringLiteral[value=framework]) PropertyAssignment:has(StringLiteral[value=name]) StringLiteral[value=@storybook/vue3-vite]'; const isUsingVite = (0, tsquery_1.tsquery)(ast, REACT_FRAMEWORK_SELECTOR_IDENTIFIERS, { visitAllChildren: true, }).length > 0 || (0, tsquery_1.tsquery)(ast, REACT_FRAMEWORK_SELECTOR_STRING_LITERALS, { visitAllChildren: true, }).length > 0 || (0, tsquery_1.tsquery)(ast, VUE_FRAMEWORK_SELECTOR_STRING_LITERALS, { visitAllChildren: true, }).length > 0 || (0, tsquery_1.tsquery)(ast, VUE_FRAMEWORK_SELECTOR_IDENTIFIERS, { visitAllChildren: true }) .length > 0; if (!isUsingVite) { return; } const VITE_CONFIG_PATH_SELECTOR = 'PropertyAssignment:has(Identifier[name=framework]) PropertyAssignment PropertyAssignment PropertyAssignment:has(Identifier[name=viteConfigPath]) > StringLiteral'; let viteConfigPathNodes = (0, tsquery_1.tsquery)(ast, VITE_CONFIG_PATH_SELECTOR, { visitAllChildren: true, }); if (viteConfigPathNodes.length === 0) { const VITE_CONFIG_PATH_SELECTOR_STRING_LITERALS = 'PropertyAssignment:has(StringLiteral[value=framework]) PropertyAssignment PropertyAssignment PropertyAssignment:has(StringLiteral[value=viteConfigPath]) > StringLiteral:not(StringLiteral[value=viteConfigPath])'; viteConfigPathNodes = (0, tsquery_1.tsquery)(ast, VITE_CONFIG_PATH_SELECTOR_STRING_LITERALS, { visitAllChildren: true, }); if (viteConfigPathNodes.length === 0) { migrationLogs.addLog({ project: projectName, executorName, log: 'Unable to find `viteConfigPath` in Storybook Config. Please ensure the `viteConfigPath` is relative to the project root.', }); return; } } const viteConfigPathNode = viteConfigPathNodes[0]; const pathToViteConfig = viteConfigPathNode.getText().replace(/('|")/g, ''); if (pathToViteConfig.match(/^(\.\.\/|\.\/)/)) { return; } const relativePathToViteConfig = (0, plugin_migration_utils_1.toProjectRelativePath)(pathToViteConfig, projectRoot); tree.write(configPath, `${configFileContents.slice(0, viteConfigPathNode.getStart() + 1)}${relativePathToViteConfig}${configFileContents.slice(viteConfigPathNode.getEnd() - 1)}`); } function getInstalledPackageVersion(tree, pkgName) { const { dependencies, devDependencies } = (0, devkit_1.readJson)(tree, 'package.json'); const version = dependencies?.[pkgName] ?? devDependencies?.[pkgName]; return version; } function getInstalledPackageVersionInfo(tree, pkgName) { const version = getInstalledPackageVersion(tree, pkgName); return version ? { major: (0, semver_1.major)((0, semver_1.coerce)(version)), version } : null; }