create-prisma-php-app 1.6.24 → 1.6.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +593 -138
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,150 +1,605 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
2
|
+
import { execSync } from "child_process";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
import path from "path";
|
|
6
|
+
import chalk from "chalk";
|
|
7
|
+
import prompts from "prompts";
|
|
8
|
+
import https from "https";
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
10
|
+
const __dirname = path.dirname(__filename);
|
|
11
|
+
function configureBrowserSyncCommand(baseDir, projectSettings) {
|
|
12
|
+
// Identify the base path dynamically up to and including 'htdocs'
|
|
13
|
+
const htdocsIndex = projectSettings.PROJECT_ROOT_PATH.indexOf("\\htdocs\\");
|
|
14
|
+
if (htdocsIndex === -1) {
|
|
15
|
+
console.error(
|
|
16
|
+
"Invalid PROJECT_ROOT_PATH. The path does not contain \\htdocs\\"
|
|
17
|
+
);
|
|
18
|
+
return ""; // Return an empty string or handle the error as appropriate
|
|
19
|
+
}
|
|
20
|
+
// Extract the path up to and including 'htdocs\\'
|
|
21
|
+
const basePathToRemove = projectSettings.PROJECT_ROOT_PATH.substring(
|
|
22
|
+
0,
|
|
23
|
+
htdocsIndex + "\\htdocs\\".length
|
|
24
|
+
);
|
|
25
|
+
// Escape backslashes for the regex pattern
|
|
26
|
+
const escapedBasePathToRemove = basePathToRemove.replace(/\\/g, "\\\\");
|
|
27
|
+
// Remove the base path and replace backslashes with forward slashes for URL compatibility
|
|
28
|
+
const relativeWebPath = projectSettings.PROJECT_ROOT_PATH.replace(
|
|
29
|
+
new RegExp(`^${escapedBasePathToRemove}`),
|
|
30
|
+
""
|
|
31
|
+
).replace(/\\/g, "/");
|
|
32
|
+
// Construct the Browser Sync command with the correct proxy URL, being careful not to affect the protocol part
|
|
33
|
+
let proxyUrl = `http://localhost/${relativeWebPath}`;
|
|
34
|
+
// Ensure the proxy URL does not end with a slash before appending '/public'
|
|
35
|
+
proxyUrl = proxyUrl.endsWith("/") ? proxyUrl.slice(0, -1) : proxyUrl;
|
|
36
|
+
// Clean the URL by replacing "//" with "/" but not affecting "http://"
|
|
37
|
+
// We replace instances of "//" that are not preceded by ":"
|
|
38
|
+
const cleanUrl = proxyUrl.replace(/(?<!:)(\/\/+)/g, "/");
|
|
39
|
+
const cleanRelativeWebPath = relativeWebPath.replace(/\/\/+/g, "/");
|
|
40
|
+
// Correct the relativeWebPath to ensure it does not start with a "/"
|
|
41
|
+
const adjustedRelativeWebPath = cleanRelativeWebPath.startsWith("/")
|
|
42
|
+
? cleanRelativeWebPath.substring(1)
|
|
43
|
+
: cleanRelativeWebPath;
|
|
44
|
+
// TypeScript content to write
|
|
45
|
+
const bsConfigTsContent = `
|
|
46
|
+
const { createProxyMiddleware } = require("http-proxy-middleware");
|
|
47
|
+
|
|
48
|
+
module.exports = {
|
|
49
|
+
// First middleware: Set Cache-Control headers
|
|
50
|
+
function (req, res, next) {
|
|
51
|
+
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
52
|
+
res.setHeader("Pragma", "no-cache");
|
|
53
|
+
res.setHeader("Expires", "0");
|
|
54
|
+
next();
|
|
55
|
+
},
|
|
56
|
+
// Use the 'middleware' option to create a proxy that masks the deep URL.
|
|
57
|
+
middleware: [
|
|
58
|
+
// This middleware intercepts requests to the root and proxies them to the deep path.
|
|
59
|
+
createProxyMiddleware("/", {
|
|
60
|
+
target:
|
|
61
|
+
"${cleanUrl}",
|
|
62
|
+
changeOrigin: true,
|
|
63
|
+
pathRewrite: {
|
|
64
|
+
"^/": "/${adjustedRelativeWebPath}", // Rewrite the path.
|
|
65
|
+
},
|
|
66
|
+
}),
|
|
67
|
+
],
|
|
68
|
+
proxy: "http://localhost:3000", // Proxy the BrowserSync server.
|
|
69
|
+
// serveStatic: ["src/app"], // Serve static files from this directory.
|
|
70
|
+
files: "src/**/*.*",
|
|
71
|
+
notify: false,
|
|
72
|
+
open: false,
|
|
73
|
+
ghostMode: false,
|
|
74
|
+
};`;
|
|
75
|
+
// Determine the path and write the bs-config.js
|
|
76
|
+
const bsConfigPath = path.join(baseDir, "settings", "bs-config.cjs");
|
|
77
|
+
fs.writeFileSync(bsConfigPath, bsConfigTsContent, "utf8");
|
|
78
|
+
// Return the Browser Sync command string, using the cleaned URL
|
|
79
|
+
return `browser-sync start --config settings/bs-config.cjs`;
|
|
80
|
+
}
|
|
81
|
+
async function updatePackageJson(baseDir, projectSettings, answer) {
|
|
82
|
+
const packageJsonPath = path.join(baseDir, "package.json");
|
|
83
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
|
84
|
+
// Use the new function to configure the Browser Sync command
|
|
85
|
+
const browserSyncCommand = configureBrowserSyncCommand(
|
|
86
|
+
baseDir,
|
|
87
|
+
projectSettings
|
|
88
|
+
);
|
|
89
|
+
packageJson.scripts = Object.assign(Object.assign({}, packageJson.scripts), {
|
|
90
|
+
postinstall: "prisma generate",
|
|
91
|
+
});
|
|
92
|
+
let answersToInclude = [];
|
|
93
|
+
if (answer.tailwindcss) {
|
|
94
|
+
packageJson.scripts = Object.assign(
|
|
95
|
+
Object.assign({}, packageJson.scripts),
|
|
96
|
+
{
|
|
97
|
+
tailwind:
|
|
98
|
+
"postcss ./src/app/css/tailwind.css -o ./src/app/css/styles.css --watch",
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
answersToInclude.push("tailwind");
|
|
102
|
+
}
|
|
103
|
+
if (answer.websocket) {
|
|
104
|
+
packageJson.scripts = Object.assign(
|
|
105
|
+
Object.assign({}, packageJson.scripts),
|
|
106
|
+
{ websocket: "node ./settings/restartWebsocket.cjs" }
|
|
107
|
+
);
|
|
108
|
+
answersToInclude.push("websocket");
|
|
109
|
+
}
|
|
110
|
+
// Initialize with existing scripts
|
|
111
|
+
const updatedScripts = Object.assign({}, packageJson.scripts);
|
|
112
|
+
// Conditionally add "browser-sync" command
|
|
113
|
+
if (answersToInclude.length > 0) {
|
|
114
|
+
updatedScripts["browser-sync"] = browserSyncCommand;
|
|
115
|
+
}
|
|
116
|
+
// Conditionally set the "dev" command
|
|
117
|
+
updatedScripts.dev =
|
|
118
|
+
answersToInclude.length > 0
|
|
119
|
+
? `npm-run-all --parallel browser-sync ${answersToInclude.join(" ")}`
|
|
120
|
+
: browserSyncCommand;
|
|
121
|
+
// Finally, assign the updated scripts back to packageJson
|
|
122
|
+
packageJson.scripts = updatedScripts;
|
|
123
|
+
packageJson.type = "module";
|
|
124
|
+
packageJson.prisma = {
|
|
125
|
+
seed: "node prisma/seed.js",
|
|
126
|
+
};
|
|
127
|
+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
128
|
+
}
|
|
129
|
+
async function updateComposerJson(baseDir, answer) {
|
|
130
|
+
if (!answer.websocket) return;
|
|
131
|
+
const composerJsonPath = path.join(baseDir, "composer.json");
|
|
132
|
+
let composerJson;
|
|
133
|
+
// Check if the composer.json file exists
|
|
134
|
+
if (fs.existsSync(composerJsonPath)) {
|
|
135
|
+
// Read the current composer.json content
|
|
136
|
+
const composerJsonContent = fs.readFileSync(composerJsonPath, "utf8");
|
|
137
|
+
composerJson = JSON.parse(composerJsonContent);
|
|
138
|
+
} else {
|
|
139
|
+
console.error("composer.json does not exist.");
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
// Conditionally add WebSocket dependency
|
|
143
|
+
if (answer.websocket) {
|
|
144
|
+
composerJson.require = Object.assign(
|
|
145
|
+
Object.assign({}, composerJson.require),
|
|
146
|
+
{ "cboden/ratchet": "^0.4.4" }
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
// Write the modified composer.json back to the file
|
|
150
|
+
fs.writeFileSync(composerJsonPath, JSON.stringify(composerJson, null, 2));
|
|
151
|
+
console.log("composer.json updated successfully.");
|
|
152
|
+
}
|
|
153
|
+
async function updateIndexJsForWebSocket(baseDir, answer) {
|
|
154
|
+
if (!answer.websocket) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const indexPath = path.join(baseDir, "src", "app", "js", "index.js");
|
|
158
|
+
let indexContent = fs.readFileSync(indexPath, "utf8");
|
|
159
|
+
// WebSocket initialization code to be appended
|
|
160
|
+
const webSocketCode = `
|
|
161
|
+
// WebSocket initialization
|
|
162
|
+
const ws = new WebSocket("ws://localhost:8080");
|
|
163
|
+
`;
|
|
164
|
+
// Append WebSocket code if user chose to use WebSocket
|
|
165
|
+
indexContent += webSocketCode;
|
|
166
|
+
fs.writeFileSync(indexPath, indexContent, "utf8");
|
|
167
|
+
console.log("WebSocket code added to index.js successfully.");
|
|
168
|
+
}
|
|
169
|
+
// This function updates the .gitignore file
|
|
170
|
+
async function createUpdateGitignoreFile(baseDir, additions) {
|
|
171
|
+
const gitignorePath = path.join(baseDir, ".gitignore");
|
|
172
|
+
// Check if the .gitignore file exists, create if it doesn't
|
|
173
|
+
let gitignoreContent = "";
|
|
174
|
+
if (fs.existsSync(gitignorePath)) {
|
|
175
|
+
gitignoreContent = fs.readFileSync(gitignorePath, "utf8");
|
|
176
|
+
}
|
|
177
|
+
additions.forEach((addition) => {
|
|
178
|
+
if (!gitignoreContent.includes(addition)) {
|
|
179
|
+
gitignoreContent += `\n${addition}`;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
// Ensure there's no leading newline if the file was just created
|
|
183
|
+
gitignoreContent = gitignoreContent.trimStart();
|
|
184
|
+
fs.writeFileSync(gitignorePath, gitignoreContent);
|
|
185
|
+
}
|
|
186
|
+
// Recursive copy function
|
|
187
|
+
function copyRecursiveSync(src, dest) {
|
|
188
|
+
const exists = fs.existsSync(src);
|
|
189
|
+
const stats = exists && fs.statSync(src);
|
|
190
|
+
const isDirectory = exists && stats && stats.isDirectory();
|
|
191
|
+
console.log("🚀 ~ copyRecursiveSync ~ isDirectory:", isDirectory);
|
|
192
|
+
if (isDirectory) {
|
|
193
|
+
if (fs.existsSync(dest)) {
|
|
194
|
+
fs.rmSync(dest, { recursive: true, force: true }); // Remove the directory if it exists
|
|
195
|
+
}
|
|
196
|
+
fs.mkdirSync(dest, { recursive: true }); // Recreate the directory
|
|
197
|
+
fs.readdirSync(src).forEach((childItemName) => {
|
|
198
|
+
copyRecursiveSync(
|
|
199
|
+
path.join(src, childItemName),
|
|
200
|
+
path.join(dest, childItemName)
|
|
201
|
+
);
|
|
202
|
+
});
|
|
203
|
+
} else {
|
|
204
|
+
fs.copyFileSync(src, dest); // This line ensures files are overwritten
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Function to execute the recursive copy for entire directories
|
|
208
|
+
async function executeCopy(baseDir, directoriesToCopy) {
|
|
209
|
+
directoriesToCopy.forEach(({ srcDir, destDir }) => {
|
|
210
|
+
const sourcePath = path.join(__dirname, srcDir);
|
|
211
|
+
const destPath = path.join(baseDir, destDir);
|
|
212
|
+
copyRecursiveSync(sourcePath, destPath);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
function modifyTailwindConfig(baseDir) {
|
|
216
|
+
const filePath = path.join(baseDir, "tailwind.config.js");
|
|
217
|
+
const newContent = [
|
|
218
|
+
"./src/app/**/*.{html,js,php}",
|
|
219
|
+
// Add more paths as needed
|
|
220
|
+
];
|
|
221
|
+
let configData = fs.readFileSync(filePath, "utf8");
|
|
222
|
+
const contentArrayString = newContent
|
|
223
|
+
.map((item) => ` "${item}"`)
|
|
224
|
+
.join(",\n");
|
|
225
|
+
configData = configData.replace(
|
|
226
|
+
/content: \[\],/g,
|
|
227
|
+
`content: [\n${contentArrayString}\n],`
|
|
228
|
+
);
|
|
229
|
+
fs.writeFileSync(filePath, configData, "utf8");
|
|
230
|
+
console.log(chalk.green("Tailwind configuration updated successfully."));
|
|
231
|
+
}
|
|
232
|
+
function modifyPostcssConfig(baseDir) {
|
|
233
|
+
const filePath = path.join(baseDir, "postcss.config.js");
|
|
234
|
+
const newContent = `export default {
|
|
235
|
+
plugins: {
|
|
236
|
+
tailwindcss: {},
|
|
237
|
+
autoprefixer: {},
|
|
238
|
+
cssnano: {},
|
|
239
|
+
},
|
|
240
|
+
};`;
|
|
241
|
+
fs.writeFileSync(filePath, newContent, "utf8");
|
|
242
|
+
console.log(chalk.green("postcss.config.js updated successfully."));
|
|
243
|
+
}
|
|
244
|
+
function modifyIndexPHP(baseDir, useTailwind) {
|
|
245
|
+
const indexPath = path.join(baseDir, "src", "app", "layout.php");
|
|
246
|
+
try {
|
|
247
|
+
let indexContent = fs.readFileSync(indexPath, "utf8");
|
|
248
|
+
const stylesAndLinks = `\n <link href="<?php echo $baseUrl; ?>css/index.css" rel="stylesheet">\n <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">\n <script src="<?php echo $baseUrl; ?>js/index.js"></script>`;
|
|
249
|
+
// Tailwind CSS link or CDN script
|
|
250
|
+
const tailwindLink = useTailwind
|
|
251
|
+
? ` <link href="<?php echo $baseUrl; ?>css/styles.css" rel="stylesheet"> ${stylesAndLinks}`
|
|
252
|
+
: ` <script src="https://cdn.tailwindcss.com"></script> ${stylesAndLinks}`;
|
|
253
|
+
// Insert before the closing </head> tag
|
|
254
|
+
indexContent = indexContent.replace("</head>", `${tailwindLink}\n</head>`);
|
|
255
|
+
fs.writeFileSync(indexPath, indexContent, "utf8");
|
|
256
|
+
console.log(
|
|
257
|
+
chalk.green(
|
|
258
|
+
`index.php modified successfully for ${
|
|
259
|
+
useTailwind ? "local Tailwind CSS" : "Tailwind CSS CDN"
|
|
260
|
+
}.`
|
|
261
|
+
)
|
|
262
|
+
);
|
|
263
|
+
} catch (error) {
|
|
264
|
+
console.error(chalk.red("Error modifying index.php:"), error);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// This function updates or creates the .env file
|
|
268
|
+
async function updateOrCreateEnvFile(baseDir, content) {
|
|
269
|
+
const envPath = path.join(baseDir, ".env");
|
|
270
|
+
let envContent = fs.existsSync(envPath)
|
|
271
|
+
? fs.readFileSync(envPath, "utf8")
|
|
272
|
+
: "";
|
|
273
|
+
envContent += `${envContent !== "" ? "\n\n" : ""}${content}`;
|
|
274
|
+
fs.writeFileSync(envPath, envContent);
|
|
275
|
+
}
|
|
276
|
+
async function createDirectoryStructure(baseDir, answer, projectSettings) {
|
|
277
|
+
const filesToCopy = [
|
|
278
|
+
{ src: "/bootstrap.php", dest: "/bootstrap.php" },
|
|
279
|
+
{ src: "/.htaccess", dest: "/.htaccess" },
|
|
280
|
+
{ src: "/../composer.json", dest: "/composer.json" },
|
|
281
|
+
];
|
|
282
|
+
// if (answer.websocket) {
|
|
283
|
+
// filesToCopy.push({
|
|
284
|
+
// src: "/../composer-websocket.lock",
|
|
285
|
+
// dest: "/composer.lock",
|
|
286
|
+
// });
|
|
287
|
+
// } else {
|
|
288
|
+
// filesToCopy.push({ src: "/../composer.lock", dest: "/composer.lock" });
|
|
289
|
+
// }
|
|
290
|
+
const directoriesToCopy = [
|
|
291
|
+
{
|
|
292
|
+
srcDir: "/settings",
|
|
293
|
+
destDir: "/settings",
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
srcDir: "/prisma",
|
|
297
|
+
destDir: "/prisma",
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
srcDir: "/src",
|
|
301
|
+
destDir: "/src",
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
srcDir: "/../vendor",
|
|
305
|
+
destDir: "/vendor",
|
|
306
|
+
},
|
|
307
|
+
];
|
|
308
|
+
console.log("🚀 ~ directoriesToCopy:", directoriesToCopy);
|
|
309
|
+
filesToCopy.forEach(({ src, dest }) => {
|
|
310
|
+
const sourcePath = path.join(__dirname, src);
|
|
311
|
+
const destPath = path.join(baseDir, dest);
|
|
312
|
+
const code = fs.readFileSync(sourcePath, "utf8");
|
|
313
|
+
fs.writeFileSync(destPath, code, { flag: "w" });
|
|
314
|
+
});
|
|
315
|
+
await executeCopy(baseDir, directoriesToCopy);
|
|
316
|
+
await updatePackageJson(baseDir, projectSettings, answer);
|
|
317
|
+
await updateComposerJson(baseDir, answer);
|
|
318
|
+
await updateIndexJsForWebSocket(baseDir, answer);
|
|
319
|
+
if (answer.tailwindcss) {
|
|
320
|
+
modifyTailwindConfig(baseDir);
|
|
321
|
+
modifyIndexPHP(baseDir, true);
|
|
322
|
+
modifyPostcssConfig(baseDir);
|
|
323
|
+
} else {
|
|
324
|
+
modifyIndexPHP(baseDir, false);
|
|
325
|
+
}
|
|
326
|
+
const envContent = `# PHPMailer
|
|
327
|
+
SMTP_HOST=
|
|
328
|
+
SMTP_USERNAME=
|
|
329
|
+
SMTP_PASSWORD=
|
|
330
|
+
SMTP_PORT=
|
|
331
|
+
SMTP_ENCRYPTION=ssl
|
|
332
|
+
MAIL_FROM=
|
|
333
|
+
MAIL_FROM_NAME=""`;
|
|
334
|
+
await updateOrCreateEnvFile(baseDir, envContent);
|
|
335
|
+
// Add vendor to .gitignore
|
|
336
|
+
await createUpdateGitignoreFile(baseDir, ["vendor"]);
|
|
337
|
+
}
|
|
338
|
+
async function getAnswer(predefinedAnswers = {}) {
|
|
339
|
+
var _a, _b, _c;
|
|
340
|
+
console.log("🚀 ~ predefinedAnswers:", predefinedAnswers);
|
|
341
|
+
const questions = [];
|
|
342
|
+
if (!predefinedAnswers.projectName) {
|
|
343
|
+
questions.push({
|
|
344
|
+
type: "text",
|
|
345
|
+
name: "projectName",
|
|
346
|
+
message: "What is your project named?",
|
|
347
|
+
initial: "my-app",
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
if (!predefinedAnswers.tailwindcss) {
|
|
351
|
+
questions.push({
|
|
352
|
+
type: "toggle",
|
|
353
|
+
name: "tailwindcss",
|
|
354
|
+
message: `Would you like to use ${chalk.blue("Tailwind CSS")}?`,
|
|
355
|
+
initial: true,
|
|
356
|
+
active: "Yes",
|
|
357
|
+
inactive: "No",
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
if (!predefinedAnswers.websocket) {
|
|
361
|
+
questions.push({
|
|
362
|
+
type: "toggle",
|
|
363
|
+
name: "websocket",
|
|
364
|
+
message: `Would you like to use ${chalk.blue("Websocket")}?`,
|
|
365
|
+
initial: true,
|
|
366
|
+
active: "Yes",
|
|
367
|
+
inactive: "No",
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
console.log("🚀 ~ questions:", questions);
|
|
371
|
+
const onCancel = () => {
|
|
372
|
+
return false;
|
|
373
|
+
};
|
|
374
|
+
try {
|
|
375
|
+
const response = await prompts(questions, { onCancel });
|
|
376
|
+
if (Object.keys(response).length === 0) {
|
|
377
|
+
return null;
|
|
378
|
+
}
|
|
379
|
+
return {
|
|
380
|
+
projectName: response.projectName
|
|
381
|
+
? String(response.projectName).trim().replace(/ /g, "-")
|
|
382
|
+
: (_a = predefinedAnswers.projectName) !== null && _a !== void 0
|
|
383
|
+
? _a
|
|
384
|
+
: "my-app",
|
|
385
|
+
tailwindcss:
|
|
386
|
+
(_b = response.tailwindcss) !== null && _b !== void 0
|
|
387
|
+
? _b
|
|
388
|
+
: predefinedAnswers.tailwindcss,
|
|
389
|
+
websocket:
|
|
390
|
+
(_c = response.websocket) !== null && _c !== void 0
|
|
391
|
+
? _c
|
|
392
|
+
: predefinedAnswers.websocket,
|
|
393
|
+
};
|
|
394
|
+
} catch (error) {
|
|
395
|
+
console.error(chalk.red("Prompt error:"), error);
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Install dependencies in the specified directory.
|
|
401
|
+
* @param {string} baseDir - The base directory where to install the dependencies.
|
|
402
|
+
* @param {string[]} dependencies - The list of dependencies to install.
|
|
403
|
+
* @param {boolean} [isDev=false] - Whether to install the dependencies as devDependencies.
|
|
404
|
+
*/
|
|
405
|
+
async function installDependencies(baseDir, dependencies, isDev = false) {
|
|
406
|
+
console.log("Initializing new Node.js project...");
|
|
407
|
+
// Initialize a package.json if it doesn't exist
|
|
408
|
+
if (!fs.existsSync(path.join(baseDir, "package.json")))
|
|
409
|
+
execSync("npm init -y", {
|
|
410
|
+
stdio: "inherit",
|
|
411
|
+
cwd: baseDir,
|
|
412
|
+
});
|
|
413
|
+
// Log the dependencies being installed
|
|
414
|
+
console.log(
|
|
415
|
+
`${
|
|
416
|
+
isDev ? "Installing development dependencies" : "Installing dependencies"
|
|
417
|
+
}:`
|
|
418
|
+
);
|
|
419
|
+
dependencies.forEach((dep) => console.log(`- ${chalk.blue(dep)}`));
|
|
420
|
+
// Prepare the npm install command with the appropriate flag for dev dependencies
|
|
421
|
+
const npmInstallCommand = `npm install ${
|
|
422
|
+
isDev ? "--save-dev" : ""
|
|
423
|
+
} ${dependencies.join(" ")}`;
|
|
424
|
+
// Execute the npm install command
|
|
425
|
+
execSync(npmInstallCommand, {
|
|
426
|
+
stdio: "inherit",
|
|
427
|
+
cwd: baseDir,
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
function fetchPackageVersion(packageName) {
|
|
431
|
+
return new Promise((resolve, reject) => {
|
|
432
|
+
https
|
|
433
|
+
.get(`https://registry.npmjs.org/${packageName}`, (res) => {
|
|
434
|
+
let data = "";
|
|
435
|
+
res.on("data", (chunk) => (data += chunk));
|
|
436
|
+
res.on("end", () => {
|
|
437
|
+
try {
|
|
438
|
+
const parsed = JSON.parse(data);
|
|
439
|
+
resolve(parsed["dist-tags"].latest);
|
|
440
|
+
} catch (error) {
|
|
441
|
+
reject(new Error("Failed to parse JSON response"));
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
})
|
|
445
|
+
.on("error", (err) => reject(err));
|
|
446
|
+
});
|
|
447
|
+
}
|
|
3
448
|
async function main() {
|
|
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
|
-
|
|
449
|
+
try {
|
|
450
|
+
const args = process.argv.slice(2);
|
|
451
|
+
let projectName = args[0];
|
|
452
|
+
console.log("🚀 ~ main ~ projectName:", projectName);
|
|
453
|
+
let answer = null;
|
|
454
|
+
if (projectName) {
|
|
455
|
+
let useTailwind = args.includes("--tailwind");
|
|
456
|
+
let useWebsocket = args.includes("--websocket");
|
|
457
|
+
const predefinedAnswers = {
|
|
458
|
+
projectName,
|
|
459
|
+
tailwindcss: useTailwind,
|
|
460
|
+
websocket: useWebsocket,
|
|
461
|
+
};
|
|
462
|
+
answer = await getAnswer(predefinedAnswers);
|
|
463
|
+
console.log("🚀 ~ main ~ answer:", answer);
|
|
464
|
+
} else {
|
|
465
|
+
answer = await getAnswer();
|
|
466
|
+
}
|
|
467
|
+
if (answer === null) {
|
|
468
|
+
console.log(chalk.red("Installation cancelled."));
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
// execSync(`npm install -g create-prisma-php-app`, { stdio: "inherit" }); // TODO: Uncomment this line before publishing the package
|
|
472
|
+
execSync(`npm install -g create-prisma-php-app@alpha-update-command`, {
|
|
473
|
+
stdio: "inherit",
|
|
474
|
+
}); // TODO: Uncomment this line before publishing the package
|
|
475
|
+
// Support for browser-sync
|
|
476
|
+
execSync(`npm install -g browser-sync`, { stdio: "inherit" });
|
|
477
|
+
// Create the project directory
|
|
478
|
+
if (!projectName) fs.mkdirSync(answer.projectName);
|
|
479
|
+
const currentDir = process.cwd();
|
|
480
|
+
console.log("🚀 ~ main ~ currentDir:", currentDir);
|
|
481
|
+
let projectPath = projectName
|
|
482
|
+
? currentDir
|
|
483
|
+
: path.join(currentDir, answer.projectName);
|
|
484
|
+
console.log("🚀 ~ main ~ projectPath:", projectPath);
|
|
485
|
+
if (!projectName) process.chdir(answer.projectName);
|
|
486
|
+
const dependencies = [
|
|
487
|
+
"prisma",
|
|
488
|
+
"@prisma/client",
|
|
489
|
+
"typescript",
|
|
490
|
+
"@types/node",
|
|
491
|
+
"ts-node",
|
|
492
|
+
"http-proxy-middleware",
|
|
493
|
+
];
|
|
494
|
+
if (answer.tailwindcss) {
|
|
495
|
+
dependencies.push(
|
|
496
|
+
"tailwindcss",
|
|
497
|
+
"autoprefixer",
|
|
498
|
+
"postcss",
|
|
499
|
+
"postcss-cli",
|
|
500
|
+
"cssnano"
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
if (answer.websocket) {
|
|
504
|
+
dependencies.push("chokidar-cli");
|
|
505
|
+
}
|
|
506
|
+
if (answer.tailwindcss || answer.websocket) {
|
|
507
|
+
dependencies.push("npm-run-all");
|
|
508
|
+
}
|
|
509
|
+
await installDependencies(projectPath, dependencies, true);
|
|
510
|
+
execSync(`npx prisma init`, { stdio: "inherit" });
|
|
511
|
+
execSync(`npx tsc --init`, { stdio: "inherit" });
|
|
512
|
+
if (answer.tailwindcss) {
|
|
513
|
+
execSync(`npx tailwindcss init -p`, { stdio: "inherit" });
|
|
514
|
+
}
|
|
515
|
+
const projectSettings = {
|
|
516
|
+
PROJECT_NAME: answer.projectName,
|
|
517
|
+
PROJECT_ROOT_PATH: projectPath.replace(/\\/g, "\\\\"),
|
|
518
|
+
PHP_ROOT_PATH_EXE: "D:\\\\xampp\\\\php\\\\php.exe",
|
|
519
|
+
PHP_GENERATE_CLASS_PATH: "src/lib/prisma/classes",
|
|
520
|
+
};
|
|
521
|
+
await createDirectoryStructure(projectPath, answer, projectSettings);
|
|
522
|
+
// if (answer.tailwindcss) {
|
|
523
|
+
// execSync(
|
|
524
|
+
// `npx tailwindcss -i ./src/app/css/tailwind.css -o ./src/app/css/styles.css --minify`,
|
|
525
|
+
// { stdio: "inherit" }
|
|
526
|
+
// );
|
|
527
|
+
// }
|
|
528
|
+
// execSync(`composer install`, { stdio: "inherit" });
|
|
529
|
+
// execSync(`composer dump-autoload`, { stdio: "inherit" });
|
|
530
|
+
// Create settings file
|
|
531
|
+
const settingsPath = path.join(
|
|
532
|
+
projectPath,
|
|
533
|
+
"settings",
|
|
534
|
+
"project-settings.js"
|
|
535
|
+
);
|
|
536
|
+
const settingsCode = `export const projectSettings = {
|
|
85
537
|
PROJECT_NAME: "${answer.projectName}",
|
|
86
538
|
PROJECT_ROOT_PATH: "${projectPath.replace(/\\/g, "\\\\")}",
|
|
87
539
|
PHP_ROOT_PATH_EXE: "D:\\\\xampp\\\\php\\\\php.exe",
|
|
88
540
|
PHP_GENERATE_CLASS_PATH: "src/lib/prisma/classes",
|
|
89
541
|
};`;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
console.log(`${file} does not exist.`);
|
|
106
|
-
}
|
|
107
|
-
});
|
|
542
|
+
fs.writeFileSync(settingsPath, settingsCode);
|
|
543
|
+
// Create public directory
|
|
544
|
+
fs.mkdirSync(path.join(projectPath, "public"));
|
|
545
|
+
// Update css if tailwindcss is not chosen by the user
|
|
546
|
+
if (!answer.tailwindcss) {
|
|
547
|
+
// delete specific files of tailwindcss if not chosen
|
|
548
|
+
const cssPath = path.join(projectPath, "src", "app", "css");
|
|
549
|
+
const tailwindFiles = ["tailwind.css", "styles.css"];
|
|
550
|
+
tailwindFiles.forEach((file) => {
|
|
551
|
+
const filePath = path.join(cssPath, file);
|
|
552
|
+
if (fs.existsSync(filePath)) {
|
|
553
|
+
fs.unlinkSync(filePath); // Delete each file if it exists
|
|
554
|
+
console.log(`${file} was deleted successfully.`);
|
|
555
|
+
} else {
|
|
556
|
+
console.log(`${file} does not exist.`);
|
|
108
557
|
}
|
|
109
|
-
|
|
110
|
-
if (!answer.websocket) {
|
|
111
|
-
const wsPath = path.join(projectPath, "src", "lib", "websocket");
|
|
112
|
-
// Check if the websocket directory exists
|
|
113
|
-
if (fs.existsSync(wsPath)) {
|
|
114
|
-
// Use fs.rmSync with recursive option set to true to delete the directory and its contents
|
|
115
|
-
fs.rmSync(wsPath, { recursive: true, force: true }); // force option is not necessary but can be used to ensure deletion
|
|
116
|
-
console.log("Websocket directory was deleted successfully.");
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
console.log("Websocket directory does not exist.");
|
|
120
|
-
}
|
|
121
|
-
// Update settings directory if websocket is not chosen
|
|
122
|
-
const settingsPath = path.join(projectPath, "settings");
|
|
123
|
-
const websocketFiles = ["restartWebsocket.cjs", "restart_websocket.bat"];
|
|
124
|
-
websocketFiles.forEach((file) => {
|
|
125
|
-
const filePath = path.join(settingsPath, file);
|
|
126
|
-
if (fs.existsSync(filePath)) {
|
|
127
|
-
fs.unlinkSync(filePath); // Delete each file if it exists
|
|
128
|
-
console.log(`${file} was deleted successfully.`);
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
console.log(`${file} does not exist.`);
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
const version = await fetchPackageVersion("create-prisma-php-app");
|
|
136
|
-
const prismaPhpConfig = {
|
|
137
|
-
projectName: answer.projectName,
|
|
138
|
-
tailwindcss: answer.tailwindcss,
|
|
139
|
-
websocket: answer.websocket,
|
|
140
|
-
version,
|
|
141
|
-
};
|
|
142
|
-
fs.writeFileSync(path.join(projectPath, "prisma-php.json"), JSON.stringify(prismaPhpConfig, null, 2));
|
|
143
|
-
console.log(`${chalk.green("Success!")} Prisma PHP project successfully created in ${answer.projectName}!`);
|
|
558
|
+
});
|
|
144
559
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
560
|
+
// Update websocket if not chosen by the user
|
|
561
|
+
if (!answer.websocket) {
|
|
562
|
+
const wsPath = path.join(projectPath, "src", "lib", "websocket");
|
|
563
|
+
// Check if the websocket directory exists
|
|
564
|
+
if (fs.existsSync(wsPath)) {
|
|
565
|
+
// Use fs.rmSync with recursive option set to true to delete the directory and its contents
|
|
566
|
+
fs.rmSync(wsPath, { recursive: true, force: true }); // force option is not necessary but can be used to ensure deletion
|
|
567
|
+
console.log("Websocket directory was deleted successfully.");
|
|
568
|
+
} else {
|
|
569
|
+
console.log("Websocket directory does not exist.");
|
|
570
|
+
}
|
|
571
|
+
// Update settings directory if websocket is not chosen
|
|
572
|
+
const settingsPath = path.join(projectPath, "settings");
|
|
573
|
+
const websocketFiles = ["restartWebsocket.cjs", "restart_websocket.bat"];
|
|
574
|
+
websocketFiles.forEach((file) => {
|
|
575
|
+
const filePath = path.join(settingsPath, file);
|
|
576
|
+
if (fs.existsSync(filePath)) {
|
|
577
|
+
fs.unlinkSync(filePath); // Delete each file if it exists
|
|
578
|
+
console.log(`${file} was deleted successfully.`);
|
|
579
|
+
} else {
|
|
580
|
+
console.log(`${file} does not exist.`);
|
|
581
|
+
}
|
|
582
|
+
});
|
|
148
583
|
}
|
|
584
|
+
const version = await fetchPackageVersion("create-prisma-php-app");
|
|
585
|
+
const prismaPhpConfig = {
|
|
586
|
+
projectName: answer.projectName,
|
|
587
|
+
tailwindcss: answer.tailwindcss,
|
|
588
|
+
websocket: answer.websocket,
|
|
589
|
+
version,
|
|
590
|
+
};
|
|
591
|
+
fs.writeFileSync(
|
|
592
|
+
path.join(projectPath, "prisma-php.json"),
|
|
593
|
+
JSON.stringify(prismaPhpConfig, null, 2)
|
|
594
|
+
);
|
|
595
|
+
console.log(
|
|
596
|
+
`${chalk.green("Success!")} Prisma PHP project successfully created in ${
|
|
597
|
+
answer.projectName
|
|
598
|
+
}!`
|
|
599
|
+
);
|
|
600
|
+
} catch (error) {
|
|
601
|
+
console.error("Error while creating the project:", error);
|
|
602
|
+
process.exit(1);
|
|
603
|
+
}
|
|
149
604
|
}
|
|
150
605
|
main();
|