Skip to content

Commit

Permalink
refactor: create function to add SB command in scripts attribute of…
Browse files Browse the repository at this point in the history
… package.json
  • Loading branch information
gaetanmaisse committed Jun 8, 2020
1 parent cb54267 commit 2ec49eb
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 87 deletions.
6 changes: 2 additions & 4 deletions lib/cli/src/generators/ANGULAR/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ async function addDependencies(
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -51,6 +47,8 @@ async function addDependencies(
...versionedPackages,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
}

function editAngularAppTsConfig() {
Expand Down
5 changes: 2 additions & 3 deletions lib/cli/src/generators/AURELIA/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ const generator: Generator = async (
const packageJson = packageManager.retrievePackageJson();
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';
writePackageJson(packageJson);
addStorybookExcludeGlobToTsConfig();
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
10 changes: 2 additions & 8 deletions lib/cli/src/generators/EMBER/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = {
...packageJson.scripts,
...{
storybook: 'start-storybook -p 6006 -s dist',
'build-storybook': 'build-storybook -s dist',
},
};

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -48,6 +40,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
`babel-plugin-htmlbars-inline-precompile@${babelPluginHtmlBarsInlinePrecompileVersion}`,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
13 changes: 4 additions & 9 deletions lib/cli/src/generators/HTML/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,26 @@ import { Generator } from '../Generator';

const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => {
const packages = ['@storybook/html', '@storybook/addon-actions', '@storybook/addon-links'];

const versionedPackages = await packageManager.getVersionedPackages(...packages);
if (storyFormat === StoryFormat.MDX) {
packages.push('@storybook/addon-docs');
}

const versionedPackages = await packageManager.getVersionedPackages(...packages);

copyTemplate(__dirname, storyFormat);

const packageJson = packageManager.retrievePackageJson();

packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);

packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/MARIONETTE/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ const generator: Generator = async (packageManager, npmOptions) => {
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -24,6 +20,8 @@ const generator: Generator = async (packageManager, npmOptions) => {
`@storybook/marionette@${storybookVersion}`,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/MARKO/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -33,6 +29,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
`@storybook/addon-knobs@${addonKnobsVersion}`,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/METEOR/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
const packageJson = packageManager.retrievePackageJson();

packageJson.devDependencies = packageJson.devDependencies || {};
packageJson.scripts = packageJson.scripts || {};
packageJson.dependencies = packageJson.dependencies || {};

const devDependencies = [
Expand Down Expand Up @@ -62,9 +61,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })

fs.writeFileSync('.babelrc', JSON.stringify(babelrc, null, 2), 'utf8');

packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -89,6 +85,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
...devDependencies,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/MITHRIL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -39,6 +35,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
`@storybook/addons@${addonsVersion}`,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/PREACT/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -36,6 +32,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
`@storybook/addons@${addonsVersion}`,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/RAX/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies['rax-text'] = packageJson.dependencies['rax-text'] || raxVersion;
packageJson.dependencies['rax-view'] = packageJson.dependencies['rax-view'] || raxVersion;

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -49,6 +45,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
`@storybook/addons@${addonsVersion}`,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/REACT/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -34,6 +30,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
...versionedPackages,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
15 changes: 5 additions & 10 deletions lib/cli/src/generators/REACT_SCRIPTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 9009';
packageJson.scripts['build-storybook'] = 'build-storybook';

if (fs.existsSync(path.resolve('./public'))) {
// has a public folder and add support to it.
packageJson.scripts.storybook += ' -s public';
packageJson.scripts['build-storybook'] += ' -s public';
}

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -44,6 +34,11 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
...versionedPackages,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts({
port: 9009,
staticFolder: fs.existsSync(path.resolve('./public')) ? 'public' : undefined,
});
};

export default generator;
9 changes: 4 additions & 5 deletions lib/cli/src/generators/RIOT/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
if (
!packageJson.devDependencies['riot-tag-loader'] &&
!packageJson.dependencies['riot-tag-loader']
)
) {
dependencies.push(`riot-tag-loader@${tagLoaderVersion}`);

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';
}

writePackageJson(packageJson);

Expand All @@ -47,6 +44,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
...dependencies,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/SFC_VUE/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -33,6 +29,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
...versionedPackages,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/SVELTE/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -42,6 +38,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
`svelte-loader@${svelteLoader}`,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/VUE/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,15 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
);
}

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/WEB-COMPONENTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -37,6 +33,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
...versionedPackages,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
6 changes: 2 additions & 4 deletions lib/cli/src/generators/WEBPACK_REACT/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.storybook = 'start-storybook -p 6006';
packageJson.scripts['build-storybook'] = 'build-storybook';

writePackageJson(packageJson);

const babelDependencies = await getBabelDependencies(packageManager, packageJson);
Expand All @@ -33,6 +29,8 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
...versionedPackages,
...babelDependencies,
]);

packageManager.addStorybookCommandInScripts();
};

export default generator;
Loading

0 comments on commit 2ec49eb

Please sign in to comment.