Add bower.json to detect frameworks

This commit is contained in:
Stijn Koopal 2017-11-20 10:27:34 +01:00
parent 1c155f7096
commit be67111422
4 changed files with 108 additions and 76 deletions

0
lib/cli/generators/POLYMER/index.js Normal file → Executable file
View File

View File

@ -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;
}

View File

@ -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');