diff --git a/packages/workspace/src/generators/move/lib/update-storybook-config.spec.ts b/packages/workspace/src/generators/move/lib/update-storybook-config.spec.ts index 17f3a2af6dc5f..b5f0d6f83c6b3 100644 --- a/packages/workspace/src/generators/move/lib/update-storybook-config.spec.ts +++ b/packages/workspace/src/generators/move/lib/update-storybook-config.spec.ts @@ -38,6 +38,7 @@ describe('updateStorybookConfig', () => { `; const storybookMainPath = '/libs/namespace/my-destination/.storybook/main.js'; + await libraryGenerator(tree, { name: 'my-source', standaloneConfig: false, @@ -92,4 +93,103 @@ describe('updateStorybookConfig', () => { `const rootWebpackConfig = require('../../../../.storybook/webpack.config');` ); }); + + describe('directory', () => { + it('should update the import path for directory/main.js', async () => { + const storybookMain = ` + const rootMain = require('../../../.storybook/main'); + module.exports = rootMain; + `; + const storybookMainPath = + '/libs/namespace/my-destination/.storybook/main.js'; + + const storybookNestedMain = ` + const rootMain = require('../../../../.storybook/main'); + module.exports = rootMain; + `; + const storybookNestedMainPath = + '/libs/namespace/my-destination/.storybook/nested/main.js'; + + await libraryGenerator(tree, { + name: 'my-source', + standaloneConfig: false, + }); + const projectConfig = readProjectConfiguration(tree, 'my-source'); + tree.write(storybookMainPath, storybookMain); + tree.write(storybookNestedMainPath, storybookNestedMain); + const schema: NormalizedSchema = { + projectName: 'my-source', + destination: 'namespace/my-destination', + importPath: '@proj/namespace-my-destination', + updateImportPath: true, + newProjectName: 'namespace-my-destination', + relativeToRootDestination: 'libs/namespace/my-destination', + }; + + updateStorybookConfig(tree, schema, projectConfig); + + const storybookMainAfter = tree.read(storybookMainPath, 'utf-8'); + expect(storybookMainAfter).toContain( + `const rootMain = require('../../../../.storybook/main');` + ); + const storybookNestedMainAfter = tree.read( + storybookNestedMainPath, + 'utf-8' + ); + expect(storybookNestedMainAfter).toContain( + `const rootMain = require('../../../../../.storybook/main');` + ); + }); + + it('should update the import path for directory/webpack.config.json', async () => { + const storybookWebpackConfig = ` + const rootWebpackConfig = require('../../../.storybook/webpack.config'); + `; + const storybookWebpackConfigPath = + '/libs/namespace/my-destination/.storybook/webpack.config.js'; + + const storybookNestedWebpackConfig = ` + const rootWebpackConfig = require('../../../../.storybook/webpack.config'); + `; + const storybookNestedWebpackConfigPath = + '/libs/namespace/my-destination/.storybook/nested/webpack.config.js'; + + await libraryGenerator(tree, { + name: 'my-source', + standaloneConfig: false, + }); + const projectConfig = readProjectConfiguration(tree, 'my-source'); + tree.write(storybookWebpackConfigPath, storybookWebpackConfig); + tree.write( + storybookNestedWebpackConfigPath, + storybookNestedWebpackConfig + ); + const schema: NormalizedSchema = { + projectName: 'my-source', + destination: 'namespace/my-destination', + importPath: '@proj/namespace-my-destination', + updateImportPath: true, + newProjectName: 'namespace-my-destination', + relativeToRootDestination: 'libs/namespace/my-destination', + }; + + updateStorybookConfig(tree, schema, projectConfig); + + const storybookWebpackConfigAfter = tree.read( + storybookWebpackConfigPath, + 'utf-8' + ); + expect(storybookWebpackConfigAfter).toContain( + `const rootWebpackConfig = require('../../../../.storybook/webpack.config');` + ); + + const storybookNestedWebpackConfigAfter = tree.read( + storybookNestedWebpackConfigPath, + 'utf-8' + ); + expect(storybookNestedWebpackConfigAfter).toContain( + `const rootWebpackConfig = require('../../../../../.storybook/webpack.config');` + ); + }); + }); }); diff --git a/packages/workspace/src/generators/move/lib/update-storybook-config.ts b/packages/workspace/src/generators/move/lib/update-storybook-config.ts index d448b6f9d1892..977dc21f31f2d 100644 --- a/packages/workspace/src/generators/move/lib/update-storybook-config.ts +++ b/packages/workspace/src/generators/move/lib/update-storybook-config.ts @@ -7,7 +7,9 @@ import { NormalizedSchema } from '../schema'; /** * Updates relative path to root storybook config for `main.js` & `webpack.config.js` * - * @param schema The options provided to the schematic + * @param {Tree} tree + * @param {NormalizedSchema} schema The options provided to the schematic + * @param {ProjectConfiguration} project */ export function updateStorybookConfig( tree: Tree, @@ -42,10 +44,24 @@ export function updateStorybookConfig( } // Replace relative import path to root storybook folder for each file under project storybook - for (const file of tree.children(storybookDir)) { - const oldContent = tree.read(join(storybookDir, file), 'utf-8'); - const newContent = oldContent.replace(oldRelativeRoot, newRelativeRoot); + updateRecursively(tree, storybookDir, oldRelativeRoot, newRelativeRoot); +} + +function updateRecursively( + tree: Tree, + dir: string, + oldRoot: string, + newRoot: string +) { + for (const child of tree.children(dir)) { + const childPath = join(dir, child); - tree.write(join(storybookDir, file), newContent); + if (tree.isFile(childPath)) { + const oldContent = tree.read(childPath, 'utf-8'); + const newContent = oldContent.replace(oldRoot, newRoot); + tree.write(childPath, newContent); + } else { + updateRecursively(tree, childPath, oldRoot, newRoot); + } } }