Skip to content

Commit

Permalink
fix(core): ensure updateStorybook in nrwl/workspace:move handle direc… (
Browse files Browse the repository at this point in the history
#10399)

* fix(core): ensure updateStorybook in nrwl/workspace:move handle directories and files

* chore(core): remove console log
  • Loading branch information
nartc committed May 20, 2022
1 parent c17a086 commit 124fd7d
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 5 deletions.
Expand Up @@ -38,6 +38,7 @@ describe('updateStorybookConfig', () => {
`;
const storybookMainPath =
'/libs/namespace/my-destination/.storybook/main.js';

await libraryGenerator(tree, {
name: 'my-source',
standaloneConfig: false,
Expand Down Expand Up @@ -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');`
);
});
});
});
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 124fd7d

Please sign in to comment.