alpe-temp 1.0.0 → 1.0.1
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/bin/epms.js +149 -60
- package/package.json +1 -1
package/bin/epms.js
CHANGED
|
@@ -5,38 +5,34 @@ const readline = require('readline');
|
|
|
5
5
|
const { execSync } = require('child_process');
|
|
6
6
|
|
|
7
7
|
const ROOT = path.resolve(__dirname, '..');
|
|
8
|
-
const
|
|
9
|
-
|
|
8
|
+
const CWD = process.cwd();
|
|
10
9
|
const args = process.argv.slice(2);
|
|
11
10
|
|
|
11
|
+
// ─── Help ─────────────────────────────────────────────────────────────────────
|
|
12
12
|
if (args.includes('--help') || args.includes('-h')) {
|
|
13
13
|
console.log(`
|
|
14
14
|
alpe-temp — Employee Payroll Management System (EPMS)
|
|
15
15
|
|
|
16
16
|
Usage:
|
|
17
|
-
npx alpe-temp
|
|
18
|
-
npx alpe-temp
|
|
17
|
+
npx alpe-temp Scaffold project + start server
|
|
18
|
+
npx alpe-temp init Scaffold project files only
|
|
19
|
+
npx alpe-temp seed Seed the database
|
|
20
|
+
npx alpe-temp start Start server (in existing project)
|
|
19
21
|
|
|
20
22
|
Options:
|
|
21
23
|
--db <uri> MongoDB connection string (non-interactive)
|
|
22
24
|
|
|
23
|
-
Environment variables
|
|
25
|
+
Environment variables:
|
|
24
26
|
PORT Server port (default: 3000)
|
|
25
27
|
MONGODB_URI MongoDB connection string
|
|
26
28
|
JWT_SECRET Secret for JWT signing
|
|
27
29
|
NODE_ENV Environment mode (development/production)
|
|
28
|
-
|
|
29
|
-
Requirements:
|
|
30
|
-
- Node.js >= 18
|
|
31
|
-
- MongoDB (will prompt if not found)
|
|
32
|
-
|
|
33
|
-
Visit http://localhost:3000 after starting.
|
|
34
30
|
`);
|
|
35
31
|
process.exit(0);
|
|
36
32
|
}
|
|
37
33
|
|
|
38
|
-
// ───
|
|
39
|
-
const ENV_PATH = path.join(
|
|
34
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
35
|
+
const ENV_PATH = path.join(CWD, '.env');
|
|
40
36
|
|
|
41
37
|
function loadEnv() {
|
|
42
38
|
if (!fs.existsSync(ENV_PATH)) return {};
|
|
@@ -60,7 +56,7 @@ function writeEnv(key, value) {
|
|
|
60
56
|
fs.writeFileSync(ENV_PATH, updated, 'utf-8');
|
|
61
57
|
}
|
|
62
58
|
|
|
63
|
-
async function
|
|
59
|
+
async function ask(question, defaultValue) {
|
|
64
60
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
65
61
|
return new Promise((resolve) => {
|
|
66
62
|
const hint = defaultValue ? ` (default: ${defaultValue})` : '';
|
|
@@ -86,76 +82,169 @@ async function waitForMongo(uri, timeoutMs = 5000) {
|
|
|
86
82
|
return false;
|
|
87
83
|
}
|
|
88
84
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
85
|
+
function isScaffolded(dir) {
|
|
86
|
+
return fs.existsSync(path.join(dir, 'backend-project', 'server.js'));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ─── Scaffold: copy project to CWD ────────────────────────────────────────────
|
|
90
|
+
function scaffold() {
|
|
91
|
+
if (isScaffolded(CWD)) {
|
|
92
|
+
console.log(' Project already exists in this directory.');
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
console.log('\n Setting up alpe-temp project...\n');
|
|
97
|
+
|
|
98
|
+
const skipDirs = new Set(['node_modules', '.git']);
|
|
99
|
+
const entries = fs.readdirSync(ROOT, { withFileTypes: true });
|
|
100
|
+
|
|
101
|
+
for (const entry of entries) {
|
|
102
|
+
if (skipDirs.has(entry.name)) continue;
|
|
103
|
+
const src = path.join(ROOT, entry.name);
|
|
104
|
+
const dest = path.join(CWD, entry.name);
|
|
105
|
+
if (entry.isDirectory()) {
|
|
106
|
+
fs.cpSync(src, dest, {
|
|
107
|
+
recursive: true,
|
|
108
|
+
filter: (s) => !skipDirs.has(path.basename(s)) && path.basename(s) !== 'node_modules',
|
|
109
|
+
});
|
|
110
|
+
} else if (!fs.existsSync(dest)) {
|
|
111
|
+
fs.copyFileSync(src, dest);
|
|
112
|
+
}
|
|
100
113
|
}
|
|
101
114
|
|
|
102
|
-
|
|
115
|
+
console.log(' ✓ Project files copied');
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ─── Install dependencies ─────────────────────────────────────────────────────
|
|
120
|
+
function installDeps() {
|
|
121
|
+
console.log(' Installing dependencies...');
|
|
122
|
+
execSync('npm install', { cwd: CWD, stdio: 'inherit' });
|
|
123
|
+
console.log(' ✓ Dependencies installed');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ─── Configure MongoDB ────────────────────────────────────────────────────────
|
|
127
|
+
async function configureMongo() {
|
|
103
128
|
const env = loadEnv();
|
|
104
|
-
if (env.MONGODB_URI)
|
|
129
|
+
if (env.MONGODB_URI) {
|
|
130
|
+
process.env.MONGODB_URI = env.MONGODB_URI;
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
105
133
|
|
|
106
|
-
// ─── --db flag overrides everything ────────────────────────────────────
|
|
107
134
|
const dbIndex = args.indexOf('--db');
|
|
108
135
|
if (dbIndex !== -1 && args[dbIndex + 1]) {
|
|
109
136
|
process.env.MONGODB_URI = args[dbIndex + 1];
|
|
137
|
+
writeEnv('MONGODB_URI', args[dbIndex + 1]);
|
|
138
|
+
return true;
|
|
110
139
|
}
|
|
111
140
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
);
|
|
119
|
-
if (uri) {
|
|
120
|
-
process.env.MONGODB_URI = uri;
|
|
121
|
-
writeEnv('MONGODB_URI', uri);
|
|
122
|
-
console.log(' Saved to .env');
|
|
123
|
-
}
|
|
141
|
+
console.log(' MongoDB not configured.');
|
|
142
|
+
const uri = await ask('Enter MongoDB URI', 'mongodb://127.0.0.1:27017/EPMS');
|
|
143
|
+
if (uri) {
|
|
144
|
+
process.env.MONGODB_URI = uri;
|
|
145
|
+
writeEnv('MONGODB_URI', uri);
|
|
146
|
+
console.log(' ✓ Saved to .env');
|
|
124
147
|
}
|
|
125
148
|
|
|
126
|
-
|
|
149
|
+
return !!process.env.MONGODB_URI;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ─── Verify MongoDB ───────────────────────────────────────────────────────────
|
|
153
|
+
async function verifyMongo() {
|
|
127
154
|
const uri = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/EPMS';
|
|
128
155
|
process.stdout.write(' Checking MongoDB connection...');
|
|
129
156
|
const reachable = await waitForMongo(uri);
|
|
130
157
|
if (reachable) {
|
|
131
158
|
console.log(' connected');
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
console.error('\n Unable to connect to MongoDB. Please ensure it is running.\n');
|
|
144
|
-
process.exit(1);
|
|
145
|
-
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
console.log(' failed');
|
|
162
|
+
console.log(`\n Could not connect to MongoDB at:\n ${uri}\n`);
|
|
163
|
+
const retry = await ask('Try a different URI', uri);
|
|
164
|
+
if (retry) {
|
|
165
|
+
process.env.MONGODB_URI = retry;
|
|
166
|
+
writeEnv('MONGODB_URI', retry);
|
|
167
|
+
console.log(' ✓ Saved to .env');
|
|
168
|
+
process.stdout.write(' Checking MongoDB connection...');
|
|
169
|
+
if (await waitForMongo(retry)) {
|
|
146
170
|
console.log(' connected');
|
|
147
|
-
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
console.log(' failed');
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ─── Start server ─────────────────────────────────────────────────────────────
|
|
179
|
+
function startServer() {
|
|
180
|
+
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
|
|
181
|
+
const serverEntry = path.join(CWD, 'backend-project', 'server.js');
|
|
182
|
+
if (!fs.existsSync(serverEntry)) {
|
|
183
|
+
console.error(' server.js not found. Run from an alpe-temp project directory.');
|
|
184
|
+
process.exit(1);
|
|
185
|
+
}
|
|
186
|
+
require(serverEntry);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// ─── Main ─────────────────────────────────────────────────────────────────────
|
|
190
|
+
async function main() {
|
|
191
|
+
// Seed
|
|
192
|
+
if (args.includes('seed')) {
|
|
193
|
+
const cwdEnv = loadEnv();
|
|
194
|
+
if (cwdEnv.MONGODB_URI) process.env.MONGODB_URI = cwdEnv.MONGODB_URI;
|
|
195
|
+
if (!process.env.MONGODB_URI) await configureMongo();
|
|
196
|
+
console.log('Seeding database...');
|
|
197
|
+
const seedEntry = path.join(isScaffolded(CWD) ? CWD : ROOT, 'backend-project', 'src', 'seed.js');
|
|
198
|
+
execSync(`node "${seedEntry}"`, { cwd: isScaffolded(CWD) ? CWD : ROOT, stdio: 'inherit' });
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Init: scaffold only
|
|
203
|
+
if (args.includes('init')) {
|
|
204
|
+
scaffold();
|
|
205
|
+
if (isScaffolded(CWD)) {
|
|
206
|
+
installDeps();
|
|
207
|
+
console.log('\n ✔ Project ready! Run:\n npm start\n');
|
|
208
|
+
}
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Start: just start the server (assume already scaffolded)
|
|
213
|
+
if (args.includes('start')) {
|
|
214
|
+
if (!isScaffolded(CWD)) {
|
|
215
|
+
console.error(' No alpe-temp project found in this directory. Run `npx alpe-temp init` first.');
|
|
216
|
+
process.exit(1);
|
|
217
|
+
}
|
|
218
|
+
const env = loadEnv();
|
|
219
|
+
if (env.MONGODB_URI) process.env.MONGODB_URI = env.MONGODB_URI;
|
|
220
|
+
if (!process.env.MONGODB_URI && !await configureMongo()) {
|
|
221
|
+
console.error(' MongoDB URI is required.');
|
|
222
|
+
process.exit(1);
|
|
223
|
+
}
|
|
224
|
+
if (!(await verifyMongo())) {
|
|
148
225
|
console.error('\n Unable to connect to MongoDB. Please ensure it is running.\n');
|
|
149
226
|
process.exit(1);
|
|
150
227
|
}
|
|
228
|
+
startServer();
|
|
229
|
+
return;
|
|
151
230
|
}
|
|
152
231
|
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
232
|
+
// Default: scaffold → install → configure → start
|
|
233
|
+
const justScaffolded = scaffold();
|
|
234
|
+
if (justScaffolded) installDeps();
|
|
235
|
+
if (!isScaffolded(CWD)) {
|
|
236
|
+
console.error(' Project setup failed.');
|
|
237
|
+
process.exit(1);
|
|
238
|
+
}
|
|
239
|
+
await configureMongo();
|
|
240
|
+
if (!(await verifyMongo())) {
|
|
241
|
+
console.error('\n Unable to connect to MongoDB.\n');
|
|
242
|
+
process.exit(1);
|
|
243
|
+
}
|
|
244
|
+
startServer();
|
|
156
245
|
}
|
|
157
246
|
|
|
158
247
|
main().catch((err) => {
|
|
159
|
-
console.error('Failed
|
|
248
|
+
console.error('Failed:', err.message);
|
|
160
249
|
process.exit(1);
|
|
161
250
|
});
|