Skip to content

Commit

Permalink
added suggested changes to use await for resolving promises
Browse files Browse the repository at this point in the history
  • Loading branch information
chirag.gandhi authored and mxcl committed Nov 17, 2022
1 parent aec1fe9 commit 8cb70fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/hooks/usePantry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ async function resolve(spec: Package | PackageRequirement): Promise<Package> {
}

//TODO take `T` and then type check it
const getYAML = (pkg: Package | PackageRequirement): { path: Path, parse: () => Promise<PlainObject>} => {
const foo = entry(pkg)
const getYAML = async (pkg: Package | PackageRequirement): Promise<{ path: Path, parse: () => Promise<PlainObject>}> => {
const foo = await entry(pkg)
return {
path: foo.dir.join("package.yml"),
parse: foo.yml
Expand All @@ -49,7 +49,7 @@ const getYAML = (pkg: Package | PackageRequirement): { path: Path, parse: () =>

/// returns ONE LEVEL of deps, to recurse use `hydrate.ts`
const getDeps = async (pkg: Package | PackageRequirement) => {
const yml = await entry(pkg).yml()
const yml = await (await entry(pkg)).yml()
return {
runtime: parse_pkgs_node(yml.dependencies),
build: parse_pkgs_node(yml.build?.dependencies),
Expand Down Expand Up @@ -84,7 +84,7 @@ const getRawDistributableURL = (yml: PlainObject) => {
const getDistributable = async (pkg: Package) => {
const moustaches = useMoustaches()

const yml = await entry(pkg).yml()
const yml = await (await entry(pkg)).yml()
let urlstr = getRawDistributableURL(yml)
if (!urlstr) return
let stripComponents: number | undefined
Expand All @@ -103,7 +103,7 @@ const getDistributable = async (pkg: Package) => {
}

const getScript = async (pkg: Package, key: 'build' | 'test', deps: Installation[]) => {
const yml = await entry(pkg).yml()
const yml = await (await entry(pkg)).yml()
const node = yml[key]

const mm = useMoustaches()
Expand Down Expand Up @@ -138,7 +138,7 @@ const getScript = async (pkg: Package, key: 'build' | 'test', deps: Installation
}

const getProvides = async (pkg: { project: string }) => {
const yml = await entry(pkg).yml()
const yml = await (await entry(pkg)).yml()
const node = yml["provides"]
if (!node) return []
if (!isArray(node)) throw new Error("bad-yaml")
Expand All @@ -154,7 +154,7 @@ const getProvides = async (pkg: { project: string }) => {
}

const getCompanions = async (pkg: {project: string}) => {
const yml = await entry(pkg).yml()
const yml = await (await entry(pkg)).yml()
const node = yml["companions"]
return parse_pkgs_node(node)
}
Expand All @@ -164,7 +164,7 @@ function coerceNumber(input: any) {
if (isNumber(input)) return input
}

function entry({ project }: { project: string }): Entry {
async function entry({ project }: { project: string }): Promise<Entry> {
for (const prefix of pantry_paths()) {
const dir = prefix.join(project)
const filename = dir.join("package.yml")
Expand All @@ -181,25 +181,20 @@ function entry({ project }: { project: string }): Entry {
const versions = dir.join("versions.txt")
return { dir, yml, versions }
}
//check for fuzzy string
const closestPkg = getClosestPackageSuggestion(project, prefix);
closestPkg.then(function(data){
console.log("did you mean", {data}, "?")
});
throw new TeaError('not-found: pantry: package.yml', {project}, )


const closestPkg = await getClosestPackageSuggestion(project);
throw new TeaError('not-found: pantry: package.yml', {project, closestPkg}, )
}
async function getClosestPackageSuggestion(orgPkg:string, projectPath:Path): Promise<string>{
async function getClosestPackageSuggestion(orgPkg:string): Promise<string>{
let closestPkg = '';
let minDistance = Infinity;
const pkgList = [];
for await (const data of projectPath.ls()){
const {name} = data[1];
pkgList.push(name);
for await (const {project} of ls()){
pkgList.push(project);
}
pkgList.forEach(pkgName=>{
const number = levenshteinDistance(pkgName, orgPkg);
if(number<minDistance){
if (number<minDistance){
minDistance = number;
closestPkg = pkgName;
}
Expand Down Expand Up @@ -229,7 +224,7 @@ function levenshteinDistance (str1: string, str2:string):number{
};
/// returns sorted versions
async function getVersions(spec: Package | PackageRequirement): Promise<SemVer[]> {
const files = entry(spec)
const files = await entry(spec)
const versions = await files.yml().then(x => x.versions)

if (isArray(versions)) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default class TeaError extends Error {
your time to shine? we’ll see you on GitHub…
https://github.com/teaxyz/pantry.extra#how-to-contribute
`
break
case 'parser: pantry: package.yml':
Expand Down

0 comments on commit 8cb70fb

Please sign in to comment.