-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
executable file
·484 lines (423 loc) · 12.8 KB
/
index.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/usr/bin/env node
/**
* Crowdbotics Modules tool
*
* Run it anywhere with: cb
*
* Commands available:
* - parse
* - demo
* - add
* - remove
* - create
* - commit
* - init
* - upgrade
* - help
*/
import arg from "arg";
import fs from "node:fs";
import path from "node:path";
import { spawnSync } from "node:child_process";
import findGitRoot from "find-git-root";
import { parseModules } from "./scripts/parse.js";
import { createDemo } from "./scripts/demo.js";
import { addModules } from "./scripts/add.js";
import { info } from "./scripts/info.js";
import { removeModules } from "./scripts/remove.js";
import { commitModules } from "./scripts/commit-module.js";
import { upgradeScaffold } from "./scripts/upgrade.js";
import {
valid,
invalid,
isNameValid,
section,
isUserEnvironment
} from "./utils.js";
import { createModule } from "./scripts/create.js";
import { login } from "./scripts/login.js";
import { configFile } from "./scripts/utils/configFile.js";
import { sendFeedback } from "./scripts/feedback.js";
import { logout } from "./scripts/logout.js";
import { modulesArchive, modulesGet, modulesList } from "./scripts/modules.js";
import { publish } from "./scripts/publish.js";
import {
validateEnvironmentDependencies,
EnvironmentDependency
} from "./scripts/utils/environment.js";
import { analytics } from "./scripts/analytics/wrapper.js";
import { HAS_ASKED_OPT_IN_NAME } from "./scripts/analytics/config.js";
import { EVENT } from "./scripts/analytics/constants.js";
import { askOptIn } from "./scripts/analytics/scripts.js";
import { sentryMonitoring } from "./scripts/utils/sentry.js";
const pkg = JSON.parse(
fs.readFileSync(new URL("package.json", import.meta.url), "utf8")
);
let sourceDir = path.dirname(path.dirname(process.argv[1]));
if (fs.existsSync(path.join(sourceDir, pkg.name))) {
// npx lib directory
sourceDir = path.join(sourceDir, pkg.name);
} else {
// npm lib directory
sourceDir = path.join(sourceDir, "lib", "node_modules", pkg.name);
}
const gitRoot = () => {
try {
return path.dirname(findGitRoot(process.cwd()));
} catch {
invalid(
`This command must be executed inside a git repository.
Visit our official documentation for more information and try again: https://docs.crowdbotics.com/creating-reusable-modules`
);
}
};
async function dispatcher() {
const useDefaults = process.env.npm_config_yes;
// check config if they have been asked opted in or out of amplitude
const hasAskedOptIn = configFile.get(HAS_ASKED_OPT_IN_NAME) || false;
if (!hasAskedOptIn && isUserEnvironment && !useDefaults) {
await askOptIn();
}
const command = process.argv[2];
if (!command) {
return commands.help();
}
if (!Object.prototype.hasOwnProperty.call(commands, command)) {
invalid(`command doesn't exist: ${command}`);
}
sentryMonitoring.registerCommandName(command);
await commands[command]();
if (!analytics.event.name && !useDefaults) {
analytics.sendEvent({
name: EVENT.OTHER,
properties: {
command,
fullCommand: process.argv.slice(2, process.argv.length).join(" ")
}
});
}
}
const commands = {
demo: () => {
validateEnvironmentDependencies([
EnvironmentDependency.Python,
EnvironmentDependency.PipEnv
]);
createDemo(
path.join(gitRoot(), "demo"),
path.join(sourceDir, "cookiecutter.yaml")
);
valid("demo app successfully generated");
},
parse: () => {
const args = arg({
"--source": String,
"--write": String
});
if (!args["--source"]) {
invalid("missing required argument: --source");
}
const data = parseModules(path.join(args["--source"]), gitRoot());
if (args["--write"] && process.exitCode !== 1) {
fs.mkdirSync(path.dirname(path.join(args["--write"])), {
recursive: true
});
fs.writeFileSync(
path.join(args["--write"]),
JSON.stringify(data, null, 2),
"utf8"
);
}
},
add: () => {
validateEnvironmentDependencies([
EnvironmentDependency.Python,
EnvironmentDependency.Yarn
]);
const args = arg({
"--source": String,
"--project": String
});
const modules = args._.slice(1);
if (!modules.length) {
invalid("please provide the name of the modules to be installed");
}
addModules(modules, args["--source"], args["--project"], gitRoot());
},
remove: () => {
validateEnvironmentDependencies([EnvironmentDependency.Yarn]);
const args = arg({
"--source": String,
"--project": String
});
const modules = args._.slice(1);
if (!modules.length) {
invalid("please provide the name of the modules to be removed");
}
removeModules(modules, args["--source"], args["--project"], gitRoot());
},
create: () => {
validateEnvironmentDependencies([
EnvironmentDependency.Python,
EnvironmentDependency.CookieCutter
]);
const args = arg({
"--name": String,
"--type": String,
"--target": String
});
if (!args["--name"]) {
invalid("missing required argument: --name");
}
if (!args["--type"]) {
invalid("missing required argument: --type");
}
if (!isNameValid(args["--name"])) {
invalid(
`invalid module name provided: '${args["--name"]}'. Use only alphanumeric characters, dashes and underscores.`
);
}
analytics.sendEvent({
name: EVENT.CREATE_MODULE,
properties: { Name: args["--name"] }
});
createModule(args["--name"], args["--type"], args["--target"], gitRoot());
},
commit: () => {
const args = arg({
"--source": String
});
const modules = args._.slice(1);
if (!modules.length) {
invalid("please provide the name of the modules to be commited");
}
commitModules(modules, args["--source"], gitRoot());
},
init: () => {
validateEnvironmentDependencies([EnvironmentDependency.Git]);
const args = arg({
"--name": String
});
if (!args["--name"]) {
invalid("missing required argument: --name");
}
const baseDir = path.join(process.cwd(), args["--name"]);
const git = spawnSync("git init", [args["--name"]], {
cwd: process.cwd(),
shell: true
});
if (git.error) {
invalid("git init failed", git.stderr);
}
const gitignore = `logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
node_modules/
.npm
.DS_Store
.idea
demo`;
fs.mkdirSync(path.join(baseDir, "modules"));
fs.writeFileSync(path.join(baseDir, ".gitignore"), gitignore, "utf8");
fs.writeFileSync(path.join(baseDir, "modules", ".keep"), "", "utf8");
spawnSync("git add .gitignore modules", [], {
cwd: baseDir,
shell: true
});
spawnSync("git commit -m 'Initial commit'", [], {
cwd: baseDir,
shell: true
});
},
upgrade: () => {
const args = arg({
"--version": String
});
analytics.sendEvent({ name: EVENT.UPGRADE });
upgradeScaffold(args["--version"]);
},
login: () => {
login();
},
logout: () => {
logout();
},
info: () => {
info();
},
config: () => {
const args = arg({});
const action = args._[1];
const key = args._[2];
const value = args._[3];
if (!action.length) {
return invalid("Please provide the action to perform on the config");
}
switch (action) {
case "set":
if (!key) {
return invalid("Please specify the config key to set.");
}
if (!value) {
return invalid("Please specify the config value to set.");
}
configFile.set(key, value);
configFile.save();
break;
case "get":
if (!key) {
return invalid("Please specify the config key to get.");
}
section(configFile.get(key));
break;
default:
invalid(`Invalid action "${action}" for config command`);
}
},
modules: async () => {
const args = arg({
"--search": String,
"--visibility": String,
"--status": String,
"--page": String,
"--unarchive": Boolean
});
let id;
const action = args._[1];
if (!action.length) {
// TODO - Print help?
return invalid(
"Please provide the action to perform on the modules, i.e. modules list"
);
}
switch (action) {
case "list":
analytics.sendEvent({ name: EVENT.LIST_MODULES });
await modulesList({
search: args["--search"],
status: args["--status"],
visibility: args["--visibility"],
page: args["--page"] ? Number(args["--page"]) : undefined
});
break;
case "get":
id = args._[2];
if (!id) {
return invalid(
"Please provide the id of the module to get, i.e. modules get <123>"
);
}
await modulesGet(id);
break;
case "archive":
id = args._[2];
if (!id) {
return invalid(
"Please provide the id of the module to archive, i.e. modules archive <123>"
);
}
await modulesArchive(id, !!args["--unarchive"]);
break;
case "help":
section(
`Commands available:
list List the current modules available to install
--search <query> Search for a module by given text
--status <published | archived> Search for a module by either published or archived modules (default all)
--visibility <private | public> Search for a module with a specific visibility (default all)
get <id> Get the details for the specified module
archive <id> Archive the specified module to prevent future installation to a project
--unarchive Undo the archive of the module to set the status back to Published
`
);
break;
default:
invalid(`Invalid action "${action}" for modules command`);
}
},
publish: () => {
analytics.sendEvent({
name: EVENT.PUBLISH_MODULES
});
publish();
},
feedback: () => {
const args = arg({});
const action = args._[1];
if (!action) {
return invalid(
"Please provide the message or action to perform for feedback"
);
}
switch (action) {
case "help":
console.log(`
Influence how Crowdbotics shapes and grows its developer tools. Use the feedback
command to send ideas and recommendations to our Product Team any time. We may
contact you to follow up.
Please contact Support for help using Crowdbotics or to report errors, bugs, and
other issues.
https://app.crowdbotics.com/dashboard/user/support
`);
break;
default:
sendFeedback(action);
}
},
help: () => {
console.log(`usage: cb <command>
Commands available:
parse Parse and validate your modules
demo Generate a local React Native and Django demo app
add Install a module in the demo app
remove Remove a module from the demo app
create Create a new module of a given type
commit Update an existing module from the demo source code
init Initialize a blank modules repository
upgrade Upgrade your existing app's scaffold to the latest version
help Show this help page
feedback Send feedback to Crowdbotics to let us know how we're doing
login Login to your Crowdbotics account. Requires 2FA authentication
logout Logout of your Crowdbotics account
publish Publish your modules to your organization's private catalog
modules Manage modules for your organization
Parse and validate your modules:
cb parse --source <path>
Parse modules and write the data to a json file:
cb parse --source <path> --write <path>
Create a demo app:
cb demo
Create a module of a given type:
cb create --name <module-name> --type <all/react-native/django>
Initialize a modules repository:
cb init --name <my-modules-repository-name>
Upgrade your scaffold to the latest master:
cb upgrade
Upgrade your scaffold to a specific version (git tag, git commit or branch name):
cb upgrade --version 2.3.0
Install one or modules to your demo app:
cb add <module-name> <module-name-2>
Remove one or modules from your demo app:
cb remove <module-name> <module-name-2>
Install modules from other directory:
cb add --source ../other-repository <module-name>
Install modules to other app that is not "demo":
cb add --project ../other-project <module-name>
Remove modules from app that is not "demo":
cb remove --project ../other-project <module-name>
Update a module definition from the demo app:
cb commit <module-name>
Update a module definition from other app:
cb commit <module-name> --source <path>
Glossary:
<module-name> stands for the name of the directory where the module is defined.
`);
}
};
try {
dispatcher();
} catch (err) {
invalid(err);
}