abc-ps-cli 1.0.0

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.
Files changed (2) hide show
  1. package/index.js +160 -0
  2. package/package.json +24 -0
package/index.js ADDED
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/env node
2
+
3
+ import inquirer from 'inquirer';
4
+ import chalk from 'chalk';
5
+ import ora from 'ora';
6
+ import fs from 'fs';
7
+ import path from 'path';
8
+ import Conf from 'conf';
9
+ import { simpleGit } from 'simple-git';
10
+
11
+ const config = new Conf({ projectName: 'abc-ps-cli' });
12
+
13
+ async function main() {
14
+ console.log(chalk.blue.bold('\n🚀 ABC TECHNOLOGY - PROJECT GENERATOR (v1.3)\n'));
15
+
16
+ // CẬP NHẬT: Thêm lệnh reset thủ công (npx abc-ps-cli --reset)
17
+ if (process.argv.includes('--reset') || process.argv.includes('-r')) {
18
+ config.delete('github_token');
19
+ console.log(chalk.green('✅ Đã xóa Token cũ thành công!\n'));
20
+ }
21
+
22
+ let githubToken = config.get('github_token');
23
+ if (!githubToken) {
24
+ const { token } = await inquirer.prompt([
25
+ {
26
+ type: 'password', // Ẩn token khi nhập cho bảo mật
27
+ name: 'token',
28
+ message: chalk.yellow('Vui lòng dán GitHub Personal Access Token (PAT) để truy cập Private Repo:'),
29
+ validate: (input) => input.length > 0 || 'Token không được để trống!'
30
+ }
31
+ ]);
32
+ config.set('github_token', token);
33
+ githubToken = token;
34
+ }
35
+
36
+ // BƯỚC 1: THU THẬP THÔNG TIN
37
+ const answers = await inquirer.prompt([
38
+ {
39
+ type: 'input',
40
+ name: 'projectName',
41
+ message: 'Project name (enter "." for current folder):',
42
+ default: 'abc-new-app',
43
+ validate: (input) => {
44
+ if (input === '.') return true;
45
+ if (/^([A-Za-z\-\\_\d])+$/.test(input)) return true;
46
+ return 'Invalid project name.';
47
+ },
48
+ },
49
+ {
50
+ type: 'list',
51
+ name: 'projectType',
52
+ message: 'Select project category:',
53
+ choices: [
54
+ { name: '🌐 Web App', value: 'web-app' },
55
+ { name: '📊 Admin Portal', value: 'admin-portal' },
56
+ { name: '📱 Mobile App', value: 'mobile-app' },
57
+ { name: '⚙️ Backend', value: 'backend' },
58
+ ],
59
+ },
60
+ {
61
+ type: 'list',
62
+ name: 'framework',
63
+ message: 'Select your framework/tech-stack:',
64
+ choices: (currentAnswers) => {
65
+ if (currentAnswers.projectType === 'web-app') return ['Next.js', 'React (Vite)'];
66
+ if (currentAnswers.projectType === 'admin-portal') return ['React (Vite)', 'Next.js'];
67
+ if (currentAnswers.projectType === 'mobile-app') return ['React Native'];
68
+ if (currentAnswers.projectType === 'backend') return ['NestJS', 'ExpressJS'];
69
+ return [];
70
+ },
71
+ },
72
+ {
73
+ type: 'list',
74
+ name: 'packageManager',
75
+ message: 'Preferred package manager:',
76
+ choices: ['pnpm', 'npm', 'yarn'],
77
+ },
78
+ ]);
79
+
80
+ // BƯỚC 2: XÁC ĐỊNH REPO
81
+ const repoMapping = {
82
+ 'Next.js': 'template-fe-web-app',
83
+ 'React (Vite)': 'template-fe-admin-portal',
84
+ 'React Native': 'template-fe-mobile-app',
85
+ 'NestJS': 'template-be-nestjs',
86
+ 'ExpressJS': 'template-be-expressjs',
87
+ };
88
+
89
+ const repoName = repoMapping[answers.framework];
90
+ const authenticatedRepoUrl = `https://${githubToken}@github.com/NguyenTranTienAnh-ABC-Dev/${repoName}.git`;
91
+
92
+ const isCurrentDir = answers.projectName === '.';
93
+ const projectPath = isCurrentDir ? process.cwd() : path.join(process.cwd(), answers.projectName);
94
+ const displayProjectName = isCurrentDir ? path.basename(process.cwd()) : answers.projectName;
95
+
96
+ // BƯỚC 3: TẢI REPO
97
+ const spinner = ora(chalk.yellow(`🚀 Downloading ${answers.framework} template (Private)...`)).start();
98
+
99
+ try {
100
+ const git = simpleGit();
101
+ await git.clone(authenticatedRepoUrl, projectPath, ['--depth', '1']);
102
+
103
+ const gitFolder = path.join(projectPath, '.git');
104
+ if (fs.existsSync(gitFolder)) {
105
+ fs.rmSync(gitFolder, { recursive: true, force: true });
106
+ }
107
+
108
+ const packageJsonPath = path.join(projectPath, 'package.json');
109
+ if (fs.existsSync(packageJsonPath)) {
110
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
111
+ packageJson.name = displayProjectName;
112
+
113
+ if (answers.packageManager === 'pnpm') packageJson.packageManager = "pnpm@10.0.0";
114
+ else if (answers.packageManager === 'yarn') packageJson.packageManager = "yarn@1.22.19";
115
+ else delete packageJson.packageManager;
116
+
117
+ fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
118
+ }
119
+
120
+ const envTemplate = path.join(projectPath, '.env.template');
121
+ const envFile = path.join(projectPath, '.env');
122
+ if (fs.existsSync(envTemplate)) {
123
+ fs.copyFileSync(envTemplate, envFile);
124
+ }
125
+
126
+ spinner.succeed(chalk.green.bold(' Initialization successful!'));
127
+
128
+ // BƯỚC 4: HƯỚNG DẪN CHẠY
129
+ console.log(chalk.gray('─'.repeat(45)));
130
+ console.log(chalk.white.bold('👉 Next steps:'));
131
+ if (!isCurrentDir) console.log(chalk.cyan(` 1. cd ${answers.projectName}`));
132
+
133
+ const pm = answers.packageManager;
134
+ const installCmd = pm === 'npm' ? 'npm install' : `${pm} install`;
135
+
136
+ // TỰ ĐỘNG PHÂN BIỆT LỆNH CHẠY: Backend dùng start:dev, Frontend dùng dev
137
+ const runCmd = answers.projectType === 'backend'
138
+ ? (pm === 'npm' ? 'npm run start:dev' : `${pm} start:dev`)
139
+ : (pm === 'npm' ? 'npm run dev' : `${pm} dev`);
140
+
141
+ console.log(chalk.cyan(` 2. ${installCmd}`));
142
+ console.log(chalk.cyan(` 3. ${runCmd}`));
143
+ console.log(chalk.gray('─'.repeat(45)));
144
+
145
+ } catch (err) {
146
+ spinner.fail(chalk.red('Execution error!'));
147
+
148
+ // CẬP NHẬT: Tự động xóa token hỏng nếu gặp lỗi xác thực
149
+ if (err.message.includes('401') || err.message.includes('Authentication failed')) {
150
+ config.delete('github_token');
151
+ console.log(chalk.red('\n❌ Lỗi: GitHub Token không đúng hoặc hết hạn.'));
152
+ console.log(chalk.yellow('💡 Đã xóa token lỗi. Vui lòng chạy lại lệnh để nhập mã mới.'));
153
+ } else {
154
+ console.log(chalk.red(`Details: ${err.message}`));
155
+ }
156
+ process.exit(1);
157
+ }
158
+ }
159
+
160
+ main();
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "abc-ps-cli",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "bin": {
6
+ "abc-template": "./index.js"
7
+ },
8
+ "scripts": {
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "keywords": [],
12
+ "author": "",
13
+ "license": "ISC",
14
+ "description": "",
15
+ "dependencies": {
16
+ "chalk": "^4.1.2",
17
+ "conf": "^15.0.2",
18
+ "degit": "^2.8.4",
19
+ "inquirer": "^8.2.7",
20
+ "ora": "^9.1.0",
21
+ "simple-git": "^3.30.0"
22
+ },
23
+ "packageManager": "pnpm@10.28.2+sha512.41872f037ad22f7348e3b1debbaf7e867cfd448f2726d9cf74c08f19507c31d2c8e7a11525b983febc2df640b5438dee6023ebb1f84ed43cc2d654d2bc326264"
24
+ }