Skip to content

Commit e1810ba

Browse files
committed
feat(common): update recipe routes
1 parent d7df657 commit e1810ba

File tree

1 file changed

+290
-0
lines changed

1 file changed

+290
-0
lines changed

recipes/index.js

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
#! /usr/bin/env node
2+
3+
const program = require("commander");
4+
const inquirer = require("inquirer");
5+
const chalk = require("chalk");
6+
const shell = require("shelljs");
7+
8+
const slimConfig = require("./slim/config");
9+
const slimSnippet = require("./slim/snippets");
10+
const basicConfig = require(`./basic/config`);
11+
const basicSnippet = require(`./basic/snippets`);
12+
13+
const {
14+
log,
15+
error,
16+
createFile,
17+
tryAccess,
18+
moduleSetInstall,
19+
} = require("./utils");
20+
21+
program.option("-f, --app-name <value>", "App Name");
22+
23+
program.parse(process.argv);
24+
25+
// Create App Directory.
26+
const appName = program.args[0];
27+
28+
if (!appName) {
29+
error("App name is mandatory to create your boiler plate");
30+
}
31+
let getConfig = slimConfig.getConfig;
32+
let getModulesList = slimConfig.getModulesList;
33+
let getDevModulesList = slimConfig.getDevModulesList;
34+
let getFileContent = slimSnippet.getFileContent;
35+
let getWebPackConfig = slimSnippet.getWebPackConfig;
36+
let getDynamicSourceCode = slimSnippet.getDynamicSourceCode;
37+
let baseConfig = getConfig();
38+
39+
const baseDirPath = `./${appName}`;
40+
const defaultProjectType = "slim";
41+
let projectType = defaultProjectType;
42+
const isSlimProject = (type) => type === defaultProjectType;
43+
44+
inquirer
45+
.prompt([
46+
{
47+
type: "list",
48+
name: "projectType",
49+
message: "choose your project type",
50+
choices: ["Slim", "Basic"],
51+
default: "Slim",
52+
},
53+
])
54+
.then((mainAnswer) => {
55+
projectType = mainAnswer.projectType.toLowerCase();
56+
log(`projectType: ${projectType}`)
57+
if (!isSlimProject(projectType)) {
58+
log(`not slim - projectType: ${projectType}`)
59+
getConfig = basicConfig.getConfig;
60+
getModulesList = basicConfig.getModulesList;
61+
getDevModulesList = basicConfig.getDevModulesList;
62+
getFileContent = basicSnippet.getFileContent;
63+
getWebPackConfig = basicSnippet.getWebPackConfig;
64+
getDynamicSourceCode = basicSnippet.getDynamicSourceCode;
65+
baseConfig = getConfig();
66+
}
67+
68+
return tryAccess(baseDirPath);
69+
})
70+
.then(() => {
71+
return shell.which("npm");
72+
})
73+
.then(() => {
74+
const projectQuestions = [
75+
{
76+
type: "list",
77+
name: "portNumber",
78+
message: "choose your port number",
79+
choices: [3000, 4000, 5000, 6000, 7000],
80+
default: 7000,
81+
},
82+
];
83+
84+
if (baseConfig.canAdd.eslint) {
85+
projectQuestions.push({
86+
type: "confirm",
87+
name: "eslint",
88+
message: "do you want to add eslint?",
89+
default: true,
90+
});
91+
}
92+
93+
if (baseConfig.canAdd.prettier) {
94+
projectQuestions.push({
95+
type: "confirm",
96+
name: "prettier",
97+
message: "do you want to add prettier?",
98+
default: false,
99+
});
100+
}
101+
102+
if (baseConfig.canAdd.husky) {
103+
projectQuestions.push({
104+
type: "confirm",
105+
name: "husky",
106+
message:
107+
"do you want to add husky which enables linting and prettier on pre-commit hook?",
108+
default: false,
109+
});
110+
}
111+
112+
if (baseConfig.canAdd.hookForm) {
113+
projectQuestions.push({
114+
type: "confirm",
115+
name: "hookForm",
116+
message: "do you want to add react-hook-form?",
117+
default: false,
118+
});
119+
}
120+
121+
return inquirer.prompt(projectQuestions);
122+
})
123+
.then((answers) => {
124+
shell.mkdir(baseDirPath);
125+
shell.cd(appName);
126+
shell.exec("npm init -y", { silent: true });
127+
128+
return answers;
129+
})
130+
.then((answers) => {
131+
const babelConfigFileName = `.babelrc`;
132+
createFile(babelConfigFileName, getFileContent(babelConfigFileName));
133+
134+
createFile(
135+
"webpack.config.js",
136+
getWebPackConfig(appName, {
137+
...baseConfig,
138+
portNumber: answers.portNumber,
139+
})
140+
);
141+
142+
if (answers.eslint) {
143+
const eslintConfigFileName = `.eslintrc.json`;
144+
createFile(eslintConfigFileName, getFileContent(eslintConfigFileName));
145+
}
146+
147+
if (answers.prettier) {
148+
const prettierConfigFileName = `.prettierrc.json`;
149+
createFile(
150+
prettierConfigFileName,
151+
getFileContent(prettierConfigFileName)
152+
);
153+
}
154+
155+
shell.mkdir(baseConfig.sourceDir.main);
156+
shell.cd(baseConfig.sourceDir.main);
157+
158+
const indexFile = "index.js";
159+
createFile(indexFile, getDynamicSourceCode(indexFile, appName, baseConfig));
160+
161+
const AppFile = "App.js";
162+
createFile(AppFile, getDynamicSourceCode(AppFile, appName, baseConfig));
163+
164+
if (baseConfig.canAdd.routes) {
165+
const RoutesFile = "Routes.js";
166+
createFile(
167+
RoutesFile,
168+
getDynamicSourceCode(RoutesFile, appName, baseConfig)
169+
);
170+
}
171+
172+
const sourceSnippetDir = `${__dirname}/${projectType}/snippets/sources`;
173+
174+
if (baseConfig.canAdd.utils) {
175+
// Copy Utils.
176+
shell.cp("-Rf", `${sourceSnippetDir}/utils`, ".");
177+
}
178+
179+
if (baseConfig.canAdd.static) {
180+
// Copy Static.
181+
shell.cp("-Rf", `${sourceSnippetDir}/static`, ".");
182+
}
183+
184+
if (baseConfig.canAdd.i18n) {
185+
// Copy i18n.
186+
shell.cp("-Rf", `${sourceSnippetDir}/i18n`, ".");
187+
188+
shell.cd(baseConfig.sourceDir.i18n);
189+
const withI18n = `withI18n.js`;
190+
createFile(withI18n, getDynamicSourceCode(withI18n, appName, baseConfig));
191+
shell.cd("..");
192+
}
193+
194+
if (baseConfig.canAdd.modules) {
195+
// Copy Modules.
196+
shell.cp("-Rf", `${sourceSnippetDir}/modules`, ".");
197+
198+
shell.cd(
199+
`${baseConfig.sourceDir.containers}/${baseConfig.modules.signIn}`
200+
);
201+
const signInModule = "SignIn.js";
202+
createFile(
203+
signInModule,
204+
getDynamicSourceCode(signInModule, appName, baseConfig)
205+
);
206+
207+
shell.cd(`../${baseConfig.modules.dashboard}`);
208+
const dashboardModule = "Dashboard.js";
209+
createFile(
210+
dashboardModule,
211+
getDynamicSourceCode(dashboardModule, appName, baseConfig)
212+
);
213+
shell.cd("../../");
214+
}
215+
216+
if (baseConfig.canAdd.componentsCopy && !baseConfig.canAdd.fullComponents) {
217+
// Copy Components.
218+
shell.cp("-Rf", `${sourceSnippetDir}/components`, ".");
219+
} else if (baseConfig.canAdd.fullComponents) {
220+
// Copy Components.
221+
shell.cp("-Rf", `${sourceSnippetDir}/components`, ".");
222+
const pageLoader = "PageLoader";
223+
shell.cd(
224+
`${baseConfig.sourceDir.components}/${baseConfig.sourceDir.businessLogic}/Loader/${pageLoader}`
225+
);
226+
const pageLoaderBlock = `${pageLoader}.js`;
227+
createFile(
228+
pageLoaderBlock,
229+
getDynamicSourceCode(pageLoaderBlock, appName, baseConfig)
230+
);
231+
232+
const sidebar = "Sidebar";
233+
shell.cd(`../../Region/${sidebar}`);
234+
const sidebarBlock = `${sidebar}.js`;
235+
createFile(
236+
sidebarBlock,
237+
getDynamicSourceCode(sidebarBlock, appName, baseConfig)
238+
);
239+
240+
const topBar = "TopBar";
241+
shell.cd(`../../Region/${topBar}`);
242+
const topBarBlock = `${topBar}.js`;
243+
createFile(
244+
topBarBlock,
245+
getDynamicSourceCode(topBarBlock, appName, baseConfig)
246+
);
247+
shell.cd("../../../../");
248+
}
249+
250+
log(chalk.green.underline.bold("Installing App dependencies..."));
251+
const dependencyList = [
252+
...getModulesList(),
253+
...(answers.hookForm ? ["form"] : []),
254+
];
255+
moduleSetInstall("-S", dependencyList);
256+
257+
log(chalk.green.underline.bold("Installing App dev dependencies..."));
258+
const devDependencyList = [
259+
...getDevModulesList(),
260+
...(answers.eslint ? ["eslint"] : []),
261+
...(answers.prettier ? ["prettier"] : []),
262+
...(answers.husky ? ["husky"] : []),
263+
];
264+
moduleSetInstall("-D", devDependencyList);
265+
266+
shell.cd("..");
267+
const packageFileContent = shell.cat("package.json");
268+
const packageFileObject = JSON.parse(packageFileContent);
269+
packageFileObject.scripts = {
270+
dev: "webpack serve --mode development",
271+
build: "webpack --mode production --progress",
272+
...(answers.eslint
273+
? {
274+
lint: "eslint src --ext .js",
275+
}
276+
: {}),
277+
...(answers.prettier
278+
? {
279+
prettier: "prettier --write src",
280+
}
281+
: {}),
282+
clean: "rm -rf node_modules",
283+
};
284+
delete packageFileObject.main;
285+
shell.rm("package.json");
286+
createFile("package.json", JSON.stringify(packageFileObject, null, 2));
287+
})
288+
.catch((e) => {
289+
error(e, true);
290+
});

0 commit comments

Comments
 (0)