Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove automatic class name resolving in ReadOptions decorator #246

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/config.webpack/src/WebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createWebpackConfiguration } from './createWebpackConfiguration';
import { OneOfModification } from './modifications/OneOfModification';
import { WebpackConfigOptions } from './WebpackConfigOptions';

@ReadOptions(WebpackConfigOptions)
@ReadOptions(WebpackConfigOptions, 'config.webpack')
export class WebpackConfig extends AbstractConfigBuilder<
Configuration,
WebpackConfigOptions
Expand Down
26 changes: 2 additions & 24 deletions packages/core/src/decorators/ReadOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readZeroScriptsOptions } from '../utils/readZeroScriptsOptions';
import { splitByUpperCase } from '../utils/splitByUpperCase';
import { AbstractOptionsContainer } from '../AbstractOptionsContainer';

/**
Expand All @@ -20,32 +19,11 @@ import { AbstractOptionsContainer } from '../AbstractOptionsContainer';
*/
export const ReadOptions = <TOptionsContainer extends AbstractOptionsContainer>(
OptionsContainer: { new (externalOptions: object): TOptionsContainer },
optionsKey?: string
optionsKey: string
) => <T extends { new (...args: any[]): any }>(DecoratedClass: T) =>
class extends DecoratedClass {
constructor(...args: any[]) {
const zeroScriptsOptions = readZeroScriptsOptions();

// if optionsKey is not defined
// find key by splitting class name
// TODO: in future we refuse from automatic correspond
// class name to package (perf, non-obviousness)
const key = !optionsKey
? zeroScriptsOptions &&
Object.keys(zeroScriptsOptions).find(packageName => {
let isSuitable = false;
splitByUpperCase(DecoratedClass.name).forEach(word => {
const currentIndexOf = packageName.indexOf(word);
isSuitable = currentIndexOf !== -1;
});
return isSuitable;
})
: optionsKey;

const externalOptions =
key && typeof zeroScriptsOptions[key] === 'object'
? zeroScriptsOptions[key]
: {};
const externalOptions = readZeroScriptsOptions(optionsKey);

const [optionsContainerInstance, ...restArgs] =
args[0] instanceof AbstractOptionsContainer
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/utils/readZeroScriptsOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { readPackageJson } from './readPackageJson';
export const packageJsonOptionsKey = 'zero-scripts';

export const readZeroScriptsOptions = (key?: string) =>
readPackageJson(data =>
key
readPackageJson(data => {
const value = key
? data[packageJsonOptionsKey] && data[packageJsonOptionsKey][key]
: data[packageJsonOptionsKey]
);
: data[packageJsonOptionsKey];

return value ? value : {};
});
2 changes: 0 additions & 2 deletions packages/core/src/utils/splitByUpperCase.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AbstractPreset, ReadOptions } from '@zero-scripts/core';
import { WebpackConfig } from '@zero-scripts/config.webpack';
import { WebpackBabelReactExtensionOptions } from './WebpackBabelReactExtensionOptions';

@ReadOptions(WebpackBabelReactExtensionOptions)
@ReadOptions(WebpackBabelReactExtensionOptions, 'extension.webpack-babel.react')
export class WebpackBabelReactExtension<
TParentExtensionOptions extends WebpackBabelReactExtensionOptions = WebpackBabelReactExtensionOptions
> extends WebpackBabelExtension<TParentExtensionOptions> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { WebpackConfig } from '@zero-scripts/config.webpack';
import { WebpackBabelExtensionOptions } from './WebpackBabelExtensionOptions';

@ReadOptions(WebpackBabelExtensionOptions)
@ReadOptions(WebpackBabelExtensionOptions, 'extension.webpack-babel')
export class WebpackBabelExtension<
TParentExtensionOptions extends WebpackBabelExtensionOptions = WebpackBabelExtensionOptions
> extends AbstractExtension<TParentExtensionOptions> {
Expand Down
2 changes: 1 addition & 1 deletion packages/extension.webpack-css/src/WebpackCssExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin';
const safePostCssParser = require('postcss-safe-parser');
const cssModuleRegex = /\.(module|m)\.css$/;

@ReadOptions(WebpackCssExtensionOptions)
@ReadOptions(WebpackCssExtensionOptions, 'extension.webpack-css')
export class WebpackCssExtension<
TParentExtensionOptions extends WebpackCssExtensionOptions = WebpackCssExtensionOptions
> extends AbstractExtension<TParentExtensionOptions> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { AbstractPreset } from '@zero-scripts/core';
import { AbstractPreset, ReadOptions } from '@zero-scripts/core';
import { WebpackEslintExtension } from '@zero-scripts/extension.webpack-eslint';
import { WebpackEslintReactExtensionOptions } from './WebpackEslintReactExtensionOptions';

@ReadOptions(
WebpackEslintReactExtensionOptions,
'extension.webpack-eslint.react'
)
export class WebpackEslintReactExtension extends WebpackEslintExtension<
WebpackEslintReactExtensionOptions
> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { WebpackConfig } from '@zero-scripts/config.webpack';
import { WebpackEslintExtensionOptions } from './WebpackEslintExtensionOptions';

@ReadOptions(WebpackEslintExtensionOptions)
@ReadOptions(WebpackEslintExtensionOptions, 'extension.webpack-eslint')
export class WebpackEslintExtension<
TParentExtensionOptions extends WebpackEslintExtensionOptions = WebpackEslintExtensionOptions
> extends AbstractExtension<TParentExtensionOptions> {
Expand Down
2 changes: 1 addition & 1 deletion packages/extension.webpack-pwa/src/WebpackPwaExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { WebpackConfig } from '@zero-scripts/config.webpack';
import { WebpackPwaExtensionOptions } from './WebpackPwaExtensionOptions';
import WorkboxWebpackPlugin from 'workbox-webpack-plugin';

@ReadOptions(WebpackPwaExtensionOptions)
@ReadOptions(WebpackPwaExtensionOptions, 'extension.webpack-pwa')
export class WebpackPwaExtension extends AbstractExtension<
WebpackPwaExtensionOptions
> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin';
const safePostCssParser = require('postcss-safe-parser');
const sassModuleRegex = /\.(module|m)\.(scss|sass)$/;

@ReadOptions(WebpackSassExtensionOptions)
@ReadOptions(WebpackSassExtensionOptions, 'extension.webpack-sass')
export class WebpackSassExtension extends AbstractExtension<
WebpackSassExtensionOptions
> {
Expand Down