-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
168 lines (142 loc) · 4.62 KB
/
setup.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
const prompts = require("prompts");
const fs = require("fs");
const { execSync } = require("child_process");
const defaultGitRepository = "[email protected]:Deveosys/symfony-react-bootstrap.git";
const steps = [
{
type: "text",
name: "appName",
message: "App name",
validate: (value) => (value == "" ? `App name is required` : true),
},
{
type: "select",
name: "symfonyProject",
message: "Symfony project generation",
choices: [
{ title: "Generic", description: "Pull the generic Deveosys symfony 4 boilerplate" },
{ title: "Yours from a repo", description: "You'll be prompted for a git repository" },
{ title: "Custom", description: "Nothing will be placed in app/ folder." },
],
initial: 0,
},
{
type: (prev) => (prev == 1 ? "text" : null),
name: "customGitRepo",
message: "Your git repository",
},
];
const init = async () => {
console.log("\nDocker Symfony configuration");
console.log("-----------------------------\n");
const response = await prompts(steps);
if (!response.appName || response.symfonyProject == undefined) {
logStateMessage("\x1b[31m\nSetup ended befor finish \n\x1b[89m\x1b[0m");
return;
}
const appName = response.appName.toLowerCase().replace(/ /g, "_");
generateDockerCompose(appName);
adaptNginxConfiguration(appName);
if (response.symfonyProject < 2) {
const repository = response.customGitRepo || defaultGitRepository;
cloneGitRepository(repository);
} else {
logStateMessage("\x1b[33m\nNo symfony project generated, you have to do it in ./app directory.\x1b[89m\x1b[0m");
}
removeCurrentGit();
console.log(
"\x1b[32m\nDocker Symfony project " + response.appName + " generated and ready to use.\n\x1b[89m\x1b[0m"
);
secondPart();
};
const secondPart = async () => {
let response = await prompts({
type: "confirm",
name: "gitInit",
message: "Do you want to generate a new git repository ? (git init)",
initial: true,
});
if (response.gitInit == undefined) {
logStateMessage("\x1b[31m\nSetup ended befor finish \n\x1b[89m\x1b[0m");
return;
}
if (response.gitInit) gitInit();
console.log("\n\n----------------- Prod -----------------\n");
console.log(" $ docker-compose up --build -d");
console.log("\n----------------- Dev -----------------\n");
console.log(" $ cd app && composer install && yarn && php bin/console doctrine:migrations:migrate");
console.log(" $ symfony server:start");
console.log(" $ yarn dev-server\n\n");
response = await prompts({
type: "confirm",
name: "callDevCommands",
message: "Do you want to prepare Symfony project for development ?",
initial: true,
});
if (response.callDevCommands == undefined) {
logStateMessage("\x1b[31m\nSetup ended befor finish \n\x1b[89m\x1b[0m");
return;
}
if (response.callDevCommands) callDevCommands();
console.log("\x1b[32m\n\n" + "***************** ENJOY ! *****************" + "\n\x1b[89m\x1b[0m");
};
const generateDockerCompose = (appName) => {
logStateMessage("\nAdapting docker-compose.yml...");
const filePath = "./docker-compose.yml";
replaceInFile(filePath, /\$APP_NAME/g, appName);
};
const adaptNginxConfiguration = (appName) => {
logStateMessage("Adapting Nginx configuration...");
const filePath = "./nginx/conf.d/default.conf";
replaceInFile(filePath, /\$APP_NAME/g, appName);
};
const replaceInFile = (filePath, searchValue, replaceValue) => {
try {
const data = fs.readFileSync(filePath, "utf8");
const result = data.replace(searchValue, replaceValue);
fs.writeFileSync(filePath, result, "utf8");
logDone();
} catch (err) {
console.error(err);
}
};
const cloneGitRepository = (repository) => {
try {
logStateMessage("Cloning from " + repository + " ...\n");
execSync("git clone " + repository + " tmp_app");
logDone();
logStateMessage("Prepare Symfony project...");
execSync("mv app/Dockerfile tmp_app");
execSync("rm -rf app");
execSync("mv tmp_app app");
execSync("rm -rf app/.git");
logDone();
} catch (error) {
console.error(error);
}
};
const removeCurrentGit = () => {
logStateMessage("Removing current Git project...");
execSync("rm -rf .git");
logDone();
};
const gitInit = () => {
logStateMessage("Generating new Git project...");
execSync("git init");
logDone();
};
const callDevCommands = () => {
logStateMessage("Symfony project preparation...\n");
execSync("cd app && composer install");
execSync("cd app && yarn");
execSync("cd app && php bin/console doctrine:migrations:migrate --no-interaction");
logDone();
};
console.clear();
init();
const logStateMessage = (message) => {
process.stdout.write(message);
};
const logDone = () => {
process.stdout.write(" \x1b[32mDone\x1b[89m\x1b[0m\n");
};