2018-12-21 11:13:52 +02:00

82 lines
2.7 KiB
JavaScript

import mergeDirs from 'merge-dirs';
import path from 'path';
import shell from 'shelljs';
import chalk from 'chalk';
import { getVersions, getPackageJson, writePackageJson, paddedLog } from '../../lib/helpers';
export default async (npmOptions, installServer) => {
const [
storybookVersion,
actionsVersion,
linksVersion,
addonsVersion,
babelCoreVersion,
babelRuntimeVersion,
propTypesVersion,
] = await getVersions(
npmOptions,
'@storybook/react-native',
'@storybook/addon-actions',
'@storybook/addon-links',
'@storybook/addons',
'babel-core',
'babel-runtime',
'prop-types'
);
// copy all files from the template directory to project directory
mergeDirs(path.resolve(__dirname, 'template/'), '.', 'overwrite');
// set correct project name on entry files if possible
const dirname = shell.ls('-d', 'ios/*.xcodeproj').stdout;
// Only notify about app name if running in React Native vanilla (Expo projects do not have ios directory)
if (dirname) {
const projectName =
dirname && dirname.slice('ios/'.length, dirname.length - '.xcodeproj'.length - 1);
if (projectName) {
shell.sed('-i', '%APP_NAME%', projectName, 'storybook/storybook.js');
} else {
paddedLog(
chalk.red(
'ERR: Could not determine project name, to fix: https://github.com/storybooks/storybook/issues/1277'
)
);
}
}
const packageJson = getPackageJson();
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};
if (installServer) {
packageJson.devDependencies['@storybook/react-native-server'] = storybookVersion;
}
packageJson.devDependencies['@storybook/addon-actions'] = actionsVersion;
packageJson.devDependencies['@storybook/addon-links'] = linksVersion;
packageJson.devDependencies['@storybook/addons'] = addonsVersion;
if (!packageJson.dependencies['babel-core'] && !packageJson.devDependencies['babel-core']) {
packageJson.devDependencies['babel-core'] = babelCoreVersion;
}
if (!packageJson.dependencies['babel-runtime'] && !packageJson.devDependencies['babel-runtime']) {
packageJson.devDependencies['babel-runtime'] = babelRuntimeVersion;
}
if (!packageJson.dependencies['react-dom'] && !packageJson.devDependencies['react-dom']) {
const reactVersion = packageJson.dependencies.react;
packageJson.devDependencies['react-dom'] = reactVersion;
}
if (!packageJson.dependencies['prop-types'] && !packageJson.devDependencies['prop-types']) {
packageJson.devDependencies['prop-types'] = propTypesVersion;
}
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'storybook start';
writePackageJson(packageJson);
};