Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CLI #1840

Merged
merged 5 commits into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/cli/bin/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,6 @@ const runGenerator = () => {
};

runGenerator().catch(ex => {
logger.error(chalk.red(ex.stack));
logger.error(`\n ${chalk.red(ex.stack)}`);
process.exit(1);
});
44 changes: 22 additions & 22 deletions lib/cli/generators/METEOR/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import path from 'path';
import fs from 'fs';
import JSON5 from 'json5';
import latestVersion from 'latest-version';
import mergeDirs from 'merge-dirs';
import { getPackageJson, writePackageJson } from '../../lib/helpers';
import { devDependencies } from '../../package.json';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
const storybookVersion = devDependencies['@storybook/react'];
const actionsVersion = devDependencies['@storybook/addon-actions'];
const linksVersion = devDependencies['@storybook/addon-links'];

const [
storybookVersion,
actionsVersion,
linksVersion,
reactVersion,
reactDomVersion,
presetEnvVersion,
presetReactVersion,
presetStage1Version,
rootSlashImportVersion,
] = await Promise.all([
latestVersion('react'),
latestVersion('react-dom'),
latestVersion('babel-preset-env'),
latestVersion('babel-preset-react'),
latestVersion('babel-preset-stage-1'),
latestVersion('babel-root-slash-import'),
]);
] = await getVersions(
'@storybook/react',
'@storybook/addon-actions',
'@storybook/addon-links',
'react',
'react-dom',
'babel-preset-env',
'babel-preset-react',
'babel-preset-stage-1',
'babel-root-slash-import'
);

mergeDirs(path.resolve(__dirname, 'template/'), '.', 'overwrite');

Expand All @@ -43,18 +43,18 @@ export default async () => {

if (babelrc.plugins.indexOf('babel-root-slash-import') < 0) {
babelrc.plugins.push('babel-root-slash-import');
packageJson.devDependencies['babel-root-slash-import'] = `^${rootSlashImportVersion}`;
packageJson.devDependencies['babel-root-slash-import'] = rootSlashImportVersion;
}
} else {
babelrc = {
presets: ['env', 'react', 'stage-1'],
plugins: ['babel-root-slash-import'],
};

packageJson.devDependencies['babel-preset-env'] = `^${presetEnvVersion}`;
packageJson.devDependencies['babel-preset-react'] = `^${presetReactVersion}`;
packageJson.devDependencies['babel-preset-stage-1'] = `^${presetStage1Version}`;
packageJson.devDependencies['babel-root-slash-import'] = `^${rootSlashImportVersion}`;
packageJson.devDependencies['babel-preset-env'] = presetEnvVersion;
packageJson.devDependencies['babel-preset-react'] = presetReactVersion;
packageJson.devDependencies['babel-preset-stage-1'] = presetStage1Version;
packageJson.devDependencies['babel-root-slash-import'] = rootSlashImportVersion;
}

fs.writeFileSync('.babelrc', JSON.stringify(babelrc, null, 2), 'utf8');
Expand All @@ -68,10 +68,10 @@ export default async () => {

// add react packages.
if (!packageJson.dependencies.react) {
packageJson.dependencies.react = `^${reactVersion}`;
packageJson.dependencies.react = reactVersion;
}
if (!packageJson.dependencies['react-dom']) {
packageJson.dependencies['react-dom'] = `^${reactDomVersion}`;
packageJson.dependencies['react-dom'] = reactDomVersion;
}

writePackageJson(packageJson);
Expand Down
11 changes: 6 additions & 5 deletions lib/cli/generators/REACT/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import path from 'path';
import mergeDirs from 'merge-dirs';
import { getPackageJson, writePackageJson } from '../../lib/helpers';
import { devDependencies } from '../../package.json';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
const storybookVersion = devDependencies['@storybook/react'];
const actionsVersion = devDependencies['@storybook/addon-actions'];
const linksVersion = devDependencies['@storybook/addon-links'];
const [storybookVersion, actionsVersion, linksVersion] = await getVersions(
'@storybook/react',
'@storybook/addon-actions',
'@storybook/addon-links'
);

mergeDirs(path.resolve(__dirname, 'template/'), '.', 'overwrite');

Expand Down
16 changes: 8 additions & 8 deletions lib/cli/generators/REACT_NATIVE/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import mergeDirs from 'merge-dirs';
import path from 'path';
import shell from 'shelljs';
import latestVersion from 'latest-version';
import chalk from 'chalk';
import { getPackageJson, writePackageJson, paddedLog } from '../../lib/helpers';
import { devDependencies } from '../../package.json';
import { getVersions, getPackageJson, writePackageJson, paddedLog } from '../../lib/helpers';

export default async () => {
const storybookVersion = devDependencies['@storybook/react-native'];
const actionsVersion = devDependencies['@storybook/addon-actions'];
const linksVersion = devDependencies['@storybook/addon-links'];
const propTypesVersion = await latestVersion('prop-types');
const [storybookVersion, actionsVersion, linksVersion, propTypesVersion] = await getVersions(
'@storybook/react-native',
'@storybook/addon-actions',
'@storybook/addon-links',
'prop-types'
);

// copy all files from the template directory to project directory
mergeDirs(path.resolve(__dirname, 'template/'), '.', 'overwrite');
Expand Down Expand Up @@ -44,7 +44,7 @@ export default async () => {
}

if (!packageJson.dependencies['prop-types'] && !packageJson.devDependencies['prop-types']) {
packageJson.devDependencies['prop-types'] = `^${propTypesVersion}`;
packageJson.devDependencies['prop-types'] = propTypesVersion;
}

packageJson.scripts = packageJson.scripts || {};
Expand Down
16 changes: 8 additions & 8 deletions lib/cli/generators/REACT_NATIVE_SCRIPTS/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import mergeDirs from 'merge-dirs';
import path from 'path';
import latestVersion from 'latest-version';
import { getPackageJson, writePackageJson } from '../../lib/helpers';
import { devDependencies } from '../../package.json';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
const storybookVersion = devDependencies['@storybook/react-native'];
const actionsVersion = devDependencies['@storybook/addon-actions'];
const linksVersion = devDependencies['@storybook/addon-links'];
const propTypesVersion = await latestVersion('prop-types');
const [storybookVersion, actionsVersion, linksVersion, propTypesVersion] = await getVersions(
'@storybook/react-native',
'@storybook/addon-actions',
'@storybook/addon-links',
'prop-types'
);

// copy all files from the template directory to project directory
mergeDirs(path.resolve(__dirname, 'template/'), '.', 'overwrite');
Expand All @@ -28,7 +28,7 @@ export default async () => {
}

if (!packageJson.dependencies['prop-types'] && !packageJson.devDependencies['prop-types']) {
packageJson.devDependencies['prop-types'] = `^${propTypesVersion}`;
packageJson.devDependencies['prop-types'] = propTypesVersion;
}

packageJson.scripts = packageJson.scripts || {};
Expand Down
11 changes: 6 additions & 5 deletions lib/cli/generators/REACT_SCRIPTS/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import mergeDirs from 'merge-dirs';
import path from 'path';
import fs from 'fs';
import { getPackageJson, writePackageJson } from '../../lib/helpers';
import { devDependencies } from '../../package.json';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
const storybookVersion = devDependencies['@storybook/react'];
const actionsVersion = devDependencies['@storybook/addon-actions'];
const linksVersion = devDependencies['@storybook/addon-links'];
const [storybookVersion, actionsVersion, linksVersion] = await getVersions(
'@storybook/react',
'@storybook/addon-actions',
'@storybook/addon-links'
);

mergeDirs(path.resolve(__dirname, 'template/'), '.', 'overwrite');

Expand Down
11 changes: 6 additions & 5 deletions lib/cli/generators/SFC_VUE/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import mergeDirs from 'merge-dirs';
import path from 'path';
import { getPackageJson, writePackageJson } from '../../lib/helpers';
import { devDependencies } from '../../package.json';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
const storybookVersion = devDependencies['@storybook/vue'];
const actionsVersion = devDependencies['@storybook/addon-actions'];
const linksVersion = devDependencies['@storybook/addon-links'];
const [storybookVersion, actionsVersion, linksVersion] = await getVersions(
'@storybook/vue',
'@storybook/addon-actions',
'@storybook/addon-links'
);

mergeDirs(path.resolve(__dirname, 'template'), '.', 'overwrite');

Expand Down
26 changes: 12 additions & 14 deletions lib/cli/generators/UPDATE_PACKAGE_ORGANIZATIONS/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import path from 'path';
import { spawn } from 'child-process-promise';
import { packageNames } from '@storybook/codemod';
import { getPackageJson, writePackageJson } from '../../lib/helpers';
import ownPackageJson from '../../package.json';
import { getVersion, getPackageJson, writePackageJson } from '../../lib/helpers';

function updatePackage(devDependencies, oldName, newName) {
async function updatePackage(devDependencies, oldName, newName) {
if (devDependencies[oldName]) {
/* eslint-disable no-param-reassign */
delete devDependencies[oldName];
devDependencies[newName] =
newName === '@storybook/cli'
? `^${ownPackageJson.version}`
: ownPackageJson.devDependencies[newName];
devDependencies[newName] = await getVersion(newName);
/* eslint-enable */
}
}

function updatePackageJson() {
async function updatePackageJson() {
const packageJson = getPackageJson();
const { devDependencies } = packageJson;

Object.keys(packageNames).map(oldName => {
const newName = packageNames[oldName];
return updatePackage(devDependencies, oldName, newName);
});
await Promise.all(
Object.keys(packageNames).map(oldName => {
const newName = packageNames[oldName];
return updatePackage(devDependencies, oldName, newName);
})
);

if (!devDependencies['@storybook/react'] && !devDependencies['@storybook/react-native']) {
throw new Error('Expected to find `@kadira/[react-native]-storybook` in devDependencies');
Expand All @@ -47,7 +45,7 @@ function updateSourceCode(parser) {
return spawn(jscodeshiftCommand, args, { stdio: 'inherit' });
}

export default parser => {
updatePackageJson();
export default async parser => {
await updatePackageJson();
return updateSourceCode(parser);
};
11 changes: 6 additions & 5 deletions lib/cli/generators/VUE/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import mergeDirs from 'merge-dirs';
import path from 'path';
import { getPackageJson, writePackageJson } from '../../lib/helpers';
import { devDependencies } from '../../package.json';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
const storybookVersion = devDependencies['@storybook/vue'];
const actionsVersion = devDependencies['@storybook/addon-actions'];
const linksVersion = devDependencies['@storybook/addon-links'];
const [storybookVersion, actionsVersion, linksVersion] = await getVersions(
'@storybook/vue',
'@storybook/addon-actions',
'@storybook/addon-links'
);

mergeDirs(path.resolve(__dirname, 'template'), '.', 'overwrite');

Expand Down
11 changes: 6 additions & 5 deletions lib/cli/generators/WEBPACK_REACT/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import mergeDirs from 'merge-dirs';
import path from 'path';
import { getPackageJson, writePackageJson } from '../../lib/helpers';
import { devDependencies } from '../../package.json';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
const storybookVersion = devDependencies['@storybook/react'];
const actionsVersion = devDependencies['@storybook/addon-actions'];
const linksVersion = devDependencies['@storybook/addon-links'];
const [storybookVersion, actionsVersion, linksVersion] = await getVersions(
'@storybook/react',
'@storybook/addon-actions',
'@storybook/addon-links'
);

mergeDirs(path.resolve(__dirname, 'template/'), '.', 'overwrite');

Expand Down
2 changes: 1 addition & 1 deletion lib/cli/lib/has_yarn.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const spawnSync = require('cross-spawn').sync;
import { sync as spawnSync } from 'cross-spawn';

export default function hasYarn() {
const result = spawnSync('yarn', ['--version'], { silent: true });
Expand Down
27 changes: 26 additions & 1 deletion lib/cli/lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
import path from 'path';
import fs from 'fs';
import chalk from 'chalk';
import latestVersion from 'latest-version';
import { sync as spawnSync } from 'cross-spawn';
import { gt } from 'semver';

const spawnSync = require('cross-spawn').sync;
import { version, devDependencies } from '../package.json';

const logger = console;

export async function getVersion(packageName) {
const current = packageName === '@storybook/cli' ? version : devDependencies[packageName];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check that packageName.match('@storybook') or something? Otherwise we may apply this logic to other things this package depends on.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable. I think it should be /storybook/.test(packageName) though, as there are plans to use storybook package name (@ndelangen has it)

let latest;
try {
latest = await latestVersion(packageName);
} catch (e) {
if (current) {
logger.warn(`\n ${chalk.yellow(e.message)}`);
return current;
}

logger.error(`\n ${chalk.red(e.message)}`);
process.exit(1);
}

return current && gt(current.replace(/^\^/, ''), latest) ? current : `^${latest}`;
}

export function getVersions(...packageNames) {
return Promise.all(packageNames.map(getVersion));
}

export function getPackageJson() {
const packageJsonPath = path.resolve('package.json');
if (!fs.existsSync(packageJsonPath)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
},
"dependencies": {
"@storybook/codemod": "^3.3.0-alpha.0",
"babel-preset-env": "^1.6.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.0",
"babel-register": "^6.26.0",
"babel-runtime": "^6.26.0",
"chalk": "^2.1.0",
Expand All @@ -42,6 +42,7 @@
"latest-version": "^3.1.0",
"merge-dirs": "^0.2.1",
"opencollective": "^1.0.3",
"semver": "^5.4.1",
"shelljs": "^0.7.8",
"update-notifier": "^2.1.0"
},
Expand Down