|
| 1 | +const path = require('path'); |
| 2 | +const { promisify } = require('util'); |
| 3 | +const exec = promisify(require('child_process').exec); |
| 4 | +const dependencyTree = require('dependency-tree'); |
| 5 | + |
| 6 | +const cwd = process.cwd(); |
| 7 | +const resolutionCache = {}; |
| 8 | + |
| 9 | +// 1. load stories |
| 10 | +// 2. load list per story |
| 11 | +// 3. filter against files |
| 12 | +module.exports = { |
| 13 | + getHighlights, |
| 14 | + getHighlightAnnouncement, |
| 15 | +}; |
| 16 | + |
| 17 | +async function getHighlightAnnouncement({ changedFiles, artifactBase }) { |
| 18 | + const highlights = await getHighlights({ changedFiles }); |
| 19 | + if (!highlights.length) { |
| 20 | + return null; |
| 21 | + } |
| 22 | + const highlightsBody = highlights |
| 23 | + .map((entry) => `\n- [${entry}](${urlForStoryFile(entry, artifactBase)})`) |
| 24 | + .join(''); |
| 25 | + const announcement = `<details> |
| 26 | + <summary>storybook</summary> |
| 27 | + ${highlightsBody} |
| 28 | + </details>\n\n`; |
| 29 | + return announcement; |
| 30 | +} |
| 31 | + |
| 32 | +async function getHighlights({ changedFiles }) { |
| 33 | + const highlights = []; |
| 34 | + const storyFiles = await getAllStories(); |
| 35 | + // check each story file for dep graph overlap with changed files |
| 36 | + for (const storyFile of storyFiles) { |
| 37 | + const list = await getLocalDependencyList(storyFile); |
| 38 | + if (list.some((entry) => changedFiles.includes(entry))) { |
| 39 | + highlights.push(storyFile); |
| 40 | + } |
| 41 | + } |
| 42 | + return highlights; |
| 43 | +} |
| 44 | + |
| 45 | +async function getAllStories() { |
| 46 | + const { stdout } = await exec('find ui -name "*.stories.js"'); |
| 47 | + const matches = stdout.split('\n').slice(0, -1); |
| 48 | + return matches; |
| 49 | +} |
| 50 | + |
| 51 | +async function getLocalDependencyList(filename) { |
| 52 | + const list = dependencyTree |
| 53 | + .toList({ |
| 54 | + filename, |
| 55 | + // not sure what this does but its mandatory |
| 56 | + directory: cwd, |
| 57 | + webpackConfig: `.storybook/main.js`, |
| 58 | + // skip all dependencies |
| 59 | + filter: (entry) => !entry.includes('node_modules'), |
| 60 | + // for memoization across trees: 30s -> 5s |
| 61 | + visited: resolutionCache, |
| 62 | + }) |
| 63 | + .map((entry) => path.relative(cwd, entry)); |
| 64 | + return list; |
| 65 | +} |
| 66 | + |
| 67 | +function urlForStoryFile(filename, artifactBase) { |
| 68 | + const storyId = sanitize(filename); |
| 69 | + return `${artifactBase}/storybook/index.html?path=/story/${storyId}`; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Remove punctuation and illegal characters from a story ID. |
| 74 | + * See: |
| 75 | + * https://gist.github.com/davidjrice/9d2af51100e41c6c4b4a |
| 76 | + * https://github.com/ComponentDriven/csf/blame/7ac941eee85816a4c567ca85460731acb5360f50/src/index.ts |
| 77 | + * |
| 78 | + * @param {string} string - The string to sanitize. |
| 79 | + * @returns The sanitized string. |
| 80 | + */ |
| 81 | +function sanitize(string) { |
| 82 | + return ( |
| 83 | + string |
| 84 | + .toLowerCase() |
| 85 | + // eslint-disable-next-line no-useless-escape |
| 86 | + .replace(/[ ’–—―′¿'`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/giu, '-') |
| 87 | + .replace(/-+/gu, '-') |
| 88 | + .replace(/^-+/u, '') |
| 89 | + .replace(/-+$/u, '') |
| 90 | + ); |
| 91 | +} |
0 commit comments