@learnpack/learnpack 2.0.2 → 2.0.5
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +33 -38
- package/bin/run +1 -1
- package/oclif.manifest.json +1 -0
- package/package.json +4 -2
- package/src/commands/clean.ts +29 -29
- package/src/commands/download.ts +62 -62
- package/src/commands/init.ts +172 -172
- package/src/commands/login.ts +42 -42
- package/src/commands/logout.ts +43 -43
- package/src/commands/publish.ts +107 -107
- package/src/commands/start.ts +234 -234
- package/src/commands/test.ts +85 -85
- package/src/managers/config/exercise.ts +302 -302
- package/src/managers/config/index.ts +412 -412
- package/src/managers/file.ts +169 -169
- package/src/managers/server/index.ts +69 -69
- package/src/managers/server/routes.ts +255 -255
- package/src/managers/test.ts +83 -83
- package/src/plugin/plugin.ts +94 -94
- package/src/plugin/utils.ts +87 -87
- package/src/utils/BaseCommand.ts +48 -48
- package/src/utils/api.ts +194 -194
- package/src/utils/fileQueue.ts +198 -198
package/src/utils/api.ts
CHANGED
@@ -1,194 +1,194 @@
|
|
1
|
-
import Console from "../utils/console";
|
2
|
-
import * as storage from "node-persist";
|
3
|
-
import cli from "cli-ux";
|
4
|
-
const HOST = "https://learnpack.herokuapp.com";
|
5
|
-
|
6
|
-
// eslint-disable-next-line
|
7
|
-
const _fetch = require("node-fetch");
|
8
|
-
|
9
|
-
interface IHeaders {
|
10
|
-
"Content-Type"?: string;
|
11
|
-
Authorization?: string;
|
12
|
-
}
|
13
|
-
|
14
|
-
interface IOptions {
|
15
|
-
headers?: IHeaders;
|
16
|
-
method?: string;
|
17
|
-
body?: string;
|
18
|
-
}
|
19
|
-
|
20
|
-
const fetch = async (url: string, options: IOptions = {}) => {
|
21
|
-
const headers: IHeaders = { "Content-Type": "application/json" };
|
22
|
-
let session = null;
|
23
|
-
try {
|
24
|
-
session = await storage.getItem("bc-payload");
|
25
|
-
if (session.token && session.token !== "" && !url.includes("/token"))
|
26
|
-
headers.Authorization = "Token " + session.token;
|
27
|
-
} catch {}
|
28
|
-
|
29
|
-
try {
|
30
|
-
const resp = await _fetch(url, {
|
31
|
-
...options,
|
32
|
-
headers: { ...headers, ...options.headers },
|
33
|
-
} as any);
|
34
|
-
|
35
|
-
if (resp.status >= 200 && resp.status < 300)
|
36
|
-
return await resp.json();
|
37
|
-
if (resp.status === 401)
|
38
|
-
throw APIError("Invalid authentication credentials", 401);
|
39
|
-
else if (resp.status === 404)
|
40
|
-
throw APIError("Package not found", 404);
|
41
|
-
else if (resp.status >= 500)
|
42
|
-
throw APIError("Impossible to connect with the server", 500);
|
43
|
-
else if (resp.status >= 400) {
|
44
|
-
const error = await resp.json();
|
45
|
-
if (error.detail || error.error) {
|
46
|
-
throw APIError(error.detail || error.error);
|
47
|
-
} else if (error.nonFieldErrors) {
|
48
|
-
throw APIError(error.nonFieldErrors[0], error);
|
49
|
-
} else if (typeof error === "object") {
|
50
|
-
if (Object.keys(error).length > 0) {
|
51
|
-
const key = error[Object.keys(error)[0]];
|
52
|
-
throw APIError(`${key}: ${error[key][0]}`, error);
|
53
|
-
}
|
54
|
-
} else {
|
55
|
-
throw APIError("Uknown error");
|
56
|
-
}
|
57
|
-
} else
|
58
|
-
throw APIError("Uknown error");
|
59
|
-
} catch (error) {
|
60
|
-
Console.error((error as TypeError).message);
|
61
|
-
throw error;
|
62
|
-
}
|
63
|
-
};
|
64
|
-
|
65
|
-
const login = async (identification: string, password: string) => {
|
66
|
-
try {
|
67
|
-
cli.action.start("Looking for credentials...");
|
68
|
-
await cli.wait(1000);
|
69
|
-
const data = await fetch(`${HOST}/v1/auth/token/`, {
|
70
|
-
body: JSON.stringify({ identification, password }),
|
71
|
-
method: "post",
|
72
|
-
});
|
73
|
-
cli.action.stop("ready");
|
74
|
-
return data;
|
75
|
-
} catch (error) {
|
76
|
-
Console.error((error as TypeError).message);
|
77
|
-
Console.debug(error);
|
78
|
-
}
|
79
|
-
};
|
80
|
-
|
81
|
-
const publish = async (config: any) => {
|
82
|
-
const keys = [
|
83
|
-
"difficulty",
|
84
|
-
"language",
|
85
|
-
"skills",
|
86
|
-
"technologies",
|
87
|
-
"slug",
|
88
|
-
"repository",
|
89
|
-
"author",
|
90
|
-
"title",
|
91
|
-
];
|
92
|
-
|
93
|
-
const payload: { [key: string]: string } = {};
|
94
|
-
for (const k of keys)
|
95
|
-
config[k] ? (payload[k] = config[k]) : null;
|
96
|
-
try {
|
97
|
-
console.log("Package to publish:", payload);
|
98
|
-
cli.action.start("Updating package information...");
|
99
|
-
await cli.wait(1000);
|
100
|
-
const data = await fetch(`${HOST}/v1/package/${config.slug}`, {
|
101
|
-
method: "PUT",
|
102
|
-
body: JSON.stringify(payload),
|
103
|
-
});
|
104
|
-
cli.action.stop("ready");
|
105
|
-
return data;
|
106
|
-
} catch (error) {
|
107
|
-
console.log("payload", payload);
|
108
|
-
Console.error((error as TypeError).message);
|
109
|
-
Console.debug(error);
|
110
|
-
throw error;
|
111
|
-
}
|
112
|
-
};
|
113
|
-
|
114
|
-
const update = async (config: any) => {
|
115
|
-
try {
|
116
|
-
cli.action.start("Updating package information...");
|
117
|
-
await cli.wait(1000);
|
118
|
-
const data = await fetch(`${HOST}/v1/package/`, {
|
119
|
-
method: "POST",
|
120
|
-
body: JSON.stringify(config),
|
121
|
-
});
|
122
|
-
cli.action.stop("ready");
|
123
|
-
return data;
|
124
|
-
} catch (error) {
|
125
|
-
Console.error((error as any).message);
|
126
|
-
Console.debug(error);
|
127
|
-
throw error;
|
128
|
-
}
|
129
|
-
};
|
130
|
-
|
131
|
-
const getPackage = async (slug: string) => {
|
132
|
-
try {
|
133
|
-
cli.action.start("Downloading package information...");
|
134
|
-
await cli.wait(1000);
|
135
|
-
const data = await fetch(`${HOST}/v1/package/${slug}`);
|
136
|
-
cli.action.stop("ready");
|
137
|
-
return data;
|
138
|
-
} catch (error) {
|
139
|
-
if ((error as any).status === 404)
|
140
|
-
Console.error(`Package ${slug} does not exist`);
|
141
|
-
else
|
142
|
-
Console.error(`Package ${slug} does not exist`);
|
143
|
-
Console.debug(error);
|
144
|
-
throw error;
|
145
|
-
}
|
146
|
-
};
|
147
|
-
|
148
|
-
const getLangs = async () => {
|
149
|
-
try {
|
150
|
-
cli.action.start("Downloading language options...");
|
151
|
-
await cli.wait(1000);
|
152
|
-
const data = await fetch(`${HOST}/v1/package/language`);
|
153
|
-
cli.action.stop("ready");
|
154
|
-
return data;
|
155
|
-
} catch (error) {
|
156
|
-
if ((error as any).status === 404)
|
157
|
-
Console.error("Package slug does not exist");
|
158
|
-
else
|
159
|
-
Console.error("Package slug does not exist");
|
160
|
-
Console.debug(error);
|
161
|
-
throw error;
|
162
|
-
}
|
163
|
-
};
|
164
|
-
|
165
|
-
const getAllPackages = async ({
|
166
|
-
lang = "",
|
167
|
-
slug = "",
|
168
|
-
}: {
|
169
|
-
lang?: string;
|
170
|
-
slug?: string;
|
171
|
-
}) => {
|
172
|
-
try {
|
173
|
-
cli.action.start("Downloading packages...");
|
174
|
-
await cli.wait(1000);
|
175
|
-
const data = await fetch(
|
176
|
-
`${HOST}/v1/package/all?limit=100&language=${lang}&slug=${slug}`
|
177
|
-
);
|
178
|
-
cli.action.stop("ready");
|
179
|
-
return data;
|
180
|
-
} catch (error) {
|
181
|
-
Console.error(`Package ${slug} does not exist`);
|
182
|
-
Console.debug(error);
|
183
|
-
throw error;
|
184
|
-
}
|
185
|
-
};
|
186
|
-
|
187
|
-
const APIError = (error: TypeError | string, code?: number) => {
|
188
|
-
const message: string = (error as TypeError).message || (error as string);
|
189
|
-
const _err = new Error(message) as any;
|
190
|
-
_err.status = code || 400;
|
191
|
-
return _err;
|
192
|
-
};
|
193
|
-
|
194
|
-
export default { login, publish, update, getPackage, getLangs, getAllPackages };
|
1
|
+
import Console from "../utils/console";
|
2
|
+
import * as storage from "node-persist";
|
3
|
+
import cli from "cli-ux";
|
4
|
+
const HOST = "https://learnpack.herokuapp.com";
|
5
|
+
|
6
|
+
// eslint-disable-next-line
|
7
|
+
const _fetch = require("node-fetch");
|
8
|
+
|
9
|
+
interface IHeaders {
|
10
|
+
"Content-Type"?: string;
|
11
|
+
Authorization?: string;
|
12
|
+
}
|
13
|
+
|
14
|
+
interface IOptions {
|
15
|
+
headers?: IHeaders;
|
16
|
+
method?: string;
|
17
|
+
body?: string;
|
18
|
+
}
|
19
|
+
|
20
|
+
const fetch = async (url: string, options: IOptions = {}) => {
|
21
|
+
const headers: IHeaders = { "Content-Type": "application/json" };
|
22
|
+
let session = null;
|
23
|
+
try {
|
24
|
+
session = await storage.getItem("bc-payload");
|
25
|
+
if (session.token && session.token !== "" && !url.includes("/token"))
|
26
|
+
headers.Authorization = "Token " + session.token;
|
27
|
+
} catch {}
|
28
|
+
|
29
|
+
try {
|
30
|
+
const resp = await _fetch(url, {
|
31
|
+
...options,
|
32
|
+
headers: { ...headers, ...options.headers },
|
33
|
+
} as any);
|
34
|
+
|
35
|
+
if (resp.status >= 200 && resp.status < 300)
|
36
|
+
return await resp.json();
|
37
|
+
if (resp.status === 401)
|
38
|
+
throw APIError("Invalid authentication credentials", 401);
|
39
|
+
else if (resp.status === 404)
|
40
|
+
throw APIError("Package not found", 404);
|
41
|
+
else if (resp.status >= 500)
|
42
|
+
throw APIError("Impossible to connect with the server", 500);
|
43
|
+
else if (resp.status >= 400) {
|
44
|
+
const error = await resp.json();
|
45
|
+
if (error.detail || error.error) {
|
46
|
+
throw APIError(error.detail || error.error);
|
47
|
+
} else if (error.nonFieldErrors) {
|
48
|
+
throw APIError(error.nonFieldErrors[0], error);
|
49
|
+
} else if (typeof error === "object") {
|
50
|
+
if (Object.keys(error).length > 0) {
|
51
|
+
const key = error[Object.keys(error)[0]];
|
52
|
+
throw APIError(`${key}: ${error[key][0]}`, error);
|
53
|
+
}
|
54
|
+
} else {
|
55
|
+
throw APIError("Uknown error");
|
56
|
+
}
|
57
|
+
} else
|
58
|
+
throw APIError("Uknown error");
|
59
|
+
} catch (error) {
|
60
|
+
Console.error((error as TypeError).message);
|
61
|
+
throw error;
|
62
|
+
}
|
63
|
+
};
|
64
|
+
|
65
|
+
const login = async (identification: string, password: string) => {
|
66
|
+
try {
|
67
|
+
cli.action.start("Looking for credentials...");
|
68
|
+
await cli.wait(1000);
|
69
|
+
const data = await fetch(`${HOST}/v1/auth/token/`, {
|
70
|
+
body: JSON.stringify({ identification, password }),
|
71
|
+
method: "post",
|
72
|
+
});
|
73
|
+
cli.action.stop("ready");
|
74
|
+
return data;
|
75
|
+
} catch (error) {
|
76
|
+
Console.error((error as TypeError).message);
|
77
|
+
Console.debug(error);
|
78
|
+
}
|
79
|
+
};
|
80
|
+
|
81
|
+
const publish = async (config: any) => {
|
82
|
+
const keys = [
|
83
|
+
"difficulty",
|
84
|
+
"language",
|
85
|
+
"skills",
|
86
|
+
"technologies",
|
87
|
+
"slug",
|
88
|
+
"repository",
|
89
|
+
"author",
|
90
|
+
"title",
|
91
|
+
];
|
92
|
+
|
93
|
+
const payload: { [key: string]: string } = {};
|
94
|
+
for (const k of keys)
|
95
|
+
config[k] ? (payload[k] = config[k]) : null;
|
96
|
+
try {
|
97
|
+
console.log("Package to publish:", payload);
|
98
|
+
cli.action.start("Updating package information...");
|
99
|
+
await cli.wait(1000);
|
100
|
+
const data = await fetch(`${HOST}/v1/package/${config.slug}`, {
|
101
|
+
method: "PUT",
|
102
|
+
body: JSON.stringify(payload),
|
103
|
+
});
|
104
|
+
cli.action.stop("ready");
|
105
|
+
return data;
|
106
|
+
} catch (error) {
|
107
|
+
console.log("payload", payload);
|
108
|
+
Console.error((error as TypeError).message);
|
109
|
+
Console.debug(error);
|
110
|
+
throw error;
|
111
|
+
}
|
112
|
+
};
|
113
|
+
|
114
|
+
const update = async (config: any) => {
|
115
|
+
try {
|
116
|
+
cli.action.start("Updating package information...");
|
117
|
+
await cli.wait(1000);
|
118
|
+
const data = await fetch(`${HOST}/v1/package/`, {
|
119
|
+
method: "POST",
|
120
|
+
body: JSON.stringify(config),
|
121
|
+
});
|
122
|
+
cli.action.stop("ready");
|
123
|
+
return data;
|
124
|
+
} catch (error) {
|
125
|
+
Console.error((error as any).message);
|
126
|
+
Console.debug(error);
|
127
|
+
throw error;
|
128
|
+
}
|
129
|
+
};
|
130
|
+
|
131
|
+
const getPackage = async (slug: string) => {
|
132
|
+
try {
|
133
|
+
cli.action.start("Downloading package information...");
|
134
|
+
await cli.wait(1000);
|
135
|
+
const data = await fetch(`${HOST}/v1/package/${slug}`);
|
136
|
+
cli.action.stop("ready");
|
137
|
+
return data;
|
138
|
+
} catch (error) {
|
139
|
+
if ((error as any).status === 404)
|
140
|
+
Console.error(`Package ${slug} does not exist`);
|
141
|
+
else
|
142
|
+
Console.error(`Package ${slug} does not exist`);
|
143
|
+
Console.debug(error);
|
144
|
+
throw error;
|
145
|
+
}
|
146
|
+
};
|
147
|
+
|
148
|
+
const getLangs = async () => {
|
149
|
+
try {
|
150
|
+
cli.action.start("Downloading language options...");
|
151
|
+
await cli.wait(1000);
|
152
|
+
const data = await fetch(`${HOST}/v1/package/language`);
|
153
|
+
cli.action.stop("ready");
|
154
|
+
return data;
|
155
|
+
} catch (error) {
|
156
|
+
if ((error as any).status === 404)
|
157
|
+
Console.error("Package slug does not exist");
|
158
|
+
else
|
159
|
+
Console.error("Package slug does not exist");
|
160
|
+
Console.debug(error);
|
161
|
+
throw error;
|
162
|
+
}
|
163
|
+
};
|
164
|
+
|
165
|
+
const getAllPackages = async ({
|
166
|
+
lang = "",
|
167
|
+
slug = "",
|
168
|
+
}: {
|
169
|
+
lang?: string;
|
170
|
+
slug?: string;
|
171
|
+
}) => {
|
172
|
+
try {
|
173
|
+
cli.action.start("Downloading packages...");
|
174
|
+
await cli.wait(1000);
|
175
|
+
const data = await fetch(
|
176
|
+
`${HOST}/v1/package/all?limit=100&language=${lang}&slug=${slug}`
|
177
|
+
);
|
178
|
+
cli.action.stop("ready");
|
179
|
+
return data;
|
180
|
+
} catch (error) {
|
181
|
+
Console.error(`Package ${slug} does not exist`);
|
182
|
+
Console.debug(error);
|
183
|
+
throw error;
|
184
|
+
}
|
185
|
+
};
|
186
|
+
|
187
|
+
const APIError = (error: TypeError | string, code?: number) => {
|
188
|
+
const message: string = (error as TypeError).message || (error as string);
|
189
|
+
const _err = new Error(message) as any;
|
190
|
+
_err.status = code || 400;
|
191
|
+
return _err;
|
192
|
+
};
|
193
|
+
|
194
|
+
export default { login, publish, update, getPackage, getLangs, getAllPackages };
|