-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcli.js
291 lines (291 loc) · 9.59 KB
/
cli.js
1
2
3
4
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
38
39
40
41
42
43
44
45
46
47
48
49
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
82
83
84
85
86
87
88
89
90
91
92
93
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const { execSync } = require("child_process");
const fs = require('fs');
const logic = require('./logic.js');
const config = require('./configLoader.js');
const utils = require('./assets/utils/cli.js');
let marked = require('marked');
const markedTerminal = require('marked-terminal');
marked.setOptions({
mangle: false,
headerIds: false,
renderer: new markedTerminal()
});
marked = marked.marked;
if (process.env.H5P_SSH_CLONE) {
config.urls.library.clone = config.urls.library.sshClone;
}
const handleMissingOptionals = (missingOptionals, result, item) => {
if (result[item].optional) {
if (!missingOptionals[item]) {
missingOptionals[item] = result[item];
}
}
else {
throw new Error(`unregistered ${item} library required by ${result[item].parent}`);
}
}
const cli = {
// exports content type as .h5p zipped file
export: async (library, folder) => {
try {
const file = await logic.export(library, folder);
console.log(file);
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// imports content type from .h5p zipped file
import: (folder, archive) => {
try {
const output = logic.import(folder, archive);
console.log(`content/${output}`);
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// lists h5p libraries
list: async (reversed, ignoreFile) => {
try {
console.log('> fetching h5p library registry');
const result = await logic.getRegistry(parseInt(ignoreFile));
for (let item in result.regular) {
console.log(`${parseInt(reversed) ? result.regular[item].id : item} (${result.regular[item].org})`);
}
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// list tags for library
tags: (org, library, mainBranch) => {
try {
console.log('> fetching h5p library tags');
const result = logic.tags(org, library, mainBranch);
console.log(result);
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// computes dependencies for h5p library
deps: async (library, mode, version, folder) => {
try {
const result = await logic.computeDependencies(library, mode, version, folder);
for (let item in result) {
console.log(result[item].id ? item : `!!! unregistered ${result[item].optional ? 'optional' : 'required'} ${item} library`);
}
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// computes missing dependencies for h5p library
missing: async (library) => {
try {
const libraryDirs = await logic.parseLibraryFolders();
const registry = await logic.getRegistry();
const missing = {}; // list of missing dependencies (key) with true for optional & false for required (value)
const parseMissing = (result, item) => {
if (!registry.regular[item] && (typeof missing[item] === 'undefined' || missing[item])) {
missing[item] = result[item].optional;
}
}
let result = await logic.computeDependencies(library, 'view', null, libraryDirs[registry.regular[library].id]);
for (let item in result) {
parseMissing(result, item);
if (registry.regular[item]) {
const list = await logic.computeDependencies(item, 'edit', null, libraryDirs[registry.regular[item].id]);
for (let elem in list) {
parseMissing(list, elem);
}
}
}
result = await logic.computeDependencies(library, 'edit', null, libraryDirs[registry.regular[library].id]);
for (let item in result) {
parseMissing(result, item);
}
if (!Object.keys(missing).length) {
console.log(`> ${library} has no unregistered dependencies`);
return;
}
console.log(`> unregistered dependencies for ${library}`);
for (let item in missing) {
console.log(`${item} (${missing[item] ? 'optional' : 'required'})`);
}
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// installs dependencies for h5p library
install: async (library, mode) => {
try {
console.log(`> downloading ${library} library and dependencies into "${config.folders.libraries}" folder`);
await logic.getWithDependencies('download', library, mode);
console.log(`> done installing ${library}`);
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// clones dependencies for h5p library
clone: async (library, mode) => {
try {
console.log(`> cloning ${library} library and dependencies into "${config.folders.libraries}" folder`);
await logic.getWithDependencies('clone', library, mode);
console.log(`> done installing ${library}`);
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// installs core h5p libraries
core: async () => {
try {
for (let item of config.core.clone) {
const folder = `${config.folders.libraries}/${item}`;
if (fs.existsSync(folder)) {
console.log(`>> ~ skipping ${item}; it already exists.`);
continue;
}
console.log(`>> + installing ${item}`);
logic.clone('h5p', item, 'master', item);
}
for (let item of config.core.setup) {
await cli.setup(item);
}
console.log('> done setting up core libraries');
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// computes & installs dependencies for h5p library
setup: async function(library, version, download) {
const isUrl = ['http', 'git@'].includes(library.slice(0, 4)) ? true : false;
const url = library;
const missingOptionals = {};
try {
if (isUrl) {
const entry = await this.register(url);
library = logic.machineToShort(Object.keys(entry)[0]);
}
let toSkip = [];
const action = parseInt(download) ? 'download' : 'clone';
const latest = version ? false : true;
let result = await logic.computeDependencies(library, 'view', version);
for (let item in result) {
// setup editor dependencies for every view dependency
if (!result[item].id) {
handleMissingOptionals(missingOptionals, result, item);
}
else {
toSkip = await logic.getWithDependencies(action, item, 'edit', latest, toSkip);
}
}
result = await logic.computeDependencies(library, 'edit', version);
for (let item in result) {
if (!result[item].id) {
handleMissingOptionals(missingOptionals, result, item);
}
}
toSkip = [];
console.log(`> ${action} ${library} library "view" dependencies into "${config.folders.libraries}" folder`);
toSkip = await logic.getWithDependencies(action, library, 'view', latest, toSkip);
console.log(`> ${action} ${library} library "edit" dependencies into "${config.folders.libraries}" folder`);
toSkip = await logic.getWithDependencies(action, library, 'edit', latest, toSkip);
if (Object.keys(missingOptionals).length) {
console.log('!!! missing optional libraries');
for (let item in missingOptionals) {
console.log(`${item} (${missingOptionals[item].optional ? 'optional' : 'required'}) required by ${missingOptionals[item].parent}`);
}
}
console.log(`> done setting up ${library}`);
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// updates local library registry entry
register: async (input) => {
try {
const isUrl = ['http', 'git@'].includes(input.slice(0, 4)) ? true : false;
let registry = await logic.getRegistry();
const entry = isUrl ? await logic.registryEntryFromRepoUrl(input) : JSON.parse(fs.readFileSync(input, 'utf-8'));
registry.reversed = {...registry.reversed, ...entry};
fs.writeFileSync(config.registry, JSON.stringify(registry.reversed));
console.log('> updated registry entry');
console.log(entry);
return entry;
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// generates report that verifies if an h5p library and its dependencies have been correctly computed & installed
verify: async (library) => {
try {
let result = await logic.verifySetup(library);
console.log(result);
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// run the dev server
server: () => {
require('./server.js');
},
// help section
help: (command) => {
try {
const help = fs.readFileSync(`${require.main.path}/assets/docs/commands.md`, 'utf-8');
if (command) {
const regexp = ` \`h5p ${command}(.*?)(\\n\\n|\\Z)`;
const data = help.match(new RegExp(regexp, 's'))?.[0];
if (!data) {
console.log(`> "${command}" is not a valid command`);
return;
}
console.log(marked(data));
return;
}
console.log(marked(help));
}
catch (error) {
console.log('> error');
console.log(error);
}
},
// various utility commands
utils: function() {
if (typeof utils[arguments[0]] != 'function') {
console.log(`> "${process.argv[3]}" is not a valid utils command`);
return;
}
utils[arguments[0]].apply(null, Array.prototype.slice.call(arguments).slice(1));
}
}
if (typeof cli[process.argv[2]] == 'function') {
cli[process.argv[2]].apply(cli, process.argv.slice(3));
}
else if (process.argv[2] === undefined) {
cli.help();
}
else {
console.log(`> "${process.argv[2]}" is not a valid command`);
}