From 4a8f3c47c0b6e7b2e4ccef82fc1b3bc7a5cf0935 Mon Sep 17 00:00:00 2001 From: Myrotvorets Date: Fri, 13 Oct 2023 09:30:38 +0300 Subject: [PATCH] Fix code --- README.md | 1 - src/controllers/lifecycle.mts | 3 +-- src/index-dev.mts | 6 +++--- src/lib/bugsnag.mts | 6 +++--- src/lib/environment.mts | 2 +- src/lib/tracing.mts | 4 ---- src/lib/wpapi.mts | 20 +++++++++++--------- test/migrate.mts | 2 +- test/migrations/20200527153110_posts.mts | 2 +- test/unit/lib/environment.test.mts | 2 +- test/unit/lib/wpapi.test.mts | 14 +++++++------- tsconfig.json | 4 ++++ 12 files changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index ff666a00..1b7b256a 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,6 @@ CHAT_ID="" NEWS_ENDPOINT=https://myrotvorets.news DEBUG="bot:*" DEBUG_DEPTH="5" -ENABLE_TRACING="1" OTEL_TRACES_EXPORTER=zipkin OTEL_EXPORTER_ZIPKIN_ENDPOINT=http://zipkin:9411/api/v2/spans ``` diff --git a/src/controllers/lifecycle.mts b/src/controllers/lifecycle.mts index 93ab3bd1..6b38dbc6 100644 --- a/src/controllers/lifecycle.mts +++ b/src/controllers/lifecycle.mts @@ -16,8 +16,7 @@ const dbg = debug('bot:debug'); async function getNewPosts(baseUrl: string, db: Knex): Promise { const result: PostData[] = []; const posts = await getPosts(baseUrl); - for (let i = posts.length - 1; i >= 0; --i) { - const post = posts[i]; + for (const post of posts) { if (!(await checkPostExists(db, post.id))) { post.img = post.featuredMedia ? await getFeaturedImageUrl(baseUrl, post.featuredMedia) : undefined; result.push(post); diff --git a/src/index-dev.mts b/src/index-dev.mts index 3e4fb86e..c3309480 100644 --- a/src/index-dev.mts +++ b/src/index-dev.mts @@ -2,11 +2,11 @@ import './lib/tracing.mjs'; import localtunnel from 'localtunnel'; -if (process.env.WEBHOOK_PORT) { - const port = +process.env.WEBHOOK_PORT || 3001; +if (process.env['WEBHOOK_PORT']) { + const port = +process.env['WEBHOOK_PORT'] || 3001; const tunnel = await localtunnel({ port }); const url = new URL(tunnel.url); - process.env.WEBHOOK_DOMAIN = url.hostname; + process.env['WEBHOOK_DOMAIN'] = url.hostname; // eslint-disable-next-line no-console console.info(`Webhook URL: ${tunnel.url}`); diff --git a/src/lib/bugsnag.mts b/src/lib/bugsnag.mts index 70e3647e..1c1c42ad 100644 --- a/src/lib/bugsnag.mts +++ b/src/lib/bugsnag.mts @@ -15,15 +15,15 @@ interface PackageJson { * However, we leave it in case in future we may need to filter out some data etc. */ function onError(): boolean { - return process.env.NODE_ENV === 'production'; + return process.env['NODE_ENV'] === 'production'; } export async function startBugsnag(env: Environment): Promise { let version: string | undefined; try { - if (process.env.npm_package_version) { - version = process.env.npm_package_version; + if (process.env['npm_package_version']) { + version = process.env['npm_package_version']; } else { const filename = await findFile('package.json'); const json = JSON.parse(await readFile(filename, 'utf-8')) as PackageJson; diff --git a/src/lib/environment.mts b/src/lib/environment.mts index 8b5566f6..b1b86046 100644 --- a/src/lib/environment.mts +++ b/src/lib/environment.mts @@ -31,7 +31,7 @@ export function environment(reset = false): Environment { PATH_PREFIX: str({ default: '' }), }); - process.env.NODE_ENV = env.NODE_ENV; + process.env['NODE_ENV'] = env.NODE_ENV; } return env; diff --git a/src/lib/tracing.mts b/src/lib/tracing.mts index a64232bb..675f3fb0 100644 --- a/src/lib/tracing.mts +++ b/src/lib/tracing.mts @@ -2,10 +2,6 @@ import { OpenTelemetryConfigurator } from '@myrotvorets/opentelemetry-configurat import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; import { KnexInstrumentation } from '@opentelemetry/instrumentation-knex'; -if (!+(process.env.ENABLE_TRACING || 0)) { - process.env.OTEL_SDK_DISABLED = 'true'; -} - export const configurator = new OpenTelemetryConfigurator({ serviceName: 'bot/myrotvorets.news', instrumentations: [new HttpInstrumentation(), new KnexInstrumentation()], diff --git a/src/lib/wpapi.mts b/src/lib/wpapi.mts index e1b9c36f..2fe52863 100644 --- a/src/lib/wpapi.mts +++ b/src/lib/wpapi.mts @@ -26,15 +26,17 @@ async function get(url: string): Promise { export async function getPosts(baseUrl: string): Promise { const posts = await get(`${baseUrl}/wp-json/wp/v2/posts`); - return posts.map( - (post): PostData => ({ - id: post.id, - link: post.link, - title: post.title.rendered, - excerpt: post.excerpt.rendered.replace(/<[^>]+(>|$)/gu, ''), - featuredMedia: post.featured_media, - }), - ); + return posts + .map( + (post): PostData => ({ + id: post.id, + link: post.link, + title: post.title.rendered, + excerpt: post.excerpt.rendered.replace(/<[^>]+(>|$)/gu, ''), + featuredMedia: post.featured_media, + }), + ) + .reverse(); } export async function getFeaturedImageUrl(baseUrl: string, id: number): Promise { diff --git a/test/migrate.mts b/test/migrate.mts index ee71a0d2..8bc56b9c 100644 --- a/test/migrate.mts +++ b/test/migrate.mts @@ -19,7 +19,7 @@ try { process.stdout.write('Creating tables\n'); await db.migrate.latest(); - if (process.env.SEED_TABLES === 'yes') { + if (process.env['SEED_TABLES'] === 'yes') { process.stdout.write('Populating tables\n'); await db.seed.run(); } diff --git a/test/migrations/20200527153110_posts.mts b/test/migrations/20200527153110_posts.mts index 79f479c9..734efa4c 100644 --- a/test/migrations/20200527153110_posts.mts +++ b/test/migrations/20200527153110_posts.mts @@ -7,7 +7,7 @@ export function up(db: Knex): Promise { } export function down(knex: Knex): Promise { - if (process.env.NODE_ENV === 'production') { + if (process.env['NODE_ENV'] === 'production') { throw new Error('This is not meant to be run in the production environment'); } diff --git a/test/unit/lib/environment.test.mts b/test/unit/lib/environment.test.mts index 80856f38..f06cb8e1 100644 --- a/test/unit/lib/environment.test.mts +++ b/test/unit/lib/environment.test.mts @@ -62,7 +62,7 @@ describe('environment', function () { let actual = { ...environment(true) }; expect(actual).to.deep.equal(expected); - process.env.NODE_ENV = 'borked'; + process.env['NODE_ENV'] = 'borked'; actual = { ...environment() }; expect(actual).to.deep.equal(expected); diff --git a/test/unit/lib/wpapi.test.mts b/test/unit/lib/wpapi.test.mts index 32c9ca95..d97e8525 100644 --- a/test/unit/lib/wpapi.test.mts +++ b/test/unit/lib/wpapi.test.mts @@ -25,13 +25,6 @@ describe('wpapi', function () { when(fetchMock(matchers.isA(String) as string)).thenResolve(new Response(JSON.stringify(getPostsResponse))); const expected: PostData[] = [ - { - id: 58808, - link: 'https://myrotvorets.news/post-1/', - title: 'Post #1', - excerpt: 'Excerpt #1', - featuredMedia: 58817, - }, { id: 58803, link: 'https://myrotvorets.news/post-2/', @@ -39,6 +32,13 @@ describe('wpapi', function () { excerpt: 'Excerpt #2', featuredMedia: 58805, }, + { + id: 58808, + link: 'https://myrotvorets.news/post-1/', + title: 'Post #1', + excerpt: 'Excerpt #1', + featuredMedia: 58817, + }, ]; return expect(getPosts('https://example.test')).to.become(expected); diff --git a/tsconfig.json b/tsconfig.json index 545017bf..860cca03 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,6 +14,10 @@ "noUnusedLocals": true, "noUnusedParameters": false, "noFallthroughCasesInSwitch": true, + "noPropertyAccessFromIndexSignature": true, + "noUncheckedIndexedAccess": true, + "noImplicitOverride": true, + "noImplicitReturns": true, "esModuleInterop": true, "resolveJsonModule": true },