mirror of
https://github.com/wassname/generator-react-webpack.git
synced 2026-09-09 11:22:44 +08:00
Added changes for version 2.0
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
let config = require('./config');
|
||||
let yeoman = require('./yeoman');
|
||||
|
||||
module.exports = {
|
||||
config: config,
|
||||
yeoman: yeoman
|
||||
};
|
||||
@@ -0,0 +1,68 @@
|
||||
'use strict';
|
||||
let opts = require('./configopts.json');
|
||||
|
||||
/**
|
||||
* Get a setting
|
||||
* @param {String} setting
|
||||
* @return {Mixed} setting or null if not found
|
||||
*/
|
||||
let getSetting = (setting) => {
|
||||
return opts[setting] !== undefined ? opts[setting] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get choices for a given setting
|
||||
* @param {String} setting
|
||||
* @return {Mixed} Result or null if nothing was found
|
||||
*/
|
||||
let getChoices = function getChoices(setting) {
|
||||
|
||||
let config = getSetting(setting);
|
||||
return config && Array.isArray(config.options) ? config.options : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the wanted choice by key
|
||||
* @param {String} setting
|
||||
* @param {String} key
|
||||
* @return {Object}
|
||||
*/
|
||||
let getChoiceByKey = (setting, key) => {
|
||||
|
||||
let choices = getChoices(setting);
|
||||
if(!choices) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let result = null;
|
||||
|
||||
for(let choice of choices) {
|
||||
|
||||
if(choice.name === key) {
|
||||
result = choice;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default choice for a config setting
|
||||
* @param {String} setting
|
||||
* @return {Mixed}
|
||||
*/
|
||||
let getDefaultChoice = (setting) => {
|
||||
let config = getSetting(setting);
|
||||
return config && config.default !== undefined && config.default.length > 0 ? config.default : null;
|
||||
}
|
||||
|
||||
// getChoices
|
||||
// getDefault
|
||||
|
||||
module.exports = {
|
||||
getSetting: getSetting,
|
||||
getChoices: getChoices,
|
||||
getChoiceByKey: getChoiceByKey,
|
||||
getDefaultChoice: getDefaultChoice
|
||||
};
|
||||
@@ -0,0 +1,84 @@
|
||||
{
|
||||
"path": {
|
||||
"options": [
|
||||
{
|
||||
"name": "base",
|
||||
"path": "src"
|
||||
},
|
||||
{
|
||||
"name": "action",
|
||||
"path": "src/actions"
|
||||
},
|
||||
{
|
||||
"name": "component",
|
||||
"path": "src/components"
|
||||
},
|
||||
{
|
||||
"name": "image",
|
||||
"path": "src/images"
|
||||
},
|
||||
{
|
||||
"name": "source",
|
||||
"path": "src/sources"
|
||||
},
|
||||
{
|
||||
"name": "store",
|
||||
"path": "src/stores"
|
||||
},
|
||||
{
|
||||
"name": "style",
|
||||
"path": "src/styles"
|
||||
},
|
||||
{
|
||||
"name": "test",
|
||||
"path": "test"
|
||||
},
|
||||
{
|
||||
"name": "dist",
|
||||
"path": "dist"
|
||||
}
|
||||
]
|
||||
},
|
||||
"style": {
|
||||
"options": [
|
||||
{
|
||||
"name": "css",
|
||||
"value": "css",
|
||||
"suffix": ".css"
|
||||
},
|
||||
{
|
||||
"name": "sass",
|
||||
"value": "sass",
|
||||
"suffix": ".sass",
|
||||
"packages": [
|
||||
{ "name": "sass-loader", "version": "^1.0.1" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "scss",
|
||||
"value": "scss",
|
||||
"suffix": ".scss",
|
||||
"packages": [
|
||||
{ "name": "sass-loader", "version": "^1.0.1" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "less",
|
||||
"value": "less",
|
||||
"suffix": ".less",
|
||||
"packages": [
|
||||
{ "name": "less-loader", "version": "^2.0.0" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stylus",
|
||||
"value": "stylus",
|
||||
"suffix": ".styl",
|
||||
"packages": [
|
||||
{ "name": "stylus-loader", "version": "^0.5.0" }
|
||||
]
|
||||
}
|
||||
],
|
||||
"default": "css"
|
||||
}
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
'use strict';
|
||||
|
||||
let path = require('path');
|
||||
let configUtils = require('./config');
|
||||
let _ = require('underscore.string');
|
||||
|
||||
// Needed directory paths
|
||||
const baseName = path.basename(process.cwd());
|
||||
|
||||
/**
|
||||
* Get the base directory
|
||||
* @return {String}
|
||||
*/
|
||||
let getBaseDir = () => {
|
||||
return baseName;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get all settings (paths and the like) from components name
|
||||
* @param {String} componentName The components name
|
||||
* @param {String} style Style language to use [optional]
|
||||
* @return {Object} Component settings
|
||||
*/
|
||||
let getAllSettingsFromComponentName = (componentName, style) => {
|
||||
|
||||
if(!style) {
|
||||
style = 'css';
|
||||
}
|
||||
|
||||
// Clean up the path and pull it to parts
|
||||
let cleanedPaths = getCleanedPathName(componentName);
|
||||
let componentParts = cleanedPaths.split('/');
|
||||
let componentBaseName = _.capitalize(componentParts.pop());
|
||||
let componentPartPath = componentParts.join('/');
|
||||
|
||||
// Configure Styles
|
||||
let stylePaths = configUtils.getChoiceByKey('path', 'style');
|
||||
let styleSettings = configUtils.getChoiceByKey('style', style);
|
||||
|
||||
// Configure components
|
||||
let componentPath = configUtils.getChoiceByKey('path', 'component');
|
||||
|
||||
// Configure tests
|
||||
let testPath = configUtils.getChoiceByKey('path', 'test');
|
||||
|
||||
let settings = {
|
||||
style: {
|
||||
webpackPath: `styles/${componentPartPath}/${componentBaseName}${styleSettings.suffix}`,
|
||||
path: `${stylePaths.path}/${componentPartPath}/`,
|
||||
fileName: `${componentBaseName}${styleSettings.suffix}`,
|
||||
className: getComponentStyleName(componentBaseName),
|
||||
suffix: styleSettings.suffix
|
||||
},
|
||||
component: {
|
||||
webpackPath: `components/${componentPartPath}/${componentBaseName}Component.js`,
|
||||
path: `${componentPath.path}/${componentPartPath}/`,
|
||||
fileName: `${componentBaseName}Component.js`,
|
||||
className: `${componentBaseName}Component`,
|
||||
suffix: '.js'
|
||||
},
|
||||
test: {
|
||||
path: `${testPath.path}/components/${componentPartPath}/`,
|
||||
fileName: `${componentBaseName}ComponentTest.js`
|
||||
}
|
||||
};
|
||||
|
||||
return settings;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a cleaned path name for a given path
|
||||
* @param {String} path
|
||||
* @param {String} suffix [optional]
|
||||
* @return {String}
|
||||
*/
|
||||
let getCleanedPathName = (path, suffix) => {
|
||||
|
||||
if(!suffix) {
|
||||
suffix = '';
|
||||
}
|
||||
|
||||
// If we have filesystem separators, use them to build the full path
|
||||
let pathArray = path.split('/');
|
||||
|
||||
// Build the full components name
|
||||
return pathArray.map((path) => {
|
||||
return _.camelize(_.slugify(_.humanize(path)));
|
||||
}).join('/') + _.capitalize(suffix);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the css/less/whatever style name to use
|
||||
* @param {String} path
|
||||
* @return {String}
|
||||
*/
|
||||
let getComponentStyleName = (path) => {
|
||||
let fileName = path.split('/').pop().toLowerCase();
|
||||
return _.slugify(_.humanize(fileName)) + '-component';
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a js friendly application name
|
||||
* @param {String} appName The input application name [optional]
|
||||
* @return {String}
|
||||
*/
|
||||
let getAppName = (appName) => {
|
||||
|
||||
// If appName is not given, use the current directory
|
||||
if(appName === undefined) {
|
||||
appName = getBaseDir();
|
||||
}
|
||||
|
||||
return _.camelize(_.slugify(_.humanize(appName)));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getBaseDir: getBaseDir,
|
||||
getAllSettingsFromComponentName: getAllSettingsFromComponentName,
|
||||
getAppName: getAppName,
|
||||
getCleanedPathName: getCleanedPathName,
|
||||
getComponentStyleName: getComponentStyleName
|
||||
};
|
||||
Reference in New Issue
Block a user