-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from happo/improve-detection
Improve storybook version detection
- Loading branch information
Showing
12 changed files
with
1,658 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
.happo-out | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.