refactor(cli): migrate initiate file to TS

This commit is contained in:
Gaëtan Maisse 2020-05-15 18:48:05 +02:00
parent 56930ac795
commit 66e077b2bc
No known key found for this signature in database
GPG Key ID: D934C0EF3714A8A8
4 changed files with 49 additions and 11 deletions

View File

@ -87,9 +87,11 @@
"@storybook/vue": "6.0.0-beta.7",
"@storybook/web-components": "6.0.0-beta.7",
"@types/cross-spawn": "^6.0.1",
"@types/inquirer": "^6.5.0",
"@types/puppeteer-core": "^2.0.0",
"@types/semver": "^7.2.0",
"@types/shelljs": "^0.8.7"
"@types/shelljs": "^0.8.7",
"@types/update-notifier": "^0.0.30"
},
"peerDependencies": {
"jest": "*"

View File

@ -122,7 +122,8 @@ export function writePackageJson(packageJson: object) {
export const commandLog = (message: string) => {
process.stdout.write(chalk.cyan(' • ') + message);
return (errorMessage?: string, errorInfo?: string) => {
// Need `void` to be able to use this function in a then of a Promise<void>
return (errorMessage?: string | void, errorInfo?: string) => {
if (errorMessage) {
process.stdout.write(`. ${chalk.red('✖')}\n`);
logger.error(`\n ${chalk.red(errorMessage)}`);

View File

@ -1,4 +1,4 @@
import updateNotifier from 'update-notifier';
import { UpdateNotifier, IPackage } from 'update-notifier';
import chalk from 'chalk';
import inquirer from 'inquirer';
import { detect, isStorybookInstalled, detectLanguage } from './detect';
@ -38,7 +38,22 @@ import raxGenerator from './generators/RAX';
const logger = console;
const installStorybook = (projectType, options) => {
type CommandOptions = {
useNpm?: boolean;
type?: any;
force?: any;
html?: boolean;
skipInstall?: boolean;
storyFormat?: string;
parser?: string;
yes?: boolean;
};
const installStorybook = (
// TODO: Improve typings using PROJECT_TYPES
projectType: string,
options: CommandOptions
): Promise<void> => {
const useYarn = Boolean(options.useNpm !== true) && hasYarn();
const npmOptions = {
@ -74,7 +89,7 @@ const installStorybook = (projectType, options) => {
const REACT_NATIVE_DISCUSSION =
'https://github.com/storybookjs/storybook/blob/master/app/react-native/docs/manual-setup.md';
const runGenerator = () => {
const runGenerator: () => Promise<void> = () => {
switch (projectType) {
case PROJECT_TYPES.ALREADY_HAS_STORYBOOK:
logger.log();
@ -105,7 +120,7 @@ const installStorybook = (projectType, options) => {
case PROJECT_TYPES.REACT_NATIVE: {
return (options.yes
? Promise.resolve({ server: true })
: inquirer.prompt([
: (inquirer.prompt([
{
type: 'confirm',
name: 'server',
@ -113,7 +128,7 @@ const installStorybook = (projectType, options) => {
'Do you want to install dependencies necessary to run storybook server? You can manually do it later by install @storybook/react-native-server',
default: false,
},
])
]) as Promise<{ server: boolean }>)
)
.then(({ server }) => reactNativeGenerator(npmOptions, server, generatorOptions))
.then(commandLog('Adding storybook support to your "React Native" app'))
@ -227,7 +242,7 @@ const installStorybook = (projectType, options) => {
});
};
const projectTypeInquirer = async (options) => {
const projectTypeInquirer = async (options: { yes?: boolean }) => {
const manualAnswer = options.yes
? true
: await inquirer.prompt([
@ -239,7 +254,7 @@ const projectTypeInquirer = async (options) => {
},
]);
if (manualAnswer.manual) {
if (manualAnswer !== true && manualAnswer.manual) {
const frameworkAnswer = await inquirer.prompt([
{
type: 'list',
@ -253,12 +268,12 @@ const projectTypeInquirer = async (options) => {
return Promise.resolve();
};
export default function (options, pkg) {
export default function (options: CommandOptions, pkg: IPackage): Promise<void> {
const welcomeMessage = 'sb init - the simplest way to add a storybook to your project.';
logger.log(chalk.inverse(`\n ${welcomeMessage} \n`));
// Update notify code.
updateNotifier({
new UpdateNotifier({
pkg,
updateCheckInterval: 1000 * 60 * 60, // every hour (we could increase this later on.)
}).notify();

View File

@ -4580,6 +4580,14 @@
resolved "https://registry.yarnpkg.com/@types/html-minifier-terser/-/html-minifier-terser-5.1.0.tgz#551a4589b6ee2cc9c1dff08056128aec29b94880"
integrity sha512-iYCgjm1dGPRuo12+BStjd1HiVQqhlRhWDOQigNxn023HcjnhsiFz9pc6CzJj4HwDCSQca9bxTL4PxJDbkdm3PA==
"@types/inquirer@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@types/inquirer/-/inquirer-6.5.0.tgz#b83b0bf30b88b8be7246d40e51d32fe9d10e09be"
integrity sha512-rjaYQ9b9y/VFGOpqBEXRavc3jh0a+e6evAbI31tMda8VlPaSy0AZJfXsvmIe3wklc7W6C3zCSfleuMXR7NOyXw==
dependencies:
"@types/through" "*"
rxjs "^6.4.0"
"@types/is-function@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/is-function/-/is-function-1.0.0.tgz#1b0b819b1636c7baf0d6785d030d12edf70c3e83"
@ -5023,6 +5031,13 @@
"@types/testing-library__dom" "*"
pretty-format "^25.1.0"
"@types/through@*":
version "0.0.30"
resolved "https://registry.yarnpkg.com/@types/through/-/through-0.0.30.tgz#e0e42ce77e897bd6aead6f6ea62aeb135b8a3895"
integrity sha512-FvnCJljyxhPM3gkRgWmxmDZyAQSiBQQWLI0A0VFL0K7W1oRUrPJSqNO0NvTnLkBcotdlp3lKvaT0JrnyRDkzOg==
dependencies:
"@types/node" "*"
"@types/tmp@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@types/tmp/-/tmp-0.1.0.tgz#19cf73a7bcf641965485119726397a096f0049bd"
@ -5040,6 +5055,11 @@
resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.3.tgz#9c088679876f374eb5983f150d4787aa6fb32d7e"
integrity sha512-FvUupuM3rlRsRtCN+fDudtmytGO6iHJuuRKS1Ss0pG5z8oX0diNEw94UEL7hgDbpN94rgaK5R7sWm6RrSkZuAQ==
"@types/update-notifier@^0.0.30":
version "0.0.30"
resolved "https://registry.yarnpkg.com/@types/update-notifier/-/update-notifier-0.0.30.tgz#5148ffc81189832870ee3f19cf2bb1a35250b05e"
integrity sha1-UUj/yBGJgyhw7j8Zzyuxo1JQsF4=
"@types/util-deprecate@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/util-deprecate/-/util-deprecate-1.0.0.tgz#341d0815fe5a661b94e3ea738d182b4c359e3958"