@learnpack/learnpack 2.1.25 → 2.1.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -16
- package/bin/run +17 -17
- package/bin/run.cmd +3 -3
- package/oclif.manifest.json +1 -1
- package/package.json +139 -138
- package/src/commands/audit.ts +134 -109
- package/src/commands/clean.ts +29 -29
- package/src/commands/download.ts +62 -62
- package/src/commands/init.ts +39 -39
- 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 +264 -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 +4 -2
- package/src/managers/config/exercise.ts +309 -302
- package/src/managers/config/index.ts +159 -138
- package/src/managers/file.ts +178 -169
- package/src/managers/gitpod.ts +84 -84
- package/src/managers/server/index.ts +78 -69
- package/src/managers/server/routes.ts +139 -90
- package/src/managers/session.ts +147 -118
- package/src/managers/socket.ts +252 -239
- package/src/managers/test.ts +83 -83
- package/src/models/action.ts +10 -3
- package/src/models/config-manager.ts +23 -23
- package/src/models/config.ts +9 -2
- package/src/models/counter.ts +11 -11
- package/src/models/errors.ts +22 -22
- package/src/models/exercise-obj.ts +6 -3
- 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 +29 -26
- package/src/models/socket.ts +54 -48
- package/src/models/status.ts +16 -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 +246 -194
- package/src/utils/audit.ts +392 -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
- package/src/utils/watcher.ts +14 -14
- package/lib/commands/audit.d.ts +0 -6
- package/lib/commands/audit.js +0 -317
- package/lib/commands/clean.d.ts +0 -8
- package/lib/commands/clean.js +0 -25
- package/lib/commands/download.d.ts +0 -13
- package/lib/commands/download.js +0 -55
- package/lib/commands/init.d.ts +0 -9
- package/lib/commands/init.js +0 -123
- package/lib/commands/login.d.ts +0 -14
- package/lib/commands/login.js +0 -37
- package/lib/commands/logout.d.ts +0 -14
- package/lib/commands/logout.js +0 -37
- package/lib/commands/publish.d.ts +0 -14
- package/lib/commands/publish.js +0 -82
- package/lib/commands/start.d.ts +0 -7
- package/lib/commands/start.js +0 -165
- package/lib/commands/test.d.ts +0 -6
- package/lib/commands/test.js +0 -62
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -4
- package/lib/managers/config/allowed_files.d.ts +0 -5
- package/lib/managers/config/allowed_files.js +0 -30
- package/lib/managers/config/defaults.d.ts +0 -37
- package/lib/managers/config/defaults.js +0 -38
- package/lib/managers/config/exercise.d.ts +0 -36
- package/lib/managers/config/exercise.js +0 -230
- package/lib/managers/config/index.d.ts +0 -3
- package/lib/managers/config/index.js +0 -302
- package/lib/managers/file.d.ts +0 -13
- package/lib/managers/file.js +0 -134
- package/lib/managers/gitpod.d.ts +0 -3
- package/lib/managers/gitpod.js +0 -67
- package/lib/managers/server/index.d.ts +0 -6
- package/lib/managers/server/index.js +0 -51
- package/lib/managers/server/routes.d.ts +0 -4
- package/lib/managers/server/routes.js +0 -167
- package/lib/managers/session.d.ts +0 -3
- package/lib/managers/session.js +0 -104
- package/lib/managers/socket.d.ts +0 -3
- package/lib/managers/socket.js +0 -164
- package/lib/managers/test.d.ts +0 -0
- package/lib/managers/test.js +0 -84
- package/lib/models/action.d.ts +0 -2
- package/lib/models/action.js +0 -2
- package/lib/models/audit.d.ts +0 -15
- package/lib/models/audit.js +0 -2
- package/lib/models/config-manager.d.ts +0 -21
- package/lib/models/config-manager.js +0 -2
- package/lib/models/config.d.ts +0 -60
- package/lib/models/config.js +0 -2
- package/lib/models/counter.d.ts +0 -11
- package/lib/models/counter.js +0 -2
- package/lib/models/errors.d.ts +0 -15
- package/lib/models/errors.js +0 -2
- package/lib/models/exercise-obj.d.ts +0 -27
- package/lib/models/exercise-obj.js +0 -2
- package/lib/models/file.d.ts +0 -5
- package/lib/models/file.js +0 -2
- package/lib/models/findings.d.ts +0 -17
- package/lib/models/findings.js +0 -2
- package/lib/models/flags.d.ts +0 -10
- package/lib/models/flags.js +0 -2
- package/lib/models/front-matter.d.ts +0 -11
- package/lib/models/front-matter.js +0 -2
- package/lib/models/gitpod-data.d.ts +0 -16
- package/lib/models/gitpod-data.js +0 -2
- package/lib/models/language.d.ts +0 -4
- package/lib/models/language.js +0 -2
- package/lib/models/package.d.ts +0 -7
- package/lib/models/package.js +0 -2
- package/lib/models/plugin-config.d.ts +0 -16
- package/lib/models/plugin-config.js +0 -2
- package/lib/models/session.d.ts +0 -23
- package/lib/models/session.js +0 -2
- package/lib/models/socket.d.ts +0 -31
- package/lib/models/socket.js +0 -2
- package/lib/models/status.d.ts +0 -1
- package/lib/models/status.js +0 -2
- package/lib/models/success-types.d.ts +0 -1
- package/lib/models/success-types.js +0 -2
- package/lib/plugin/command/compile.d.ts +0 -6
- package/lib/plugin/command/compile.js +0 -18
- package/lib/plugin/command/test.d.ts +0 -6
- package/lib/plugin/command/test.js +0 -25
- package/lib/plugin/index.d.ts +0 -27
- package/lib/plugin/index.js +0 -7
- package/lib/plugin/plugin.d.ts +0 -8
- package/lib/plugin/plugin.js +0 -68
- package/lib/plugin/utils.d.ts +0 -16
- package/lib/plugin/utils.js +0 -58
- package/lib/ui/download.d.ts +0 -5
- package/lib/ui/download.js +0 -61
- package/lib/utils/BaseCommand.d.ts +0 -8
- package/lib/utils/BaseCommand.js +0 -41
- package/lib/utils/SessionCommand.d.ts +0 -10
- package/lib/utils/SessionCommand.js +0 -47
- package/lib/utils/api.d.ts +0 -12
- package/lib/utils/api.js +0 -173
- package/lib/utils/audit.d.ts +0 -16
- package/lib/utils/audit.js +0 -302
- package/lib/utils/console.d.ts +0 -12
- package/lib/utils/console.js +0 -19
- package/lib/utils/errors.d.ts +0 -17
- package/lib/utils/errors.js +0 -100
- package/lib/utils/exercisesQueue.d.ts +0 -9
- package/lib/utils/exercisesQueue.js +0 -38
- package/lib/utils/fileQueue.d.ts +0 -40
- package/lib/utils/fileQueue.js +0 -168
- package/lib/utils/misc.d.ts +0 -1
- package/lib/utils/misc.js +0 -23
- package/lib/utils/validators.d.ts +0 -5
- package/lib/utils/validators.js +0 -17
- package/lib/utils/watcher.d.ts +0 -2
- package/lib/utils/watcher.js +0 -23
package/src/utils/BaseCommand.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import { Command } from "@oclif/command"
|
|
2
|
-
import Console from "./console"
|
|
3
|
-
import { createInterface } from "readline"
|
|
4
|
-
// import SessionManager from '../managers/session'
|
|
5
|
-
|
|
6
|
-
class BaseCommand extends Command {
|
|
7
|
-
async catch(err: any) {
|
|
8
|
-
Console.debug("COMMAND CATCH", err)
|
|
9
|
-
|
|
10
|
-
throw err
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
async init() {
|
|
14
|
-
const { flags, args } = this.parse(BaseCommand)
|
|
15
|
-
Console.debug("COMMAND INIT")
|
|
16
|
-
Console.debug("These are your flags: ", flags)
|
|
17
|
-
Console.debug("These are your args: ", args)
|
|
18
|
-
|
|
19
|
-
// quick fix for listening to the process termination on windows
|
|
20
|
-
if (process.platform === "win32") {
|
|
21
|
-
const rl = createInterface({
|
|
22
|
-
input: process.stdin,
|
|
23
|
-
output: process.stdout,
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
rl.on("SIGINT", function () {
|
|
27
|
-
// process.emit('SIGINT')
|
|
28
|
-
// process.emit('SIGINT')
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
process.on("SIGINT", function () {
|
|
33
|
-
Console.debug("Terminated (SIGINT)")
|
|
34
|
-
process.exit()
|
|
35
|
-
})
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async finally() {
|
|
39
|
-
Console.debug("COMMAND FINALLY")
|
|
40
|
-
// called after run and catch regardless of whether or not the command errored
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async run() {
|
|
44
|
-
// console.log('running my command')
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export default BaseCommand
|
|
1
|
+
import { Command } from "@oclif/command"
|
|
2
|
+
import Console from "./console"
|
|
3
|
+
import { createInterface } from "readline"
|
|
4
|
+
// import SessionManager from '../managers/session'
|
|
5
|
+
|
|
6
|
+
class BaseCommand extends Command {
|
|
7
|
+
async catch(err: any) {
|
|
8
|
+
Console.debug("COMMAND CATCH", err)
|
|
9
|
+
|
|
10
|
+
throw err
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async init() {
|
|
14
|
+
const { flags, args } = this.parse(BaseCommand)
|
|
15
|
+
Console.debug("COMMAND INIT")
|
|
16
|
+
Console.debug("These are your flags: ", flags)
|
|
17
|
+
Console.debug("These are your args: ", args)
|
|
18
|
+
|
|
19
|
+
// quick fix for listening to the process termination on windows
|
|
20
|
+
if (process.platform === "win32") {
|
|
21
|
+
const rl = createInterface({
|
|
22
|
+
input: process.stdin,
|
|
23
|
+
output: process.stdout,
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
rl.on("SIGINT", function () {
|
|
27
|
+
// process.emit('SIGINT')
|
|
28
|
+
// process.emit('SIGINT')
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
process.on("SIGINT", function () {
|
|
33
|
+
Console.debug("Terminated (SIGINT)")
|
|
34
|
+
process.exit()
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async finally() {
|
|
39
|
+
Console.debug("COMMAND FINALLY")
|
|
40
|
+
// called after run and catch regardless of whether or not the command errored
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async run() {
|
|
44
|
+
// console.log('running my command')
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export default BaseCommand
|
|
@@ -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,246 @@
|
|
|
1
|
-
import Console from "../utils/console"
|
|
2
|
-
import * as storage from "node-persist"
|
|
3
|
-
import cli from "cli-ux"
|
|
4
|
-
const HOST = "https://
|
|
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
|
-
if (resp.status
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
throw APIError("
|
|
41
|
-
else if (resp.status
|
|
42
|
-
|
|
43
|
-
else if (resp.status >=
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
throw APIError(error.
|
|
49
|
-
} else if (
|
|
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
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
"
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
)
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
1
|
+
import Console from "../utils/console"
|
|
2
|
+
import * as storage from "node-persist"
|
|
3
|
+
import cli from "cli-ux"
|
|
4
|
+
const HOST = "https://breathecode.herokuapp.com"
|
|
5
|
+
const RIGOBOT_HOST = "https://rigobot.herokuapp.com"
|
|
6
|
+
|
|
7
|
+
// eslint-disable-next-line
|
|
8
|
+
const _fetch = require("node-fetch");
|
|
9
|
+
|
|
10
|
+
interface IHeaders {
|
|
11
|
+
"Content-Type"?: string;
|
|
12
|
+
Authorization?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface IOptions {
|
|
16
|
+
headers?: IHeaders;
|
|
17
|
+
method?: string;
|
|
18
|
+
body?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const fetch = async (url: string, options: IOptions = {}) => {
|
|
22
|
+
const headers: IHeaders = { "Content-Type": "application/json" }
|
|
23
|
+
Console.log(`Fetching ${url}`)
|
|
24
|
+
let session = null
|
|
25
|
+
try {
|
|
26
|
+
session = await storage.getItem("bc-payload")
|
|
27
|
+
if (session.token && session.token !== "" && !url.includes("/token"))
|
|
28
|
+
headers.Authorization = "Token " + session.token
|
|
29
|
+
} catch {}
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const resp = await _fetch(url, {
|
|
33
|
+
...options,
|
|
34
|
+
headers: { ...headers, ...options.headers },
|
|
35
|
+
} as any)
|
|
36
|
+
|
|
37
|
+
if (resp.status >= 200 && resp.status < 300)
|
|
38
|
+
return await resp.json()
|
|
39
|
+
if (resp.status === 401)
|
|
40
|
+
throw APIError("Invalid authentication credentials", 401)
|
|
41
|
+
else if (resp.status === 404)
|
|
42
|
+
throw APIError("Package not found", 404)
|
|
43
|
+
else if (resp.status >= 500)
|
|
44
|
+
throw APIError("Impossible to connect with the server", 500)
|
|
45
|
+
else if (resp.status >= 400) {
|
|
46
|
+
const error = await resp.json()
|
|
47
|
+
if (error.detail || error.error) {
|
|
48
|
+
throw APIError(error.detail || error.error)
|
|
49
|
+
} else if (error.nonFieldErrors) {
|
|
50
|
+
throw APIError(error.nonFieldErrors[0], error)
|
|
51
|
+
} else if (typeof error === "object") {
|
|
52
|
+
if (Object.keys(error).length > 0) {
|
|
53
|
+
const key = error[Object.keys(error)[0]]
|
|
54
|
+
throw APIError(`${key}: ${error[key][0]}`, error)
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
throw APIError("Uknown error")
|
|
58
|
+
}
|
|
59
|
+
} else
|
|
60
|
+
throw APIError("Uknown error")
|
|
61
|
+
} catch (error) {
|
|
62
|
+
Console.error((error as TypeError).message)
|
|
63
|
+
throw error
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const login = async (identification: string, password: string) => {
|
|
68
|
+
try {
|
|
69
|
+
cli.action.start(`Looking for credentials with ${identification}`)
|
|
70
|
+
await cli.wait(1000)
|
|
71
|
+
const url = `${HOST}/v1/auth/login/`
|
|
72
|
+
// Console.log(url);
|
|
73
|
+
const data = await fetch(url, {
|
|
74
|
+
body: JSON.stringify({
|
|
75
|
+
email: identification,
|
|
76
|
+
password: password,
|
|
77
|
+
}),
|
|
78
|
+
method: "post",
|
|
79
|
+
})
|
|
80
|
+
cli.action.stop("ready")
|
|
81
|
+
const payload = await loginRigo(data.token)
|
|
82
|
+
|
|
83
|
+
return { ...data, rigobot: payload }
|
|
84
|
+
} catch (error) {
|
|
85
|
+
cli.action.stop("error")
|
|
86
|
+
Console.error((error as TypeError).message)
|
|
87
|
+
Console.debug(error)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const loginRigo = async (token: string) => {
|
|
92
|
+
const rigoUrl = `${RIGOBOT_HOST}/v1/auth/me/token?breathecode_token=${token}`
|
|
93
|
+
const rigoResp = await _fetch(rigoUrl)
|
|
94
|
+
const rigobotJson = await rigoResp.json()
|
|
95
|
+
return rigobotJson
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const getOpenAIToken = async () => {
|
|
99
|
+
const token = await storage.getItem("openai-token")
|
|
100
|
+
return token
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const getRigoFeedback = async (readme: string, currentCode: string) => {
|
|
104
|
+
const payload = {
|
|
105
|
+
current_code: Buffer.from(currentCode).toString("base64"), // Encode currentCode as base64
|
|
106
|
+
tutorial: Buffer.from(readme).toString("base64"), // Encode readme as base64
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const session = await storage.getItem("bc-payload")
|
|
110
|
+
|
|
111
|
+
const response = await _fetch(`${RIGOBOT_HOST}/v1/conversation/feedback/`, {
|
|
112
|
+
method: "POST",
|
|
113
|
+
headers: {
|
|
114
|
+
"Content-Type": "application/json",
|
|
115
|
+
Authorization: `Token ${session.rigobot.key}`,
|
|
116
|
+
},
|
|
117
|
+
body: JSON.stringify(payload),
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
const responseData = await response.json()
|
|
121
|
+
return responseData.feedback
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const publish = async (config: any) => {
|
|
125
|
+
const keys = [
|
|
126
|
+
"difficulty",
|
|
127
|
+
"language",
|
|
128
|
+
"skills",
|
|
129
|
+
"technologies",
|
|
130
|
+
"slug",
|
|
131
|
+
"repository",
|
|
132
|
+
"author",
|
|
133
|
+
"title",
|
|
134
|
+
]
|
|
135
|
+
|
|
136
|
+
const payload: { [key: string]: string } = {}
|
|
137
|
+
for (const k of keys)
|
|
138
|
+
config[k] ? (payload[k] = config[k]) : null
|
|
139
|
+
try {
|
|
140
|
+
console.log("Package to publish:", payload)
|
|
141
|
+
cli.action.start("Updating package information...")
|
|
142
|
+
await cli.wait(1000)
|
|
143
|
+
const data = await fetch(`${HOST}/v1/package/${config.slug}`, {
|
|
144
|
+
method: "PUT",
|
|
145
|
+
body: JSON.stringify(payload),
|
|
146
|
+
})
|
|
147
|
+
cli.action.stop("ready")
|
|
148
|
+
return data
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.log("payload", payload)
|
|
151
|
+
Console.error((error as TypeError).message)
|
|
152
|
+
Console.debug(error)
|
|
153
|
+
throw error
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const update = async (config: any) => {
|
|
158
|
+
try {
|
|
159
|
+
cli.action.start("Updating package information...")
|
|
160
|
+
await cli.wait(1000)
|
|
161
|
+
const data = await fetch(`${HOST}/v1/package/`, {
|
|
162
|
+
method: "POST",
|
|
163
|
+
body: JSON.stringify(config),
|
|
164
|
+
})
|
|
165
|
+
cli.action.stop("ready")
|
|
166
|
+
return data
|
|
167
|
+
} catch (error) {
|
|
168
|
+
Console.error((error as any).message)
|
|
169
|
+
Console.debug(error)
|
|
170
|
+
throw error
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const getPackage = async (slug: string) => {
|
|
175
|
+
try {
|
|
176
|
+
cli.action.start("Downloading package information...")
|
|
177
|
+
await cli.wait(1000)
|
|
178
|
+
const data = await fetch(`${HOST}/v1/package/${slug}`)
|
|
179
|
+
cli.action.stop("ready")
|
|
180
|
+
return data
|
|
181
|
+
} catch (error) {
|
|
182
|
+
if ((error as any).status === 404)
|
|
183
|
+
Console.error(`Package ${slug} does not exist`)
|
|
184
|
+
else
|
|
185
|
+
Console.error(`Package ${slug} does not exist`)
|
|
186
|
+
Console.debug(error)
|
|
187
|
+
throw error
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const getLangs = async () => {
|
|
192
|
+
try {
|
|
193
|
+
cli.action.start("Downloading language options...")
|
|
194
|
+
await cli.wait(1000)
|
|
195
|
+
const data = await fetch(`${HOST}/v1/package/language`)
|
|
196
|
+
cli.action.stop("ready")
|
|
197
|
+
return data
|
|
198
|
+
} catch (error) {
|
|
199
|
+
if ((error as any).status === 404)
|
|
200
|
+
Console.error("Package slug does not exist")
|
|
201
|
+
else
|
|
202
|
+
Console.error("Package slug does not exist")
|
|
203
|
+
Console.debug(error)
|
|
204
|
+
throw error
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const getAllPackages = async ({
|
|
209
|
+
lang = "",
|
|
210
|
+
slug = "",
|
|
211
|
+
}: {
|
|
212
|
+
lang?: string;
|
|
213
|
+
slug?: string;
|
|
214
|
+
}) => {
|
|
215
|
+
try {
|
|
216
|
+
cli.action.start("Downloading packages...")
|
|
217
|
+
await cli.wait(1000)
|
|
218
|
+
const data = await fetch(
|
|
219
|
+
`${HOST}/v1/package/all?limit=100&language=${lang}&slug=${slug}`
|
|
220
|
+
)
|
|
221
|
+
cli.action.stop("ready")
|
|
222
|
+
return data
|
|
223
|
+
} catch (error) {
|
|
224
|
+
Console.error(`Package ${slug} does not exist`)
|
|
225
|
+
Console.debug(error)
|
|
226
|
+
throw error
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const APIError = (error: TypeError | string, code?: number) => {
|
|
231
|
+
const message: string = (error as TypeError).message || (error as string)
|
|
232
|
+
const _err = new Error(message) as any
|
|
233
|
+
_err.status = code || 400
|
|
234
|
+
return _err
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export default {
|
|
238
|
+
login,
|
|
239
|
+
publish,
|
|
240
|
+
update,
|
|
241
|
+
getPackage,
|
|
242
|
+
getLangs,
|
|
243
|
+
getAllPackages,
|
|
244
|
+
getRigoFeedback,
|
|
245
|
+
getOpenAIToken,
|
|
246
|
+
}
|