This repository was archived by the owner on Apr 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·79 lines (71 loc) · 1.91 KB
/
cli.js
File metadata and controls
executable file
·79 lines (71 loc) · 1.91 KB
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
#!/usr/bin/env node
const meow = require("meow");
const chalk = require("chalk");
const execa = require("execa");
const commandExists = require("command-exists");
const hasModule = require("has-module");
const pkg = require("./package.json");
function canUseYarn() {
return commandExists("yarn");
}
async function install(module) {
if (await canUseYarn()) {
await execa("yarn", ["add", module]);
} else {
await execa("npm", ["install", module]);
}
}
async function installAndRun(cmd, args) {
const module = `@contentz/${cmd}`;
if (!hasModule(module)) {
console.log(`Module @contentz/${cmd} not found, starting to install it...`);
await install(module);
}
const command = require(module);
await command(args);
}
async function main() {
const cli = meow(
`${chalk.white("Usage")}
$ ${chalk.cyan("contentz init")} Initialize a new project using Contentz
$ ${chalk.cyan("contentz build")} Build the whole website
$ ${chalk.cyan(
"contentz social <path>"
)} Generate the social images for the given files
$ ${chalk.cyan(
"contentz write <path>"
)} Generate a base article inside "/articles" with the current date and non published
$ ${chalk.cyan("contentz help")} Show this message
`,
{
description: `${chalk.cyan("Contentz")} - ${pkg.description}`
}
);
let [cmd = "help", ...files] = cli.input;
cmd = cmd.toLowerCase();
switch (cmd) {
case "help": {
return cli.showHelp(0);
}
case "build": {
return await installAndRun(cmd);
}
case "social": {
return await installAndRun(cmd, files);
}
case "write": {
return await installAndRun(cmd, files);
}
case "init": {
return await installAndRun(cmd, files[0]);
}
default: {
console.error(`Command ${cmd} is not supported.`);
cli.showHelp(1);
}
}
}
main().catch(error => {
console.error(error);
process.exit(1);
});