From 5620d0938b8266532bb2ef9f0493ffdabb007534 Mon Sep 17 00:00:00 2001 From: Kevin Gilpin Date: Mon, 15 Jan 2024 12:10:16 -0500 Subject: [PATCH] ci: Migrate runStoryboook test to GitHub --- packages/components/.storybook/run.js | 34 ++++++++++++++------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/components/.storybook/run.js b/packages/components/.storybook/run.js index 12741a7009..7b0a42b520 100644 --- a/packages/components/.storybook/run.js +++ b/packages/components/.storybook/run.js @@ -3,31 +3,26 @@ const { join } = require('path'); const { promisify } = require('util'); const exec = promisify(require('child_process').exec); -const dependentPackages = ['components', 'models']; - -function isTag() { - return Boolean(process.env['TRAVIS_TAG']); -} +// Changes to these packages will not trigger a storybook run, because they are +// downstream of this package. +const nonDependencyPackages = ['cli', 'scanner']; async function isBranch(branch) { - let currentBranch = process.env['TRAVIS_BRANCH']; - if (!currentBranch) { - const { stdout } = await exec('git branch --show-current'); - currentBranch = stdout.trim(); - } + const { stdout } = await exec('git branch --show-current'); + const currentBranch = stdout.trim(); return currentBranch === branch; } -async function hasDependencyChanged() { +async function noDependencyChanged() { const fetchResult = await exec('git fetch origin main:refs/remotes/origin/main'); if (fetchResult.stderr || fetchResult.stdout) - console.warn("output from 'git fetch origin main:refs/remotes/origin/main': ", fetchResult); + console.log("output from 'git fetch origin main:refs/remotes/origin/main': ", fetchResult); const { stdout: changedFiles } = await exec('git diff origin/main --name-only'); return changedFiles .split('\n') - .some((file) => dependentPackages.some((pkg) => file.startsWith(join('packages', pkg)))); + .every((file) => nonDependencyPackages.some((pkg) => file.startsWith(join('packages', pkg)))); } async function runStorybook() { @@ -51,9 +46,16 @@ function isWindows() { } (async () => { - const shouldRunStorybook = - !isWindows() && ((await isBranch('main')) || isTag() || (await hasDependencyChanged())); - + const detectDependencyChanged = !(await noDependencyChanged()); + const platformToTest = !isWindows(); + const runningOnMainBranch = await isBranch('main'); + console.log('Determining whether to run storybook.'); + console.log(`Has dependency changed? ${detectDependencyChanged}`); + console.log(`Running on a platform we want to test? ${platformToTest}`); + console.log(`Running on main branch? ${runningOnMainBranch}`); + + const shouldRunStorybook = platformToTest && (detectDependencyChanged || runningOnMainBranch); + console.log(`Should run storybook? ${shouldRunStorybook}`); if (shouldRunStorybook) { process.exitCode = await runStorybook(); }