From be6711142246d8996860609b7f8dd2626d7bebcd Mon Sep 17 00:00:00 2001 From: Stijn Koopal Date: Mon, 20 Nov 2017 10:27:34 +0100 Subject: [PATCH] Add bower.json to detect frameworks --- lib/cli/generators/POLYMER/index.js | 0 .../{ => src}/stories/index.stories.js | 0 lib/cli/lib/detect.js | 174 ++++++++++-------- lib/cli/lib/helpers.js | 10 + 4 files changed, 108 insertions(+), 76 deletions(-) mode change 100644 => 100755 lib/cli/generators/POLYMER/index.js rename lib/cli/generators/POLYMER/template/{ => src}/stories/index.stories.js (100%) diff --git a/lib/cli/generators/POLYMER/index.js b/lib/cli/generators/POLYMER/index.js old mode 100644 new mode 100755 diff --git a/lib/cli/generators/POLYMER/template/stories/index.stories.js b/lib/cli/generators/POLYMER/template/src/stories/index.stories.js similarity index 100% rename from lib/cli/generators/POLYMER/template/stories/index.stories.js rename to lib/cli/generators/POLYMER/template/src/stories/index.stories.js diff --git a/lib/cli/lib/detect.js b/lib/cli/lib/detect.js index 4708ac5f196..8226013b13d 100644 --- a/lib/cli/lib/detect.js +++ b/lib/cli/lib/detect.js @@ -1,20 +1,95 @@ import path from 'path'; import fs from 'fs'; import types from './project_types'; -import { getPackageJson } from './helpers'; +import { getBowerJson, getPackageJson } from './helpers'; -export default function detect(options) { - const packageJson = getPackageJson(); - if (!packageJson) { - return types.UNDETECTED; +function detectFramework(dependencies) { + if (!dependencies) { + return false; + } + if ( + dependencies.devDependencies && + (dependencies.devDependencies['vue-loader'] || dependencies.devDependencies.vueify) + ) { + return types.SFC_VUE; } - if (!options.force && packageJson.devDependencies) { + if ( + (dependencies.dependencies && dependencies.dependencies.vue) || + (dependencies.devDependencies && dependencies.devDependencies.vue) || + (dependencies.dependencies && dependencies.dependencies.nuxt) || + (dependencies.devDependencies && dependencies.devDependencies.nuxt) + ) { + return types.VUE; + } + + if ( + (dependencies.dependencies && dependencies.dependencies['react-scripts']) || + (dependencies.devDependencies && dependencies.devDependencies['react-scripts']) + ) { + return types.REACT_SCRIPTS; + } + + if ( + ((dependencies.devDependencies && dependencies.devDependencies.webpack) || + (dependencies.dependencies && dependencies.dependencies.webpack)) && + ((dependencies.devDependencies && dependencies.devDependencies.react) || + (dependencies.dependencies && dependencies.dependencies.react)) + ) { + return types.WEBPACK_REACT; + } + + if (dependencies.peerDependencies && dependencies.peerDependencies.react) { + return types.REACT_PROJECT; + } + + if ( + (dependencies.dependencies && dependencies.dependencies['react-native-scripts']) || + (dependencies.devDependencies && dependencies.devDependencies['react-native-scripts']) + ) { + return types.REACT_NATIVE_SCRIPTS; + } + + if (dependencies.dependencies && dependencies.dependencies['react-native']) { + return types.REACT_NATIVE; + } + + if ( + (dependencies.dependencies && dependencies.dependencies.react) || + (dependencies.devDependencies && dependencies.devDependencies.react) + ) { + return types.REACT; + } + + if ( + (dependencies.dependencies && dependencies.dependencies['@angular/core']) || + (dependencies.devDependencies && dependencies.devDependencies['@angular/core']) + ) { + return types.ANGULAR; + } + + if ( + (dependencies.dependencies && dependencies.dependencies['@polymer/polymer']) || + (dependencies.devDependencies && dependencies.devDependencies['@polymer/polymer']) || + (dependencies.dependencies && dependencies.dependencies.polymer) || + (dependencies.devDependencies && dependencies.devDependencies.polymer) + ) { + return types.POLYMER; + } + return false; +} + +function isStorybookInstalled(dependencies, force) { + if (!dependencies) { + return false; + } + + if (!force && dependencies.devDependencies) { const supportedFrameworks = ['react', 'react-native', 'vue', 'angular', 'polymer']; if ( supportedFrameworks.reduce( (storybookPresent, framework) => - storybookPresent || packageJson.devDependencies[`@storybook/${framework}`], + storybookPresent || dependencies.devDependencies[`@storybook/${framework}`], false ) ) { @@ -22,84 +97,31 @@ export default function detect(options) { } if ( - packageJson.devDependencies['@kadira/storybook'] || - packageJson.devDependencies['@kadira/react-native-storybook'] + dependencies.devDependencies['@kadira/storybook'] || + dependencies.devDependencies['@kadira/react-native-storybook'] ) { return types.UPDATE_PACKAGE_ORGANIZATIONS; } } + return false; +} + +export default function detect(options) { + const packageJson = getPackageJson(); + const bowerJson = getBowerJson(); + + if (!packageJson && !bowerJson) { + return types.UNDETECTED; + } if (fs.existsSync(path.resolve('.meteor'))) { return types.METEOR; } - if ( - packageJson.devDependencies && - (packageJson.devDependencies['vue-loader'] || packageJson.devDependencies.vueify) - ) { - return types.SFC_VUE; + const storyBookInstalled = isStorybookInstalled(packageJson, options.force); + if (storyBookInstalled) { + return storyBookInstalled; } - if ( - (packageJson.dependencies && packageJson.dependencies.vue) || - (packageJson.devDependencies && packageJson.devDependencies.vue) || - (packageJson.dependencies && packageJson.dependencies.nuxt) || - (packageJson.devDependencies && packageJson.devDependencies.nuxt) - ) { - return types.VUE; - } - - if ( - (packageJson.dependencies && packageJson.dependencies['react-scripts']) || - (packageJson.devDependencies && packageJson.devDependencies['react-scripts']) - ) { - return types.REACT_SCRIPTS; - } - - if ( - ((packageJson.devDependencies && packageJson.devDependencies.webpack) || - (packageJson.dependencies && packageJson.dependencies.webpack)) && - ((packageJson.devDependencies && packageJson.devDependencies.react) || - (packageJson.dependencies && packageJson.dependencies.react)) - ) { - return types.WEBPACK_REACT; - } - - if (packageJson.peerDependencies && packageJson.peerDependencies.react) { - return types.REACT_PROJECT; - } - - if ( - (packageJson.dependencies && packageJson.dependencies['react-native-scripts']) || - (packageJson.devDependencies && packageJson.devDependencies['react-native-scripts']) - ) { - return types.REACT_NATIVE_SCRIPTS; - } - - if (packageJson.dependencies && packageJson.dependencies['react-native']) { - return types.REACT_NATIVE; - } - - if ( - (packageJson.dependencies && packageJson.dependencies.react) || - (packageJson.devDependencies && packageJson.devDependencies.react) - ) { - return types.REACT; - } - - if ( - (packageJson.dependencies && packageJson.dependencies['@angular/core']) || - (packageJson.devDependencies && packageJson.devDependencies['@angular/core']) - ) { - return types.ANGULAR; - } - - if ( - (packageJson.dependencies && packageJson.dependencies['@polymer/polymer']) || - (packageJson.devDependencies && packageJson.devDependencies['@polymer/polymer']) - ) { - return types.POLYMER; - } - - return types.UNDETECTED; + return detectFramework(packageJson) || detectFramework(bowerJson) || types.UNDETECTED; } diff --git a/lib/cli/lib/helpers.js b/lib/cli/lib/helpers.js index 6a3034df079..3b481312cac 100644 --- a/lib/cli/lib/helpers.js +++ b/lib/cli/lib/helpers.js @@ -47,6 +47,16 @@ export function getPackageJson() { return JSON.parse(jsonContent); } +export function getBowerJson() { + const bowerJsonPath = path.resolve('bower.json'); + if (!fs.existsSync(bowerJsonPath)) { + return false; + } + + const jsonContent = fs.readFileSync(bowerJsonPath, 'utf8'); + return JSON.parse(jsonContent); +} + export function writePackageJson(packageJson) { const content = `${JSON.stringify(packageJson, null, 2)}\n`; const packageJsonPath = path.resolve('package.json');