UNPKG

1.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.updateStorybookConfig = void 0;
4const devkit_1 = require("@nrwl/devkit");
5const path = require("path");
6const path_1 = require("path");
7/**
8 * Updates relative path to root storybook config for `main.js` & `webpack.config.js`
9 *
10 * @param {Tree} tree
11 * @param {NormalizedSchema} schema The options provided to the schematic
12 * @param {ProjectConfiguration} project
13 */
14function updateStorybookConfig(tree, schema, project) {
15 const oldRelativeRoot = path
16 .relative(path.join(devkit_1.workspaceRoot, `${project.root}/.storybook`), devkit_1.workspaceRoot)
17 .split(path.sep)
18 .join('/');
19 const newRelativeRoot = path
20 .relative(path.join(devkit_1.workspaceRoot, `${schema.relativeToRootDestination}/.storybook`), devkit_1.workspaceRoot)
21 .split(path.sep)
22 .join('/');
23 const storybookDir = path.join(schema.relativeToRootDestination, '.storybook');
24 if (!storybookDir) {
25 return;
26 }
27 // Replace relative import path to root storybook folder for each file under project storybook
28 updateRecursively(tree, storybookDir, oldRelativeRoot, newRelativeRoot);
29}
30exports.updateStorybookConfig = updateStorybookConfig;
31function updateRecursively(tree, dir, oldRoot, newRoot) {
32 for (const child of tree.children(dir)) {
33 const childPath = (0, path_1.join)(dir, child);
34 if (tree.isFile(childPath)) {
35 const oldContent = tree.read(childPath, 'utf-8');
36 const newContent = oldContent.replace(oldRoot, newRoot);
37 tree.write(childPath, newContent);
38 }
39 else {
40 updateRecursively(tree, childPath, oldRoot, newRoot);
41 }
42 }
43}
44//# sourceMappingURL=update-storybook-config.js.map
\No newline at end of file