Skip to content

Commit

Permalink
refactor: Remove Browserify entirely in favor of Rollup.
Browse files Browse the repository at this point in the history
  • Loading branch information
misteroneill committed May 16, 2017
1 parent 1dd813b commit 3132eae
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 765 deletions.
167 changes: 41 additions & 126 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ module.exports = yeoman.generators.Base.extend({

// At this point, the `defaults.name` may still have the full scope and
// constants.PREFIX included; so, we get the scope now.
defaults.scope = naming.getScopeFromPackageName(defaults.name);
defaults.scope = naming.getScope(defaults.name);

// Strip out the "videojs-" constants.PREFIX from the name for the purposes of
// the prompt (otherwise it will be rejected by validation).
defaults.name = naming.getDefaultNameFromPackageName(defaults.name);
// Strip out the "videojs-" constants.PREFIX from the name for the
// purposes of the prompt (otherwise it will be rejected by validation).
defaults.name = naming.getBasicName(defaults.name);

// The package.json stores a value from `LICENSE_NAMES`, so in that
// case, we need to find the key instead of the value.
Expand Down Expand Up @@ -174,6 +174,28 @@ module.exports = yeoman.generators.Base.extend({
return prompts.filter(p => !_.contains(this._promptsToFilter, p.name));
},

/**
* Generates a context object used for providing data to EJS file templates.
*
* @param {Object} [configs]
* Optionally provide custom configs.
*
* @return {Object}
*/
_getContext(configs) {
configs = configs || this.config.getAll();

return _.assign(configs, {
className: `vjs-${configs.name}`,
functionName: naming.getFunctionName(configs.name),
isPrivate: this._isPrivate(),
licenseName: constants.LICENSE_NAMES[configs.license],
packageName: naming.getPackageName(configs.name, configs.scope),
pluginName: naming.getPackageName(configs.name),
version: this._currentPkgJSON && this._currentPkgJSON.version || '0.0.0'
});
},

/**
* Sets up the generator.
*
Expand All @@ -192,42 +214,27 @@ module.exports = yeoman.generators.Base.extend({
defaults: false
});

this.option('limit-to', {
desc: tsmlj`
Limit files to be updated by passing any comma-separated combination
of: dotfiles, pkg, and scripts
`,
type: 'string',
defaults: ''
});

this.option('limit-to-meta', {
desc: 'Only update "meta files" - dotfiles, package.json, and scripts.',
type: 'boolean',
defaults: false
});

this._currentPkgJSON = this.fs.readJSON(this.destinationPath('package.json'), null);

this._filesToCopy = [
'scripts/_banner.ejs',
'_.editorconfig',
'_.gitignore',
'_.npmignore',
'_jsdoc.json'
'_jsdoc.json',
'scripts/_banner.ejs'
];

this._templatesToCopy = [
'scripts/_build-test.js',
'scripts/_server.js',
'scripts/_build.rollup.config.js',
'scripts/_test.rollup.config.js',
'src/_plugin.js',
'test/_karma.conf.js',
'test/_index.html',
'test/_karma.conf.js',
'test/_plugin.test.js',
'_.babelrc',
'_index.html',
'_CONTRIBUTING.md',
'_README.md',
'_.babelrc'
'_README.md'
];

this._promptsToFilter = [];
Expand All @@ -237,54 +244,13 @@ module.exports = yeoman.generators.Base.extend({
this.options.skipPrompt = this.options.skipInstall = true;
}

this._configsTemp = {};

// Defines the files that are allowed for various `--limit-to` options.
this._limits = {
dotfiles: {
files: [
'_.editorconfig',
'_.gitignore',
'_.npmignore',
'_.travis.yml'
],
templates: []
},

// These are empty because package.json has special handling.
pkg: {
files: [],
templates: []
},
scripts: {
files: [
'scripts/_banner.ejs'
],
templates: [
'scripts/_build-test.js',
'scripts/_server.js'
]
}
};

// this._limitTo will always be an array of the requested keys that are
// valid (i.e. found in the this._limits object).
if (this.options.limitToMeta) {
this._limitTo = Object.keys(this._limits);
} else if (this.options.limitTo) {
this._limitTo = _.intersection(
this.options.limitTo.split(',').map(s => s.trim()).filter(_.identity),
Object.keys(this._limits)
);
} else {
this._limitTo = [];
}
this._preconfigs = {};

// Make sure we filter out the author prompt if there is a current
// package.json file with an object for the author field.
if (this._currentPkgJSON && _.isPlainObject(this._currentPkgJSON.author)) {
this._promptsToFilter.push('author');
this._configsTemp.author = this._currentPkgJSON.author;
this._preconfigs.author = this._currentPkgJSON.author;
}
},

Expand All @@ -302,52 +268,21 @@ module.exports = yeoman.generators.Base.extend({

const done = this.async();

this.prompt(this._getPrompts(), responses => {
_.assign(this._configsTemp, responses);
this.prompt(this._getPrompts(), (responses) => {
_.assign(this._preconfigs, responses);
done();
});
},

/**
* Generates a context object used for providing data to EJS file templates.
*
* @param {Object} [configs]
* Optionally provide custom configs.
*
* @return {Object}
*/
_getContext(configs) {
configs = configs || this.config.getAll();

return _.assign(_.pick(configs, [
'author',
'changelog',
'description',
'docs',
'husky',
'lang',
'sass',
'ie8'
]), {
className: `vjs-${configs.name}`,
functionName: _.camelCase(configs.name),
isPrivate: this._isPrivate(),
licenseName: constants.LICENSE_NAMES[configs.license],
packageName: naming.getPackageName(configs.name, configs.scope),
pluginName: naming.getPackageName(configs.name),
version: this._currentPkgJSON && this._currentPkgJSON.version || '0.0.0'
});
},

/**
* Store configs, generate template rendering context, alter the setup for
* file structure.
*
* @method configuring
*/
configuring() {
this.config.set(this._configsTemp);
delete this._configsTemp;
this.config.set(this._preconfigs);
delete this._preconfigs;

this.context = this._getContext();

Expand All @@ -362,14 +297,6 @@ module.exports = yeoman.generators.Base.extend({
if (this.context.sass) {
this._templatesToCopy.push('src/_plugin.scss');
}

if (this._limitTo && this._limitTo.length) {
const files = _.union.apply(null, this._limitTo.map(k => this._limits[k].files));
const templates = _.union.apply(null, this._limitTo.map(k => this._limits[k].templates));

this._filesToCopy = _.intersection(this._filesToCopy, files);
this._templatesToCopy = _.intersection(this._templatesToCopy, templates);
}
},

/**
Expand All @@ -385,10 +312,7 @@ module.exports = yeoman.generators.Base.extend({
* @function changelog
*/
changelog() {

// There is no _limitTo setting that includes CHANGELOG.md, so we only
// need to check for it existing to know to skip this.
if (!this.context.changelog || this._limitTo.length) {
if (!this.context.changelog) {
return;
}

Expand Down Expand Up @@ -422,9 +346,7 @@ module.exports = yeoman.generators.Base.extend({
license() {
const file = constants.LICENSE_FILES[this.config.get('license')];

// There is no _limitTo setting that includes LICENSE, so we only
// need to check for it existing to know to skip this.
if (!file || this._limitTo.length) {
if (!file) {
return;
}

Expand All @@ -441,13 +363,6 @@ module.exports = yeoman.generators.Base.extend({
* @function package
*/
packageJSON() {

// The only time we don't want to write package.json is when there are
// any _limitTo values and "pkg" is not one of them.
if (this._limitTo.length && !_.includes(this._limitTo, 'pkg')) {
return;
}

const json = packageJSON(this._currentPkgJSON, this.context);

// We want to use normal JSON.stringify here because we want to
Expand Down
78 changes: 57 additions & 21 deletions generators/app/naming.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require('lodash');
const PREFIX = require('./constants').PREFIX;

module.exports = {
Expand All @@ -6,10 +7,14 @@ module.exports = {
* Gets the name of the scope including the "@" symbol. Will not prepend
* the "@" if it is already included.
*
* @param {String} scope
* @return {String}
* @private
* @param {string} scope
* An initial scope to normalize (e.g. "scope").
*
* @return {string}
* A normalized scope (e.g. "@scope") or an empty string.
*/
getScope(scope) {
_getScope(scope) {
if (!scope || typeof scope !== 'string') {
return '';
}
Expand All @@ -19,34 +24,49 @@ module.exports = {
/**
* Gets the name of the plugin (without scope) including the "videojs-"
* prefix.
*@private
* @param {string} name
* A plugin name without scope or "videojs-" prefix (e.g. "foo-bar").
*
* @param {String} name
* @return {String}
* @return {string}
* A plugin name without scope and including the "videojs-" prefix.
*/
getPluginName(name) {
return name && typeof name === 'string' ? PREFIX + name : '';
_getPrefixedName(name) {
if (!name || typeof name !== 'string') {
return '';
}
name = this.getBasicName(name);
return name.substr(0, PREFIX.length) === PREFIX ? name : PREFIX + name;
},

/**
* Gets the full package name, taking scope into account.
*
* @param {String} name
* @param {String} [scope='']
* @return {String}
* @param {string} name
* A plugin name without scope or "videojs-" prefix (e.g. "foo-bar").
*
* @param {string} [scope='']
* An optional package scope with or without "@" (e.g. "scope").
*
* @return {string}
* A full package name (e.g. "@scope/videojs-foo-bar")
*/
getPackageName(name, scope) {
scope = scope ? this.getScope(scope) : '';
name = this.getPluginName(name);
scope = this._getScope(scope);
name = this._getPrefixedName(name);
return scope ? `${scope}/${name}` : name;
},

/**
* Gets the scope of the plugin (without scope or "videojs-" prefix).
* Gets the scope from a package name.
*
* @param {string} packageName
* A full package name, including scope (e.g. "@scope/videojs-foo-bar").
*
* @param {String} name
* @return {String}
* @return {string}
* The scope only (e.g. "scope") or an empty string.
*/
getScopeFromPackageName(name) {
getScope(name) {
if (!name) {
return '';
}
Expand All @@ -57,16 +77,32 @@ module.exports = {
},

/**
* Gets the core/default - that is, without scope or "videojs-"
* prefix - name of the plugin.
* Gets the basic - that is, without scope or "videojs-" prefix - name of
* a plugin from a package name, prefixed name, or basic name.
*
* @param {String} name
* @return {String}
* @param {string} name
* A plugin name, possibly with scope and "videojs-" prefix (e.g.
* "@scope/videojs-foo-bar").
*
* @return {string}
* A basic plugin name (e.g. "foo-bar").
*/
getDefaultNameFromPackageName(name) {
getBasicName(name) {
if (!name) {
return '';
}
return name.split('/').reverse()[0].replace(PREFIX, '');
},

/**
* Gets a function-friendly name for the plugin (e.g. "videojsFooBar") from
* either a basic name (e.g. "foo-bar") or a prefixed
*
* @param {string} name
* A plugin name.
* @return {string}
*/
getFunctionName(name) {
return _.camelCase(this.getBasicName(name));
}
};
Loading

0 comments on commit 3132eae

Please sign in to comment.