mwalajs 1.1.13 → 1.1.14
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/createProject.mjs +111 -0
- package/package.json +1 -1
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// createProject.mjs
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import readline from 'readline';
|
|
5
|
+
import os from 'os';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Detect base source path
|
|
9
|
+
*/
|
|
10
|
+
function getMwalajsPath() {
|
|
11
|
+
const envPath = process.env.MWALAJSPATH;
|
|
12
|
+
|
|
13
|
+
if (envPath && fs.existsSync(envPath)) return envPath;
|
|
14
|
+
|
|
15
|
+
const defaultPaths = {
|
|
16
|
+
win32: 'C:\\Program Files\\mwalajs',
|
|
17
|
+
linux: '/usr/local/lib/mwalajs',
|
|
18
|
+
darwin: '/usr/local/lib/mwalajs'
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const fallback = defaultPaths[os.platform()];
|
|
22
|
+
if (fs.existsSync(fallback)) return fallback;
|
|
23
|
+
|
|
24
|
+
console.warn("⚠ mwalajs source path not found. Using current directory.");
|
|
25
|
+
return process.cwd();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Ask helper
|
|
30
|
+
*/
|
|
31
|
+
function askQuestion(rl, question) {
|
|
32
|
+
return new Promise(resolve => {
|
|
33
|
+
rl.question(question, answer => resolve(answer.trim()));
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Create project
|
|
39
|
+
*/
|
|
40
|
+
export async function createProject(projectArg) {
|
|
41
|
+
const rl = readline.createInterface({
|
|
42
|
+
input: process.stdin,
|
|
43
|
+
output: process.stdout
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
// ✅ ONLY ONE SOURCE OF INPUT
|
|
48
|
+
let projectName = projectArg?.trim();
|
|
49
|
+
|
|
50
|
+
if (!projectName) {
|
|
51
|
+
projectName = await askQuestion(rl, "Enter the name of the new project: ");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!projectName) {
|
|
55
|
+
console.error("❌ Project name cannot be empty.");
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const currentDir = process.cwd();
|
|
60
|
+
const newProjectPath = path.join(currentDir, projectName);
|
|
61
|
+
|
|
62
|
+
const mwalajsSourcePath = getMwalajsPath();
|
|
63
|
+
|
|
64
|
+
if (fs.existsSync(newProjectPath)) {
|
|
65
|
+
console.error(`❌ Error: Folder '${projectName}' already exists.`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
console.log(`Creating folder: ${newProjectPath}`);
|
|
70
|
+
fs.mkdirSync(newProjectPath, { recursive: true });
|
|
71
|
+
|
|
72
|
+
const itemsToCopy = [
|
|
73
|
+
"app.mjs",
|
|
74
|
+
"controllers",
|
|
75
|
+
"migrations",
|
|
76
|
+
"routes",
|
|
77
|
+
"views",
|
|
78
|
+
"middlewares",
|
|
79
|
+
"models",
|
|
80
|
+
"README.md",
|
|
81
|
+
"public"
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
for (const item of itemsToCopy) {
|
|
85
|
+
const src = path.join(mwalajsSourcePath, item);
|
|
86
|
+
const dest = path.join(newProjectPath, item);
|
|
87
|
+
|
|
88
|
+
if (fs.existsSync(src)) {
|
|
89
|
+
console.log(`Copying '${item}'...`);
|
|
90
|
+
fs.copySync(src, dest);
|
|
91
|
+
} else {
|
|
92
|
+
console.warn(`⚠ '${item}' not found. Skipping...`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
console.log(`\n✅ Project '${projectName}' created successfully!`);
|
|
97
|
+
console.log(`📁 Location: ${newProjectPath}`);
|
|
98
|
+
|
|
99
|
+
} catch (err) {
|
|
100
|
+
console.error("❌ Project creation failed:", err.message);
|
|
101
|
+
} finally {
|
|
102
|
+
rl.close();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* CLI run support
|
|
108
|
+
*/
|
|
109
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
110
|
+
createProject(process.argv[2]);
|
|
111
|
+
}
|