From cc6aed8e084b63b7faca5fa0ba8b7c57aaef134d Mon Sep 17 00:00:00 2001 From: Pascal Garber Date: Sun, 3 Mar 2024 11:17:31 +0100 Subject: [PATCH] Generate global ambient module for latest version --- NEWS.md | 3 ++ .../templates/gjs/module-ambient.d.ts | 2 +- packages/lib/src/dependency-manager.ts | 42 +++++++++++++++---- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/NEWS.md b/NEWS.md index 86e3a5476..3521fa0bc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ - Create a Reporter to create a text or json file with warnings like unknown types, renaming, etc and a summary of all that (e.g. 5 type conflicts resolved, 8 unknown types of *gint) - Update types for GJS v1.75.2, see https://gitlab.gnome.org/GNOME/gjs/-/commit/666755b3b09d765e43d415e76105b828517b5509 +# Dev +- Generate global ambient module for latest version, e.g. 'gi://Gtk' or 'gi://GObject' + # 3.2.8 - Upgrade dependencies - Update examples and removed deprecated function calls like `byteArray.toString()` diff --git a/packages/generator-typescript/templates/gjs/module-ambient.d.ts b/packages/generator-typescript/templates/gjs/module-ambient.d.ts index 5d6c59846..2542de729 100644 --- a/packages/generator-typescript/templates/gjs/module-ambient.d.ts +++ b/packages/generator-typescript/templates/gjs/module-ambient.d.ts @@ -24,7 +24,7 @@ declare module 'gi://<%= name %>?version=<%= version %>' { } <%# // Generate ambient module declarations Without version number if there are no conflicts or the target is an NPM package _%> -<%_ if(package || !dep.hasConflict(name, version)){ _%> +<%_ if(package && dep.isLatestVersion(pkg.namespace, pkg.version)){ _%> declare module 'gi://<%= name %>' { <%- moduleImportStr %>; export default <%- girModule.importNamespace -%>; diff --git a/packages/lib/src/dependency-manager.ts b/packages/lib/src/dependency-manager.ts index 12f21927f..73fb418ed 100644 --- a/packages/lib/src/dependency-manager.ts +++ b/packages/lib/src/dependency-manager.ts @@ -52,6 +52,10 @@ export class DependencyManager { return Object.values(this.cache) } + getAllPackageNames(): string[] { + return Object.keys(this.cache) + } + /** * Get the core dependencies * @returns @@ -130,6 +134,19 @@ export class DependencyManager { return dependency } + /** + * Get all dependencies with the given namespace + * @param namespace The namespace of the dependency + * @returns All dependencies with the given namespace + */ + list(namespace: string): Dependency[] { + const packageNames = this.all() + const candidates = packageNames.filter((dep) => { + return dep.namespace === namespace + }) + return candidates + } + /** * Get girModule for dependency * @param girModules @@ -165,10 +182,6 @@ export class DependencyManager { return dependencies } - getAllPackageNames(): string[] { - return Object.keys(this.cache) - } - /** * Check if multiple dependencies with the given namespace exist in the cache * @param namespace The namespace of the dependency @@ -183,6 +196,21 @@ export class DependencyManager { return candidates.length > 1 } + /** + * get the latest version of the dependency with the given namespace + * @param namespace The namespace of the dependency + * @returns The latest version of the dependency + */ + getLatestVersion(namespace: string): Dependency | undefined { + const candidates = this.list(namespace) + const latestVersion = candidates + .sort((a, b) => { + return a.version.localeCompare(b.version) + }) + .pop() + return latestVersion + } + /** * Check if the given version is the latest version of the dependency * @param namespace The namespace of the dependency @@ -190,11 +218,7 @@ export class DependencyManager { * @returns */ isLatestVersion(namespace: string, version: string): boolean { - const hasConflict = this.hasConflict(namespace) - if (!hasConflict) { - return true - } - const latestVersion = this.find(namespace) + const latestVersion = this.getLatestVersion(namespace) return latestVersion?.version === version }