Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): ensure updateStorybook in nrwl/workspace:move handle direc… #10399

Merged
merged 2 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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');`
);
});
});
});
Original file line number Diff line number Diff line change
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);
}
}
}