| | | 1 | | package repometa |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "encoding/json" |
| | | 5 | | "path/filepath" |
| | | 6 | | |
| | | 7 | | "gopkg.in/yaml.v3" |
| | | 8 | | ) |
| | | 9 | | |
| | | 10 | | type jsDetector struct{} |
| | | 11 | | |
| | | 12 | | type packageJSON struct { |
| | | 13 | | Name string `json:"name"` |
| | | 14 | | Workspaces json.RawMessage `json:"workspaces"` |
| | | 15 | | Dependencies map[string]string `json:"dependencies"` |
| | | 16 | | DevDependencies map[string]string `json:"devDependencies"` |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | type pnpmWorkspaceYAML struct { |
| | | 20 | | Packages []string `yaml:"packages"` |
| | | 21 | | } |
| | | 22 | | |
| | 84 | 23 | | func (jsDetector) detect(dv dirVisit, cfg options) []finding { |
| | 74 | 24 | | if !hasFile(dv.files, "package.json") { |
| | 74 | 25 | | return nil |
| | 74 | 26 | | } |
| | | 27 | | |
| | 10 | 28 | | attrs := map[string]string{} |
| | 10 | 29 | | evidence := []Evidence{{Path: relJoin(dv.rel, "package.json"), Reason: "package.json at directory root"}} |
| | 10 | 30 | | var workspaces []Workspace |
| | 10 | 31 | | confidence := 1.0 |
| | 10 | 32 | | |
| | 10 | 33 | | data, err := readManifest(filepath.Join(dv.abs, "package.json"), cfg) |
| | 10 | 34 | | switch { |
| | 1 | 35 | | case err != nil: |
| | 1 | 36 | | confidence = confidenceUnreadable |
| | 1 | 37 | | evidence = append(evidence, evidenceUnreadable(dv.rel, "package.json", err)) |
| | 9 | 38 | | default: |
| | 9 | 39 | | var pkg packageJSON |
| | 1 | 40 | | if uerr := json.Unmarshal(data, &pkg); uerr != nil { |
| | 1 | 41 | | confidence = confidenceUnparsable |
| | 1 | 42 | | evidence = append(evidence, evidenceUnparsable(dv.rel, "package.json", uerr)) |
| | 1 | 43 | | break |
| | | 44 | | } |
| | 2 | 45 | | if fw := detectJSFramework(pkg); fw != "" { |
| | 2 | 46 | | attrs["js.framework"] = fw |
| | 2 | 47 | | } |
| | 1 | 48 | | if members, ok := parseNpmYarnWorkspaces(pkg.Workspaces); ok { |
| | 1 | 49 | | expanded := expandMembers(dv.abs, members, dv.rel) |
| | 1 | 50 | | workspaces = append(workspaces, Workspace{Kind: WorkspaceNpmYarn, Members: expanded}) |
| | 1 | 51 | | evidence = append(evidence, Evidence{ |
| | 1 | 52 | | Path: relJoin(dv.rel, "package.json"), Reason: `package.json "workspaces" declared`, |
| | 1 | 53 | | }) |
| | 1 | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | 1 | 57 | | if hasFile(dv.files, "pnpm-workspace.yaml") { |
| | 1 | 58 | | members := parsePnpmWorkspace(filepath.Join(dv.abs, "pnpm-workspace.yaml"), cfg) |
| | 1 | 59 | | expanded := expandMembers(dv.abs, members, dv.rel) |
| | 1 | 60 | | workspaces = append(workspaces, Workspace{Kind: WorkspacePnpm, Members: expanded}) |
| | 1 | 61 | | evidence = append(evidence, Evidence{Path: relJoin(dv.rel, "pnpm-workspace.yaml"), Reason: "pnpm-workspace.yaml pres |
| | 1 | 62 | | } |
| | 1 | 63 | | if hasFile(dv.files, "nx.json") { |
| | 1 | 64 | | workspaces = append(workspaces, Workspace{Kind: WorkspaceNx}) |
| | 1 | 65 | | evidence = append(evidence, Evidence{Path: relJoin(dv.rel, "nx.json"), Reason: "nx.json present"}) |
| | 1 | 66 | | } |
| | 1 | 67 | | if hasFile(dv.files, "turbo.json") { |
| | 1 | 68 | | workspaces = append(workspaces, Workspace{Kind: WorkspaceTurborepo}) |
| | 1 | 69 | | evidence = append(evidence, Evidence{Path: relJoin(dv.rel, "turbo.json"), Reason: "turbo.json present"}) |
| | 1 | 70 | | } |
| | | 71 | | |
| | | 72 | | // Angular's canonical marker is angular.json, which may exist even |
| | | 73 | | // without @angular/core being an obvious dependency line. |
| | 1 | 74 | | if hasFile(dv.files, "angular.json") { |
| | 1 | 75 | | attrs["js.framework"] = "angular" |
| | 1 | 76 | | evidence = append(evidence, Evidence{Path: relJoin(dv.rel, "angular.json"), Reason: "angular.json present"}) |
| | 1 | 77 | | } |
| | | 78 | | |
| | 10 | 79 | | return []finding{{ |
| | 10 | 80 | | Kind: KindNodePackage, |
| | 10 | 81 | | Confidence: confidence, |
| | 10 | 82 | | Evidence: evidence, |
| | 10 | 83 | | Workspaces: workspaces, |
| | 10 | 84 | | Attributes: attrs, |
| | 10 | 85 | | }} |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | func detectJSFramework(pkg packageJSON) string { |
| | | 89 | | if _, ok := pkg.Dependencies["next"]; ok { |
| | | 90 | | return "nextjs" |
| | | 91 | | } |
| | | 92 | | if _, ok := pkg.DevDependencies["next"]; ok { |
| | | 93 | | return "nextjs" |
| | | 94 | | } |
| | | 95 | | if _, ok := pkg.Dependencies["@angular/core"]; ok { |
| | | 96 | | return "angular" |
| | | 97 | | } |
| | | 98 | | if _, ok := pkg.DevDependencies["@angular/core"]; ok { |
| | | 99 | | return "angular" |
| | | 100 | | } |
| | | 101 | | return "" |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | // parseNpmYarnWorkspaces handles both shapes: |
| | | 105 | | // |
| | | 106 | | // "workspaces": ["packages/*"] |
| | | 107 | | // "workspaces": {"packages": ["packages/*"], "nohoist": [...]} |
| | | 108 | | func parseNpmYarnWorkspaces(raw json.RawMessage) ([]string, bool) { |
| | | 109 | | if len(raw) == 0 { |
| | | 110 | | return nil, false |
| | | 111 | | } |
| | | 112 | | var arr []string |
| | | 113 | | if err := json.Unmarshal(raw, &arr); err == nil { |
| | | 114 | | return arr, true |
| | | 115 | | } |
| | | 116 | | var obj struct { |
| | | 117 | | Packages []string `json:"packages"` |
| | | 118 | | } |
| | | 119 | | if err := json.Unmarshal(raw, &obj); err == nil { |
| | | 120 | | return obj.Packages, true |
| | | 121 | | } |
| | | 122 | | return nil, false |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | func parsePnpmWorkspace(path string, cfg options) []string { |
| | | 126 | | data := readManifestOrNil(path, cfg) |
| | | 127 | | if data == nil { |
| | | 128 | | return nil |
| | | 129 | | } |
| | | 130 | | var ws pnpmWorkspaceYAML |
| | | 131 | | if err := yaml.Unmarshal(data, &ws); err != nil { |
| | | 132 | | return nil |
| | | 133 | | } |
| | | 134 | | return ws.Packages |
| | | 135 | | } |