create-prisma-php-app 1.6.5 → 1.6.7
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 +147 -1
- package/dist/prisma-php.js +29 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,2 +1,148 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{execSync}from"child_process";import fs from"fs";import{fileURLToPath}from"url";import path from"path";import chalk from"chalk";import prompts from"prompts";import https from"https";const __filename=fileURLToPath(import.meta.url),__dirname=path.dirname(__filename);function configureBrowserSyncCommand(e,
|
|
2
|
+
import{execSync}from"child_process";import fs from"fs";import{fileURLToPath}from"url";import path from"path";import chalk from"chalk";import prompts from"prompts";import https from"https";const __filename=fileURLToPath(import.meta.url),__dirname=path.dirname(__filename);function configureBrowserSyncCommand(e,t){const s=t.PROJECT_ROOT_PATH.indexOf("\\htdocs\\");if(-1===s)return"";const n=t.PROJECT_ROOT_PATH.substring(0,s+"\\htdocs\\".length).replace(/\\/g,"\\\\"),i=t.PROJECT_ROOT_PATH.replace(new RegExp(`^${n}`),"").replace(/\\/g,"/");let r=`http://localhost/${i}`;r=r.endsWith("/")?r.slice(0,-1):r;const c=r.replace(/(?<!:)(\/\/+)/g,"/"),o=i.replace(/\/\/+/g,"/"),a=`\n const { createProxyMiddleware } = require("http-proxy-middleware");\n\n module.exports = {\n // First middleware: Set Cache-Control headers\n function (req, res, next) {\n res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");\n res.setHeader("Pragma", "no-cache");\n res.setHeader("Expires", "0");\n next();\n },\n // Use the 'middleware' option to create a proxy that masks the deep URL.\n middleware: [\n // This middleware intercepts requests to the root and proxies them to the deep path.\n createProxyMiddleware("/", {\n target:\n "${c}",\n changeOrigin: true,\n pathRewrite: {\n "^/": "/${o.startsWith("/")?o.substring(1):o}", // Rewrite the path.\n },\n }),\n ],\n proxy: "http://localhost:3000", // Proxy the BrowserSync server.\n // serveStatic: ["src/app"], // Serve static files from this directory.\n files: "src/**/*.*",\n notify: false,\n open: false,\n ghostMode: false,\n };`,p=path.join(e,"settings","bs-config.cjs");return fs.writeFileSync(p,a,"utf8"),"browser-sync start --config settings/bs-config.cjs"}async function updatePackageJson(e,t,s){const n=path.join(e,"package.json"),i=JSON.parse(fs.readFileSync(n,"utf8")),r=configureBrowserSyncCommand(e,t);i.scripts=Object.assign(Object.assign({},i.scripts),{postinstall:"prisma generate"});let c=[];s.tailwindcss&&(i.scripts=Object.assign(Object.assign({},i.scripts),{tailwind:"postcss ./src/app/css/tailwind.css -o ./src/app/css/styles.css --watch"}),c.push("tailwind")),s.websocket&&(i.scripts=Object.assign(Object.assign({},i.scripts),{websocket:"node ./settings/restartWebsocket.cjs"}),c.push("websocket"));const o=Object.assign({},i.scripts);c.length>0&&(o["browser-sync"]=r),o.dev=c.length>0?`npm-run-all --parallel browser-sync ${c.join(" ")}`:r,i.scripts=o,i.type="module",i.prisma={seed:"node prisma/seed.js"},fs.writeFileSync(n,JSON.stringify(i,null,2))}async function updateComposerJson(e,t){if(!t.websocket)return;const s=path.join(e,"composer.json");let n;if(fs.existsSync(s)){{const e=fs.readFileSync(s,"utf8");n=JSON.parse(e)}t.websocket&&(n.require=Object.assign(Object.assign({},n.require),{"cboden/ratchet":"^0.4.4"})),fs.writeFileSync(s,JSON.stringify(n,null,2))}}async function updateIndexJsForWebSocket(e,t){if(!t.websocket)return;const s=path.join(e,"src","app","js","index.js");let n=fs.readFileSync(s,"utf8");n+='\n// WebSocket initialization\nconst ws = new WebSocket("ws://localhost:8080");\n',fs.writeFileSync(s,n,"utf8")}async function createUpdateGitignoreFile(e,t){const s=path.join(e,".gitignore");let n="";fs.existsSync(s)&&(n=fs.readFileSync(s,"utf8")),t.forEach((e=>{n.includes(e)||(n+=`\n${e}`)})),n=n.trimStart(),fs.writeFileSync(s,n)}function copyRecursiveSync(e,t){const s=fs.existsSync(e),n=s&&fs.statSync(e);s&&n&&n.isDirectory()?(fs.mkdirSync(t,{recursive:!0}),fs.readdirSync(e).forEach((s=>copyRecursiveSync(path.join(e,s),path.join(t,s))))):fs.copyFileSync(e,t)}async function executeCopy(e,t){t.forEach((({srcDir:t,destDir:s})=>{copyRecursiveSync(path.join(__dirname,t),path.join(e,s))}))}function modifyTailwindConfig(e){const t=path.join(e,"tailwind.config.js");let s=fs.readFileSync(t,"utf8");const n=["./src/app/**/*.{html,js,php}"].map((e=>` "${e}"`)).join(",\n");s=s.replace(/content: \[\],/g,`content: [\n${n}\n],`),fs.writeFileSync(t,s,"utf8")}function modifyPostcssConfig(e){const t=path.join(e,"postcss.config.js");fs.writeFileSync(t,"export default {\n plugins: {\n tailwindcss: {},\n autoprefixer: {},\n cssnano: {},\n },\n};","utf8")}function modifyIndexPHP(e,t){const s=path.join(e,"src","app","layout.php");try{let e=fs.readFileSync(s,"utf8");const n='\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>',i=t?` <link href="<?php echo $baseUrl; ?>css/styles.css" rel="stylesheet"> ${n}`:` <script src="https://cdn.tailwindcss.com"><\/script> ${n}`;e=e.replace("</head>",`${i}\n</head>`),fs.writeFileSync(s,e,"utf8")}catch(e){}}async function updateOrCreateEnvFile(e,t){const s=path.join(e,".env");let n=fs.existsSync(s)?fs.readFileSync(s,"utf8"):"";n+=`${""!==n?"\n\n":""}${t}`,fs.writeFileSync(s,n)}async function createDirectoryStructure(e,t,s){[{src:"/bootstrap.php",dest:"/bootstrap.php"},{src:"/.htaccess",dest:"/.htaccess"},{src:"/../composer.json",dest:"/composer.json"}].forEach((({src:t,dest:s})=>{const n=path.join(__dirname,t),i=path.join(e,s),r=fs.readFileSync(n,"utf8");fs.writeFileSync(i,r)})),await executeCopy(e,[{srcDir:"/settings",destDir:"/settings"},{srcDir:"/prisma",destDir:"/prisma"},{srcDir:"/src",destDir:"/src"},{srcDir:"/../vendor",destDir:"/vendor"}]),await updatePackageJson(e,s,t),await updateComposerJson(e,t),await updateIndexJsForWebSocket(e,t),t.tailwindcss?(modifyTailwindConfig(e),modifyIndexPHP(e,!0),modifyPostcssConfig(e)):modifyIndexPHP(e,!1);await updateOrCreateEnvFile(e,'# PHPMailer\nSMTP_HOST=\nSMTP_USERNAME=\nSMTP_PASSWORD=\nSMTP_PORT=\nSMTP_ENCRYPTION=ssl\nMAIL_FROM=\nMAIL_FROM_NAME=""'),await createUpdateGitignoreFile(e,["vendor"])}async function getAnswer(e={}){const t=[...e.projectName?[]:[{type:"text",name:"projectName",message:"What is your project named?",initial:"my-app"}],...e.projectName?[]:[{type:"toggle",name:"tailwindcss",message:`Would you like to use ${chalk.blue("Tailwind CSS")}?`,initial:!0,active:"Yes",inactive:"No"}],...e.projectName?[]:[{type:"toggle",name:"websocket",message:`Would you like to use ${chalk.blue("Websocket")}?`,initial:!0,active:"Yes",inactive:"No"}]],s=()=>!1;try{const e=await prompts(t,{onCancel:s});return 0===Object.keys(e).length?null:{projectName:String(e.projectName).trim().replace(/ /g,"-"),tailwindcss:e.tailwindcss,websocket:e.websocket}}catch(e){return null}}async function installDependencies(e,t,s=!1){execSync("npm init -y",{stdio:"inherit",cwd:e}),t.forEach((e=>{}));const n=`npm install ${s?"--save-dev":""} ${t.join(" ")}`;execSync(n,{stdio:"inherit",cwd:e})}function fetchPackageVersion(e){return new Promise(((t,s)=>{https.get(`https://registry.npmjs.org/${e}`,(e=>{let n="";e.on("data",(e=>n+=e)),e.on("end",(()=>{try{const e=JSON.parse(n);t(e["dist-tags"].latest)}catch(e){s(new Error("Failed to parse JSON response"))}}))})).on("error",(e=>s(e)))}))}
|
|
3
|
+
async function main() {
|
|
4
|
+
try {
|
|
5
|
+
const args = process.argv.slice(2);
|
|
6
|
+
let projectName = args[0];
|
|
7
|
+
console.log("🚀 ~ main ~ projectName:", projectName);
|
|
8
|
+
let answer = null;
|
|
9
|
+
if (projectName) {
|
|
10
|
+
let useTailwind = args.includes("--tailwind");
|
|
11
|
+
let useWebsocket = args.includes("--websocket");
|
|
12
|
+
const predefinedAnswers = {
|
|
13
|
+
projectName,
|
|
14
|
+
tailwindcss: useTailwind,
|
|
15
|
+
websocket: useWebsocket,
|
|
16
|
+
};
|
|
17
|
+
const response = await getAnswer(predefinedAnswers);
|
|
18
|
+
projectName = response.projectName;
|
|
19
|
+
useTailwind = useTailwind || response.tailwindcss;
|
|
20
|
+
useWebsocket = useWebsocket || response.websocket;
|
|
21
|
+
answer = {
|
|
22
|
+
projectName,
|
|
23
|
+
tailwindcss: useTailwind,
|
|
24
|
+
websocket: useWebsocket,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
answer = await getAnswer();
|
|
29
|
+
}
|
|
30
|
+
if (answer === null) {
|
|
31
|
+
console.log(chalk.red("Installation cancelled."));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
// execSync(`npm install -g create-prisma-php-app`, { stdio: "inherit" }); // TODO: Uncomment this line before publishing the package
|
|
35
|
+
execSync(`npm install -g create-prisma-php-app@alpha-update-command`, { stdio: "inherit" }); // TODO: Uncomment this line before publishing the package
|
|
36
|
+
// Support for browser-sync
|
|
37
|
+
execSync(`npm install -g browser-sync`, { stdio: "inherit" });
|
|
38
|
+
// Create the project directory
|
|
39
|
+
fs.mkdirSync(answer.projectName);
|
|
40
|
+
const projectPath = path.join(process.cwd(), answer.projectName);
|
|
41
|
+
process.chdir(answer.projectName);
|
|
42
|
+
const dependencies = [
|
|
43
|
+
"prisma",
|
|
44
|
+
"@prisma/client",
|
|
45
|
+
"typescript",
|
|
46
|
+
"@types/node",
|
|
47
|
+
"ts-node",
|
|
48
|
+
"http-proxy-middleware",
|
|
49
|
+
];
|
|
50
|
+
if (answer.tailwindcss) {
|
|
51
|
+
dependencies.push("tailwindcss", "autoprefixer", "postcss", "postcss-cli", "cssnano");
|
|
52
|
+
}
|
|
53
|
+
if (answer.websocket) {
|
|
54
|
+
dependencies.push("chokidar-cli");
|
|
55
|
+
}
|
|
56
|
+
if (answer.tailwindcss || answer.websocket) {
|
|
57
|
+
dependencies.push("npm-run-all");
|
|
58
|
+
}
|
|
59
|
+
await installDependencies(projectPath, dependencies, true);
|
|
60
|
+
execSync(`npx prisma init`, { stdio: "inherit" });
|
|
61
|
+
execSync(`npx tsc --init`, { stdio: "inherit" });
|
|
62
|
+
if (answer.tailwindcss) {
|
|
63
|
+
execSync(`npx tailwindcss init -p`, { stdio: "inherit" });
|
|
64
|
+
}
|
|
65
|
+
const projectSettings = {
|
|
66
|
+
PROJECT_NAME: answer.projectName,
|
|
67
|
+
PROJECT_ROOT_PATH: projectPath.replace(/\\/g, "\\\\"),
|
|
68
|
+
PHP_ROOT_PATH_EXE: "D:\\\\xampp\\\\php\\\\php.exe",
|
|
69
|
+
PHP_GENERATE_CLASS_PATH: "src/lib/prisma/classes",
|
|
70
|
+
};
|
|
71
|
+
await createDirectoryStructure(projectPath, answer, projectSettings);
|
|
72
|
+
// if (answer.tailwindcss) {
|
|
73
|
+
// execSync(
|
|
74
|
+
// `npx tailwindcss -i ./src/app/css/tailwind.css -o ./src/app/css/styles.css --minify`,
|
|
75
|
+
// { stdio: "inherit" }
|
|
76
|
+
// );
|
|
77
|
+
// }
|
|
78
|
+
// execSync(`composer install`, { stdio: "inherit" });
|
|
79
|
+
// execSync(`composer dump-autoload`, { stdio: "inherit" });
|
|
80
|
+
// Create settings file
|
|
81
|
+
const settingsPath = path.join(projectPath, "settings", "project-settings.js");
|
|
82
|
+
const settingsCode = `export const projectSettings = {
|
|
83
|
+
PROJECT_NAME: "${answer.projectName}",
|
|
84
|
+
PROJECT_ROOT_PATH: "${projectPath.replace(/\\/g, "\\\\")}",
|
|
85
|
+
PHP_ROOT_PATH_EXE: "D:\\\\xampp\\\\php\\\\php.exe",
|
|
86
|
+
PHP_GENERATE_CLASS_PATH: "src/lib/prisma/classes",
|
|
87
|
+
};`;
|
|
88
|
+
fs.writeFileSync(settingsPath, settingsCode);
|
|
89
|
+
// Create public directory
|
|
90
|
+
fs.mkdirSync(path.join(projectPath, "public"));
|
|
91
|
+
// Update css if tailwindcss is not chosen by the user
|
|
92
|
+
if (!answer.tailwindcss) {
|
|
93
|
+
// delete specific files of tailwindcss if not chosen
|
|
94
|
+
const cssPath = path.join(projectPath, "src", "app", "css");
|
|
95
|
+
const tailwindFiles = ["tailwind.css", "styles.css"];
|
|
96
|
+
tailwindFiles.forEach((file) => {
|
|
97
|
+
const filePath = path.join(cssPath, file);
|
|
98
|
+
if (fs.existsSync(filePath)) {
|
|
99
|
+
fs.unlinkSync(filePath); // Delete each file if it exists
|
|
100
|
+
console.log(`${file} was deleted successfully.`);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
console.log(`${file} does not exist.`);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
// Update websocket if not chosen by the user
|
|
108
|
+
if (!answer.websocket) {
|
|
109
|
+
const wsPath = path.join(projectPath, "src", "lib", "websocket");
|
|
110
|
+
// Check if the websocket directory exists
|
|
111
|
+
if (fs.existsSync(wsPath)) {
|
|
112
|
+
// Use fs.rmSync with recursive option set to true to delete the directory and its contents
|
|
113
|
+
fs.rmSync(wsPath, { recursive: true, force: true }); // force option is not necessary but can be used to ensure deletion
|
|
114
|
+
console.log("Websocket directory was deleted successfully.");
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
console.log("Websocket directory does not exist.");
|
|
118
|
+
}
|
|
119
|
+
// Update settings directory if websocket is not chosen
|
|
120
|
+
const settingsPath = path.join(projectPath, "settings");
|
|
121
|
+
const websocketFiles = ["restartWebsocket.cjs", "restart_websocket.bat"];
|
|
122
|
+
websocketFiles.forEach((file) => {
|
|
123
|
+
const filePath = path.join(settingsPath, file);
|
|
124
|
+
if (fs.existsSync(filePath)) {
|
|
125
|
+
fs.unlinkSync(filePath); // Delete each file if it exists
|
|
126
|
+
console.log(`${file} was deleted successfully.`);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
console.log(`${file} does not exist.`);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
const version = await fetchPackageVersion("create-prisma-php-app");
|
|
134
|
+
const prismaPhpConfig = {
|
|
135
|
+
projectName: answer.projectName,
|
|
136
|
+
tailwindcss: answer.tailwindcss,
|
|
137
|
+
websocket: answer.websocket,
|
|
138
|
+
version,
|
|
139
|
+
};
|
|
140
|
+
fs.writeFileSync(path.join(projectPath, "prisma-php.json"), JSON.stringify(prismaPhpConfig, null, 2));
|
|
141
|
+
console.log(`${chalk.green("Success!")} Prisma PHP project successfully created in ${answer.projectName}!`);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
console.error("Error while creating the project:", error);
|
|
145
|
+
process.exit(1);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
main();
|
package/dist/prisma-php.js
CHANGED
|
@@ -1,2 +1,30 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{spawn}from"child_process";import fs from"fs";import path from"path";import{fileURLToPath}from"url";const __filename=fileURLToPath(import.meta.url),__dirname=path.dirname(__filename),args=process.argv.slice(2),readJsonFile=e=>{const
|
|
2
|
+
import{spawn}from"child_process";import fs from"fs";import path from"path";import{fileURLToPath}from"url";const __filename=fileURLToPath(import.meta.url),__dirname=path.dirname(__filename),args=process.argv.slice(2),readJsonFile=e=>{const r=fs.readFileSync(e,"utf8");return JSON.parse(r)},executeCommand=(e,r=[],o={})=>new Promise(((s,i)=>{const t=spawn(e,r,Object.assign({stdio:"inherit",shell:!0},o));t.on("error",(e=>{i(e)})),t.on("close",(e=>{0===e?s():i(new Error(`Process exited with code ${e}`))}))}));
|
|
3
|
+
const main = async () => {
|
|
4
|
+
if (args.length > 0 && args[0] === "update") {
|
|
5
|
+
try {
|
|
6
|
+
const currentDir = process.cwd();
|
|
7
|
+
const configPath = path.join(currentDir, "prisma-php.json");
|
|
8
|
+
const localSettings = readJsonFile(configPath);
|
|
9
|
+
const commandArgs = [localSettings.projectName];
|
|
10
|
+
if (localSettings.tailwindcss)
|
|
11
|
+
commandArgs.push("--tailwindcss");
|
|
12
|
+
if (localSettings.websocket)
|
|
13
|
+
commandArgs.push("--websocket");
|
|
14
|
+
console.log("Executing command...\n");
|
|
15
|
+
await executeCommand("npx", [
|
|
16
|
+
"create-prisma-php-app@alpha-update-command",
|
|
17
|
+
...commandArgs,
|
|
18
|
+
]);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
console.error("Error in script execution:", error);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
console.log("Command not recognized.");
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
main().catch((error) => {
|
|
29
|
+
console.error("Unhandled error in main function:", error);
|
|
30
|
+
});
|