create-jinmankn-app 1.0.7 → 1.0.8

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 (2) hide show
  1. package/bin/index.js +48 -88
  2. package/package.json +1 -1
package/bin/index.js CHANGED
@@ -4,92 +4,76 @@ const fs = require("fs");
4
4
  const path = require("path");
5
5
  const readline = require("readline");
6
6
 
7
- const PKG_ROOT = path.join(__dirname, "..");
8
-
9
- const TEMPLATES = path.join(
10
- PKG_ROOT,
11
- "templates"
7
+ const templatesDir = path.join(
8
+ __dirname,
9
+ "../templates"
12
10
  );
13
11
 
14
- const MANIFEST = path.join(
15
- TEMPLATES,
12
+ const manifestPath = path.join(
13
+ templatesDir,
16
14
  "projects.json"
17
15
  );
18
16
 
19
- // Question helper
20
17
  function question(rl, prompt) {
21
18
  return new Promise((resolve) =>
22
19
  rl.question(prompt, resolve)
23
20
  );
24
21
  }
25
22
 
26
- // Copy folder recursively
27
23
  function copyTree(src, dest) {
28
24
 
29
25
  fs.mkdirSync(dest, {
30
26
  recursive: true
31
27
  });
32
28
 
33
- for (const name of fs.readdirSync(src, {
29
+ const entries = fs.readdirSync(src, {
34
30
  withFileTypes: true
35
- })) {
31
+ });
36
32
 
37
- const s = path.join(src, name.name);
33
+ for (const entry of entries) {
38
34
 
39
- const d = path.join(dest, name.name);
35
+ const srcPath = path.join(
36
+ src,
37
+ entry.name
38
+ );
39
+
40
+ const destPath = path.join(
41
+ dest,
42
+ entry.name
43
+ );
40
44
 
41
- if (name.isDirectory()) {
45
+ if (entry.isDirectory()) {
42
46
 
43
- // Ignore unnecessary folders
44
47
  if (
45
- name.name === "node_modules" ||
46
- name.name === ".git" ||
47
- name.name === "dist"
48
+ entry.name === "node_modules" ||
49
+ entry.name === ".git" ||
50
+ entry.name === "dist"
48
51
  ) {
49
52
  continue;
50
53
  }
51
54
 
52
- copyTree(s, d);
55
+ copyTree(srcPath, destPath);
53
56
 
54
57
  } else {
55
58
 
56
- fs.copyFileSync(s, d);
59
+ fs.copyFileSync(srcPath, destPath);
57
60
  }
58
61
  }
59
62
  }
60
63
 
61
- // Create .env from example
62
- function ensureEnvFromExample(projectRoot) {
64
+ async function main() {
63
65
 
64
- const backend = path.join(
65
- projectRoot,
66
- "backend"
67
- );
66
+ if (!fs.existsSync(manifestPath)) {
68
67
 
69
- const example = path.join(
70
- backend,
71
- ".env.example"
72
- );
73
-
74
- const envFile = path.join(
75
- backend,
76
- ".env"
77
- );
78
-
79
- if (
80
- fs.existsSync(example) &&
81
- !fs.existsSync(envFile)
82
- ) {
68
+ console.log(
69
+ "projects.json not found"
70
+ );
83
71
 
84
- fs.copyFileSync(example, envFile);
72
+ process.exit(1);
85
73
  }
86
- }
87
-
88
- async function main() {
89
74
 
90
- // Read manifest
91
75
  const manifest = JSON.parse(
92
- fs.readFileSync(MANIFEST, "utf8")
76
+ fs.readFileSync(manifestPath, "utf8")
93
77
  );
94
78
 
95
79
  const projects = manifest.projects;
@@ -97,53 +81,45 @@ async function main() {
97
81
  if (!projects.length) {
98
82
 
99
83
  console.log(
100
- "No templates found."
84
+ "No templates found"
101
85
  );
102
86
 
103
87
  process.exit(1);
104
88
  }
105
89
 
106
- // Show projects
107
90
  console.log("");
108
91
 
109
- for (let i = 0; i < projects.length; i++) {
110
-
111
- const p = projects[i];
92
+ projects.forEach((project, index) => {
112
93
 
113
94
  console.log(
114
- `[${i + 1}] ${p.title}`
95
+ `[${index + 1}] ${project.title}`
115
96
  );
116
- }
97
+ });
117
98
 
118
99
  console.log("");
119
100
 
120
- // CLI input
121
101
  const rl = readline.createInterface({
122
102
  input: process.stdin,
123
103
  output: process.stdout
124
104
  });
125
105
 
126
- // Choose template
127
- const ans = await question(
106
+ const answer = await question(
128
107
  rl,
129
108
  `Pick (1-${projects.length}): `
130
109
  );
131
110
 
132
- let choice = parseInt(ans);
111
+ let choice = parseInt(answer);
133
112
 
134
113
  if (
135
114
  isNaN(choice) ||
136
115
  choice < 1 ||
137
116
  choice > projects.length
138
117
  ) {
139
-
140
118
  choice = 1;
141
119
  }
142
120
 
143
- const selected =
144
- projects[choice - 1];
121
+ const selected = projects[choice - 1];
145
122
 
146
- // Ask project name
147
123
  const projectName = await question(
148
124
  rl,
149
125
  "Project name: "
@@ -151,38 +127,29 @@ async function main() {
151
127
 
152
128
  rl.close();
153
129
 
154
- const target = path.resolve(
130
+ const templatePath = path.join(
131
+ templatesDir,
132
+ selected.templateDir
133
+ );
134
+
135
+ const targetPath = path.join(
155
136
  process.cwd(),
156
137
  projectName
157
138
  );
158
139
 
159
- // Prevent overwrite
160
- if (fs.existsSync(target)) {
140
+ if (fs.existsSync(targetPath)) {
161
141
 
162
- console.log("");
163
142
  console.log(
164
- "Folder already exists."
143
+ "Folder already exists"
165
144
  );
166
145
 
167
146
  process.exit(1);
168
147
  }
169
148
 
170
- // Template path
171
- const templatePath = path.join(
172
- TEMPLATES,
173
- selected.templateDir
174
- );
175
-
176
149
  console.log("");
177
- console.log(
178
- "Creating project..."
179
- );
180
-
181
- // Copy files
182
- copyTree(templatePath, target);
150
+ console.log("Creating project...");
183
151
 
184
- // Create env
185
- ensureEnvFromExample(target);
152
+ copyTree(templatePath, targetPath);
186
153
 
187
154
  console.log("");
188
155
  console.log(
@@ -190,11 +157,9 @@ async function main() {
190
157
  );
191
158
 
192
159
  console.log("");
193
-
194
160
  console.log(`cd ${projectName}`);
195
161
 
196
162
  console.log("");
197
-
198
163
  console.log(
199
164
  "Frontend:"
200
165
  );
@@ -214,9 +179,4 @@ async function main() {
214
179
  );
215
180
  }
216
181
 
217
- main().catch((err) => {
218
-
219
- console.error(err);
220
-
221
- process.exit(1);
222
- });
182
+ main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-jinmankn-app",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "",
5
5
  "bin": {
6
6
  "create-jinmankn-app": "./bin/index.js"