-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·172 lines (157 loc) · 5.1 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
#!/usr/bin/env node
// Usage: npx @dataesr/doadify my-app
import fs from "fs";
import path from "path";
import inquirer from "inquirer";
import chalk from "chalk";
import { fileURLToPath } from "url";
async function scaffoldFullstack(projectName) {
const currentDir = process.cwd();
const projectDir = path.resolve(currentDir, projectName);
fs.mkdirSync(projectDir, { recursive: true });
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const templateDir = path.resolve(__dirname, "templates/fullstack");
fs.cpSync(templateDir, projectDir, { recursive: true });
const publicDirSrc = path.resolve(__dirname, "templates/public");
const publicDirDest = path.join(projectDir,"public");
fs.cpSync(publicDirSrc, publicDirDest, { recursive: true });
// Update the project's package.json with the new project name
const { default: projectPackageJson } = await import(
`${path.join(projectDir, "package.json")}`,
{ assert: { type: "json" } }
);
projectPackageJson.name = projectName;
fs.writeFileSync(
path.join(projectDir, "package.json"),
JSON.stringify(projectPackageJson, null, 2)
);
// Rename the dotfiles after we have copied them over to the new project directory.
fs.renameSync(
path.join(projectDir, "_github"),
path.join(projectDir, ".github")
);
fs.renameSync(
path.join(projectDir, "_dockerignore"),
path.join(projectDir, ".dockerignore")
);
const clientDir = path.resolve(projectDir, "client");
fs.renameSync(
path.join(clientDir, "_env.staging"),
path.join(clientDir, ".env.staging")
);
fs.renameSync(
path.join(clientDir, "_env.production"),
path.join(clientDir, ".env.production")
);
fs.renameSync(
path.join(clientDir, "_eslintrc"),
path.join(clientDir, ".eslintrc")
);
fs.renameSync(
path.join(clientDir, "_gitignore"),
path.join(clientDir, ".gitignore")
);
const serverDir = path.resolve(projectDir, "server");
fs.renameSync(
path.join(serverDir, "_eslintrc"),
path.join(serverDir, ".eslintrc")
);
fs.renameSync(
path.join(serverDir, "_gitignore"),
path.join(serverDir, ".gitignore")
);
}
async function scaffoldClient(projectName) {
const currentDir = process.cwd();
const projectDir = path.resolve(currentDir, projectName);
fs.mkdirSync(projectDir, { recursive: true });
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const templateDir = path.resolve(__dirname, "templates/client");
fs.cpSync(templateDir, projectDir, { recursive: true });
const publicDirSrc = path.resolve(__dirname, "templates/public");
const publicDirDest = path.join(projectDir,"public");
fs.cpSync(publicDirSrc, publicDirDest, { recursive: true });
// rename the dotfiles after we have copied them over to the new project directory.
fs.renameSync(
path.join(projectDir, "_github"),
path.join(projectDir, ".github")
);
fs.renameSync(
path.join(projectDir, "_env"),
path.join(projectDir, ".env"));
fs.renameSync(
path.join(projectDir, "_env.staging"),
path.join(projectDir, ".env.staging")
);
fs.renameSync(
path.join(projectDir, "_env.production"),
path.join(projectDir, ".env.production")
);
fs.renameSync(
path.join(projectDir, "_eslintrc"),
path.join(projectDir, ".eslintrc")
);
fs.renameSync(
path.join(projectDir, "_gitignore"),
path.join(projectDir, ".gitignore")
);
// Update the project's package.json with the new project name
const { default: projectPackageJson } = await import(
`${path.join(projectDir, "package.json")}`,
{ assert: { type: "json" } }
);
projectPackageJson.name = projectName;
fs.writeFileSync(
path.join(projectDir, "package.json"),
JSON.stringify(projectPackageJson, null, 2)
);
}
const projectName = process.argv[2];
const questions = [
{
type: "list",
name: "type",
message: "Do you need a server ?",
choices: ["Yes", "No"],
filter(val) {
return val.toLowerCase();
},
},
];
const answers = await inquirer.prompt(questions).catch((error) => {
if (error.isTtyError) {
console.log(chalk.red("Error"));
} else {
console.log(chalk.red("Error"));
}
});
if (answers.type === "no") {
await scaffoldClient(projectName);
console.log("");
console.log(
chalk.yellow.bold(`React project scaffolded in '/${projectName}'`)
);
console.log("-------------------------");
console.log(` To get started, run:`);
console.log(chalk.italic(` > cd ${projectName}`));
console.log(chalk.italic(` > npm install`));
console.log(chalk.italic(` > npm run start`));
console.log("-------------------------");
console.log("");
console.log("");
}
if (answers.type === "yes") {
await scaffoldFullstack(projectName);
console.log("");
console.log(
chalk.yellow.bold(`Fullstack project scaffolded in '/${projectName}'`)
);
console.log("-------------------------");
console.log(` To get started, run:`);
console.log(chalk.italic(` > cd ${projectName}`));
console.log(chalk.italic(` > npm install`));
console.log(chalk.italic(` > npm run start`));
console.log("-------------------------");
console.log("");
console.log("");
}