Skip to content

Commit

Permalink
refactor: fix eslint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
negezor committed Jul 27, 2023
1 parent 82757bb commit d9d1d67
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
10 changes: 5 additions & 5 deletions packages/cacher/src/adapters/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
IAdapter,
IAdapterIncrementOptions,
IAdapterSetOptions,
IAdapterTouchOptions
IAdapterTouchOptions,
} from './adapter';

export interface IUnionAdapterOptions {
Expand All @@ -23,7 +23,7 @@ export class UnionAdapter implements IAdapter {
adapters,

replicaTtl,
replicaMode = true
replicaMode = true,
}: IUnionAdapterOptions) {
this.adapters = adapters;

Expand All @@ -38,7 +38,7 @@ export class UnionAdapter implements IAdapter {

const keysForSecond = keys.filter((item, index) => (
firstItems[index] === undefined
)) as string[];
)) ;

if (keysForSecond.length === 0) {
return firstItems;
Expand All @@ -60,7 +60,7 @@ export class UnionAdapter implements IAdapter {
key,
value,

ttl: this.replicaTtl
ttl: this.replicaTtl,
});
});

Expand Down Expand Up @@ -90,7 +90,7 @@ export class UnionAdapter implements IAdapter {

const [firstKeys, secondKeys] = await Promise.all([
firstAdapter.increment(keys),
secondAdapter.increment(keys)
secondAdapter.increment(keys),
]);

return keys.map((_, index) => firstKeys[index] || secondKeys[index]);
Expand Down
10 changes: 5 additions & 5 deletions packages/cacher/src/cacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Serializer,
Deserializer,

AllowArray
AllowArray,
} from './types';
import { arraify } from './helpers';

Expand All @@ -29,7 +29,7 @@ export class Cacher<V = any> {
namespace,

serializer = JSON.stringify,
deserializer = JSON.parse
deserializer = JSON.parse,
}: ICacherOptions<V>) {
this.adapter = adapter;

Expand Down Expand Up @@ -69,7 +69,7 @@ export class Cacher<V = any> {
return this.adapter.set(keys.map(item => ({
key: this.getNamespaceKey(item.key),
value: this.serializer(item.value),
ttl: item.ttl
ttl: item.ttl,
})));
}

Expand All @@ -78,7 +78,7 @@ export class Cacher<V = any> {

return this.adapter.increment(keys.map(item => ({
key: this.getNamespaceKey(item.key),
value: item.value
value: item.value,
})));
}

Expand All @@ -95,7 +95,7 @@ export class Cacher<V = any> {

return this.adapter.touch(keys.map(item => ({
key: this.getNamespaceKey(item.key),
ttl: item.ttl
ttl: item.ttl,
})));
}

Expand Down
20 changes: 10 additions & 10 deletions packages/redis/src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IAdapter,
IAdapterIncrementOptions,
IAdapterSetOptions,
IAdapterTouchOptions
IAdapterTouchOptions,
} from '@cacher/cacher';

export interface IRedisAdapterConnectionOptions {
Expand All @@ -18,11 +18,11 @@ export interface IRedisAdapterConnectionOptions {
}

export interface IRedisAdapterOptions {
connection?: IRedisAdapterConnectionOptions | IORedis.Redis | string;
connection?: IRedisAdapterConnectionOptions | IORedis | string;
}

export class RedisAdapter implements IAdapter {
protected readonly redis: IORedis.Redis;
protected readonly redis: IORedis;

public constructor({ connection = {} }: IRedisAdapterOptions = {}) {
if (typeof connection === 'string') {
Expand All @@ -35,7 +35,7 @@ export class RedisAdapter implements IAdapter {
port: connection.port,
username: connection.username,
password: connection.password,
db: connection.database
db: connection.database,
});
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ export class RedisAdapter implements IAdapter {

const result = await this.redis.incrbyfloat(key, value);

return [result];
return [Number(result)];
}

const pipeline = this.redis.multi();
Expand All @@ -106,10 +106,10 @@ export class RedisAdapter implements IAdapter {

const { 1: result } = await Promise.all([
pipeline.exec(),
promise
promise,
]);

return result;
return result.map(Number);
}

public async delete(keys: string[]): Promise<void> {
Expand All @@ -134,17 +134,17 @@ export class RedisAdapter implements IAdapter {

await Promise.all([
pipeline.exec(),
promise
promise,
]);
}

public async clear(namespace: string): Promise<void> {
const stream = this.redis.scanStream({
match: `${namespace}:*`
match: `${namespace}:*`,
});

for await (const keys of stream) {
await this.redis.del(...keys);
await this.redis.del(...keys as string[]);
}
}
}
44 changes: 30 additions & 14 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import typescriptPlugin from 'rollup-plugin-typescript2';

import { tmpdir } from 'os';
import { builtinModules } from 'module';
import { join as pathJoin } from 'path';
import { tmpdir } from 'node:os';
import { fileURLToPath } from 'node:url';
import { builtinModules } from 'node:module';
import { dirname, join as pathJoin } from 'node:path';

const MODULES = [
'cacher',
'redis'
'redis',
];

const coreModules = builtinModules.filter(name => (
!/(^_|\/)/.test(name)
));

const rootDir = dirname(fileURLToPath(import.meta.url));

const cacheRoot = pathJoin(tmpdir(), '.rpt2_cache');

/** @param {string} path */
const getModulePath = path => (
pathJoin(__dirname, 'packages', path)
pathJoin(rootDir, 'packages', path)
);

// eslint-disable-next-line import/no-default-export
Expand All @@ -25,8 +29,20 @@ export default async () => (
MODULES
.map(getModulePath)
.map(async (modulePath) => {
/**
* @type {{
* dependencies?: Record<string, string>,
* peerDependencies?: Record<string, string>,
* }}
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const modulePkg = await import(
pathJoin(modulePath, 'package.json')
pathJoin(modulePath, 'package.json'),
{
assert: {
type: 'json',
},
}
);

const src = pathJoin(modulePath, 'src');
Expand All @@ -43,28 +59,28 @@ export default async () => (
tsconfigOverride: {
outDir: lib,
rootDir: src,
include: [src]
}
})
include: [src],
},
}),
],
external: [
...Object.keys(modulePkg.dependencies || {}),
...Object.keys(modulePkg.peerDependencies || {}),
// TODO: To make better
...MODULES.map(moduleName => `@cacher/${moduleName}`),
...coreModules
...coreModules,
],
output: [
{
file: pathJoin(modulePath, 'lib/index.js'),
format: 'cjs',
exports: 'named'
exports: 'named',
},
{
file: pathJoin(modulePath, 'lib/index.mjs'),
format: 'esm'
}
]
format: 'esm',
},
],
};
})
)
Expand Down

0 comments on commit d9d1d67

Please sign in to comment.