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

CLI refactor #4168

Merged
merged 14 commits into from
Sep 16, 2018
2 changes: 1 addition & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ To update your app to use the new package names, you can use the cli:
npm install --global @storybook/cli

# if you run this inside a v2 app, it should perform the necessary codemods.
getstorybook
storybook init
```

#### Details
Expand Down
2 changes: 1 addition & 1 deletion app/angular/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ So you can develop UI components in isolation without worrying about app specifi
```sh
npm i -g @storybook/cli
cd my-angular-app
getstorybook
storybook init
```

For more information visit: [storybook.js.org](https://storybook.js.org)
Expand Down
2 changes: 1 addition & 1 deletion app/html/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ So you can develop UI components in isolation without worrying about app specifi
```sh
npm i -g @storybook/cli
cd my-app
getstorybook --html
storybook init --html
```

For more information visit: [storybook.js.org](https://storybook.js.org)
Expand Down
2 changes: 1 addition & 1 deletion app/marko/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ So you can develop UI components in isolation without worrying about app specifi
```sh
npm i -g @storybook/cli
cd my-marko-app
getstorybook
storybook init
```

For more information visit: [storybook.js.org](https://storybook.js.org)
Expand Down
2 changes: 1 addition & 1 deletion app/mithril/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ So you can develop UI components in isolation without worrying about app specifi
```sh
npm i -g @storybook/cli
cd my-mithril-app
getstorybook
storybook init
```

For more information visit: [storybook.js.org](https://storybook.js.org)
Expand Down
2 changes: 1 addition & 1 deletion app/polymer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ So you can develop UI components in isolation without worrying about app specifi
```sh
npm i -g @storybook/cli
cd my-polymer-app
getstorybook
storybook init
```

For more information visit: [storybook.js.org](https://storybook.js.org)
Expand Down
6 changes: 3 additions & 3 deletions app/react-native/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ For more information visit: [storybook.js.org](https://storybook.js.org)

## Getting Started

The `getstorybook` tool can be used to add Storybook to your React Native app. Install the `getstorybook` tool if necessary and run it from your project directory with these commands:
The `storybook` CLI tool can be used to add Storybook to your React Native app. Install the `storybook` tool if necessary and run it from your project directory with these commands:

```shell
npm -g i @storybook/cli
getstorybook
storybook init
```

After you have installed, there are additional steps for `create-react-native-app` apps. See the section for details, otherwise skip to [Start Storybook](#start-storybook)
to see the next step.

## Create React Native App (CRNA)

If you run `getstorybook` inside a CRNA app, you'll be notified that there is an extra step required to use Storybook.
If you run `storybook init` inside a CRNA app, you'll be notified that there is an extra step required to use Storybook.

The easiest way to use Storybook inside CRNA is to simply replace your App with the Storybook UI, which is possible by replacing `App.js` with a single line of code:

Expand Down
2 changes: 1 addition & 1 deletion app/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ So you can develop UI components in isolation without worrying about app specifi
```sh
npm i -g @storybook/cli
cd my-react-app
getstorybook
storybook init
```

For more information visit: [storybook.js.org](https://storybook.js.org)
Expand Down
2 changes: 1 addition & 1 deletion app/riot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ So you can develop UI components in isolation without worrying about app specifi
```sh
npm i -g @storybook/cli
cd my-app
getstorybook
storybook init
```

For more information visit: [storybook.js.org](https://storybook.js.org)
Expand Down
2 changes: 1 addition & 1 deletion app/svelte/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ So you can develop UI components in isolation without worrying about app specifi
```sh
npm i -g @storybook/cli
cd my-svelte-app
getstorybook
storybook init
```

For more information visit: [storybook.js.org](https://storybook.js.org)
Expand Down
2 changes: 1 addition & 1 deletion app/vue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ So you can develop UI components in isolation without worrying about app specifi
```sh
npm i -g @storybook/cli
cd my-vue-app
getstorybook
storybook init
```

For more information visit: [storybook.js.org](https://storybook.js.org)
Expand Down
2 changes: 1 addition & 1 deletion examples/crna-kitchen-sink/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# CRNA Kitchen Sink

This project was bootstrapped with [Create React Native App](https://github.com/react-community/create-react-native-app) and storybook using [getstorybook](https://www.npmjs.com/package/@storybook/cli).
This project was bootstrapped with [Create React Native App](https://github.com/react-community/create-react-native-app) and storybook using [storybook CLI](https://www.npmjs.com/package/@storybook/cli).
2 changes: 1 addition & 1 deletion examples/marko-cli/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Marko Kitchen Sink

This project was bootstrapped with [Marko cli (create command)](https://github.com/marko-js/marko-cli) and storybook using [getstorybook](https://www.npmjs.com/package/@storybook/cli).
This project was bootstrapped with [Marko cli (create command)](https://github.com/marko-js/marko-cli) and storybook using [storybook CLI](https://www.npmjs.com/package/@storybook/cli).
6 changes: 3 additions & 3 deletions lib/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ npm i -g @storybook/cli
Then go to your project run:

```sh
getstorybook
storybook init
```

That's all you've to do.
Expand All @@ -30,7 +30,7 @@ If you have installed yarn in your system, it'll detect it and use `yarn` instea
If you don't want to use `yarn` always you can use the `--use-npm` option like this:

```sh
getstorybook --use-npm
storybook init --use-npm
```

* * *
Expand All @@ -40,7 +40,7 @@ getstorybook --use-npm
It also supports flow files. By default, [jscodeshift](https://github.com/facebook/jscodeshift), the tool used to transform the source files, uses babel to read the files. To be able to transform any flow annotated file, you need to use the flow parser.

```sh
getstorybook --parser flow
storybook init --parser flow
```

For more information visit: [storybook.js.org](https://storybook.js.org)
215 changes: 32 additions & 183 deletions lib/cli/bin/generate.js
Original file line number Diff line number Diff line change
@@ -1,201 +1,50 @@
import updateNotifier from 'update-notifier';
import program from 'commander';
import chalk from 'chalk';
import detect from '../lib/detect';
import hasYarn from '../lib/has_yarn';
import types from '../lib/project_types';
import { commandLog, codeLog, paddedLog, installDeps } from '../lib/helpers';
import pkg from '../package.json';
import angularGenerator from '../generators/ANGULAR';
import meteorGenerator from '../generators/METEOR';
import reactGenerator from '../generators/REACT';
import reactNativeGenerator from '../generators/REACT_NATIVE';
import reactNativeScriptsGenerator from '../generators/REACT_NATIVE_SCRIPTS';
import reactScriptsGenerator from '../generators/REACT_SCRIPTS';
import sfcVueGenerator from '../generators/SFC_VUE';
import updateOrganisationsGenerator from '../generators/UPDATE_PACKAGE_ORGANIZATIONS';
import vueGenerator from '../generators/VUE';
import polymerGenerator from '../generators/POLYMER';
import webpackReactGenerator from '../generators/WEBPACK_REACT';
import mithrilGenerator from '../generators/MITHRIL';
import markoGenerator from '../generators/MARKO';
import htmlGenerator from '../generators/HTML';
import riotGenerator from '../generators/RIOT';
import initiate from '../lib/initiate';
import yarnSpawnSync from '../lib/yarn_spawn_sync';
import { codeLog } from '../lib/helpers';

const logger = console;

if (process.argv[1].includes('getstorybook')) {
logger.log(chalk.yellow('WARNING: getstorybook is deprecated.'));
logger.log(chalk.yellow('The official command to install Storybook from now on is:\n'));
codeLog(['storybook init']);
logger.log();
initiate({}, pkg);
process.exit(0);
}

program
.version(pkg.version)
.command('init')
.description('Initialize Storybook into your project.')
.option('-f --force', 'Forcely add storybook')
.option('-s --skip-install', 'Skip installing deps')
.option('-N --use-npm', 'Use npm to install deps')
.option('-p --parser <babel | babylon | flow>', 'jscodeshift parser')
.option('-h --html', 'Add storybook for HTML')
.parse(process.argv);

const welcomeMessage = 'getstorybook - the simplest way to add a storybook to your project.';
logger.log(chalk.inverse(`\n ${welcomeMessage} \n`));

const useYarn = Boolean(program.useNpm !== true) && hasYarn();
.action(options => initiate(options, pkg));

const npmOptions = {
useYarn,
};

const runStorybookCommand = useYarn ? 'yarn run storybook' : 'npm run storybook';

// Update notify code.
updateNotifier({
pkg,
updateCheckInterval: 1000 * 60 * 60, // every hour (we could increase this later on.)
}).notify();
program
.command('start')
.description('Start the local Storybook server')
.option('-N --use-npm', 'Use NPM to start the Storybook server')
Copy link
Member

Choose a reason for hiding this comment

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

npm is widely used outside storybook in my opinion. We can identify that easily but figuring out wether we have a package-lock.json or a yarn.lock
np is able to do that (https://github.com/sindresorhus/np)

Copy link
Member

Choose a reason for hiding this comment

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

Sorry to say that at the very end of the review process, I did not see your PR earlier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When the CLI was made, I don't think NPM had the lockfile yet (i think), that's why the flag was added to the command. On the other hand, we can also argue that what should happen when both files are present (for whatever reason). Although I definitely agree with your solution if we want to prioritize NPM over Yarn, or the other way around which happens now, it would be a great idea

Copy link
Member

Choose a reason for hiding this comment

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

also npm-shrinkwrap.json 😅

.action(options => yarnSpawnSync(options, ['run', 'storybook']));

let projectType;
program
.command('build')
.description('Build the Storybook static application')
.option('-N --use-npm', 'Use NPM to build the Storybook server')
.action(options => yarnSpawnSync(options, ['build', 'storybook']));

const done = commandLog('Detecting project type');
try {
projectType = detect(program);
} catch (ex) {
done(ex.message);
program.command('*', { noHelp: true }).action(cmd => {
logger.error('Invalid command: %s.\nSee --help for a list of available commands.', cmd);
process.exit(1);
}
done();

const end = () => {
if (!program.skipInstall) {
installDeps(npmOptions);
}

logger.log('\nTo run your storybook, type:\n');
codeLog([runStorybookCommand]);
logger.log('\nFor more information visit:', chalk.cyan('https://storybook.js.org'));

// Add a new line for the clear visibility.
logger.log();
};

const CRNA_DISCUSSION =
'https://github.com/storybooks/storybook/blob/master/app/react-native/docs/manual-setup.md';

const runGenerator = () => {
switch (projectType) {
case types.ALREADY_HAS_STORYBOOK:
logger.log();
paddedLog('There seems to be a storybook already available in this project.');
paddedLog('Apply following command to force:\n');
codeLog(['getstorybook -f']);

// Add a new line for the clear visibility.
logger.log();
return Promise.resolve();

case types.UPDATE_PACKAGE_ORGANIZATIONS:
return updateOrganisationsGenerator(program.parser, npmOptions)
.then(() => null) // commmandLog doesn't like to see output
.then(commandLog('Upgrading your project to the new storybook packages.'))
.then(end);

case types.REACT_SCRIPTS:
return reactScriptsGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Create React App" based project'))
.then(end);

case types.REACT:
return reactGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "React" app'))
.then(end);

case types.REACT_NATIVE_SCRIPTS: {
const app = chalk.bold('"./App.js"');
return reactNativeScriptsGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Create React Native App" app'))
.then(end)
.then(() => {
logger.log(chalk.red('NOTE: CRNA app installation is not 100% automated.'));
logger.log(`To quickly run storybook, replace contents of ${app} with:\n`);
codeLog(["export default from './storybook';"]);
logger.log('\nFor a more complete discussion of options, see:\n');
logger.log(chalk.cyan(CRNA_DISCUSSION));
logger.log();
});
}

case types.REACT_NATIVE:
return reactNativeGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "React Native" app'))
.then(end);

case types.METEOR:
return meteorGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Meteor" app'))
.then(end);

case types.WEBPACK_REACT:
return webpackReactGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Webpack React" app'))
.then(end);

case types.REACT_PROJECT:
return reactGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "React" library'))
.then(end);

case types.SFC_VUE:
return sfcVueGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Single File Components Vue" app'))
.then(end);

case types.VUE:
return vueGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Vue" app'))
.then(end);

case types.ANGULAR:
return angularGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Angular" app'))
.then(end);

case types.POLYMER:
return polymerGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Polymer" app'))
.then(end);

case types.MITHRIL:
return mithrilGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Mithril" app'))
.then(end);

case types.MARKO:
return markoGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Marko" app'))
.then(end);

case types.HTML:
return htmlGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "HTML" app'))
.then(end);

case types.RIOT:
return riotGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "riot.js" app'))
.then(end);

default:
paddedLog(`We couldn't detect your project type. (code: ${projectType})`);
paddedLog(
"Please make sure you are running the `getstorybook` command in your project's root directory."
);
paddedLog(
'You can also install storybook for plain HTML snippets with `getstorybook --html` or follow some of the slow start guides: https://storybook.js.org/basics/slow-start-guide/'
);
});

// Add a new line for the clear visibility.
logger.log();
return Promise.resolve();
}
};
program.version(pkg.version).parse(process.argv);

runGenerator().catch(ex => {
logger.error(`\n ${chalk.red(ex.stack)}`);
process.exit(1);
});
if (!program.args.length) {
program.help();
}