forked from solidjs-community/solid-primitives
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-modules-data.ts
More file actions
101 lines (85 loc) · 2.87 KB
/
get-modules-data.ts
File metadata and controls
101 lines (85 loc) · 2.87 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import path from "node:path";
import fs from "node:fs";
import fsp from "node:fs/promises";
import { MODULE_PREFIX, PACKAGES_DIR, isNonNullable, logLine } from "./utils.js";
import * as vb from "valibot";
const pkg_schema = vb.object({
name: vb.string(),
version: vb.string(),
description: vb.string(),
keywords: vb.optional(vb.array(vb.string())),
dependencies: vb.optional(vb.record(vb.string())),
peerDependencies: vb.record(vb.string()),
primitive: vb.object(
{
list: vb.array(vb.string()),
category: vb.string(),
stage: vb.number(),
},
'package.json lacks "primitive" filed',
),
});
export type ModulePkgSchema = typeof pkg_schema;
export type ModulePkg = vb.Output<ModulePkgSchema>;
export function getPackagePkg(name: string): ModulePkg | Error {
const pkg_path = path.join(PACKAGES_DIR, name, "package.json");
if (!fs.existsSync(pkg_path)) {
return new Error(`package "${name}" doesn't have package.json`);
}
const pkg = JSON.parse(fs.readFileSync(pkg_path, "utf8")) as unknown;
const result = vb.safeParse(pkg_schema, pkg);
if (!result.success) {
const issue = result.issues[0];
return new Error(`package "${name}" has invalid package.json: ${issue.message}`);
}
return result.output;
}
export type ModuleData = {
name: string;
version: string;
description: string;
tags: string[];
category: string;
stage: number;
primitives: string[];
workspace_deps: string[];
peer_deps: string[];
};
export async function getModulesData(): Promise<ModuleData[]>;
export async function getModulesData<T>(mapFn: (data: ModuleData) => T | Promise<T>): Promise<T[]>;
export async function getModulesData<T = ModuleData>(
mapFn: (data: ModuleData) => T = moduleData => moduleData as unknown as T,
): Promise<T[]> {
const module_names = await fsp.readdir(PACKAGES_DIR);
const promises = module_names.map(async name => {
const pkg = getPackagePkg(name);
if (pkg instanceof Error) {
logLine(pkg.message);
return null;
}
const dependencies = Object.keys(pkg.dependencies ?? {});
const peer_deps = Object.keys(pkg.peerDependencies);
const workspace_deps: string[] = [];
for (const dep of dependencies) {
if (dep.startsWith(MODULE_PREFIX)) {
const dep_name = dep.slice(MODULE_PREFIX.length);
workspace_deps.push(dep_name);
}
}
const excludedKeywords = ["primitive", "solid", pkg.name];
return {
data: await mapFn({
name,
version: pkg.version,
description: pkg.description,
tags: pkg.keywords?.filter(keyword => !excludedKeywords.includes(keyword)) ?? [],
category: pkg.primitive.category,
stage: pkg.primitive.stage,
primitives: pkg.primitive.list,
workspace_deps,
peer_deps,
}),
};
});
return (await Promise.all(promises)).filter(isNonNullable).map(a => a.data);
}