refactor: move installDepsFromPackageJson function to JsPackageManager class and subclasses

This commit is contained in:
Gaëtan Maisse 2020-06-07 18:40:31 +02:00
parent aa02e872dc
commit a858990fa6
No known key found for this signature in database
GPG Key ID: D934C0EF3714A8A8
6 changed files with 44 additions and 36 deletions

View File

@ -182,27 +182,6 @@ export function codeLog(codeLines: string[], leftPadAmount?: number) {
logger.log(finalResult);
}
export function installDepsFromPackageJson(options: NpmOptions) {
let done = commandLog('Preparing to install dependencies');
done();
logger.log();
let result;
if (options.useYarn) {
result = spawnSync('yarn', { stdio: 'inherit' });
} else {
result = spawnSync('npm', ['install'], { stdio: 'inherit' });
}
logger.log();
done = commandLog('Installing dependencies');
if (result.status !== 0) {
done('An error occurred while installing dependencies.');
process.exit(1);
}
done();
}
/**
* Add dependencies to a project using `yarn add` or `npm install`.
*

View File

@ -9,13 +9,7 @@ import {
StoryFormat,
SupportedLanguage,
} from './project_types';
import {
commandLog,
codeLog,
paddedLog,
installDepsFromPackageJson,
getPackageJson,
} from './helpers';
import { commandLog, codeLog, paddedLog, getPackageJson } from './helpers';
import angularGenerator from './generators/ANGULAR';
import emberGenerator from './generators/EMBER';
import meteorGenerator from './generators/METEOR';
@ -37,6 +31,7 @@ import svelteGenerator from './generators/SVELTE';
import raxGenerator from './generators/RAX';
import { warn } from './warn';
import { JsPackageManagerFactory } from './js-package-manager/JsPackageManagerFactory';
import { NpmOptions } from './NpmOptions';
const logger = console;
@ -54,7 +49,7 @@ type CommandOptions = {
const installStorybook = (projectType: ProjectType, options: CommandOptions): Promise<void> => {
const packageManager = JsPackageManagerFactory.getPackageManager(options.useNpm);
const npmOptions = {
const npmOptions: NpmOptions = {
useYarn: Boolean(options.useNpm !== true) && hasYarn(),
installAsDevDependencies: true,
skipInstall: options.skipInstall,
@ -72,7 +67,7 @@ const installStorybook = (projectType: ProjectType, options: CommandOptions): Pr
const end = () => {
if (!options.skipInstall) {
installDepsFromPackageJson(npmOptions);
packageManager.installDependencies();
}
logger.log('\nTo run your storybook, type:\n');

View File

@ -1,5 +1,27 @@
import { commandLog } from '../helpers';
const logger = console;
export abstract class JsPackageManager {
public abstract initPackageJson(): void;
public abstract getRunStorybookCommand(): string;
public installDependencies(): void {
let done = commandLog('Preparing to install dependencies');
done();
logger.log();
const result = this.runInstall();
logger.log();
done = commandLog('Installing dependencies');
if (result.status !== 0) {
done('An error occurred while installing dependencies.');
process.exit(1);
}
done();
}
protected abstract runInstall(): { status: number };
}

View File

@ -1,9 +1,9 @@
import spawn from 'cross-spawn';
import { sync as spawnSync } from 'cross-spawn';
import { JsPackageManager } from './JsPackageManager';
export class NPMProxy extends JsPackageManager {
initPackageJson() {
const results = spawn.sync('npm', ['init', '-y'], {
const results = spawnSync('npm', ['init', '-y'], {
cwd: process.cwd(),
env: process.env,
stdio: 'pipe',
@ -15,4 +15,8 @@ export class NPMProxy extends JsPackageManager {
getRunStorybookCommand(): string {
return 'npm run storybook';
}
protected runInstall(): { status: number } {
return spawnSync('npm', ['install'], { stdio: 'inherit' });
}
}

View File

@ -1,9 +1,9 @@
import spawn from 'cross-spawn';
import { sync as spawnSync } from 'cross-spawn';
import { JsPackageManager } from './JsPackageManager';
export class Yarn1Proxy extends JsPackageManager {
initPackageJson() {
const results = spawn.sync('yarn', ['init', '-y'], {
const results = spawnSync('yarn', ['init', '-y'], {
cwd: process.cwd(),
env: process.env,
stdio: 'pipe',
@ -15,4 +15,8 @@ export class Yarn1Proxy extends JsPackageManager {
getRunStorybookCommand(): string {
return 'yarn storybook';
}
protected runInstall(): { status: number } {
return spawnSync('yarn', { stdio: 'inherit' });
}
}

View File

@ -1,9 +1,9 @@
import spawn from 'cross-spawn';
import { sync as spawnSync } from 'cross-spawn';
import { JsPackageManager } from './JsPackageManager';
export class Yarn2Proxy extends JsPackageManager {
initPackageJson() {
const results = spawn.sync('yarn', ['init'], {
const results = spawnSync('yarn', ['init'], {
cwd: process.cwd(),
env: process.env,
stdio: 'pipe',
@ -15,4 +15,8 @@ export class Yarn2Proxy extends JsPackageManager {
getRunStorybookCommand(): string {
return 'yarn storybook';
}
protected runInstall(): { status: number } {
return spawnSync('yarn', { stdio: 'inherit' });
}
}