-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
238 lines (202 loc) · 6.35 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
#!/usr/bin/env node
const { copyFileSync, mkdir } = require("fs");
const { resolve } = require("path");
const { execSync } = require("child_process");
const select = require("@inquirer/select").default;
const { name: packageName } = require("./package.json");
function help(code) {
console.log(`Usage:
${packageName} init
${packageName} init --force
`);
process.exit(code);
}
const reactPackagesInstallCmd = ({ install, devInstall, forceCMD }) => {
execSync(
`${install} @types/node @types/react @types/react-dom eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-next eslint-config-prettier eslint-plugin-jsx-a11y eslint-plugin-prettier eslint-plugin-promise eslint-plugin-react eslint-plugin-react-hooks husky lint-staged prettier ${devInstall} ${forceCMD}`,
{
stdio: "inherit",
}
);
};
const nestPackagesInstallCmd = ({ install, devInstall, forceCMD }) => {
execSync(
`${install} eslint-config-prettier eslint-plugin-prettier eslint-plugin-promise husky lint-staged prettier ${devInstall} ${forceCMD}`,
{
stdio: "inherit",
}
);
};
const angularPackagesInstallCmd = ({ install, devInstall, forceCMD }) => {
execSync(
`${install} @types/node eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin @angular-eslint/builder @angular-eslint/eslint-plugin @angular-eslint/eslint-plugin-template @angular-eslint/schematics @angular-eslint/template-parser husky lint-staged prettier ${devInstall} ${forceCMD}`,
{
stdio: "inherit",
}
);
};
const reactEslintConfigCmd = () => {
const configPath = resolve(__dirname, `./config/react/.eslintrc.json`);
const configPathForPrettier = resolve(
__dirname,
`./config/react/.prettierrc.json`
);
const configPathForLintStage = resolve(
__dirname,
`./config/react/.lintstagedrc.json`
);
copyFileSync(configPath, ".eslintrc.json");
copyFileSync(configPathForPrettier, ".prettierrc.json");
copyFileSync(configPathForLintStage, ".lintstagedrc.json");
};
const nestEslintConfigCmd = () => {
const configPathForLint = resolve(__dirname, `./config/nestjs/.eslintrc.js`);
const configPathForPrettier = resolve(
__dirname,
`./config/nestjs/.prettierrc`
);
const configPathForLintStage = resolve(
__dirname,
`./config/nestjs/.lintstagedrc.json`
);
copyFileSync(configPathForLint, ".eslintrc.js");
copyFileSync(configPathForPrettier, ".prettierrc");
copyFileSync(configPathForLintStage, ".lintstagedrc.json");
};
const angularEslintConfigCmd = () => {
const configPath = resolve(__dirname, `./config/angular/.eslintrc.json`);
const configPathForPrettier = resolve(
__dirname,
`./config/angular/.prettierrc.json`
);
const configPathForLintStage = resolve(
__dirname,
`./config/angular/.lintstagedrc.json`
);
copyFileSync(configPath, ".eslintrc.json");
copyFileSync(configPathForPrettier, ".prettierrc.json");
copyFileSync(configPathForLintStage, ".lintstagedrc.json");
};
const packagesInstallCmds = {
react: reactPackagesInstallCmd,
nestjs: nestPackagesInstallCmd,
angular: angularPackagesInstallCmd,
};
const eslintConfigCmds = {
react: reactEslintConfigCmd,
nestjs: nestEslintConfigCmd,
angular: angularEslintConfigCmd,
};
async function init() {
const packageManager = await select({
message: "Select a package manager",
choices: [
{
name: "npm",
value: "npm",
},
{
name: "yarn",
value: "yarn",
},
{
name: "pnpm",
value: "pnpm",
},
],
});
const technology = await select({
message: "Select a technology",
choices: [
{
name: "Nextjs",
value: "react",
},
{
name: "Angular",
value: "angular",
},
{
name: "NestJs",
value: "nestjs",
},
],
});
const isForceCmd = process?.argv.some((arg) => arg === "--force");
const commandsForPackageManager = {
npm: {
install: "npm install",
devInstall: "--save-dev",
uninstall: "npm uninstall",
},
yarn: {
install: "yarn add",
devInstall: "--dev",
uninstall: "yarn remove",
},
pnpm: {
install: "pnpm add",
devInstall: "--save-dev",
uninstall: "pnpm remove",
},
};
const forceCMD = isForceCmd ? "--force" : "";
const { install, uninstall, devInstall } =
commandsForPackageManager[packageManager];
const installRequiredPackages = packagesInstallCmds[technology];
console.log("Installing required plugins...");
installRequiredPackages({ install, forceCMD, devInstall });
const eslintRunCmds = {
react: 'next lint --fix',
nestjs: 'eslint \"{src,apps,libs,test}/**/*.ts\" --fix',
angular: 'ng lint',
};
execSync(`npm pkg set scripts.lint="${eslintRunCmds[technology]}"`, {
stdio: "inherit",
});
console.log("Required plugins installed successfully.");
console.log("Adding husky script");
execSync(`npm pkg set scripts.prepare="husky install"`, {
stdio: "inherit",
});
console.log("Running husky script");
execSync(`npm run prepare`, {
stdio: "inherit",
});
execSync(`npm pkg delete scripts.prepare`, {
stdio: "inherit",
});
const configNames = [".gitattributes", ".prettierignore"];
console.log("Coping configuration files");
const eslintConfigCmd = eslintConfigCmds[technology];
eslintConfigCmd();
configNames.forEach((configName) => {
const configPath = resolve(__dirname, `./config/common/${configName}`);
copyFileSync(configPath, configName);
});
const preCommitConfigPath = resolve(__dirname, "./config/common/pre-commit");
copyFileSync(preCommitConfigPath, "./.husky/pre-commit");
mkdir("./.vscode", () => { });
const vscodeConfigPath = resolve(__dirname, "./config/common/settings.json");
const vscodeExtensionsPath = resolve(
__dirname,
"./config/common/extensions.json"
);
copyFileSync(vscodeConfigPath, "./.vscode/settings.json");
copyFileSync(vscodeExtensionsPath, "./.vscode/extensions.json");
console.log("configuration files copied successfully.");
execSync(`${uninstall} ${packageName} ${forceCMD}`, {
stdio: "inherit",
});
console.log("Configuration completed successfully.");
}
const cmds = {
init,
};
try {
const [, , cmd] = process.argv;
cmds[cmd] ? cmds[cmd]() : help(0);
} catch (error) {
console.error("Configuration failed:", error);
process.exit(1);
}