From 1504dd5bfe292ad5bedfad4a7684b7cba5f45559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Maisse?= Date: Mon, 8 Jun 2020 20:49:31 +0200 Subject: [PATCH] refactor: ensure `dep` and `devDeps` are always defined when retrieving `package.json` Doing this just after having read the file enable us to remove duplication and unneeded calls to `writePackageJson`. --- lib/cli/src/generators/ANGULAR/index.ts | 7 +----- lib/cli/src/generators/AURELIA/index.ts | 4 ---- lib/cli/src/generators/EMBER/index.ts | 7 ------ lib/cli/src/generators/HTML/index.ts | 5 ---- lib/cli/src/generators/MARIONETTE/index.ts | 6 ----- lib/cli/src/generators/MARKO/index.ts | 6 ----- lib/cli/src/generators/METEOR/index.ts | 3 --- lib/cli/src/generators/MITHRIL/index.ts | 6 ----- lib/cli/src/generators/PREACT/index.ts | 6 ----- lib/cli/src/generators/RAX/index.ts | 3 --- lib/cli/src/generators/REACT/index.ts | 6 ----- lib/cli/src/generators/REACT_SCRIPTS/index.ts | 6 ----- lib/cli/src/generators/SFC_VUE/index.ts | 6 ----- lib/cli/src/generators/SVELTE/index.ts | 6 ----- lib/cli/src/generators/VUE/index.ts | 3 --- .../src/generators/WEB-COMPONENTS/index.ts | 6 ----- lib/cli/src/generators/WEBPACK_REACT/index.ts | 6 ----- lib/cli/src/helpers.ts | 4 ++-- .../js-package-manager/JsPackageManager.ts | 24 +++++++++++-------- lib/cli/src/js-package-manager/PackageJson.ts | 3 +++ 20 files changed, 20 insertions(+), 103 deletions(-) diff --git a/lib/cli/src/generators/ANGULAR/index.ts b/lib/cli/src/generators/ANGULAR/index.ts index ded6299eb15f..9ab5196a0192 100644 --- a/lib/cli/src/generators/ANGULAR/index.ts +++ b/lib/cli/src/generators/ANGULAR/index.ts @@ -9,7 +9,7 @@ import { getBabelDependencies, writeFileAsJson, copyTemplate } from '../../helpe import { StoryFormat } from '../../project_types'; import { NpmOptions } from '../../NpmOptions'; import { Generator, GeneratorOptions } from '../Generator'; -import { JsPackageManager, writePackageJson } from '../../js-package-manager'; +import { JsPackageManager } from '../../js-package-manager'; async function addDependencies( packageManager: JsPackageManager, @@ -31,11 +31,6 @@ async function addDependencies( const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/AURELIA/index.ts b/lib/cli/src/generators/AURELIA/index.ts index b1c3a6197947..04ec62b89ed8 100644 --- a/lib/cli/src/generators/AURELIA/index.ts +++ b/lib/cli/src/generators/AURELIA/index.ts @@ -1,7 +1,6 @@ import { getBabelDependencies, writeFileAsJson, copyTemplate, readFileAsJson } from '../../helpers'; import { Generator } from '../Generator'; import { StoryFormat } from '../../project_types'; -import { writePackageJson } from '../../js-package-manager'; function addStorybookExcludeGlobToTsConfig() { const tsConfigJson = readFileAsJson('tsconfig.json', true); @@ -43,9 +42,6 @@ const generator: Generator = async ( const versionedPackages = await packageManager.getVersionedPackages(...packages); const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - writePackageJson(packageJson); addStorybookExcludeGlobToTsConfig(); const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/EMBER/index.ts b/lib/cli/src/generators/EMBER/index.ts index be3576407ade..36fb1c1ef533 100644 --- a/lib/cli/src/generators/EMBER/index.ts +++ b/lib/cli/src/generators/EMBER/index.ts @@ -1,6 +1,5 @@ import { getBabelDependencies, copyTemplate } from '../../helpers'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const [ @@ -24,12 +23,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) copyTemplate(__dirname, storyFormat); const packageJson = packageManager.retrievePackageJson(); - - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/HTML/index.ts b/lib/cli/src/generators/HTML/index.ts index 5b8684f165b9..eb96ae0ff49f 100755 --- a/lib/cli/src/generators/HTML/index.ts +++ b/lib/cli/src/generators/HTML/index.ts @@ -1,7 +1,6 @@ import { getBabelDependencies, copyTemplate } from '../../helpers'; import { StoryFormat } from '../../project_types'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const packages = ['@storybook/html', '@storybook/addon-actions', '@storybook/addon-links']; @@ -14,10 +13,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) copyTemplate(__dirname, storyFormat); const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ ...versionedPackages, diff --git a/lib/cli/src/generators/MARIONETTE/index.ts b/lib/cli/src/generators/MARIONETTE/index.ts index 3eb86c1f8ae4..c29c891b00ed 100644 --- a/lib/cli/src/generators/MARIONETTE/index.ts +++ b/lib/cli/src/generators/MARIONETTE/index.ts @@ -2,7 +2,6 @@ import fse from 'fs-extra'; import path from 'path'; import { getBabelDependencies } from '../../helpers'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions) => { const storybookVersion = await packageManager.getVersion('@storybook/marionette'); @@ -10,11 +9,6 @@ const generator: Generator = async (packageManager, npmOptions) => { const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/MARKO/index.ts b/lib/cli/src/generators/MARKO/index.ts index 6f5a91b48264..f28c62ff86d1 100644 --- a/lib/cli/src/generators/MARKO/index.ts +++ b/lib/cli/src/generators/MARKO/index.ts @@ -1,6 +1,5 @@ import { getBabelDependencies, copyTemplate } from '../../helpers'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const [ @@ -17,11 +16,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/METEOR/index.ts b/lib/cli/src/generators/METEOR/index.ts index e44a102fca69..95ae01834906 100644 --- a/lib/cli/src/generators/METEOR/index.ts +++ b/lib/cli/src/generators/METEOR/index.ts @@ -31,9 +31,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.devDependencies = packageJson.devDependencies || {}; - packageJson.dependencies = packageJson.dependencies || {}; - const devDependencies = [ `@storybook/react@${storybookVersion}`, `@storybook/addon-actions@${actionsVersion}`, diff --git a/lib/cli/src/generators/MITHRIL/index.ts b/lib/cli/src/generators/MITHRIL/index.ts index b90a1a5b837d..329b521c9d7f 100644 --- a/lib/cli/src/generators/MITHRIL/index.ts +++ b/lib/cli/src/generators/MITHRIL/index.ts @@ -1,6 +1,5 @@ import { getBabelDependencies, copyTemplate } from '../../helpers'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const [ @@ -21,11 +20,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/PREACT/index.ts b/lib/cli/src/generators/PREACT/index.ts index b733d04fa94f..f6ceceac04d8 100644 --- a/lib/cli/src/generators/PREACT/index.ts +++ b/lib/cli/src/generators/PREACT/index.ts @@ -1,6 +1,5 @@ import { getBabelDependencies, copyTemplate } from '../../helpers'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const [ @@ -19,11 +18,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/RAX/index.ts b/lib/cli/src/generators/RAX/index.ts index 76c3552db151..ed0b4bdfa3cb 100644 --- a/lib/cli/src/generators/RAX/index.ts +++ b/lib/cli/src/generators/RAX/index.ts @@ -21,9 +21,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - const raxVersion = packageJson.dependencies.rax || latestRaxVersion; // in case Rax project is not detected, `rax` package is not available either diff --git a/lib/cli/src/generators/REACT/index.ts b/lib/cli/src/generators/REACT/index.ts index 5fde4ca7af59..2a6606bc4d2b 100644 --- a/lib/cli/src/generators/REACT/index.ts +++ b/lib/cli/src/generators/REACT/index.ts @@ -1,7 +1,6 @@ import { getBabelDependencies, copyTemplate } from '../../helpers'; import { StoryFormat } from '../../project_types'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const packages = [ @@ -20,11 +19,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/REACT_SCRIPTS/index.ts b/lib/cli/src/generators/REACT_SCRIPTS/index.ts index 776f24156dec..bba68ba18b73 100644 --- a/lib/cli/src/generators/REACT_SCRIPTS/index.ts +++ b/lib/cli/src/generators/REACT_SCRIPTS/index.ts @@ -3,7 +3,6 @@ import fs from 'fs'; import { getBabelDependencies, copyTemplate } from '../../helpers'; import { StoryFormat } from '../../project_types'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const packages = [ @@ -24,11 +23,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/SFC_VUE/index.ts b/lib/cli/src/generators/SFC_VUE/index.ts index 081f23ab303d..df945f683faa 100644 --- a/lib/cli/src/generators/SFC_VUE/index.ts +++ b/lib/cli/src/generators/SFC_VUE/index.ts @@ -1,7 +1,6 @@ import { getBabelDependencies, copyTemplate } from '../../helpers'; import { StoryFormat } from '../../project_types'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const packages = [ @@ -19,11 +18,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/SVELTE/index.ts b/lib/cli/src/generators/SVELTE/index.ts index ded5f825eb36..646e234961c6 100644 --- a/lib/cli/src/generators/SVELTE/index.ts +++ b/lib/cli/src/generators/SVELTE/index.ts @@ -1,6 +1,5 @@ import { getBabelDependencies, copyTemplate } from '../../helpers'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const [ @@ -23,11 +22,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/VUE/index.ts b/lib/cli/src/generators/VUE/index.ts index 9a2e31d10674..0efb2d56bb0f 100644 --- a/lib/cli/src/generators/VUE/index.ts +++ b/lib/cli/src/generators/VUE/index.ts @@ -24,9 +24,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - const packageBabelCoreVersion = packageJson.dependencies['babel-core'] || packageJson.devDependencies['babel-core']; diff --git a/lib/cli/src/generators/WEB-COMPONENTS/index.ts b/lib/cli/src/generators/WEB-COMPONENTS/index.ts index a8a8c93662f6..03ced0f5e0c3 100755 --- a/lib/cli/src/generators/WEB-COMPONENTS/index.ts +++ b/lib/cli/src/generators/WEB-COMPONENTS/index.ts @@ -3,7 +3,6 @@ import path from 'path'; import { getBabelDependencies } from '../../helpers'; import { StoryFormat } from '../../project_types'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const packages = [ @@ -23,11 +22,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/generators/WEBPACK_REACT/index.ts b/lib/cli/src/generators/WEBPACK_REACT/index.ts index 0447fc15b47d..5c030def1ee4 100644 --- a/lib/cli/src/generators/WEBPACK_REACT/index.ts +++ b/lib/cli/src/generators/WEBPACK_REACT/index.ts @@ -1,7 +1,6 @@ import { getBabelDependencies, copyTemplate } from '../../helpers'; import { StoryFormat } from '../../project_types'; import { Generator } from '../Generator'; -import { writePackageJson } from '../../js-package-manager'; const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => { const packages = [ @@ -19,11 +18,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat }) const packageJson = packageManager.retrievePackageJson(); - packageJson.dependencies = packageJson.dependencies || {}; - packageJson.devDependencies = packageJson.devDependencies || {}; - - writePackageJson(packageJson); - const babelDependencies = await getBabelDependencies(packageManager, packageJson); packageManager.addDependencies({ ...npmOptions, packageJson }, [ diff --git a/lib/cli/src/helpers.ts b/lib/cli/src/helpers.ts index 4bea186fe075..9f32d3c4facc 100644 --- a/lib/cli/src/helpers.ts +++ b/lib/cli/src/helpers.ts @@ -7,7 +7,7 @@ import { satisfies } from '@storybook/semver'; import stripJsonComments from 'strip-json-comments'; import { StoryFormat } from './project_types'; -import { JsPackageManager, PackageJson } from './js-package-manager'; +import { JsPackageManager, PackageJson, PackageJsonWithDepsAndDevDeps } from './js-package-manager'; const logger = console; @@ -118,7 +118,7 @@ export function codeLog(codeLines: string[], leftPadAmount?: number) { */ export async function getBabelDependencies( packageManager: JsPackageManager, - packageJson: PackageJson + packageJson: PackageJsonWithDepsAndDevDeps ) { const dependenciesToAdd = []; let babelLoaderVersion = '^8.0.0-0'; diff --git a/lib/cli/src/js-package-manager/JsPackageManager.ts b/lib/cli/src/js-package-manager/JsPackageManager.ts index 1829b29d7f4b..a06feae04510 100644 --- a/lib/cli/src/js-package-manager/JsPackageManager.ts +++ b/lib/cli/src/js-package-manager/JsPackageManager.ts @@ -2,7 +2,7 @@ import chalk from 'chalk'; import { gt, satisfies } from '@storybook/semver'; import { sync as spawnSync } from 'cross-spawn'; import { commandLog } from '../helpers'; -import { PackageJson } from './PackageJson'; +import { PackageJson, PackageJsonWithDepsAndDevDeps } from './PackageJson'; import { readPackageJson, writePackageJson } from './PackageJsonHelper'; const logger = console; @@ -34,17 +34,21 @@ export abstract class JsPackageManager { done(); } - public retrievePackageJson(): PackageJson { - const existing = readPackageJson(); - if (existing) { - return existing; - } + public retrievePackageJson(): PackageJsonWithDepsAndDevDeps { + let packageJson = readPackageJson(); + if (!packageJson) { + // It will create a new package.json file + this.initPackageJson(); - // It will create a new package.json file - this.initPackageJson(); + // read the newly created package.json file + packageJson = readPackageJson() || {}; + } - // read the newly created package.json file - return readPackageJson() || {}; + return { + ...packageJson, + dependencies: { ...packageJson.dependencies }, + devDependencies: { ...packageJson.devDependencies }, + }; } /** diff --git a/lib/cli/src/js-package-manager/PackageJson.ts b/lib/cli/src/js-package-manager/PackageJson.ts index d2313640227d..3a1cf2c9d2a1 100644 --- a/lib/cli/src/js-package-manager/PackageJson.ts +++ b/lib/cli/src/js-package-manager/PackageJson.ts @@ -4,3 +4,6 @@ export type PackageJson = { peerDependencies?: Record; scripts?: Record; }; + +export type PackageJsonWithDepsAndDevDeps = PackageJson & + Required>;