-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.js
More file actions
140 lines (121 loc) · 3.42 KB
/
cli.js
File metadata and controls
140 lines (121 loc) · 3.42 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
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
#!/usr/bin/env node
import { Command } from "commander";
import { generateFile } from "./utils.js";
import color from "ansi-colors";
import { execa } from "execa";
const program = new Command();
const generate = program.command("generate").alias("g");
program
.name("next-cli")
.version("0.0.1")
.description(
`${color.bgGreenBright(" NEXT.JS CLI ")} - ${color.bgCyan(
" Author: Kristiyan Velkov "
)}`
);
// Create new Next.js project
program
.command("init")
.alias("i")
.argument("<projectName>", "Name of project")
.description("Create a new Next.js App.")
.action(async (projectName) => {
try {
await execa("npx", ["create-next-app", projectName], {
stdio: "inherit",
});
console.log(
color.greenBright(`Next.js app ${projectName} created successfully!`)
);
} catch (error) {
console.error(
color.red(`Failed to create Next.js app: ${error.message}`)
);
}
});
generate
.description("Generate a new Next.js App routes with files.")
.argument("<path>", "Provide Path where to create a file.")
.option("-n, --name <value>", "Name of the function in the file.")
.option("-p, --page", "Generate page.txs file.")
.option("-l, --layout", "Generate layout.tsx file.")
.option("-load, --loading", "Generate loading.tsx file.")
.option("-err, --error", "Generate error.tsx file.")
.option("-not, --not-found ", "Generate not-found.tsx file.")
.option("-t, --template", "Generate template.tsx file.")
.option("-d, --default-file", "Generate default.tsx file.")
.option("-r, --route", "Generate route.tsx file.")
.option(
"-a, --all",
"Generate page.tsx, loading.tsx, error.tsx, not-found.tsx files."
)
.action((path, options) => {
const {
all,
name,
page,
layout,
loading,
error,
notFound,
template,
defaultFile,
route,
} = options;
if (all) {
generateFile("page", path, name);
generateFile("loading", path, name);
generateFile("error", path, name);
generateFile("not-found", path, name);
}
if (page) {
generateFile("page", path, name);
}
if (layout) {
generateFile("layout", path, name);
}
if (loading) {
generateFile("loading", path, name);
}
if (error) {
generateFile("error", path, name);
}
if (notFound) {
generateFile("not-found", path, name);
}
if (template) {
generateFile("template", path, name);
}
if (defaultFile) {
generateFile("default", path, name);
}
if (route) {
generateFile("route", `api/${path}`, "");
}
if (Object.keys(options).length === 0) {
console.log(
color.red(
"Please specify option to generate files. View nc generate --help"
)
);
}
});
generate
.command("root")
.alias("r")
.description("Generate middleware.tsx and global-error.tsx files.")
.option("-g,--global-error", "Generate global-error.tsx file in the root")
.option("-m,--middleware", "Generate middleware.tsx file")
.action((options) => {
const { middleware, globalError } = options;
if (globalError) {
generateFile("global-error", "", "");
}
if (middleware) {
generateFile("middleware", "", "");
}
if (!options || options.length === 0) {
console.log(color.red("Please specify options to generate files."));
}
});
program.parse(process.argv);