Skip to content

Commit

Permalink
Merge pull request #109 from happo/improve-detection
Browse files Browse the repository at this point in the history
Improve storybook version detection
  • Loading branch information
trotzig authored Jan 16, 2024
2 parents 6e02b97 + f6ed785 commit ccc6269
Show file tree
Hide file tree
Showing 12 changed files with 1,658 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
browser: true,
es2021: true,
node: true,
'jest/globals': true,
},
extends: ['eslint:recommended', 'plugin:react/recommended'],
parserOptions: {
Expand All @@ -12,6 +13,6 @@ module.exports = {
ecmaVersion: 13,
sourceType: 'module',
},
plugins: ['react'],
plugins: ['react', 'jest'],
rules: {},
};
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- run: corepack enable
- run: yarn install --frozen-lockfile
- run: yarn lint
- run: yarn test
- run: yarn build && yarn happo run
- run: yarn build-local-storybook && HAPPO_USE_PREBUILT_PACKAGE=yes yarn happo run

Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.happo-out
test
29 changes: 29 additions & 0 deletions getStorybookVersionFromPackageJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require('fs');
const path = require('path');

module.exports = function getStorybookVersionFromPackageJson(
packageJsonPath = path.join(process.cwd(), 'package.json'),
) {
const data = fs.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(data);

const combinedDependencies = {
...packageJson.dependencies,
...packageJson.devDependencies,
};

const storybookPackage = [
'storybook',
'@storybook/react',
'@storybook/angular',
'@storybook/vue',
].find((pkg) => combinedDependencies[pkg]);

if (storybookPackage) {
const storybookVersion = combinedDependencies[storybookPackage];
const majorVersion = parseInt(storybookVersion.match(/\d/)[0], 10);
return majorVersion;
} else {
throw new Error('Storybook is not listed as a dependency in package.json');
}
};
28 changes: 3 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,10 @@ const path = require('path');
const Archiver = require('archiver');
const rimraf = require('rimraf');

const { HAPPO_DEBUG, HAPPO_STORYBOOK_BUILD_COMMAND } = process.env;

function getStorybookVersionFromPackageJson() {
const packageJsonPath = path.join(process.cwd(), 'package.json');
const data = fs.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(data);

const combinedDependencies = {
...packageJson.dependencies,
...packageJson.devDependencies,
};
const getStorybookVersionFromPackageJson =
require('./getStorybookVersionFromPackageJson');

const storybookPackage = [
'@storybook/react',
'@storybook/angular',
'@storybook/vue',
].find((pkg) => combinedDependencies[pkg]);

if (storybookPackage) {
const storybookVersion = combinedDependencies[storybookPackage];
const majorVersion = parseInt(storybookVersion.match(/\d/)[0], 10);
return majorVersion;
} else {
throw new Error('Storybook is not listed as a dependency in package.json');
}
}
const { HAPPO_DEBUG, HAPPO_STORYBOOK_BUILD_COMMAND } = process.env;

function zipFolderToBuffer(outputDir) {
return new Promise((resolve, reject) => {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@
"@storybook/testing-library": "^0.2.2",
"babel-loader": "^8.0.5",
"eslint": "^8.6.0",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-react": "^7.28.0",
"file-loader": "^3.0.1",
"happo.io": "^5.1.3",
"jest": "^29.7.0",
"raw-loader": "^1.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
47 changes: 47 additions & 0 deletions test/getStorybookVersionFromPackageJson-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const path = require('path');

const getStorybookVersionFromPackageJson = require('../getStorybookVersionFromPackageJson');

describe('with project package.json', () => {
it('finds the right version', () => {
const version = getStorybookVersionFromPackageJson();
expect(version).toBe(7);
});
});

describe('with storybook 6', () => {
it('finds the right version', () => {
const version = getStorybookVersionFromPackageJson(
path.resolve(__dirname, 'v6-package.json'),
);
expect(version).toBe(6);
});
});

describe('with storybook 7', () => {
it('finds the right version', () => {
const version = getStorybookVersionFromPackageJson(
path.resolve(__dirname, 'v7-package.json'),
);
expect(version).toBe(7);
});
});

describe('with no dev dependencies', () => {
it('finds the right version', () => {
const version = getStorybookVersionFromPackageJson(
path.resolve(__dirname, 'no-devdeps-package.json'),
);
expect(version).toBe(7);
});
});

describe('with no storybook dependencies', () => {
it('throws', () => {
expect(() =>
getStorybookVersionFromPackageJson(
path.resolve(__dirname, 'no-storybook-package.json'),
),
).toThrow(/not listed/);
});
});
8 changes: 8 additions & 0 deletions test/no-devdeps-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test-project-no-storybook",
"version": "0.0.0",
"dependencies": {
"@babel/runtime": "7.23.8",
"storybook": "7"
}
}
10 changes: 10 additions & 0 deletions test/no-storybook-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "test-project-no-storybook",
"version": "0.0.0",
"dependencies": {
"@babel/runtime": "7.23.8"
},
"devDependencies": {
"webpack-dev-server": "4.15.1"
}
}
13 changes: 13 additions & 0 deletions test/v6-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "test-project-v6",
"version": "0.0.0",
"dependencies": {
"@babel/runtime": "7.23.8"
},
"devDependencies": {
"@storybook/react": "^6.1.0",
"@storybook/testing-library": "0.2.2",
"storybook": "~6.1.0",
"webpack-dev-server": "4.15.1"
}
}
11 changes: 11 additions & 0 deletions test/v7-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "test-project-v7",
"version": "0.0.0",
"dependencies": {
"@babel/runtime": "7.23.8"
},
"devDependencies": {
"@storybook/vue": "^7.1.0",
"webpack-dev-server": "4.15.1"
}
}
Loading

0 comments on commit ccc6269

Please sign in to comment.