-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (107 loc) · 2.65 KB
/
index.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import rawData from "./dist.js";
import structuredClone from "@ungap/structured-clone";
const { projects: rawProjects, vendors: rawVendors, licenses } = rawData;
function addRaw(obj) {
obj.raw = structuredClone(obj);
}
function denormalizeProject(project) {
addRaw(project);
project.projects = {};
project.parts = {};
for (const subproject of project.projectList) {
denormalizeProject(subproject);
project.projects[subproject.id] = subproject;
}
for (const part of project.partList) {
denormalizePart(part);
project.projects[part.partNumber] = part;
}
projectList.push(project);
partList.push(...project.partList);
}
function resolveVendor(id) {
if (vendors[id]) {
return vendors[id];
} else {
throw new Error(`Invalid vendor ${id}`);
}
}
function resolvePart(id) {
if (parts[id]) {
return parts[id];
} else {
throw new Error(`Invalid part number ${id}`);
}
}
function denormalizePart(part) {
addRaw(part);
for (const file of part.files ?? []) {
denormalizeFile(file);
}
if (part.vendor) {
part.vendorID = part.vendor;
part.vendor = resolveVendor(part.vendorID);
}
}
const partReferences = [["manufacturing", "material"]];
function denormalizePartPass2(part) {
for (const referencePath of partReferences) {
let parent = null;
let current = part;
for (const portion of referencePath) {
parent = current;
current = current[portion];
if (!current) {
break;
}
}
if (current) {
parent[referencePath[referencePath.length - 1]] = resolvePart(current);
}
}
}
function denormalizeFile(file) {
if (file.license) {
Object.defineProperty(file, "licenseText", {
get() {
let licenseText = "LICENSES\n";
for (const license of file.licenses) {
const text = licenses[license];
if (text) {
licenseText += `---
${license}
${text}`;
} else {
licenseText += `---
Couldn't find license text for ${license}
`;
}
}
return licenseText;
},
});
}
}
export const projectList = [];
export const partList = [];
export const vendorList = rawVendors;
export const vendors = {};
export const projects = {};
export const topLevelProjects = {};
export const parts = {};
for (const vendor of vendorList) {
vendors[vendor.id] = vendor;
}
for (const project of rawProjects) {
denormalizeProject(project);
}
for (const project of projectList) {
projects[project.id] = project;
}
for (const project of rawProjects) {
topLevelProjects[project.id] = project;
}
for (const part of partList) {
parts[part.partNumber] = part;
denormalizePartPass2(part);
}