Skip to content

Commit

Permalink
ci: Migrate runStoryboook test to GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
kgilpin committed Jan 15, 2024
1 parent 19a2b90 commit 5620d09
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions packages/components/.storybook/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
}
Expand Down

0 comments on commit 5620d09

Please sign in to comment.