refactor: use getRunStorybookCommand from package manager

It avoids to use a ternary expression to define the displayed string and encapsulate specific package managers behaviors in their own classes.
This commit is contained in:
Gaëtan Maisse 2020-06-07 18:29:41 +02:00
parent 0334c801fe
commit 100ffac29c
No known key found for this signature in database
GPG Key ID: D934C0EF3714A8A8
5 changed files with 18 additions and 5 deletions

View File

@ -36,6 +36,7 @@ import preactGenerator from './generators/PREACT';
import svelteGenerator from './generators/SVELTE';
import raxGenerator from './generators/RAX';
import { warn } from './warn';
import { JsPackageManagerFactory } from './js-package-manager/JsPackageManagerFactory';
const logger = console;
@ -51,10 +52,10 @@ type CommandOptions = {
};
const installStorybook = (projectType: ProjectType, options: CommandOptions): Promise<void> => {
const useYarn = Boolean(options.useNpm !== true) && hasYarn();
const packageManager = JsPackageManagerFactory.getPackageManager(options.useNpm);
const npmOptions = {
useYarn,
useYarn: Boolean(options.useNpm !== true) && hasYarn(),
installAsDevDependencies: true,
skipInstall: options.skipInstall,
};
@ -69,15 +70,13 @@ const installStorybook = (projectType: ProjectType, options: CommandOptions): Pr
storyFormat: options.storyFormat || defaultStoryFormat,
};
const runStorybookCommand = useYarn ? 'yarn storybook' : 'npm run storybook';
const end = () => {
if (!options.skipInstall) {
installDepsFromPackageJson(npmOptions);
}
logger.log('\nTo run your storybook, type:\n');
codeLog([runStorybookCommand]);
codeLog([packageManager.getRunStorybookCommand()]);
logger.log('\nFor more information visit:', chalk.cyan('https://storybook.js.org'));
// Add a new line for the clear visibility.

View File

@ -1,3 +1,5 @@
export abstract class JsPackageManager {
public abstract initPackageJson(): void;
public abstract getRunStorybookCommand(): string;
}

View File

@ -11,4 +11,8 @@ export class NPMProxy extends JsPackageManager {
});
return results.stdout;
}
getRunStorybookCommand(): string {
return 'npm run storybook';
}
}

View File

@ -11,4 +11,8 @@ export class Yarn1Proxy extends JsPackageManager {
});
return results.stdout;
}
getRunStorybookCommand(): string {
return 'yarn storybook';
}
}

View File

@ -11,4 +11,8 @@ export class Yarn2Proxy extends JsPackageManager {
});
return results.stdout;
}
getRunStorybookCommand(): string {
return 'yarn storybook';
}
}