11import { is_what , PlainObject } from "../deps.ts"
22const { isNumber, isPlainObject, isString, isArray, isPrimitive, isBoolean } = is_what
33import { Package , Installation , PackageRequirement } from "../types.ts"
4+ import { provides as cache_provides , available as cache_available , runtime_env as cache_runtime_env , companions as cache_companions , dependencies as cache_dependencies } from "./useSyncCache.ts" ;
45import SemVer , * as semver from "../utils/semver.ts"
56import useMoustaches from "./useMoustaches.ts"
67import { PkgxError } from "../utils/error.ts"
78import { validate } from "../utils/misc.ts"
9+ import * as pkgutils from "../utils/pkg.ts"
810import useConfig from "./useConfig.ts"
911import host from "../utils/host.ts"
1012import Path from "../utils/Path.ts"
@@ -45,6 +47,7 @@ export class PantryNotFoundError extends PantryError {
4547
4648export default function usePantry ( ) {
4749 const prefix = useConfig ( ) . data . join ( "pantry/projects" )
50+ const is_cache_available = cache_available ( ) && pantry_paths ( ) . length == 1
4851
4952 async function * ls ( ) : AsyncGenerator < LsEntry > {
5053 const seen = new Set ( )
@@ -78,11 +81,23 @@ export default function usePantry() {
7881 throw new PackageNotFoundError ( project )
7982 } ) ( )
8083
81- const companions = async ( ) => parse_pkgs_node ( ( await yaml ( ) ) [ "companions" ] )
84+ const companions = async ( ) => {
85+ if ( is_cache_available ) {
86+ return await cache_companions ( project ) ?? parse_pkgs_node ( ( await yaml ( ) ) [ "companions" ] )
87+ } else {
88+ return parse_pkgs_node ( ( await yaml ( ) ) [ "companions" ] )
89+ }
90+ }
8291
8392 const runtime_env = async ( version : SemVer , deps : Installation [ ] ) => {
84- const yml = await yaml ( )
85- const obj = validate . obj ( yml [ "runtime" ] ?. [ "env" ] ?? { } )
93+ const obj = await ( async ( ) => {
94+ if ( is_cache_available ) {
95+ const cached = await cache_runtime_env ( project )
96+ if ( cached ) return cached
97+ }
98+ const yml = await yaml ( )
99+ return validate . obj ( yml [ "runtime" ] ?. [ "env" ] ?? { } )
100+ } ) ( )
86101 return expand_env_obj ( obj , { project, version } , deps )
87102 }
88103
@@ -94,7 +109,13 @@ export default function usePantry() {
94109 return platforms . includes ( host ( ) . platform ) || platforms . includes ( `${ host ( ) . platform } /${ host ( ) . arch } ` )
95110 }
96111
97- const drydeps = async ( ) => parse_pkgs_node ( ( await yaml ( ) ) . dependencies )
112+ const drydeps = async ( ) => {
113+ if ( is_cache_available ) {
114+ return await cache_dependencies ( project ) ?? parse_pkgs_node ( ( await yaml ( ) ) . dependencies )
115+ } else {
116+ return parse_pkgs_node ( ( await yaml ( ) ) . dependencies )
117+ }
118+ }
98119
99120 const provides = async ( ) => {
100121 let node = ( await yaml ( ) ) [ "provides" ]
@@ -164,6 +185,27 @@ export default function usePantry() {
164185 async function find ( name : string ) {
165186 type Foo = ReturnType < typeof project > & LsEntry
166187
188+ //lol FIXME
189+ name = pkgutils . parse ( name ) . project
190+
191+ if ( prefix . join ( name ) . isDirectory ( ) ) {
192+ const foo = project ( name )
193+ return [ { ...foo , project : name } ]
194+ }
195+
196+ /// only use cache if PKGX_PANTRY_PATH is not set
197+ if ( is_cache_available ) {
198+ const cached = await cache_provides ( name )
199+ if ( cached ?. length ) {
200+ return cached . map ( x => ( {
201+ ...project ( x ) ,
202+ project : x
203+ } ) )
204+ }
205+
206+ // else we need to still check for display-names
207+ }
208+
167209 name = name . toLowerCase ( )
168210
169211 //TODO not very performant due to serial awaits
@@ -234,7 +276,8 @@ export default function usePantry() {
234276 parse_pkgs_node,
235277 expand_env_obj,
236278 missing,
237- neglected
279+ neglected,
280+ pantry_paths
238281 }
239282
240283 function pantry_paths ( ) : Path [ ] {
0 commit comments