@learnpack/learnpack 2.1.25 → 2.1.26
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +16 -16
- package/bin/run +17 -17
- package/bin/run.cmd +3 -3
- package/lib/commands/audit.d.ts +6 -6
- package/lib/commands/audit.js +327 -302
- package/lib/commands/clean.d.ts +8 -8
- package/lib/commands/clean.js +25 -25
- package/lib/commands/download.d.ts +13 -13
- package/lib/commands/download.js +55 -55
- package/lib/commands/init.d.ts +9 -9
- package/lib/commands/init.js +123 -123
- package/lib/commands/login.d.ts +14 -14
- package/lib/commands/login.js +37 -37
- package/lib/commands/logout.d.ts +14 -14
- package/lib/commands/logout.js +37 -37
- package/lib/commands/publish.d.ts +14 -14
- package/lib/commands/publish.js +82 -82
- package/lib/commands/start.d.ts +7 -7
- package/lib/commands/start.js +165 -165
- package/lib/commands/test.d.ts +6 -6
- package/lib/commands/test.js +62 -62
- package/lib/index.d.ts +1 -1
- package/lib/index.js +4 -4
- package/lib/managers/config/allowed_files.d.ts +5 -5
- package/lib/managers/config/allowed_files.js +30 -30
- package/lib/managers/config/defaults.d.ts +39 -37
- package/lib/managers/config/defaults.js +40 -38
- package/lib/managers/config/exercise.d.ts +36 -36
- package/lib/managers/config/exercise.js +233 -230
- package/lib/managers/config/index.d.ts +3 -3
- package/lib/managers/config/index.js +320 -302
- package/lib/managers/file.d.ts +13 -13
- package/lib/managers/file.js +134 -134
- package/lib/managers/gitpod.d.ts +3 -3
- package/lib/managers/gitpod.js +67 -67
- package/lib/managers/server/index.d.ts +6 -6
- package/lib/managers/server/index.js +58 -51
- package/lib/managers/server/routes.d.ts +4 -4
- package/lib/managers/server/routes.js +167 -167
- package/lib/managers/session.d.ts +3 -3
- package/lib/managers/session.js +104 -104
- package/lib/managers/socket.d.ts +3 -3
- package/lib/managers/socket.js +164 -164
- package/lib/managers/test.js +84 -84
- package/lib/models/action.d.ts +2 -2
- package/lib/models/action.js +2 -2
- package/lib/models/audit.d.ts +15 -15
- package/lib/models/audit.js +2 -2
- package/lib/models/config-manager.d.ts +21 -21
- package/lib/models/config-manager.js +2 -2
- package/lib/models/config.d.ts +62 -60
- package/lib/models/config.js +2 -2
- package/lib/models/counter.d.ts +11 -11
- package/lib/models/counter.js +2 -2
- package/lib/models/errors.d.ts +15 -15
- package/lib/models/errors.js +2 -2
- package/lib/models/exercise-obj.d.ts +27 -27
- package/lib/models/exercise-obj.js +2 -2
- package/lib/models/file.d.ts +5 -5
- package/lib/models/file.js +2 -2
- package/lib/models/findings.d.ts +17 -17
- package/lib/models/findings.js +2 -2
- package/lib/models/flags.d.ts +10 -10
- package/lib/models/flags.js +2 -2
- package/lib/models/front-matter.d.ts +11 -11
- package/lib/models/front-matter.js +2 -2
- package/lib/models/gitpod-data.d.ts +16 -16
- package/lib/models/gitpod-data.js +2 -2
- package/lib/models/language.d.ts +4 -4
- package/lib/models/language.js +2 -2
- package/lib/models/package.d.ts +7 -7
- package/lib/models/package.js +2 -2
- package/lib/models/plugin-config.d.ts +16 -16
- package/lib/models/plugin-config.js +2 -2
- package/lib/models/session.d.ts +23 -23
- package/lib/models/session.js +2 -2
- package/lib/models/socket.d.ts +31 -31
- package/lib/models/socket.js +2 -2
- package/lib/models/status.d.ts +1 -1
- package/lib/models/status.js +2 -2
- package/lib/models/success-types.d.ts +1 -1
- package/lib/models/success-types.js +2 -2
- package/lib/plugin/command/compile.d.ts +6 -6
- package/lib/plugin/command/compile.js +18 -18
- package/lib/plugin/command/test.d.ts +6 -6
- package/lib/plugin/command/test.js +25 -25
- package/lib/plugin/index.d.ts +27 -27
- package/lib/plugin/index.js +7 -7
- package/lib/plugin/plugin.d.ts +8 -8
- package/lib/plugin/plugin.js +68 -68
- package/lib/plugin/utils.d.ts +16 -16
- package/lib/plugin/utils.js +58 -58
- package/lib/ui/download.d.ts +5 -5
- package/lib/ui/download.js +61 -61
- package/lib/utils/BaseCommand.d.ts +8 -8
- package/lib/utils/BaseCommand.js +41 -41
- package/lib/utils/SessionCommand.d.ts +10 -10
- package/lib/utils/SessionCommand.js +47 -47
- package/lib/utils/api.d.ts +12 -12
- package/lib/utils/api.js +173 -173
- package/lib/utils/audit.d.ts +16 -16
- package/lib/utils/audit.js +302 -302
- package/lib/utils/console.d.ts +12 -12
- package/lib/utils/console.js +19 -19
- package/lib/utils/errors.d.ts +17 -17
- package/lib/utils/errors.js +100 -100
- package/lib/utils/exercisesQueue.d.ts +9 -9
- package/lib/utils/exercisesQueue.js +38 -38
- package/lib/utils/fileQueue.d.ts +40 -40
- package/lib/utils/fileQueue.js +168 -168
- package/lib/utils/misc.d.ts +1 -1
- package/lib/utils/misc.js +23 -23
- package/lib/utils/validators.d.ts +5 -5
- package/lib/utils/validators.js +17 -17
- package/lib/utils/watcher.d.ts +2 -2
- package/lib/utils/watcher.js +23 -23
- package/oclif.manifest.json +1 -1
- package/package.json +138 -138
- package/src/commands/audit.ts +25 -0
- package/src/commands/clean.ts +29 -29
- package/src/commands/download.ts +62 -62
- 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 +238 -234
- package/src/commands/test.ts +85 -85
- package/src/index.ts +1 -1
- package/src/managers/config/allowed_files.ts +29 -29
- package/src/managers/config/defaults.ts +2 -0
- package/src/managers/config/exercise.ts +309 -302
- package/src/managers/config/index.ts +22 -1
- package/src/managers/file.ts +169 -169
- package/src/managers/gitpod.ts +84 -84
- package/src/managers/server/index.ts +77 -69
- package/src/managers/session.ts +118 -118
- package/src/managers/socket.ts +239 -239
- package/src/managers/test.ts +83 -83
- package/src/models/action.ts +3 -3
- package/src/models/config-manager.ts +23 -23
- package/src/models/config.ts +2 -0
- package/src/models/counter.ts +11 -11
- package/src/models/errors.ts +22 -22
- package/src/models/file.ts +5 -5
- package/src/models/findings.ts +18 -18
- package/src/models/flags.ts +10 -10
- package/src/models/front-matter.ts +11 -11
- package/src/models/gitpod-data.ts +19 -19
- package/src/models/language.ts +4 -4
- package/src/models/package.ts +7 -7
- package/src/models/plugin-config.ts +17 -17
- package/src/models/session.ts +26 -26
- package/src/models/socket.ts +48 -48
- package/src/models/status.ts +15 -15
- package/src/models/success-types.ts +1 -1
- package/src/plugin/command/compile.ts +17 -17
- package/src/plugin/command/test.ts +30 -30
- package/src/plugin/index.ts +6 -6
- package/src/plugin/plugin.ts +94 -94
- package/src/plugin/utils.ts +87 -87
- package/src/types/node-fetch.d.ts +1 -1
- package/src/ui/download.ts +71 -71
- package/src/utils/BaseCommand.ts +48 -48
- package/src/utils/SessionCommand.ts +48 -48
- package/src/utils/api.ts +194 -194
- package/src/utils/audit.ts +395 -395
- package/src/utils/console.ts +24 -24
- package/src/utils/errors.ts +117 -117
- package/src/utils/exercisesQueue.ts +51 -51
- package/src/utils/fileQueue.ts +198 -198
- package/src/utils/misc.ts +23 -23
- package/src/utils/templates/gitignore.txt +19 -19
- package/src/utils/templates/incremental/.learn/exercises/01-hello-world/README.es.md +24 -24
- package/src/utils/templates/incremental/.learn/exercises/01-hello-world/README.md +24 -24
- package/src/utils/templates/incremental/README.ejs +4 -4
- package/src/utils/templates/incremental/README.es.ejs +4 -4
- package/src/utils/templates/isolated/01-hello-world/README.es.md +26 -26
- package/src/utils/templates/isolated/01-hello-world/README.md +26 -26
- package/src/utils/templates/isolated/README.ejs +4 -4
- package/src/utils/templates/isolated/README.es.ejs +4 -4
- package/src/utils/templates/no-grading/README.ejs +4 -4
- package/src/utils/templates/no-grading/README.es.ejs +4 -4
- package/src/utils/validators.ts +18 -18
@@ -1,48 +1,48 @@
|
|
1
|
-
// import { flags } from "@oclif/command";
|
2
|
-
import BaseCommand from './BaseCommand'
|
3
|
-
import Console from './console'
|
4
|
-
import SessionManager from '../managers/session'
|
5
|
-
import configManager from '../managers/config/index'
|
6
|
-
import {AuthError} from './errors'
|
7
|
-
import {IConfigManager} from '../models/config-manager'
|
8
|
-
|
9
|
-
export default class SessionCommand extends BaseCommand {
|
10
|
-
session: any = null
|
11
|
-
configManager: IConfigManager | null = null
|
12
|
-
static flags: any
|
13
|
-
|
14
|
-
async initSession(flags: any, _private = false) {
|
15
|
-
try {
|
16
|
-
if (!this.configManager) {
|
17
|
-
await this.buildConfig(flags)
|
18
|
-
}
|
19
|
-
|
20
|
-
this.session = await SessionManager.get(this.configManager?.get())
|
21
|
-
if (this.session) {
|
22
|
-
Console.debug(`Session open for ${this.session.payload.email}.`)
|
23
|
-
} else {
|
24
|
-
if (_private)
|
25
|
-
throw AuthError(
|
26
|
-
'You need to log in, run the following command to continue: $ learnpack login',
|
27
|
-
)
|
28
|
-
Console.debug('No active session available', _private)
|
29
|
-
}
|
30
|
-
} catch (error) {
|
31
|
-
Console.error((error as TypeError).message)
|
32
|
-
}
|
33
|
-
}
|
34
|
-
|
35
|
-
async buildConfig(flags: any) {
|
36
|
-
this.configManager = await configManager(flags)
|
37
|
-
}
|
38
|
-
|
39
|
-
async catch(err: any) {
|
40
|
-
Console.debug('COMMAND CATCH', err)
|
41
|
-
throw err
|
42
|
-
}
|
43
|
-
}
|
44
|
-
|
45
|
-
// SessionCommand.description = `Describe the command here
|
46
|
-
// ...
|
47
|
-
// Extra documentation goes here
|
48
|
-
// `
|
1
|
+
// import { flags } from "@oclif/command";
|
2
|
+
import BaseCommand from './BaseCommand'
|
3
|
+
import Console from './console'
|
4
|
+
import SessionManager from '../managers/session'
|
5
|
+
import configManager from '../managers/config/index'
|
6
|
+
import {AuthError} from './errors'
|
7
|
+
import {IConfigManager} from '../models/config-manager'
|
8
|
+
|
9
|
+
export default class SessionCommand extends BaseCommand {
|
10
|
+
session: any = null
|
11
|
+
configManager: IConfigManager | null = null
|
12
|
+
static flags: any
|
13
|
+
|
14
|
+
async initSession(flags: any, _private = false) {
|
15
|
+
try {
|
16
|
+
if (!this.configManager) {
|
17
|
+
await this.buildConfig(flags)
|
18
|
+
}
|
19
|
+
|
20
|
+
this.session = await SessionManager.get(this.configManager?.get())
|
21
|
+
if (this.session) {
|
22
|
+
Console.debug(`Session open for ${this.session.payload.email}.`)
|
23
|
+
} else {
|
24
|
+
if (_private)
|
25
|
+
throw AuthError(
|
26
|
+
'You need to log in, run the following command to continue: $ learnpack login',
|
27
|
+
)
|
28
|
+
Console.debug('No active session available', _private)
|
29
|
+
}
|
30
|
+
} catch (error) {
|
31
|
+
Console.error((error as TypeError).message)
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
async buildConfig(flags: any) {
|
36
|
+
this.configManager = await configManager(flags)
|
37
|
+
}
|
38
|
+
|
39
|
+
async catch(err: any) {
|
40
|
+
Console.debug('COMMAND CATCH', err)
|
41
|
+
throw err
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
// SessionCommand.description = `Describe the command here
|
46
|
+
// ...
|
47
|
+
// Extra documentation goes here
|
48
|
+
// `
|
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 };
|