UNPKG

@applitools/eyes-storybook

Version:
44 lines (37 loc) 1.62 kB
const {resolve} = require('path'); const {execSync} = require('child_process'); const fs = require('fs'); const findNpmModuleCommandPath = require('../findNpmModuleCommandPath'); async function determineStorybookVersion({packagePath, isWindows}) { let sbArg, storybookPath; const commandV6 = 'start-storybook' + (isWindows ? '.cmd' : ''); const commandV7 = 'sb' + (isWindows ? '.cmd' : ''); // works for v8 const commandV9 = 'storybook' + (isWindows ? '.cmd' : ''); // first we look for the binaries in the local node_modules/.bin folder, // in case there are multiple versions of storybook installed for (const command of [commandV6, commandV7, commandV9]) { const storybookPathCommand = resolve(packagePath, 'node_modules/.bin', command); if (fs.existsSync(storybookPathCommand)) { storybookPath = storybookPathCommand; sbArg = command === commandV6 ? '' : 'dev'; break; } } // the binary is not in the local node_modules/.bin folder, // so we need to find it, we do a fallback search up the tree if (!storybookPath) { for (const command of [commandV6, commandV7, commandV9]) { storybookPath = await findNpmModuleCommandPath(command, packagePath); if (storybookPath) { sbArg = command === commandV6 ? '' : 'dev'; break; } } } if (!storybookPath) { throw new Error('Unable to determine storybook version, storybook path not found'); } const sbVersion = execSync(`"${storybookPath}" ${sbArg ?? ''} --version`).toString(); return {storybookPath, sbVersion, sbArg}; } module.exports = determineStorybookVersion;