@learnpack/learnpack 2.1.26 → 2.1.28

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.
Files changed (56) hide show
  1. package/README.md +10 -10
  2. package/lib/commands/start.js +15 -4
  3. package/lib/managers/file.d.ts +1 -0
  4. package/lib/managers/file.js +8 -1
  5. package/lib/managers/server/routes.js +48 -14
  6. package/lib/managers/session.d.ts +1 -1
  7. package/lib/managers/session.js +39 -12
  8. package/lib/managers/socket.d.ts +1 -1
  9. package/lib/managers/socket.js +57 -43
  10. package/lib/models/action.d.ts +1 -1
  11. package/lib/models/config.d.ts +1 -1
  12. package/lib/models/exercise-obj.d.ts +3 -0
  13. package/lib/models/session.d.ts +4 -1
  14. package/lib/models/socket.d.ts +7 -6
  15. package/lib/models/status.d.ts +1 -1
  16. package/lib/utils/api.d.ts +2 -0
  17. package/lib/utils/api.js +51 -6
  18. package/oclif.manifest.json +1 -1
  19. package/package.json +3 -1
  20. package/src/commands/audit.ts +113 -113
  21. package/src/commands/clean.ts +10 -10
  22. package/src/commands/download.ts +18 -18
  23. package/src/commands/init.ts +39 -39
  24. package/src/commands/login.ts +13 -13
  25. package/src/commands/logout.ts +9 -9
  26. package/src/commands/publish.ts +25 -25
  27. package/src/commands/start.ts +101 -75
  28. package/src/commands/test.ts +34 -34
  29. package/src/managers/config/allowed_files.ts +2 -2
  30. package/src/managers/config/defaults.ts +2 -2
  31. package/src/managers/config/exercise.ts +79 -79
  32. package/src/managers/config/index.ts +145 -145
  33. package/src/managers/file.ts +74 -65
  34. package/src/managers/server/index.ts +32 -31
  35. package/src/managers/server/routes.ts +139 -90
  36. package/src/managers/session.ts +53 -24
  37. package/src/managers/socket.ts +92 -79
  38. package/src/models/action.ts +8 -1
  39. package/src/models/config-manager.ts +2 -2
  40. package/src/models/config.ts +7 -2
  41. package/src/models/exercise-obj.ts +6 -3
  42. package/src/models/plugin-config.ts +2 -2
  43. package/src/models/session.ts +5 -2
  44. package/src/models/socket.ts +12 -6
  45. package/src/models/status.ts +15 -14
  46. package/src/plugin/command/compile.ts +10 -10
  47. package/src/plugin/command/test.ts +14 -14
  48. package/src/plugin/index.ts +5 -5
  49. package/src/plugin/plugin.ts +26 -26
  50. package/src/plugin/utils.ts +23 -23
  51. package/src/utils/BaseCommand.ts +16 -16
  52. package/src/utils/api.ts +143 -91
  53. package/src/utils/audit.ts +93 -96
  54. package/src/utils/exercisesQueue.ts +15 -15
  55. package/src/utils/fileQueue.ts +85 -85
  56. package/src/utils/watcher.ts +14 -14
@@ -1,7 +1,7 @@
1
- import logger from "../utils/console";
2
- import * as fs from "fs";
1
+ import logger from "../utils/console"
2
+ import * as fs from "fs"
3
3
  // import em from "events"
4
- import * as XXH from "xxhashjs";
4
+ import * as XXH from "xxhashjs"
5
5
 
6
6
  // possible events to dispatch
7
7
  const events = {
@@ -13,186 +13,186 @@ const events = {
13
13
  OPEN_FILES: "open_files",
14
14
  OPEN_WINDOW: "open_window",
15
15
  INSTRUCTIONS_CLOSED: "instructions_closed",
16
- };
16
+ }
17
17
 
18
18
  let options = {
19
19
  path: null,
20
20
  create: false,
21
- };
22
- let lastHash: any = null;
23
- let watcher: any = null; // subscribe to file and listen to changes
24
- let actions: any = null; // action queue
21
+ }
22
+ let lastHash: any = null
23
+ let watcher: any = null // subscribe to file and listen to changes
24
+ let actions: any = null // action queue
25
25
 
26
26
  const loadDispatcher = (opts: any) => {
27
- actions = [{ name: "initializing", time: now() }];
28
- logger.debug(`Loading from ${opts.path}`);
27
+ actions = [{ name: "initializing", time: now() }]
28
+ logger.debug(`Loading from ${opts.path}`)
29
29
 
30
- let exists = fs.existsSync(opts.path);
30
+ let exists = fs.existsSync(opts.path)
31
31
  if (opts.create) {
32
32
  if (exists)
33
- actions.push({ name: "reset", time: now() });
34
- fs.writeFileSync(opts.path, JSON.stringify(actions), { flag: "w" });
35
- exists = true;
33
+ actions.push({ name: "reset", time: now() })
34
+ fs.writeFileSync(opts.path, JSON.stringify(actions), { flag: "w" })
35
+ exists = true
36
36
  }
37
37
 
38
38
  if (!exists)
39
- throw new Error(`Invalid queue path, missing file at: ${opts.path}`);
39
+ throw new Error(`Invalid queue path, missing file at: ${opts.path}`)
40
40
 
41
- let incomingActions = [];
41
+ let incomingActions = []
42
42
  try {
43
- const content = fs.readFileSync(opts.path, "utf-8");
44
- incomingActions = JSON.parse(content);
43
+ const content = fs.readFileSync(opts.path, "utf-8")
44
+ incomingActions = JSON.parse(content)
45
45
  if (!Array.isArray(incomingActions))
46
- incomingActions = [];
46
+ incomingActions = []
47
47
  } catch {
48
- incomingActions = [];
49
- logger.debug("Error loading VSCode Actions file");
48
+ incomingActions = []
49
+ logger.debug("Error loading VSCode Actions file")
50
50
  }
51
51
 
52
- logger.debug("Actions load ", incomingActions);
53
- return incomingActions;
54
- };
52
+ logger.debug("Actions load ", incomingActions)
53
+ return incomingActions
54
+ }
55
55
 
56
56
  // eslint-disable-next-line
57
57
  const enqueue = (name: string, data: any | undefined = undefined) => {
58
58
  if (!Object.values(events).includes(name)) {
59
- logger.debug(`Invalid event ${name}`);
60
- throw new Error(`Invalid action ${name}`);
59
+ logger.debug(`Invalid event ${name}`)
60
+ throw new Error(`Invalid action ${name}`)
61
61
  }
62
62
 
63
63
  if (!actions)
64
- actions = [];
64
+ actions = []
65
65
 
66
- actions.push({ name, time: now(), data: data });
67
- logger.debug(`EMIT -> ${name}:Exporting changes to ${options.path}`);
66
+ actions.push({ name, time: now(), data: data })
67
+ logger.debug(`EMIT -> ${name}:Exporting changes to ${options.path}`)
68
68
 
69
- return fs.writeFileSync(options.path || "", JSON.stringify(actions));
70
- };
69
+ return fs.writeFileSync(options.path || "", JSON.stringify(actions))
70
+ }
71
71
 
72
72
  const now = () => {
73
- const hrTime = process.hrtime();
73
+ const hrTime = process.hrtime()
74
74
  // eslint-disable-next-line
75
75
  const htTime0 = hrTime[0] * 1000000;
76
- return (htTime0 + hrTime[1]) / 1000;
77
- };
76
+ return (htTime0 + hrTime[1]) / 1000
77
+ }
78
78
 
79
79
  const loadFile = (filePath: string) => {
80
80
  if (!fs.existsSync(filePath))
81
- throw new Error(`No queue.json file to load on ${filePath}`);
81
+ throw new Error(`No queue.json file to load on ${filePath}`)
82
82
 
83
- const content = fs.readFileSync(filePath, "utf8");
84
- const newHash = XXH.h32(content, 0xAB_CD).toString(16);
85
- const isUpdated = lastHash !== newHash;
86
- lastHash = newHash;
87
- const incomingActions = JSON.parse(content);
88
- return { isUpdated, incomingActions };
89
- };
83
+ const content = fs.readFileSync(filePath, "utf8")
84
+ const newHash = XXH.h32(content, 0xAB_CD).toString(16)
85
+ const isUpdated = lastHash !== newHash
86
+ lastHash = newHash
87
+ const incomingActions = JSON.parse(content)
88
+ return { isUpdated, incomingActions }
89
+ }
90
90
 
91
91
  const dequeue = () => {
92
92
  // first time dequeue loads
93
93
  if (!actions)
94
- actions = [];
94
+ actions = []
95
95
 
96
- const { isUpdated, incomingActions } = loadFile(options.path || "");
96
+ const { isUpdated, incomingActions } = loadFile(options.path || "")
97
97
 
98
98
  if (!isUpdated) {
99
99
  /**
100
100
  * make sure no tasks are executed from the queue by matching both
101
101
  * queues (the incoming with current one)
102
102
  */
103
- actions = incomingActions;
103
+ actions = incomingActions
104
104
  logger.debug(
105
105
  `No new actions to process: ${actions.length}/${incomingActions.length}`
106
- );
107
- return null;
106
+ )
107
+ return null
108
108
  }
109
109
 
110
110
  // do i need to reset actions to zero?
111
111
  if (actions.length > 0 && actions[0].time !== incomingActions[0].time) {
112
- actions = [];
112
+ actions = []
113
113
  }
114
114
 
115
- const action = incomingActions[incomingActions.length - 1];
116
- logger.debug("Dequeing action ", action);
117
- actions.push(action);
118
- return action;
119
- };
115
+ const action = incomingActions[incomingActions.length - 1]
116
+ logger.debug("Dequeing action ", action)
117
+ actions.push(action)
118
+ return action
119
+ }
120
120
 
121
121
  const pull = (callback: (T: any) => any) => {
122
- logger.debug("Pulling actions");
123
- let incoming = dequeue();
122
+ logger.debug("Pulling actions")
123
+ let incoming = dequeue()
124
124
  while (incoming) {
125
- callback(incoming);
126
- incoming = dequeue();
125
+ callback(incoming)
126
+ incoming = dequeue()
127
127
  }
128
- };
128
+ }
129
129
 
130
130
  const reset = (callback: (T?: any) => any) => {
131
- logger.debug("Queue reseted");
132
- actions = [];
131
+ logger.debug("Queue reseted")
132
+ actions = []
133
133
  if (fs.existsSync(options.path || "")) {
134
- fs.writeFileSync(options.path || "", "[]");
135
- callback();
134
+ fs.writeFileSync(options.path || "", "[]")
135
+ callback()
136
136
  }
137
- };
137
+ }
138
138
 
139
139
  const onPull = (callback: (T?: any) => any) => {
140
140
  // eslint-disable-next-line
141
141
  const chokidar = require("chokidar");
142
142
 
143
- logger.debug("Starting to listen...");
143
+ logger.debug("Starting to listen...")
144
144
  try {
145
- loadFile(options.path || "");
145
+ loadFile(options.path || "")
146
146
  } catch {
147
- logger.debug("No previoues queue file, waiting for it to be created...");
147
+ logger.debug("No previoues queue file, waiting for it to be created...")
148
148
  }
149
149
 
150
150
  if (!watcher) {
151
- logger.debug(`Watching ${options.path}`);
151
+ logger.debug(`Watching ${options.path}`)
152
152
  watcher = chokidar.watch(`${options.path}`, {
153
153
  persistent: true,
154
- });
154
+ })
155
155
  } else
156
- logger.debug("Already watching queue path");
156
+ logger.debug("Already watching queue path")
157
157
 
158
- watcher.on("add", () => pull(callback)).on("change", () => pull(callback));
158
+ watcher.on("add", () => pull(callback)).on("change", () => pull(callback))
159
159
 
160
- return true;
161
- };
160
+ return true
161
+ }
162
162
 
163
163
  const onReset = (callback: (T?: any) => any) => {
164
164
  // eslint-disable-next-line
165
165
  const chokidar = require("chokidar");
166
166
 
167
167
  if (!watcher) {
168
- logger.debug(`Watching ${options.path}`);
168
+ logger.debug(`Watching ${options.path}`)
169
169
  watcher = chokidar.watch(`${options.path}`, {
170
170
  persistent: true,
171
- });
171
+ })
172
172
  }
173
173
 
174
- watcher.on("unlink", () => reset(callback));
174
+ watcher.on("unlink", () => reset(callback))
175
175
 
176
- return true;
177
- };
176
+ return true
177
+ }
178
178
 
179
179
  export default {
180
180
  events,
181
181
  dispatcher: (opts: any = {}) => {
182
182
  if (!actions) {
183
- options = { ...options, ...opts };
184
- logger.debug("Initializing queue dispatcher", options);
185
- actions = loadDispatcher(options);
183
+ options = { ...options, ...opts }
184
+ logger.debug("Initializing queue dispatcher", options)
185
+ actions = loadDispatcher(options)
186
186
  }
187
187
 
188
- return { enqueue, events };
188
+ return { enqueue, events }
189
189
  },
190
190
  listener: (opts: any = {}) => {
191
191
  if (!actions) {
192
- options = { ...options, ...opts };
193
- logger.debug("Initializing queue listener", options);
192
+ options = { ...options, ...opts }
193
+ logger.debug("Initializing queue listener", options)
194
194
  }
195
195
 
196
- return { onPull, onReset, events };
196
+ return { onPull, onReset, events }
197
197
  },
198
- };
198
+ }
@@ -1,25 +1,25 @@
1
- import * as chokidar from "chokidar";
2
- import Console from "./console";
3
- import * as debounce from "debounce";
4
- import { IConfigManager } from "../models/config-manager";
1
+ import * as chokidar from "chokidar"
2
+ import Console from "./console"
3
+ import * as debounce from "debounce"
4
+ import { IConfigManager } from "../models/config-manager"
5
5
 
6
6
  export default (path: string, reloadSocket: () => void) =>
7
7
  new Promise((resolve /* , reject */) => {
8
- Console.debug("PATH:", path);
8
+ Console.debug("PATH:", path)
9
9
  const watcher = chokidar.watch(path, {
10
10
  // TODO: This watcher is not ready yet
11
11
  ignored: /^(?=.*(\.\w+)$)(?!.*\.md$).*$/gm,
12
12
  ignoreInitial: true,
13
- });
13
+ })
14
14
  const onChange = (eventname: string, _filename: string) => {
15
- resolve(eventname /* , filename */);
16
- reloadSocket();
17
- };
15
+ resolve(eventname /* , filename */)
16
+ reloadSocket()
17
+ }
18
18
 
19
- watcher.on("all", debounce(onChange, 500, true));
19
+ watcher.on("all", debounce(onChange, 500, true))
20
20
  // watcher.on('all', onChange)
21
21
  process.on("SIGINT", function () {
22
- watcher.close();
23
- process.exit();
24
- });
25
- });
22
+ watcher.close()
23
+ process.exit()
24
+ })
25
+ })