From 5d0d125e4398b579f8e122c9cd194127ae71ddf5 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sat, 14 Dec 2024 00:30:35 +0100 Subject: [PATCH] feat(core): Refactor TypeScript interfaces and improve module exports --- changelog.md | 7 +++++ package.json | 6 ++-- ts/00_commitinfo_data.ts | 2 +- ts/codefeed.plugins.ts | 7 +++++ ts/index.ts | 66 +++++++--------------------------------- ts/interfaces/index.ts | 44 +++++++++++++++++++++++++++ tsconfig.json | 3 +- 7 files changed, 76 insertions(+), 59 deletions(-) create mode 100644 ts/interfaces/index.ts diff --git a/changelog.md b/changelog.md index 5fe2943..e12c8f0 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2024-12-14 - 1.5.0 - feat(core) +Refactor TypeScript interfaces and improve module exports + +- Moved TypeScript interfaces to a dedicated file (ts/interfaces/index.ts). +- Updated import/export structure to improve code readability and maintainability. +- Enhanced the package.json to utilize a module exports field for better resolution. + ## 2024-12-13 - 1.4.1 - fix(core) Corrected log formatting for commit information output in CodeFeed diff --git a/package.json b/package.json index 5ea1899..e50c301 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,10 @@ "version": "1.4.1", "private": false, "description": "a module for creating feeds for code development", - "main": "dist_ts/index.js", - "typings": "dist_ts/index.d.ts", + "exports": { + ".": "./dist_ts/index.js", + "./interfaces": "./dist_ts/interfaces/index.js" + }, "type": "module", "author": "Task Venture Capital GmbH", "license": "MIT", diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index fda5cea..3ac2fdb 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@foss.global/codefeed', - version: '1.4.1', + version: '1.5.0', description: 'a module for creating feeds for code development' } diff --git a/ts/codefeed.plugins.ts b/ts/codefeed.plugins.ts index 0ad4b25..200e3ac 100644 --- a/ts/codefeed.plugins.ts +++ b/ts/codefeed.plugins.ts @@ -1,3 +1,10 @@ +// module +import * as interfaces from './interfaces/index.js'; + +export { + interfaces, +} + // @push.rocks import * as qenv from '@push.rocks/qenv'; import * as smartnpm from '@push.rocks/smartnpm'; diff --git a/ts/index.ts b/ts/index.ts index 61c3e13..13080c8 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,49 +1,5 @@ import * as plugins from './codefeed.plugins.js'; -interface RepositoryOwner { - login: string; -} - -interface Repository { - owner: RepositoryOwner; - name: string; -} - -interface CommitAuthor { - date: string; -} - -interface CommitDetail { - message: string; - author: CommitAuthor; -} - -interface Commit { - sha: string; - commit: CommitDetail; -} - -interface Tag { - commit?: { - sha?: string; - }; -} - -interface RepoSearchResponse { - data: Repository[]; -} - -export interface CommitResult { - baseUrl: string; - org: string; - repo: string; - timestamp: string; - hash: string; - commitMessage: string; - tagged: boolean; - publishedOnNpm: boolean; - prettyAgoTime: string; -} export class CodeFeed { private baseUrl: string; @@ -123,9 +79,9 @@ export class CodeFeed { /** * Fetch all repositories accessible to the token/user. */ - private async fetchAllRepositories(): Promise { + private async fetchAllRepositories(): Promise { let page = 1; - const allRepos: Repository[] = []; + const allRepos: plugins.interfaces.Repository[] = []; while (true) { const url = new URL(`${this.baseUrl}/api/v1/repos/search`); @@ -140,7 +96,7 @@ export class CodeFeed { throw new Error(`Failed to fetch repositories: ${resp.statusText}`); } - const data: RepoSearchResponse = await resp.json(); + const data: plugins.interfaces.RepoSearchResponse = await resp.json(); allRepos.push(...data.data); if (data.data.length < 50) { @@ -157,7 +113,7 @@ export class CodeFeed { */ private async fetchTags(owner: string, repo: string): Promise> { let page = 1; - const tags: Tag[] = []; + const tags: plugins.interfaces.Tag[] = []; while (true) { const url = new URL(`${this.baseUrl}/api/v1/repos/${owner}/${repo}/tags`); @@ -173,7 +129,7 @@ export class CodeFeed { throw new Error(`Failed to fetch tags for ${owner}/${repo}: ${resp.statusText}`); } - const data: Tag[] = await resp.json(); + const data: plugins.interfaces.Tag[] = await resp.json(); tags.push(...data); if (data.length < 50) { @@ -195,10 +151,10 @@ export class CodeFeed { /** * Fetch commits from the last 24 hours for a repository. */ - private async fetchRecentCommitsForRepo(owner: string, repo: string): Promise { + private async fetchRecentCommitsForRepo(owner: string, repo: string): Promise { const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000); let page = 1; - const recentCommits: Commit[] = []; + const recentCommits: plugins.interfaces.Commit[] = []; while (true) { const url = new URL(`${this.baseUrl}/api/v1/repos/${owner}/${repo}/commits`); @@ -214,7 +170,7 @@ export class CodeFeed { throw new Error(`Failed to fetch commits for ${owner}/${repo}: ${resp.statusText}`); } - const data: Commit[] = await resp.json(); + const data: plugins.interfaces.Commit[] = await resp.json(); if (data.length === 0) { break; } @@ -237,10 +193,10 @@ export class CodeFeed { /** * Fetch all commits by querying all organizations. */ - public async fetchAllCommitsFromInstance(): Promise { + public async fetchAllCommitsFromInstance(): Promise { const orgs = await this.fetchAllOrganizations(); console.log(`Found ${orgs.length} organizations`); - let allCommits: CommitResult[] = []; + let allCommits: plugins.interfaces.CommitResult[] = []; for (const orgName of orgs) { console.log(`Checking activity for organization: ${orgName}`); @@ -284,7 +240,7 @@ export class CodeFeed { const commits = await this.fetchRecentCommitsForRepo(org, repo); const commitResults = commits.map((c) => { - const commit: CommitResult = { + const commit: plugins.interfaces.CommitResult = { baseUrl: this.baseUrl, org, repo, diff --git a/ts/interfaces/index.ts b/ts/interfaces/index.ts new file mode 100644 index 0000000..ff7b6ee --- /dev/null +++ b/ts/interfaces/index.ts @@ -0,0 +1,44 @@ +export interface RepositoryOwner { + login: string; +} + +export interface Repository { + owner: RepositoryOwner; + name: string; +} + +export interface CommitAuthor { + date: string; +} + +export interface CommitDetail { + message: string; + author: CommitAuthor; +} + +export interface Commit { + sha: string; + commit: CommitDetail; +} + +export interface Tag { + commit?: { + sha?: string; + }; +} + +export interface RepoSearchResponse { + data: Repository[]; +} + +export interface CommitResult { + baseUrl: string; + org: string; + repo: string; + timestamp: string; + hash: string; + commitMessage: string; + tagged: boolean; + publishedOnNpm: boolean; + prettyAgoTime: string; +} diff --git a/tsconfig.json b/tsconfig.json index 2413b93..572af45 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,8 @@ "esModuleInterop": true, "verbatimModuleSyntax": true, "baseUrl": ".", - "paths": {} + "paths": { + } }, "exclude": [ "dist_*/**/*.d.ts"