-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·216 lines (193 loc) · 6.68 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
#!/usr/local/bin/node
var projectPath = "";
var projectName = "";
var projectAuthor = "";
var projectMobileReverseDomain = "";
console.log("create-fullstack-app\n");
var exec = require('child_process').execSync;
const process = require('process');
var path = require('path');
var fs = require('fs');
var chalk = require('chalk');
var printStatus = require('./utils/print').printStatus;
var commandExists = require('command-exists').sync;
var prompt = require('prompt-sync')();
const replace = require('replace-in-file');
var failed = false;
var args = process.argv.slice(2);
if (args.length === 0 || args[0] === "." || args[0] === "./") {
projectPath = path.join(__dirname, "/");
projectName = path.basename(projectPath);
if (!/^[a-z_]+$/.test(projectName)) {
console.log(chalk.red(`${projectName} is not an valid project name! Only lowercase letters and _ are allowed!`));
return 1;
}
} else {
projectName = args[0];
if (!/^[a-z_]+$/.test(projectName)) {
console.log(chalk.red(`${projectName} is not an valid project name! Only lowercase letters and _ are allowed!`));
return 1;
}
projectPath = path.join(__dirname, args[0]);
projectPath = path.join(projectPath, "/");
if (!fs.existsSync(projectPath)) {
fs.mkdirSync(projectPath);
}
}
if (fs.readdirSync(projectPath).length !== 0) {
console.log(chalk.red(`${projectPath} is not an empty directory!`));
return 1;
}
projectAuthor = prompt("Author: ");
projectMobileReverseDomain = prompt("Mobile Reverse Domain (ex: com.example): ");
if (!/^[a-z.]+$/.test(projectMobileReverseDomain) || projectMobileReverseDomain === "") {
console.log(chalk.red(`${projectName} is not an valid reverse domain! ex: com.example`));
return 1;
}
var answer = prompt("Do you have dotnet, flutter, make, docker and nodejs installed? y/n: ");
if (answer.toUpperCase() !== "Y") {
console.log("Please install dotnet, flutter, make, docker and nodejs!")
return 1;
}
console.log();
// Checking prerequisites
printStatus("Checking prerequisites", "started");
if (!commandExists('dotnet')) {
console.log(chalk.red("dotnet missing in PATH, please install dotnet sdk: https://dotnet.microsoft.com/download/dotnet-core/3.1"))
failed = true;
}
if (!commandExists('flutter')) {
console.log(chalk.red("flutter missing in PATH, please install flutter sdk: https://flutter.dev/docs/get-started/install"))
failed = true;
}
if (!commandExists('make')) {
console.log(chalk.red("make missing in PATH"))
failed = true;
}
if (!commandExists('docker')) {
console.log(chalk.red("docker missing in PATH"))
failed = true;
}
if (failed) {
return 1;
} else {
console.log("All good!");
}
printStatus("Checking prerequisites", "complete");
console.log();
// Creating project
printStatus("Creating project", "started");
exec(`cp -a ./templates/. ${projectPath}`);
const listDir = (dir, fileList = []) => {
let files = fs.readdirSync(dir);
files.forEach(file => {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = listDir(path.join(dir, file), fileList);
}
if (/____template_placeholder____/.test(file)) {
let name = file.replace("____template_placeholder____", projectName);
let src = path.join(dir, file);
let newSrc = path.join(dir, name);
fileList.push({
oldSrc: src,
newSrc: newSrc
});
}
if (/____templatePlaceholderMRDExtension____/.test(file)) {
let name = file.replace("____templatePlaceholderMRDExtension____", projectMobileReverseDomain.split(".")[0]);
let src = path.join(dir, file);
let newSrc = path.join(dir, name);
fileList.push({
oldSrc: src,
newSrc: newSrc
});
}
if (/____templatePlaceholderMRDName____/.test(file)) {
let name = file.replace("____templatePlaceholderMRDName____", projectMobileReverseDomain.split(".")[1]);
let src = path.join(dir, file);
let newSrc = path.join(dir, name);
fileList.push({
oldSrc: src,
newSrc: newSrc
});
}
});
return fileList;
};
let foundFiles = listDir(projectPath);
foundFiles.forEach(f => {
fs.renameSync(f.oldSrc, f.newSrc);
console.log("Created: " + f.newSrc);
});
const listDirReplaceText = (dir, fileList = []) => {
let files = fs.readdirSync(dir);
files.forEach(file => {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = listDirReplaceText(path.join(dir, file), fileList);
} else {
fileList.push(path.join(dir, file));
}
});
return fileList;
};
let foundFilesReplaceText = listDirReplaceText(projectPath);
function _snake2Pascal(str) {
str += '';
str = str.split('_');
for (var i = 1; i < str.length; i++) {
str[i] = str[i].slice(0, 1).toUpperCase() + str[i].slice(1, str[i].length);
}
return str.join('');
}
foundFilesReplaceText.forEach(f => {
const options = {
files: f,
from: /____template_placeholder____/g,
to: projectName,
};
const options2 = {
files: f,
from: /____template-placeholder____/g,
to: projectName.replace("_", "-"),
};
const options3 = {
files: f,
from: /____templatePlaceholder____/g,
to: _snake2Pascal(projectName)
};
const options4 = {
files: f,
from: /____templatePlaceholderMobileReverseDomain____/g,
to: projectMobileReverseDomain,
};
try {
let results = replace.sync(options);
if (results[0].hasChanged)
console.log("Configured: " + f);
results = replace.sync(options2);
if (results[0].hasChanged)
console.log("Configured: " + f);
results = replace.sync(options3);
if (results[0].hasChanged)
console.log("Configured: " + f);
results = replace.sync(options4);
if (results[0].hasChanged)
console.log("Configured: " + f);
}
catch (error) {
console.error('Error occurred:', error);
}
});
fs.writeFileSync(path.join(projectPath, "deployment/.env"), "", function (err) {
if (err) throw err;
console.log("Created: " + path.join(projectPath, "deployment/.env"));
});
printStatus("Creating project", "complete");
console.log();
// Setup
printStatus("Setup", "started");
console.log("Executing setup.sh ...");
exec(`cd ${projectPath} && ./setup.sh`);
printStatus("Setup", "complete");
console.log("To run backend: cd " + projectName + "/deployment && make");
console.log("Frontends available in " + projectName + "/frontend");