Skip to content

Commit

Permalink
ci: format files on commit, validate commit msg
Browse files Browse the repository at this point in the history
  • Loading branch information
stas-nc committed Dec 24, 2024
1 parent 3556cff commit 273db3f
Show file tree
Hide file tree
Showing 16 changed files with 1,037 additions and 48 deletions.
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx --no -- commitlint --edit $1
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npm run format:check
npx lint-staged
6 changes: 6 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('@commitlint/types').UserConfig}
*/
module.exports = {
extends: ['@commitlint/config-conventional'],
};
5 changes: 2 additions & 3 deletions ilc/client/BundleLoader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe('BundleLoader', () => {
delete: sinon.stub(),
};
let registry;
let registryConfigurationStub;
const configRoot = getIlcConfigRoot();

const mockFactoryFn = () => {};
Expand Down Expand Up @@ -54,12 +53,12 @@ describe('BundleLoader', () => {
},
},
}).getConfig();
registryConfigurationStub = sinon.stub(configRoot, 'registryConfiguration').value(registry);
sinon.stub(configRoot, 'registryConfiguration').value(registry);
});

afterEach(() => {
SystemJs.import.reset();
registryConfigurationStub.restore();
sinon.stub(configRoot, 'registryConfiguration').restore();
});

describe('preloadApp()', () => {
Expand Down
2 changes: 1 addition & 1 deletion ilc/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class Client {
}

#configure() {
addNavigationHook((url) => (this.#transitionHooksExecutor.hasAccessTo(url) ? url : null));
addNavigationHook((url) => (this.#transitionHooksExecutor.shouldNavigate(url) ? url : null));
addNavigationHook((url) => this.#urlProcessor.process(url));

// TODO: window.ILC.importLibrary - calls bootstrap function with props (if supported), and returns exposed API
Expand Down
2 changes: 1 addition & 1 deletion ilc/client/TransitionHooksExecutor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class TransitionHooksExecutor {
this.#logger = logger;
}

hasAccessTo(url) {
shouldNavigate(url) {
const route = this.#router.match(url);
// This code is executed before the router change, so current = previous
const prevRoute = this.#router.getCurrentRoute();
Expand Down
10 changes: 5 additions & 5 deletions ilc/client/TransitionHooksExecutor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('TransitionHooksExecutor', () => {

const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(transitionHooksExecutor.hasAccessTo('/router/does/not/have/route')).to.be.true;
chai.expect(transitionHooksExecutor.shouldNavigate('/router/does/not/have/route')).to.be.true;
});

it(`if none of hooks returns "${actionTypes.stopNavigation}" or "${actionTypes.redirect}" action types`, () => {
Expand All @@ -87,7 +87,7 @@ describe('TransitionHooksExecutor', () => {

const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.true;
chai.expect(transitionHooksExecutor.shouldNavigate(url)).to.be.true;

for (const hook of hooks) {
sinon.assert.calledOnceWithExactly(hook, {
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('TransitionHooksExecutor', () => {

const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.false;
chai.expect(transitionHooksExecutor.shouldNavigate(url)).to.be.false;
sinon.assert.calledOnce(errorHandler);
chai.expect(errorHandler.getCall(0).args[0]).to.have.property('cause', error);
chai.expect(errorHandler.getCall(0).args[0]).to.be.instanceOf(TransitionHookError);
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('TransitionHooksExecutor', () => {

const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.false;
chai.expect(transitionHooksExecutor.shouldNavigate(url)).to.be.false;
sinon.assert.calledOnceWithExactly(
logger.info,
`ILC: Stopped navigation due to the Route Guard with index #${1}`,
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('TransitionHooksExecutor', () => {

const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.false;
chai.expect(transitionHooksExecutor.shouldNavigate(url)).to.be.false;
sinon.assert.notCalled(router.navigateToUrl);

for (const hook of [hooks[0], hooks[1]]) {
Expand Down
10 changes: 1 addition & 9 deletions ilc/common/DefaultCacheWrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,7 @@ describe('DefaultCacheWrapper', () => {
});

it('should return the same value in case of concurrent invocation', async () => {
fn.withArgs().callsFake(
() =>
new Promise((resolve) =>
setTimeout(() => {
console.log('CALLED');
return resolve(data);
}, 100),
),
);
fn.withArgs().callsFake(() => new Promise((resolve) => setTimeout(() => resolve(data), 100)));

const [firstValue, secondValue, thirdValue] = await Promise.all([wrappedFn(), wrappedFn(), wrappedFn()]);

Expand Down
20 changes: 16 additions & 4 deletions ilc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ilc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"fast-glob": "^3.3.2",
"fastify": "^2.15.3",
"http-status-codes": "^2.3.0",
"ilc-plugins-sdk": "^2.2.0",
"ilc-plugins-sdk": "^2.3.0",
"ilc-sdk": "^5.2.4",
"is-url": "^1.2.4",
"js-cookie": "^2.2.1",
Expand Down
9 changes: 5 additions & 4 deletions ilc/server/TransitionHooksExecutor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ describe('TransitionHooksExecutor', () => {
protected: true,
},
reqUrl: '/some/unlocalized/and/processed/url',
route: '/some/*',
});
const log = Object.freeze({
info: () => {},
Expand Down Expand Up @@ -177,7 +178,7 @@ describe('TransitionHooksExecutor', () => {
for (const hook of hooks) {
chai.expect(
hook.calledOnceWith({
route: { meta: route.meta, url: route.reqUrl, hostname: req.hostname },
route: { meta: route.meta, url: route.reqUrl, hostname: req.hostname, route: route.route },
req: rawReq,
log,
}),
Expand Down Expand Up @@ -214,7 +215,7 @@ describe('TransitionHooksExecutor', () => {
for (const hook of [hooks[0], hooks[1]]) {
chai.expect(
hook.calledOnceWith({
route: { meta: route.meta, url: route.reqUrl, hostname: req.hostname },
route: { meta: route.meta, url: route.reqUrl, hostname: req.hostname, route: route.route },
req: rawReq,
log,
}),
Expand Down Expand Up @@ -243,7 +244,7 @@ describe('TransitionHooksExecutor', () => {
for (const hook of [hooks[0], hooks[1]]) {
chai.expect(
hook.calledOnceWith({
route: { meta: route.meta, url: route.reqUrl, hostname: req.hostname },
route: { meta: route.meta, url: route.reqUrl, hostname: req.hostname, route: route.route },
req: rawReq,
log,
}),
Expand Down Expand Up @@ -273,7 +274,7 @@ describe('TransitionHooksExecutor', () => {
for (const hook of [hooks[0], hooks[1]]) {
chai.expect(
hook.calledOnceWith({
route: { meta: route.meta, url: route.reqUrl, hostname: req.hostname },
route: { meta: route.meta, url: route.reqUrl, hostname: req.hostname, route: route.route },
req: rawReq,
log,
}),
Expand Down
10 changes: 8 additions & 2 deletions ilc/server/TransitionHooksExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { PluginManager, TransitionHooksPlugin } from 'ilc-plugins-sdk';
import { TransitionHookError } from '../common/guard/errors';
import { FastifyRequest } from 'fastify';
import { PatchedHttpRequest } from './types/PatchedHttpRequest';

type TransitionResult = {
location: string;
Expand All @@ -13,7 +15,10 @@ export class TransitionHooksExecutor {
this.transitionHooksPlugin = pluginManager.getTransitionHooksPlugin();
}

async redirectTo(req: any): Promise<TransitionResult | null> {
async redirectTo(req: FastifyRequest<PatchedHttpRequest>): Promise<TransitionResult | null> {
if (!req.raw.router) {
throw new Error('Router not initialized');
}
const route = req.raw.router.getRoute();

if (route.specialRole !== null) {
Expand All @@ -33,6 +38,7 @@ export class TransitionHooksExecutor {
meta: route.meta,
url: route.reqUrl,
hostname: req.hostname,
route: route.route,
},
log: req.log,
req: req.raw,
Expand All @@ -41,7 +47,7 @@ export class TransitionHooksExecutor {
if (action.type === 'redirect' && action.newLocation) {
const code = action.code ?? 302;
if (code < 300 || code > 308) {
throw new TransitionHookError({ message: 'Invlid redirect code' });
throw new TransitionHookError({ message: 'Invalid redirect code' });
}
return {
location: action.newLocation,
Expand Down
18 changes: 8 additions & 10 deletions ilc/server/types/RegistryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@ export type Slot = {
props: {};
};

export type Route = {
type BaseRoute = {
slots: Record<string, Slot>;
meta: {};
next: boolean;
versionId: string;
domain?: string;
};

export type Route = BaseRoute & {
routeId?: number;
route: string;
next: boolean;
template?: string;
orderPos: number;
versionId: string;
domain?: string;
};

export type SpecialRoute = {
slots: Record<string, Slot>;
meta: {};
export type SpecialRoute = BaseRoute & {
routeId: number;
next: boolean;
template: string;
specialRole: string;
versionId: string;
domain?: string;
};

export interface RegistryConfig {
Expand Down
6 changes: 6 additions & 0 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('lint-staged').Config}
*/
module.exports = {
'*': 'prettier --ignore-unknown --write',
};
Loading

0 comments on commit 273db3f

Please sign in to comment.