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

Added Storybook template #318

Merged
merged 3 commits into from
Dec 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
66 changes: 18 additions & 48 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import getInstallArgs from './getInstallArgs';
import { Input, Select } from 'enquirer';
import { PackageJson, TsdxOptions } from './types';
import { createProgressEstimator } from './createProgressEstimator';
import { templates } from './templates';
import { composePackageJson } from './templates/utils';
const pkg = require('../package.json');

const prog = sade('tsdx');
Expand Down Expand Up @@ -170,7 +172,12 @@ prog
.command('create <pkg>')
.describe('Create a new package with TSDX')
.example('create mypackage')
.option('--template', 'Specify a template. Allowed choices: [basic, react]')
.option(
'--template',
`Specify a template. Allowed choices: [${Object.keys(templates).join(
', '
)}]`
)
.example('create --template react mypackage')
.action(async (pkg: string, opts: any) => {
console.log(
Expand Down Expand Up @@ -220,7 +227,7 @@ prog

const prompt = new Select({
message: 'Choose a template',
choices: ['basic', 'react'],
choices: Object.keys(templates),
});

if (opts.template) {
Expand Down Expand Up @@ -276,41 +283,13 @@ prog
encoding: 'utf-8',
});

const templateConfig = templates[template as keyof typeof templates];
const generatePackageJson = composePackageJson(templateConfig);

// Install deps
process.chdir(projectPath);
const safeName = safePackageName(pkg);
const pkgJson = {
name: safeName,
version: '0.1.0',
license: 'MIT',
author: author,
main: 'dist/index.js',
module: `dist/${safeName}.esm.js`,
typings: `dist/index.d.ts`,
files: ['dist'],
scripts: {
start: 'tsdx watch',
build: 'tsdx build',
test:
template === 'react'
? 'tsdx test --env=jsdom --passWithNoTests'
: 'tsdx test',
lint: 'tsdx lint',
prepare: 'tsdx build',
},
peerDependencies: template === 'react' ? { react: '>=16' } : {},
husky: {
hooks: {
'pre-commit': 'tsdx lint',
},
},
prettier: {
printWidth: 80,
semi: true,
singleQuote: true,
trailingComma: 'es5',
},
};
const pkgJson = generatePackageJson({ name: safeName, author });
await fs.outputJSON(path.resolve(projectPath, 'package.json'), pkgJson);
bootSpinner.succeed(`Created ${chalk.bold.green(pkg)}`);
await Messages.start(pkg);
Expand All @@ -320,19 +299,10 @@ prog
process.exit(1);
}

let deps = ['@types/jest', 'husky', 'tsdx', 'tslib', 'typescript'].sort();
const templateConfig = templates[template as keyof typeof templates];
const { dependencies: deps } = templateConfig;

if (template === 'react') {
deps = [
...deps,
'@types/react',
'@types/react-dom',
'react',
'react-dom',
].sort();
}

const installSpinner = ora(Messages.installing(deps)).start();
const installSpinner = ora(Messages.installing(deps.sort())).start();
try {
const cmd = await getInstallCmd();
await execa(cmd, getInstallArgs(cmd, deps));
Expand Down Expand Up @@ -577,9 +547,9 @@ prog
...jestConfig,
})
);

if (!process.env.CI) {
argv.push('--watch') // run jest in watch mode unless in CI
argv.push('--watch'); // run jest in watch mode unless in CI
}

const [, ...argsToPassToJestCli] = argv;
Expand Down
37 changes: 37 additions & 0 deletions src/templates/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Template } from './template';

const basicTemplate: Template = {
name: 'basic',
dependencies: ['@types/jest', 'husky', 'tsdx', 'tslib', 'typescript'],
packageJson: {
// name: safeName,
version: '0.1.0',
license: 'MIT',
// author: author,
main: 'dist/index.js',
// module: `dist/${safeName}.esm.js`,
typings: `dist/index.d.ts`,
files: ['dist'],
scripts: {
start: 'tsdx watch',
build: 'tsdx build',
test: 'tsdx test',
lint: 'tsdx lint',
prepare: 'tsdx build',
},
peerDependencies: {},
husky: {
hooks: {
'pre-commit': 'tsdx lint',
},
},
prettier: {
printWidth: 80,
semi: true,
singleQuote: true,
trailingComma: 'es5',
},
},
};

export default basicTemplate;
9 changes: 9 additions & 0 deletions src/templates/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import reactTemplate from './react';
import basicTemplate from './basic';
import storybookTemplate from './react-with-storybook';

export const templates = {
basic: basicTemplate,
react: reactTemplate,
'react-with-storybook': storybookTemplate,
};
27 changes: 27 additions & 0 deletions src/templates/react-with-storybook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Template } from './template';
import reactTemplate from './react';
import { PackageJson } from 'type-fest';

const storybookTemplate: Template = {
dependencies: [
...reactTemplate.dependencies,
'@babel/core',
'@storybook/addon-actions',
'@storybook/addon-links',
'@storybook/addons',
'@storybook/react',
'babel-loader',
'ts-loader',
],
name: 'react-with-storybook',
packageJson: {
...reactTemplate.packageJson,
scripts: {
...reactTemplate.packageJson.scripts,
storybook: 'start-storybook -p 6006',
'build-storybook': 'build-storybook',
} as PackageJson['scripts'],
},
};

export default storybookTemplate;
27 changes: 27 additions & 0 deletions src/templates/react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Template } from './template';

import basicTemplate from './basic';
import { PackageJson } from 'type-fest';

const reactTemplate: Template = {
name: 'react',
dependencies: [
...basicTemplate.dependencies,
'@types/react',
'@types/react-dom',
'react',
'react-dom',
],
packageJson: {
...basicTemplate.packageJson,
peerDependencies: {
react: '>=16',
},
scripts: {
...basicTemplate.packageJson.scripts,
test: 'tsdx test --env=jsdom --passWithNoTests',
} as PackageJson['scripts'],
},
};

export default reactTemplate;
7 changes: 7 additions & 0 deletions src/templates/template.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PackageJson } from 'type-fest';

interface Template {
dependencies: string[];
name: string;
packageJson: PackageJson;
}
17 changes: 17 additions & 0 deletions src/templates/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Template } from '../template';

interface ProjectArgs {
name: string;
author: string;
}
export const composePackageJson = (template: Template) => ({
name,
author,
}: ProjectArgs) => {
return {
...template.packageJson,
name,
author,
module: `dist/${name}.esm.js`,
};
};
2 changes: 2 additions & 0 deletions templates/react-with-storybook/.storybook/addons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import '@storybook/addon-actions/register';
import '@storybook/addon-links/register';
4 changes: 4 additions & 0 deletions templates/react-with-storybook/.storybook/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { configure } from '@storybook/react';

// automatically import all files ending in *.stories.js
configure(require.context('../stories', true, /\.stories\.(js|ts)x?$/), module);
17 changes: 17 additions & 0 deletions templates/react-with-storybook/.storybook/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require('path')
module.exports = ({ config }) => {
config.module.rules.push({
test: /\.tsx?$/,
use: [
{
loader: require.resolve('ts-loader'),
options: {
reportFiles: ['stories/**/*.{ts|tsx}']
}
}
]
})
config.resolve.extensions.push('.ts', '.tsx')
config.resolve.alias = Object.assign(config.resolve.alias, { '@': path.resolve(__dirname, '..') })
return config
}
21 changes: 21 additions & 0 deletions templates/react-with-storybook/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) <year> <author>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading