Skip to content

Commit

Permalink
refactor: transpile @storybook/cli to CJS with Babel
Browse files Browse the repository at this point in the history
 - move all source code in `src` as it's, by convention in SB monorepo, the source folder for Babel
 - update `.babelrc.json` to ignore all templates files
 - add `prepare` NPM script as in all other libs
 - update `cleanup` stage of `prepare` script to avoid removing anything from `@storybook/cli/dist/generators/**/template*` folder because there are the template files that will be copied to initialize SB on users' projects
  • Loading branch information
gaetanmaisse committed Feb 11, 2020
1 parent db4fa14 commit 3dd714d
Show file tree
Hide file tree
Showing 138 changed files with 103 additions and 87 deletions.
11 changes: 0 additions & 11 deletions lib/cli/.babelrc

This file was deleted.

19 changes: 19 additions & 0 deletions lib/cli/.babelrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": 8
},
"useBuiltIns": "usage",
"corejs": "3"
}
]
],
"ignore": [
"./src/generators/**/template",
"./src/generators/**/template-csf",
"./src/generators/**/template-mdx"
]
}
15 changes: 9 additions & 6 deletions lib/cli/bin/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import chalk from 'chalk';
import envinfo from 'envinfo';
import didYouMean from 'didyoumean';
import pkg from '../package.json';
import initiate from '../lib/initiate';
import { codeLog } from '../lib/helpers';
import add from '../lib/add';
import { migrate } from '../lib/migrate';
import initiate from '../dist/initiate';
import { codeLog } from '../dist/helpers';
import add from '../dist/add';
import { migrate } from '../dist/migrate';

const logger = console;

Expand Down Expand Up @@ -78,8 +78,11 @@ if (process.argv[1].includes('getstorybook')) {
' Invalid command: %s.\n See --help for a list of available commands.',
invalidCmd
);
// eslint-disable-next-line
const suggestion = didYouMean(invalidCmd, program.commands.map(cmd => cmd._name));
const suggestion = didYouMean(
invalidCmd,
// eslint-disable-next-line
program.commands.map(cmd => cmd._name)
);
if (suggestion) {
logger.log(`\n Did you mean ${suggestion}?`);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
},
"files": [
"bin/**/*",
"generators/**/*",
"lib/**/*",
"dist/**/*",
"README.md",
"*.js",
"*.d.ts"
],
"scripts": {
"prepare": "node ../../scripts/prepare.js",
"test": "cd test && ./run_tests.sh",
"test-latest-cra": "cd test && ./test_latest_cra.sh"
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import * as fs from 'fs';
import { readFileAsJson, writeFileAsJson } from '../../lib/helpers';
import { readFileAsJson, writeFileAsJson } from '../../helpers';

export function getAngularAppTsConfigPath() {
const angularJson = readFileAsJson('angular.json', true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
installDependencies,
writeFileAsJson,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

async function addDependencies(npmOptions, { storyFormat }) {
const packages = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const [storybookVersion, linksVersion, actionsVersion, addonsVersion] = await getVersions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const packages = ['@storybook/html'];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fse from 'fs-extra';
import path from 'path';
import npmInit from '../../lib/npm_init';
import npmInit from '../../npm_init';
import {
getVersion,
getPackageJson,
writePackageJson,
getBabelDependencies,
installDependencies,
} from '../../lib/helpers';
} from '../../helpers';

export default async npmOptions => {
const storybookVersion = await getVersion(npmOptions, '@storybook/marionette');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const [storybookVersion, addonActionVersion, addonKnobsVersion] = await getVersions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const [storybookVersion, actionsVersion, linksVersion, addonsVersion] = await getVersions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const [storybookVersion, actionsVersion, linksVersion, addonsVersion] = await getVersions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const packages = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, installServer, { storyFormat = 'csf' }) => {
const [storybookVersion, addonsVersion, actionsVersion, linksVersion] = await getVersions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const packages = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<my-button>
<button class="buttonStyles" onClick="{ onClick }">
<yield/>
</button>
<style>
.buttonStyles {
border: solid 1px #eee;
border-radius: 3px;
background-color: #FFFFFF;
cursor: pointer;
font-size: 15px;
padding: 3px 10px;
margin: 10px;
}
</style>
<script>
onClick(e){
return (this.opts.callback ||
(() => { console.log('clicked') })).bind(this, e)
}
</script>
</my-button>
<my-button>
<button class="buttonStyles" onClick="{ onClick }">
<yield/>
</button>

<style>
.buttonStyles {
border: solid 1px #eee;
border-radius: 3px;
background-color: #FFFFFF;
cursor: pointer;
font-size: 15px;
padding: 3px 10px;
margin: 10px;
}
</style>

<script>
onClick(e){
return (this.opts.callback ||
(() => { console.log('clicked') })).bind(this, e)
}
</script>
</my-button>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const packages = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
installDependencies,
getPackageJson,
writePackageJson,
} from '../../lib/helpers';
} from '../../helpers';

async function updatePackage(devDependencies, oldName, newName, npmOptions) {
if (devDependencies[oldName]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
installDependencies,
addToDevDependenciesIfNotPresent,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const packages = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
writePackageJson,
getBabelDependencies,
installDependencies,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const storybookVersion = await getVersion(npmOptions, '@storybook/web-components');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBabelDependencies,
installDependencies,
copyTemplate,
} from '../../lib/helpers';
} from '../../helpers';

export default async (npmOptions, { storyFormat = 'csf' }) => {
const packages = [
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/cli/lib/helpers.js → lib/cli/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function retrievePackageJson() {
// npmInit will create a new package.json file
npmInit();

// read the newly created packafe,json file
// read the newly created package.json file
return getPackageJson() || {};
}

Expand Down
38 changes: 19 additions & 19 deletions lib/cli/lib/initiate.js → lib/cli/src/initiate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import {
installDepsFromPackageJson,
getPackageJson,
} from './helpers';
import angularGenerator from '../generators/ANGULAR';
import emberGenerator from '../generators/EMBER';
import meteorGenerator from '../generators/METEOR';
import reactGenerator from '../generators/REACT';
import reactNativeGenerator from '../generators/REACT_NATIVE';
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 webpackReactGenerator from '../generators/WEBPACK_REACT';
import mithrilGenerator from '../generators/MITHRIL';
import marionetteGenerator from '../generators/MARIONETTE';
import markoGenerator from '../generators/MARKO';
import htmlGenerator from '../generators/HTML';
import webComponentsGenerator from '../generators/WEB-COMPONENTS';
import riotGenerator from '../generators/RIOT';
import preactGenerator from '../generators/PREACT';
import svelteGenerator from '../generators/SVELTE';
import raxGenerator from '../generators/RAX';
import angularGenerator from './generators/ANGULAR';
import emberGenerator from './generators/EMBER';
import meteorGenerator from './generators/METEOR';
import reactGenerator from './generators/REACT';
import reactNativeGenerator from './generators/REACT_NATIVE';
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 webpackReactGenerator from './generators/WEBPACK_REACT';
import mithrilGenerator from './generators/MITHRIL';
import marionetteGenerator from './generators/MARIONETTE';
import markoGenerator from './generators/MARKO';
import htmlGenerator from './generators/HTML';
import webComponentsGenerator from './generators/WEB-COMPONENTS';
import riotGenerator from './generators/RIOT';
import preactGenerator from './generators/PREACT';
import svelteGenerator from './generators/SVELTE';
import raxGenerator from './generators/RAX';

const logger = console;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 8 additions & 3 deletions scripts/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ function cleanup() {
// https://github.com/babel/babel/issues/6226
if (fs.existsSync(path.join(process.cwd(), 'dist'))) {
const files = shell.find('dist').filter(filePath => {
// Do not remove folder
// And do not clean anything for @storybook/cli/dist/generators/**/template* because these are the template files
// that will be copied to init SB on users' projects
if (fs.lstatSync(filePath).isDirectory() || /generators\/.+\/template.*/.test(filePath)) {
return false;
}

// Remove all copied TS files (but not the .d.ts)
if (/\.tsx?$/.test(filePath) && !/\.d\.ts$/.test(filePath)) {
return true;
}
if (fs.lstatSync(filePath).isDirectory()) {
return false;
}

return ignore.reduce((acc, pattern) => {
return acc || !!filePath.match(pattern);
}, false);
Expand Down

0 comments on commit 3dd714d

Please sign in to comment.