Skip to content

Commit

Permalink
ambient: Only define general definition for latest versions
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Mar 2, 2024
1 parent 2a50209 commit 95bd723
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ 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 (dep.isLatestVersion(pkg.namespace, pkg.version)) { _%>
declare module 'gi://<%= name %>' {
<%- moduleImportStr %>;
export default <%- girModule.importNamespace -%>;
}
}
<%_ } _%>
42 changes: 33 additions & 9 deletions packages/lib/src/dependency-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export class DependencyManager {
return Object.values(this.cache)
}

getAllPackageNames(): string[] {
return Object.keys(this.cache)
}

/**
* Get the core dependencies
* @returns
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -183,18 +196,29 @@ 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
* @param version The version of the dependency
* @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
}

Expand Down
2 changes: 1 addition & 1 deletion types
Submodule types updated 422 files

0 comments on commit 95bd723

Please sign in to comment.