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

feat: add support for mono repos #2

Open
wants to merge 1 commit into
base: v8
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions dist/buildTree.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#! /usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

/**
* CommonJS does not have top-level `await`, so we can wrap
* everything in an `async` IIFE to make our lives a little easier.
Expand All @@ -10,27 +11,55 @@ Object.defineProperty(exports, "__esModule", { value: true });
const { glob } = require("glob");
const path = require("path");
const fs = require("fs");
const { transpileTypescript, getComponentPathExtension, getStoryMetadata, getBarrelFileComponentPath, editGetDependenciesTree, } = require("./utils/buildTreeUtils.cjs");
const tsfiles = await glob(["**/[A-Z]*.stories.*"], {
ignore: "node_modules/**",
const {
transpileTypescript,
getComponentPathExtension,
getStoryMetadata,
getBarrelFileComponentPath,
editGetDependenciesTree,
} = require("./utils/buildTreeUtils.cjs");

// Determine the base path for globbing based on environment variable
const storybookStoriesPath = process.env.STORYBOOK_STORIES_PATH;

// If environment variable is set, use it; otherwise, use the default behavior
const globPattern = storybookStoriesPath
? [`${storybookStoriesPath}/**/*.stories.@(js|jsx|ts|tsx|mdx)`]
: ["**/[A-Z]*.stories.*"];

// Perform the glob search based on the determined pattern
const tsfiles = await glob(globPattern, {
ignore: ["**/node_modules/**"],
windowsPathsNoEscape: true,
});
const tspaths = tsfiles.map((relativePath) => path.resolve(relativePath));

// If using environment variable path, resolve relative to the provided path; otherwise, use default resolution
const tspaths = storybookStoriesPath
? tsfiles.map((relativePath) => path.resolve(storybookStoriesPath, relativePath))
: tsfiles.map((relativePath) => path.resolve(relativePath));

let storiesComponents = {};

const searchStory = (filePath) => {
return storiesComponents[filePath];
};

for (const storyPath of tspaths) {
const fileContent = fs.readFileSync(storyPath, "utf-8");
const fileString = transpileTypescript(fileContent);
const { title, relativeComponentPath } = getStoryMetadata(fileString);
const componentPath = path.join(path.dirname(storyPath), relativeComponentPath);
storiesComponents[getComponentPathExtension(componentPath)] = title;
try {
const fileContent = fs.readFileSync(storyPath, "utf-8");
const fileString = transpileTypescript(fileContent);
const { title, relativeComponentPath } = getStoryMetadata(fileString);
const componentPath = path.resolve(path.dirname(storyPath), relativeComponentPath); // Correct resolution
storiesComponents[getComponentPathExtension(componentPath)] = title;
} catch (error) {
console.error(`Error processing story path: ${storyPath}`);
console.error(error);
}
}

const dependenciesRecursive = (node) => {
const children = node.children;
if (children.length === 0)
return [];
if (children.length === 0) return [];
const dependencies = children.map((child) => {
const title = child.error
? searchStory(getBarrelFileComponentPath(child.name, child.filePath))
Expand All @@ -43,30 +72,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
});
return dependencies.flat();
};

const dependentsRecursive = (nodeTitle, node) => {
if (!node.parent)
return;
if (!node.parent) return;
const parentTitle = searchStory(node.parent.filePath);
if (!parentTitle) {
return dependentsRecursive(nodeTitle, node.parent);
}
const rootNode = getRootNode(nodeTitle);
rootNode.dependents.push(parentTitle);
};

let tree = {};
const getRootNode = (title) => {
if (!tree[title]) {
tree[title] = { dependencies: [], dependents: [] };
}
return tree[title];
};

for (const componentPath in storiesComponents) {
const title = storiesComponents[componentPath];
const rootNode = getRootNode(title);
rootNode.dependencies = dependenciesRecursive(SaplingParser.parse(componentPath));
}
// console.log(storiesComponents, tree);
// const jsonString = JSON.stringify(tree, null, 2);
// fs.writeFileSync("tree.json", jsonString, "utf8");

editGetDependenciesTree(tree);
})();