Skip to content

Commit

Permalink
Merge pull request #3535 from Keraito/use-npm
Browse files Browse the repository at this point in the history
Use the --use-npm flag also for version checking
# Conflicts:
#	lib/cli/bin/generate.js
#	lib/cli/generators/HTML/index.js
#	lib/cli/generators/MARKO/index.js
#	lib/cli/generators/MITHRIL/index.js
  • Loading branch information
Hypnosphi committed May 17, 2018
1 parent e54c39c commit cd6f6d0
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 38 deletions.
24 changes: 12 additions & 12 deletions lib/cli/bin/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,24 @@ const runGenerator = () => {
return Promise.resolve();

case types.UPDATE_PACKAGE_ORGANIZATIONS:
return updateOrganisationsGenerator(program.parser)
return updateOrganisationsGenerator(program.parser, npmOptions)
.then(() => null) // commmandLog doesn't like to see output
.then(commandLog('Upgrading your project to the new storybook packages.'))
.then(end);

case types.REACT_SCRIPTS:
return reactScriptsGenerator()
return reactScriptsGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Create React App" based project'))
.then(end);

case types.REACT:
return reactGenerator()
return reactGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "React" app'))
.then(end);

case types.REACT_NATIVE_SCRIPTS: {
const app = chalk.bold('"./App.js"');
return reactNativeScriptsGenerator()
return reactNativeScriptsGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Create React Native App" app'))
.then(end)
.then(() => {
Expand All @@ -118,42 +118,42 @@ const runGenerator = () => {
}

case types.REACT_NATIVE:
return reactNativeGenerator()
return reactNativeGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "React Native" app'))
.then(end);

case types.METEOR:
return meteorGenerator()
return meteorGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Meteor" app'))
.then(end);

case types.WEBPACK_REACT:
return webpackReactGenerator()
return webpackReactGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Webpack React" app'))
.then(end);

case types.REACT_PROJECT:
return reactGenerator()
return reactGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "React" library'))
.then(end);

case types.SFC_VUE:
return sfcVueGenerator()
return sfcVueGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Single File Components Vue" app'))
.then(end);

case types.VUE:
return vueGenerator()
return vueGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Vue" app'))
.then(end);

case types.ANGULAR:
return angularGenerator()
return angularGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Angular" app'))
.then(end);

case types.POLYMER:
return polymerGenerator()
return polymerGenerator(npmOptions)
.then(commandLog('Adding storybook support to your "Polymer" app'))
.then(end);

Expand Down
3 changes: 2 additions & 1 deletion lib/cli/generators/ANGULAR/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import mergeDirs from 'merge-dirs';
import path from 'path';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
export default async npmOptions => {
const [
storybookVersion,
notesVersion,
actionsVersion,
linksVersion,
addonsVersion,
] = await getVersions(
npmOptions,
'@storybook/angular',
'@storybook/addon-notes',
'@storybook/addon-actions',
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/generators/METEOR/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import JSON5 from 'json5';
import mergeDirs from 'merge-dirs';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
export default async npmOptions => {
const [
storybookVersion,
actionsVersion,
Expand All @@ -18,6 +18,7 @@ export default async () => {
presetStage1Version,
rootSlashImportVersion,
] = await getVersions(
npmOptions,
'@storybook/react',
'@storybook/addon-actions',
'@storybook/addon-links',
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/generators/POLYMER/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import mergeDirs from 'merge-dirs';
import path from 'path';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
export default async npmOptions => {
const [
storybookVersion,
babelCoreVersion,
babelRuntimeVersion,
polymerLoaderVarion,
] = await getVersions(
npmOptions,
'@storybook/polymer',
'babel-core',
'babel-runtime',
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/generators/REACT/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import mergeDirs from 'merge-dirs';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
export default async npmOptions => {
const [
storybookVersion,
actionsVersion,
Expand All @@ -11,6 +11,7 @@ export default async () => {
babelCoreVersion,
babelRuntimeVersion,
] = await getVersions(
npmOptions,
'@storybook/react',
'@storybook/addon-actions',
'@storybook/addon-links',
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/generators/REACT_NATIVE/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import shell from 'shelljs';
import chalk from 'chalk';
import { getVersions, getPackageJson, writePackageJson, paddedLog } from '../../lib/helpers';

export default async () => {
export default async npmOptions => {
const [
storybookVersion,
actionsVersion,
Expand All @@ -14,6 +14,7 @@ export default async () => {
babelRuntimeVersion,
propTypesVersion,
] = await getVersions(
npmOptions,
'@storybook/react-native',
'@storybook/addon-actions',
'@storybook/addon-links',
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/generators/REACT_NATIVE_SCRIPTS/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mergeDirs from 'merge-dirs';
import path from 'path';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
export default async npmOptions => {
const [
storybookVersion,
actionsVersion,
Expand All @@ -12,6 +12,7 @@ export default async () => {
babelRuntimeVersion,
propTypesVersion,
] = await getVersions(
npmOptions,
'@storybook/react-native',
'@storybook/addon-actions',
'@storybook/addon-links',
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/generators/REACT_SCRIPTS/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import fs from 'fs';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
export default async npmOptions => {
const [
storybookVersion,
actionsVersion,
Expand All @@ -12,6 +12,7 @@ export default async () => {
babelCoreVersion,
babelRuntimeVersion,
] = await getVersions(
npmOptions,
'@storybook/react',
'@storybook/addon-actions',
'@storybook/addon-links',
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/generators/SFC_VUE/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
writeBabelRc,
} from '../../lib/helpers';

export default async () => {
export default async npmOptions => {
const [
storybookVersion,
actionsVersion,
Expand All @@ -17,6 +17,7 @@ export default async () => {
babelCoreVersion,
babelPresetVersion,
] = await getVersions(
npmOptions,
'@storybook/vue',
'@storybook/addon-actions',
'@storybook/addon-links',
Expand Down
17 changes: 9 additions & 8 deletions lib/cli/generators/UPDATE_PACKAGE_ORGANIZATIONS/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import { spawn } from 'child-process-promise';
import { packageNames } from '@storybook/codemod';
import { getVersion, getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

async function updatePackage(devDependencies, oldName, newName) {
async function updatePackage(devDependencies, oldName, newName, npmOptions) {
if (devDependencies[oldName]) {
delete devDependencies[oldName];
devDependencies[newName] = await getVersion(newName);
devDependencies[newName] = await getVersion(npmOptions, newName);
}
}

async function addPeerDependencies(packageJson) {
async function addPeerDependencies(packageJson, npmOptions) {
const [addonsVersion, babelCoreVersion, babelRuntimeVersion] = await getVersions(
npmOptions,
'@storybook/addons',
'babel-core',
'babel-runtime'
Expand All @@ -30,22 +31,22 @@ async function addPeerDependencies(packageJson) {
}
}

async function updatePackageJson() {
async function updatePackageJson(npmOptions) {
const packageJson = getPackageJson();
const { devDependencies } = packageJson;

await Promise.all(
Object.keys(packageNames).map(oldName => {
const newName = packageNames[oldName];
return updatePackage(devDependencies, oldName, newName);
return updatePackage(devDependencies, oldName, newName, npmOptions);
})
);

if (!devDependencies['@storybook/react'] && !devDependencies['@storybook/react-native']) {
throw new Error('Expected to find `@kadira/[react-native]-storybook` in devDependencies');
}

await addPeerDependencies(packageJson);
await addPeerDependencies(packageJson, npmOptions);

writePackageJson(packageJson);
}
Expand All @@ -66,7 +67,7 @@ function updateSourceCode(parser) {
return spawn(jscodeshiftCommand, args, { stdio: 'inherit' });
}

export default async parser => {
await updatePackageJson();
export default async (parser, npmOptions) => {
await updatePackageJson(npmOptions);
return updateSourceCode(parser);
};
3 changes: 2 additions & 1 deletion lib/cli/generators/VUE/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
writeBabelRc,
} from '../../lib/helpers';

export default async () => {
export default async npmOptions => {
const [
storybookVersion,
actionsVersion,
Expand All @@ -17,6 +17,7 @@ export default async () => {
babelCoreVersion,
babelPresetVersion,
] = await getVersions(
npmOptions,
'@storybook/vue',
'@storybook/addon-actions',
'@storybook/addon-links',
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/generators/WEBPACK_REACT/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mergeDirs from 'merge-dirs';
import path from 'path';
import { getVersions, getPackageJson, writePackageJson } from '../../lib/helpers';

export default async () => {
export default async npmOptions => {
const [
storybookVersion,
actionsVersion,
Expand All @@ -11,6 +11,7 @@ export default async () => {
babelCoreVersion,
babelRuntimeVersion,
] = await getVersions(
npmOptions,
'@storybook/react',
'@storybook/addon-actions',
'@storybook/addon-links',
Expand Down
8 changes: 4 additions & 4 deletions lib/cli/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { version, devDependencies } from '../package.json';

const logger = console;

export async function getVersion(packageName) {
export async function getVersion(npmOptions, packageName) {
let current;
if (packageName === '@storybook/cli') {
current = version;
Expand All @@ -19,7 +19,7 @@ export async function getVersion(packageName) {

let latest;
try {
latest = await latestVersion(packageName);
latest = await latestVersion(npmOptions, packageName);
} catch (e) {
if (current) {
logger.warn(`\n ${chalk.yellow(e.message)}`);
Expand All @@ -34,8 +34,8 @@ export async function getVersion(packageName) {
return `^${versionToUse}`;
}

export function getVersions(...packageNames) {
return Promise.all(packageNames.map(getVersion));
export function getVersions(npmOptions, ...packageNames) {
return Promise.all(packageNames.map(packageName => getVersion(npmOptions, packageName)));
}

export function getPackageJson() {
Expand Down
6 changes: 2 additions & 4 deletions lib/cli/lib/latest_version.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { spawn } from 'cross-spawn';
import hasYarn from './has_yarn';

const packageManager = hasYarn() ? 'yarn' : 'npm';

export default function latestVersion(packageName) {
export default function latestVersion(npmOptions, packageName) {
const packageManager = npmOptions.useYarn ? 'yarn' : 'npm';
return new Promise((resolve, reject) => {
const command = spawn(packageManager, ['info', packageName, 'version', '--json', '--silent'], {
cwd: process.cwd(),
Expand Down

0 comments on commit cd6f6d0

Please sign in to comment.