Skip to content

Commit 3ff92f7

Browse files
committed
Fix uploads
1 parent 5537a11 commit 3ff92f7

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

src/upload/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ export async function upload(manifest: string, key: string): Promise<void> {
1010
const state = {
1111
itemCount: 0,
1212
idsMap: {},
13+
key,
1314
};
1415

1516
// Open the initial manifest.
16-
await readManifest(state, null, null, manifest, key);
17+
await readManifest(state, null, null, manifest);
1718

1819
// Now, we should clean up websockets. If all websockets are closed, we can safely exit
1920
// the program, otherwise we should wait 30 seconds, then force close them and force exit after 5 seconds.
@@ -44,6 +45,10 @@ export interface State {
4445
* A map from friendly names to IDs.
4546
*/
4647
idsMap: Record<string, string>;
48+
/**
49+
* Is the API key to use while uploading.
50+
*/
51+
key: string;
4752
}
4853

4954
/**

src/upload/lesson.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export const websockets: WebSocket[] = [];
2222
* @param spec {string | null} - is the specification of the lesson.
2323
* @param templateDir {string | null} - is the directory that contains template files for this lesson.
2424
* @param dir {string} - is the directory that this manifest is contained in.
25-
* @param key {string} - is the API key used to upload items.
2625
*/
2726
export async function handleLesson(
2827
state: State,
@@ -31,14 +30,13 @@ export async function handleLesson(
3130
spec: string | null,
3231
templateDir: string | null,
3332
dir: string,
34-
key: string,
3533
): Promise<void> {
3634
// We need to first figure out what the ID of this lesson is.
3735
// If it doesn't exist, we'll just set it to null.
3836
const actualID: string | null = await axios
3937
.get("https://cratecode.com/internal/api/id/" + id, {
4038
headers: {
41-
authorization: key,
39+
authorization: state.key,
4240
},
4341
})
4442
.then((res) => res.data.id)
@@ -55,7 +53,7 @@ export async function handleLesson(
5553
? await axios
5654
.get("https://cratecode.com/internal/api/lesson/" + actualID, {
5755
headers: {
58-
authorization: key,
56+
authorization: state.key,
5957
},
6058
})
6159
.then((res) => res.data.project)
@@ -69,7 +67,7 @@ export async function handleLesson(
6967
{},
7068
{
7169
headers: {
72-
authorization: key,
70+
authorization: state.key,
7371
},
7472
},
7573
)
@@ -93,7 +91,7 @@ export async function handleLesson(
9391
},
9492
{
9593
headers: {
96-
authorization: key,
94+
authorization: state.key,
9795
},
9896
},
9997
)
@@ -117,7 +115,7 @@ export async function handleLesson(
117115
{
118116
headers: {
119117
...configForm.getHeaders(),
120-
authorization: key,
118+
authorization: state.key,
121119
},
122120
},
123121
);
@@ -138,7 +136,7 @@ export async function handleLesson(
138136
{
139137
headers: {
140138
...videoForm.getHeaders(),
141-
authorization: key,
139+
authorization: state.key,
142140
},
143141
},
144142
);
@@ -182,7 +180,7 @@ export async function handleLesson(
182180
const token = await axios
183181
.get("https://cratecode.com/internal/api/token/" + project, {
184182
headers: {
185-
authorization: key,
183+
authorization: state.key,
186184
},
187185
})
188186
.then((res) => res.data.token);

src/upload/manifest.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ import { handleLesson } from "./lesson";
1010
* @param parent {string | null} - is the manifest that referenced this manifest.
1111
* @param templates {string | null} - is the path to the templates.
1212
* @param manifest {string} - is the manifest to read.
13-
* @param key {string} - is the API key used to upload items.
1413
*/
1514
export async function readManifest(
1615
state: State,
1716
parent: string | null,
1817
templates: string | null,
1918
manifest: string,
20-
key: string,
2119
): Promise<void> {
2220
try {
2321
const data = JSON.parse(await fs.promises.readFile(manifest, "utf-8"));
@@ -48,7 +46,6 @@ export async function readManifest(
4846
manifest,
4947
templates,
5048
Path.join(newBase, item, "manifest.json"),
51-
key,
5249
);
5350
}
5451
}
@@ -71,7 +68,7 @@ export async function readManifest(
7168
if (typeof lessons !== "object" || Array.isArray(lessons))
7269
throw new Error("lessons must be an object!");
7370

74-
await handleUnit(state, id, name, lessons, key);
71+
await handleUnit(state, id, name, lessons);
7572
break;
7673
}
7774
case "lesson": {
@@ -102,7 +99,6 @@ export async function readManifest(
10299
spec,
103100
extends1 ? Path.join(templates, extends1) : null,
104101
Path.dirname(manifest),
105-
key,
106102
);
107103
break;
108104
}

src/upload/unit.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { delay, State } from "./index";
77
* @param id {string} - is the friendly name of the unit.
88
* @param name {string} - is the display name of the unit.
99
* @param lessons {Record<string, object>} - is the lessons map for the unit.
10-
* @param key {string} - is the API key used to upload items.
1110
*/
1211
export async function handleUnit(
1312
state: State,
@@ -17,14 +16,13 @@ export async function handleUnit(
1716
string,
1817
{ next: string[]; previous: string[]; requireAll?: boolean }
1918
>,
20-
key: string,
2119
): Promise<void> {
2220
// First, we need to figure out what the actual ID of our unit is.
2321
// If there isn't one, we'll just set it to null.
2422
const actualID: string | null = await axios
2523
.get("https://cratecode.com/internal/api/id/" + id, {
2624
headers: {
27-
authorization: key,
25+
authorization: state.key,
2826
},
2927
})
3028
.then((res) => res.data.id)
@@ -41,13 +39,13 @@ export async function handleUnit(
4139
{ next: string[]; previous: string[]; requireAll: boolean }
4240
> = {};
4341

44-
for (const key in lessons) {
42+
for (const lessonID in lessons) {
4543
// Map the key.
46-
const newKey = await mapID(key, state, key);
44+
const newKey = await mapID(lessonID, state);
4745

4846
// Map next and previous.
49-
const next = lessons[key].next ?? [];
50-
const previous = lessons[key].previous ?? [];
47+
const next = lessons[lessonID].next ?? [];
48+
const previous = lessons[lessonID].previous ?? [];
5149

5250
if (typeof next !== "object" || !Array.isArray(next))
5351
throw new Error("next must be a string array!");
@@ -58,17 +56,17 @@ export async function handleUnit(
5856
const newPrevious: string[] = [];
5957

6058
for (const item of next) {
61-
newNext.push(await mapID(item, state, key));
59+
newNext.push(await mapID(item, state));
6260
}
6361

6462
for (const item of previous) {
65-
newPrevious.push(await mapID(item, state, key));
63+
newPrevious.push(await mapID(item, state));
6664
}
6765

6866
map[newKey] = {
6967
next: newNext,
7068
previous: newPrevious,
71-
requireAll: Boolean(lessons[key].requireAll),
69+
requireAll: Boolean(lessons[lessonID].requireAll),
7270
};
7371
}
7472

@@ -83,7 +81,7 @@ export async function handleUnit(
8381
},
8482
{
8583
headers: {
86-
authorization: key,
84+
authorization: state.key,
8785
},
8886
},
8987
);
@@ -92,17 +90,16 @@ export async function handleUnit(
9290

9391
/**
9492
* Maps a friendly name or ID to an ID.
95-
* @param id {string} - is
96-
* @param state
97-
* @param key
93+
* @param id {string} - is the ID or friendly name to lookup.
94+
* @param state {State} - is the application's state.
9895
*/
99-
async function mapID(id: string, state: State, key: string): Promise<string> {
96+
async function mapID(id: string, state: State): Promise<string> {
10097
const newKey =
10198
state.idsMap[id] ||
10299
(await axios
103100
.get("https://cratecode.com/internal/api/id/" + id, {
104101
headers: {
105-
authorization: key,
102+
authorization: state.key,
106103
},
107104
})
108105
.then((res) => {

0 commit comments

Comments
 (0)