aboutsummaryrefslogtreecommitdiff
path: root/routes/analyze.tsx
blob: 0889986936ad7cf566a359351c9a2b3089913de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Handlers, PageProps } from "$fresh/server.ts";
import ListPackageDetails from "../islands/ListPackageDetails.tsx";
import { handler as fetchPackages  } from "./api/npm.ts";

export const handler: Handlers<any> = {
  async POST(req, ctx) {
    const form = await req.formData();
    const fileData = form.get("packagefile") as File;
    const packagejson = await parsePackageJson(fileData);
    const packages = {...packagejson.dependencies, ...packagejson.devDependencies};
    const {name, version, description} = packagejson
    return ctx.render({packages, name, version, description});
  },
};

const parsePackageJson = (fileData: File) => {
  return new Promise<any>((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsText(fileData);
    reader.addEventListener("load", (event: any) => {
      resolve(JSON.parse(reader.result as string));
    });
  });
};

export default function Analyze({ data }: PageProps<any>) {
    const {packages, name, version, description} = data;
  return (
    <div>
      Analyzer
      <ListPackageDetails 
        packages={packages} 
        projectname={name} 
        projectversion={version}
        projectdesc={description}
      ></ListPackageDetails>
    </div>
  );
}