Skip to content

Commit

Permalink
chore: add tests for Filter and some changes for url
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksiikhr committed Jun 12, 2021
1 parent 948969e commit 3eb7a98
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 41 deletions.
24 changes: 12 additions & 12 deletions src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import IService from './interfaces/IService';
import { TYPES } from './types';
import IApplication from './interfaces/IApplication';
import ObjectUtils from './utils/ObjectUtils';
import SiteUrlResolver from './modules/core/SiteUrlResolver';

// TODO Refactoring

Expand All @@ -18,6 +19,8 @@ export default class Application implements IApplication {

private templates: ITemplate[];

private siteUrl: string;

constructor(
@inject(TYPES.UserData) userData: IConfig,
@multiInject(TYPES.Services) services: IService[],
Expand All @@ -32,17 +35,16 @@ export default class Application implements IApplication {
data: this.defaultData,
services: this.registerServices(),
templates: this.registerTemplates(),
webpack: () => {},
};

this.mergeDataFromServices(this.data.data);

if (!this.data.global.www.domain && this.data.services.github.configuration.nickname) {
this.data.global.www.domain = `${this.data.services.github.configuration.nickname}.github.io`;
}

ObjectUtils.deepMerge(this.data, userData);

this.bootServices();
this.services.forEach((service) => service.applyConfigurations(this));

this.siteUrl = new SiteUrlResolver(this.config.global.www).resolve();
}

get config(): IConfig {
Expand All @@ -53,6 +55,10 @@ export default class Application implements IApplication {
return this.data.templates[this.data.template];
}

get url(): string {
return this.siteUrl;
}

protected registerServices(): any {
const services: any = {};

Expand All @@ -73,15 +79,9 @@ export default class Application implements IApplication {
return templates;
}

protected bootServices(): void {
this.services.forEach((service) => {
service.boot(this);
});
}

private mergeDataFromServices(data: IConfigData): void {
this.services.forEach((service) => {
const config = service.proxy(this);
const config = service.configDataAdapter();

if (config !== undefined) {
/* eslint-disable no-param-reassign */
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/IApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export default interface IApplication {
config: IConfig

template: ITemplate

url: string
}
1 change: 1 addition & 0 deletions src/interfaces/IConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default interface IConfig {
template: string
global: IConfigGlobal
data: IConfigData
webpack: ((config: any) => void),
services: {
github: IGithub
}
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/IService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default interface IService {

configuration(): any

boot(app: IApplication): void
applyConfigurations(app: IApplication): void

proxy(app: IApplication): IConfigData | undefined
configDataAdapter(): IConfigData | undefined
}
16 changes: 9 additions & 7 deletions src/services/github/GithubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class GithubService implements IService {
public configuration(): IGithub {
return {
configuration: {
nickname: '',
nickname: this.profileData ? this.profileData.login : '',
fetcher: {
repositories: {
type: 'owner',
Expand All @@ -63,8 +63,12 @@ export default class GithubService implements IService {
};
}

public boot(app: IApplication): void {
const { github } = app.config.services;
public applyConfigurations(app: IApplication): void {
const { global, services: { github } } = app.config;

if (!global.www.domain && github.configuration.nickname) {
global.www.domain = `${github.configuration.nickname.toLowerCase()}.github.io`;
}

github.configuration.sort.repositories.forEach((rule) => {
this.sorter.sortByRule(github.data.repositories, rule);
Expand All @@ -76,9 +80,7 @@ export default class GithubService implements IService {
);
}

public proxy(app: IApplication): IConfigData | undefined {
const { github } = app.config.services;

return github.data.profile && new GithubDataAdapter(github.data.profile);
public configDataAdapter(): IConfigData | undefined {
return this.profileData && new GithubDataAdapter(this.profileData);
}
}
5 changes: 1 addition & 4 deletions src/templates/_common/templates/header/opg.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
const { resolveFile } = require('../../scripts/template');
const { di } = require('../../../../di');
const { TYPES } = require('../../../../types');
const { default: SiteUrlResolver } = require('../../../../modules/core/SiteUrlResolver');
/** @type {IApplication} */
const app = di.get(TYPES.Application);
const { config } = app;
const url = new SiteUrlResolver(config.global.www).resolve();
const { config, url } = app;
%>

<meta property="og:title" content="Portfolio by <%= `${config.data.first_name} ${config.data.last_name}` %>" />
Expand Down
34 changes: 34 additions & 0 deletions tests/modules/Filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,40 @@ describe('Filter module', () => {
{ a: [1, 2] },
]);
});

it('empty filters', () => {
const filter = new Filter(new FilterCompare());

const result = filter.handle([
{ a: [1, 2, 3] },
{ a: [1, 2] },
{ b: [2, 3] },
], []);

expect(result).toStrictEqual([
{ a: [1, 2, 3] },
{ a: [1, 2] },
{ b: [2, 3] },
]);
});

it('empty filter group', () => {
const filter = new Filter(new FilterCompare());

const result = filter.handle([
{ a: [1, 2, 3] },
{ a: [1, 2] },
{ b: [2, 3] },
], [
[],
]);

expect(result).toStrictEqual([
{ a: [1, 2, 3] },
{ a: [1, 2] },
{ b: [2, 3] },
]);
});
});

describe('Compare Items', () => {
Expand Down
38 changes: 22 additions & 16 deletions webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import path from 'path';
import WebpackPwaManifest from 'webpack-pwa-manifest';
import { GenerateSW } from 'workbox-webpack-plugin';
import fs from 'fs';
import { WebpackPluginInstance, EntryObject } from 'webpack';
import { WebpackPluginInstance, Configuration } from 'webpack';
import { Configuration as DevServerConfiguration } from 'webpack-dev-server';
import { di } from './src/di';
import Server from './src/modules/server/Server';
import IApplication from './src/interfaces/IApplication';
import TYPES from './src/types';
import SiteUrlResolver from './src/modules/core/SiteUrlResolver';

function resolvePage(name: string, file: string) {
return `./src/pages/${name}/${file}`;
Expand All @@ -23,13 +22,12 @@ function resolveTemplate(name: string, file: string) {
return `./src/templates/${name}/${file}`;
}

const { config } = di.get<IApplication>(TYPES.Application);
const { config, url } = di.get<IApplication>(TYPES.Application);

export default (env: any, argv: { mode: string; }) => {
/** @type {boolean} */
export default (env: any, argv: { mode: string; }): Configuration => {
const isProd: boolean = argv.mode === 'production';

const webpackConfig = {
const webpackConfig: Configuration = {
devServer: {
index: 'index.html',
clientLogLevel: 'info',
Expand All @@ -49,8 +47,8 @@ export default (env: any, argv: { mode: string; }) => {
resolveTemplate(config.template, 'index.scss'),
]
: resolveTemplate('_autoload', 'index.ts'),
} as EntryObject,
mode: argv.mode,
},
mode: isProd ? 'production' : 'development',
module: {
rules: [
{
Expand Down Expand Up @@ -166,8 +164,8 @@ export default (env: any, argv: { mode: string; }) => {
};

if (isProd) {
// @ts-ignore
webpackConfig.plugins.push(
// @ts-ignore
new CleanWebpackPlugin(),
new WebpackPwaManifest({
background_color: '#fff',
Expand All @@ -182,7 +180,7 @@ export default (env: any, argv: { mode: string; }) => {
],
name: `${config.data.first_name} ${config.data.last_name}`,
short_name: `${config.data.first_name} ${config.data.last_name}`,
start_url: new SiteUrlResolver(config.global.www).resolve(),
start_url: url,
theme_color: '#fff',
...config.global.pwa,
}) as WebpackPluginInstance, // https://github.com/arthurbergmz/webpack-pwa-manifest/pull/151
Expand Down Expand Up @@ -221,19 +219,27 @@ export default (env: any, argv: { mode: string; }) => {
} else {
fs.readdirSync(path.resolve(__dirname, './src/pages'))
.forEach((folder: string) => {
// @ts-ignore
webpackConfig.entry[folder] = [
resolvePage(folder, 'index.ts'),
resolvePage(folder, 'index.scss'),
];

webpackConfig.plugins.push(new HtmlWebpackPlugin({
filename: `${folder}.html`,
inject: 'head',
chunks: [folder],
template: resolvePage(folder, 'index.ejs'),
}));
// @ts-ignore
webpackConfig.plugins.push(
new HtmlWebpackPlugin({
filename: `${folder}.html`,
inject: 'head',
chunks: [folder],
template: resolvePage(folder, 'index.ejs'),
}),
);
});
}

if (config.webpack) {
config.webpack(webpackConfig);
}

return webpackConfig;
};

0 comments on commit 3eb7a98

Please sign in to comment.