@learnpack/learnpack 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,107 +1,107 @@
1
- import { prompt } from "enquirer";
2
- import SessionCommand from "../utils/SessionCommand";
3
- import Console from "../utils/console";
4
- import api from "../utils/api";
5
- import { validURL } from "../utils/validators";
6
-
7
- // eslint-disable-next-line
8
- const fetch = require("node-fetch");
9
-
10
- class PublishCommand extends SessionCommand {
11
- static description = `Describe the command here
12
- ...
13
- Extra documentation goes here
14
- `;
15
-
16
- static flags: any = {
17
- // name: flags.string({char: 'n', description: 'name to print'}),
18
- };
19
-
20
- static args = [
21
- {
22
- name: "package", // name of arg to show in help and reference with args[name]
23
- required: false, // make the arg required with `required: true`
24
- description:
25
- "The unique string that identifies this package on learnpack", // help description
26
- hidden: false, // hide this arg from help
27
- },
28
- ];
29
-
30
- async init() {
31
- const { flags } = this.parse(PublishCommand);
32
- await this.initSession(flags, true);
33
- }
34
-
35
- async run() {
36
- const { flags, args } = this.parse(PublishCommand);
37
-
38
- // avoid annonymus sessions
39
- // eslint-disable-next-line
40
- if (!this.session) return;
41
-
42
- Console.info(
43
- `Session found for ${this.session.payload.email}, publishing the package...`
44
- );
45
-
46
- const configObject = this.configManager?.get();
47
- if (
48
- configObject?.config?.slug === undefined ||
49
- !configObject.config?.slug
50
- ) {
51
- throw new Error(
52
- "The package is missing a slug (unique name identifier), please check your learn.json file and make sure it has a 'slug'"
53
- );
54
- }
55
-
56
- if (!validURL(configObject?.config?.repository ?? "")) {
57
- throw new Error(
58
- "The package has a missing or invalid 'repository' on the configuration file, it needs to be a Github URL"
59
- );
60
- } else {
61
- const validateResp = await fetch(configObject.config?.repository, {
62
- method: "HEAD",
63
- });
64
- if (!validateResp.ok || validateResp.status !== 200) {
65
- throw new Error(
66
- `The specified repository URL on the configuration file does not exist or its private, only public repositories are allowed at the moment: ${configObject.config?.repository}`
67
- );
68
- }
69
- }
70
-
71
- // start watching for file changes
72
- try {
73
- await api.publish({
74
- ...configObject,
75
- author: this.session.payload.user_id,
76
- });
77
- Console.success(
78
- `Package updated and published successfully: ${configObject.config?.slug}`
79
- );
80
- } catch (error) {
81
- if ((error as any).status === 404) {
82
- const answer = await prompt([
83
- {
84
- type: "confirm",
85
- name: "create",
86
- message: `Package with slug ${configObject.config?.slug} does not exist, do you want to create it?`,
87
- },
88
- ]);
89
- if (answer) {
90
- await api.update({
91
- ...configObject,
92
- author: this.session.payload.user_id,
93
- });
94
- Console.success(
95
- `Package created and published successfully: ${configObject.config?.slug}`
96
- );
97
- } else {
98
- Console.error("No answer from server");
99
- }
100
- } else {
101
- Console.error((error as TypeError).message);
102
- }
103
- }
104
- }
105
- }
106
-
107
- export default PublishCommand;
1
+ import { prompt } from "enquirer";
2
+ import SessionCommand from "../utils/SessionCommand";
3
+ import Console from "../utils/console";
4
+ import api from "../utils/api";
5
+ import { validURL } from "../utils/validators";
6
+
7
+ // eslint-disable-next-line
8
+ const fetch = require("node-fetch");
9
+
10
+ class PublishCommand extends SessionCommand {
11
+ static description = `Describe the command here
12
+ ...
13
+ Extra documentation goes here
14
+ `;
15
+
16
+ static flags: any = {
17
+ // name: flags.string({char: 'n', description: 'name to print'}),
18
+ };
19
+
20
+ static args = [
21
+ {
22
+ name: "package", // name of arg to show in help and reference with args[name]
23
+ required: false, // make the arg required with `required: true`
24
+ description:
25
+ "The unique string that identifies this package on learnpack", // help description
26
+ hidden: false, // hide this arg from help
27
+ },
28
+ ];
29
+
30
+ async init() {
31
+ const { flags } = this.parse(PublishCommand);
32
+ await this.initSession(flags, true);
33
+ }
34
+
35
+ async run() {
36
+ const { flags, args } = this.parse(PublishCommand);
37
+
38
+ // avoid annonymus sessions
39
+ // eslint-disable-next-line
40
+ if (!this.session) return;
41
+
42
+ Console.info(
43
+ `Session found for ${this.session.payload.email}, publishing the package...`
44
+ );
45
+
46
+ const configObject = this.configManager?.get();
47
+ if (
48
+ configObject?.config?.slug === undefined ||
49
+ !configObject.config?.slug
50
+ ) {
51
+ throw new Error(
52
+ "The package is missing a slug (unique name identifier), please check your learn.json file and make sure it has a 'slug'"
53
+ );
54
+ }
55
+
56
+ if (!validURL(configObject?.config?.repository ?? "")) {
57
+ throw new Error(
58
+ "The package has a missing or invalid 'repository' on the configuration file, it needs to be a Github URL"
59
+ );
60
+ } else {
61
+ const validateResp = await fetch(configObject.config?.repository, {
62
+ method: "HEAD",
63
+ });
64
+ if (!validateResp.ok || validateResp.status !== 200) {
65
+ throw new Error(
66
+ `The specified repository URL on the configuration file does not exist or its private, only public repositories are allowed at the moment: ${configObject.config?.repository}`
67
+ );
68
+ }
69
+ }
70
+
71
+ // start watching for file changes
72
+ try {
73
+ await api.publish({
74
+ ...configObject,
75
+ author: this.session.payload.user_id,
76
+ });
77
+ Console.success(
78
+ `Package updated and published successfully: ${configObject.config?.slug}`
79
+ );
80
+ } catch (error) {
81
+ if ((error as any).status === 404) {
82
+ const answer = await prompt([
83
+ {
84
+ type: "confirm",
85
+ name: "create",
86
+ message: `Package with slug ${configObject.config?.slug} does not exist, do you want to create it?`,
87
+ },
88
+ ]);
89
+ if (answer) {
90
+ await api.update({
91
+ ...configObject,
92
+ author: this.session.payload.user_id,
93
+ });
94
+ Console.success(
95
+ `Package created and published successfully: ${configObject.config?.slug}`
96
+ );
97
+ } else {
98
+ Console.error("No answer from server");
99
+ }
100
+ } else {
101
+ Console.error((error as TypeError).message);
102
+ }
103
+ }
104
+ }
105
+ }
106
+
107
+ export default PublishCommand;