Skip to content

Commit

Permalink
fix: more agressive import separation
Browse files Browse the repository at this point in the history
  • Loading branch information
Figedi committed Jul 23, 2023
1 parent 500ace0 commit 6e9bcd1
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module.exports = {
project: "./tsconfig.json",
},
rules: {
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/no-import-type-side-effects": "error",
// Too restrictive, writing ugly code to defend against a very unlikely scenario: https://eslint.org/docs/rules/no-prototype-builtins
"no-prototype-builtins": "off",
// https://basarat.gitbooks.io/typescript/docs/tips/defaultIsBad.html
Expand Down
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require("./dist/app/index.js");
module.exports = require("./dist/index.js");
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"repository": "https://github.com/figedi/svc.git",
"author": "Felix Kaminski <[email protected]>",
"license": "MIT",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"scripts": {
"clean": "rm -rf dist && mkdir -p dist",
"build": "npm run clean && tsup",
Expand Down
2 changes: 1 addition & 1 deletion src/app/ApplicationBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from "chai";
import { assert, stub } from "sinon";

import { ApplicationBuilder } from "./ApplicationBuilder";
import { Command, Provider, ErrorHandle, ShutdownHandle } from "./types/app";
import { type Command, type Provider, ErrorHandle, ShutdownHandle } from "./types/app";
import { createStubbedLogger } from "../logger";

describe("ApplicationBuilder", function AppBuilderTest() {
Expand Down
4 changes: 2 additions & 2 deletions src/app/ApplicationBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type {
DynamicObservableTransformFn,
DynamicConfigFnArgs,
} from "./types";
import { type RemoteDependencyArgs, IRemoteSource } from "./remoteConfig";
import type { RemoteDependencyArgs, IRemoteSource } from "./remoteConfig";
import type { DeepMerge } from "./types/base";

import { onExit } from "signal-exit";
Expand All @@ -42,7 +42,7 @@ import {
toConstantCase,
reduceTree,
serviceWithPreflightOrShutdown,
TreeNodeTransformerConfig,
type TreeNodeTransformerConfig,
} from "./utils";
import { ShutdownHandle, ErrorHandle, REF_SYMBOLS, REF_TYPES, isTransformer } from "./types";
import buildOptions from "minimist-options";
Expand Down
8 changes: 4 additions & 4 deletions src/app/remoteConfig/RemoteConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getVersion, ConfigRepository, getRootSchema, SCHEMA_BASE_DIR } from "@figedi/svc-config";
import { getVersion, type ConfigRepository, getRootSchema, SCHEMA_BASE_DIR } from "@figedi/svc-config";
import { SopsClient } from "@figedi/sops";
import { KmsKeyDecryptor, setupStubbedKms } from "@figedi/sops/kms";
import nock from "nock";
Expand All @@ -10,15 +10,15 @@ import type { KeyManagementServiceClient } from "@google-cloud/kms";
import {
createStubbedConfigValues,
createStubbedResponses,
StubbedResponses,
StubbedConfigValues,
type StubbedResponses,
type StubbedConfigValues,
createUpdateStrategyStub,
} from "./shared.specFiles";
import { TestApplicationBuilder } from "../TestApplicationBuilder";
import { PollingRemoteSource } from "./remoteSource/PollingRemoteSource";
import { createStubbedLogger } from "../../logger";
import { ApplicationBuilder } from "../ApplicationBuilder";
import { ReactsOnFn } from "./types";
import type { ReactsOnFn } from "./types";
import { InvalidConfigWithoutDataError, MaxRetriesWithoutDataError } from "./remoteSource";
import { sleep } from "../utils";
import { assertInTestAppBuilder, assertErrorInTestAppBuilder } from "../shared.specFiles/helpers";
Expand Down
4 changes: 2 additions & 2 deletions src/app/remoteConfig/RemoteConfigHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observable, Subscription } from "rxjs";
import { filter, concatMap, pairwise, startWith } from "rxjs/operators";
import { ServiceWithLifecycleHandlers } from "../types/service";
import type { Observable, Subscription } from "rxjs";
import type { ServiceWithLifecycleHandlers } from "../types/service";

export class RemoteConfigHandler<ParentSchema> implements ServiceWithLifecycleHandlers {
private subscription?: Subscription;
Expand Down
9 changes: 5 additions & 4 deletions src/app/remoteConfig/remoteSource/BaseRemoteSource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-classes-per-file */
import type { MeteringRecorder } from "@figedi/metering";
import { createValidator, SchemaValidationError, SchemaValidator, JSONSchema } from "@figedi/typecop";
import { Subject, Observable, lastValueFrom } from "rxjs";
import type { SchemaValidator, JSONSchema } from "@figedi/typecop";
import { Subject, type Observable, lastValueFrom } from "rxjs";
import { take } from "rxjs/operators";
import { parse } from "semver";
import stringify from "fast-json-stable-stringify";
Expand Down Expand Up @@ -48,7 +48,7 @@ export abstract class BaseRemoteSource<TProject, Schema> {
try {
return this.validator!.validate(this.rootSchema!, data);
} catch (e) {
if (e instanceof SchemaValidationError) {
if (e.constructor.name === "SchemaValidationError") {
return false;
}
throw e;
Expand Down Expand Up @@ -217,7 +217,8 @@ export abstract class BaseRemoteSource<TProject, Schema> {
this.initMetrics();
this.trySetRequiredMetrics();
this.stopped = false;
this.validator = createValidator();
const typecopMod = await import("@figedi/typecop");
this.validator = typecopMod.createValidator();

this.rootSchema = await this.validator.compile(this.schema, [this.schemaBaseDir]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/remoteConfig/remoteSource/PollingRemoteSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MeteringRecorder } from "@figedi/metering";
import type { JSONSchema } from "@figedi/typecop";
import axios, { AxiosResponse } from "axios";
import axios, { type AxiosResponse } from "axios";

import { remapTree, sleep } from "../../utils";
import { BaseRemoteSource } from "./BaseRemoteSource";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { set, cloneDeep } from "lodash";
// eslint-disable-next-line import/no-extraneous-dependencies
import { getFallback, getVersion, SecretsConfiguration } from "@figedi/svc-config";
import { getFallback, getVersion, type SecretsConfiguration } from "@figedi/svc-config";
// eslint-disable-next-line import/no-extraneous-dependencies
import { encryptJson } from "@figedi/sops/test";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IReloadingStrategy } from "../types";
import type { IReloadingStrategy } from "../types";

export const createUpdateStrategyStub = (): IReloadingStrategy => ({
execute: async () => {},
Expand Down
2 changes: 1 addition & 1 deletion src/app/remoteConfig/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DynamicOnceTransformFn, DynamicStreamedTransformFn } from "../../types";
import type { DynamicOnceTransformFn, DynamicStreamedTransformFn } from "../../types";

export type RemoteDependencyArgs<RemoteConfig> = {
streamed: DynamicStreamedTransformFn<RemoteConfig>;
Expand Down
4 changes: 2 additions & 2 deletions src/app/shared.specFiles/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { expect } from "chai";
import { TestApplicationBuilder } from "../TestApplicationBuilder";
import { RegisterFnArgs } from "../ApplicationBuilder";
import type { TestApplicationBuilder } from "../TestApplicationBuilder";
import type { RegisterFnArgs } from "../ApplicationBuilder";
import { sleep } from "../utils";

export const assertInTestAppBuilder = async <C>(
Expand Down
2 changes: 1 addition & 1 deletion src/app/utils/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ServiceWithLifecycleHandlers } from "../types";
import type { ServiceWithLifecycleHandlers } from "../types";

export const serviceWithPreflightOrShutdown = (svc: any): svc is ServiceWithLifecycleHandlers =>
"preflight" in svc || "shutdown" in svc;
11 changes: 7 additions & 4 deletions src/k8s/K8sRollingUpdateStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IReloadingStrategy, IReplicaService } from "../app/remoteConfig";
import { ServiceWithLifecycleHandlers } from "../app/types";
import type { IReloadingStrategy, IReplicaService } from "../app/remoteConfig";
import type { ServiceWithLifecycleHandlers } from "../app/types";
import { sleep } from "../app/utils";
import { Logger } from "../logger";
import type { Logger } from "../logger";

/**
* Reloading-strategy to gracefully restart a pod with neighbouring replicas.
Expand All @@ -17,7 +17,10 @@ import { Logger } from "../logger";
export class K8sRollingUpdateStrategy implements IReloadingStrategy, ServiceWithLifecycleHandlers {
private static RESTART_SLEEP_TIME_RANGE_MS = 10000;

constructor(private replicaService: IReplicaService, private logger: Logger) {}
constructor(
private replicaService: IReplicaService,
private logger: Logger,
) {}

public preflight(): void {
if (!this.replicaService || !this.logger) {
Expand Down
2 changes: 1 addition & 1 deletion src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pino, { LoggerOptions as PinoLoggerOptions } from "pino";
import pino, { type LoggerOptions as PinoLoggerOptions } from "pino";

export interface LoggerBaseProperties {
service: string;
Expand Down
3 changes: 1 addition & 2 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts", "src/k8s/index.ts", "src/logger/index.ts", "src/app/index.ts"],
entry: ["src/index.ts", "src/k8s/index.ts", "src/logger/index.ts", "src/remoteConfig/index.ts"],
format: ["cjs", "esm"],
outDir: "dist",
sourcemap: false,
dts: true,
shims: true,
clean: true,
treeshake: true,

});

0 comments on commit 6e9bcd1

Please sign in to comment.