@pwc-ra/components
Version:
PwC RA shared components library
164 lines (142 loc) • 4.5 kB
JavaScript
#!/usr/bin/env node
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const inquirer = require('inquirer');
const { program } = require('commander');
const { execSync } = require('child_process');
// 版本信息
const packageJson = require('./package.json');
// 命令行参数配置
program
.version(packageJson.version)
.argument('[project-directory]', '项目目录名称')
.option('--use-npm', '使用 npm 而不是 yarn')
.option('--skip-install', '跳过依赖安装')
.parse(process.argv);
const options = program.opts();
const projectName = program.args[0];
// 主函数
async function main() {
console.log(chalk.blue('欢迎使用 PwC RA App 创建工具!'));
console.log(chalk.blue('这个工具将帮助您快速创建一个带有 MainLayout、Sidebar、Breadcrumb 和 PageHeader 组件的 React 应用。\n'));
// 如果没有提供项目名称,则提示用户输入
const { projectDirectory } = projectName
? { projectDirectory: projectName }
: await inquirer.prompt([
{
type: 'input',
name: 'projectDirectory',
message: '请输入项目目录名称:',
default: 'ra-app',
},
]);
// 项目路径
const projectPath = path.resolve(process.cwd(), projectDirectory);
// 检查目录是否已存在
if (fs.existsSync(projectPath)) {
const { overwrite } = await inquirer.prompt([
{
type: 'confirm',
name: 'overwrite',
message: `目录 ${projectDirectory} 已存在。是否覆盖?`,
default: false,
},
]);
if (!overwrite) {
console.log(chalk.yellow('操作已取消。'));
return;
}
// 清空目录
fs.emptyDirSync(projectPath);
} else {
// 创建目录
fs.mkdirSync(projectPath, { recursive: true });
}
// 创建项目
await createProject(projectPath, options);
}
// 创建项目
async function createProject(projectPath, options) {
console.log(chalk.green('\n开始创建项目...'));
// 复制模板文件
const templatePath = path.resolve(__dirname, 'template');
fs.copySync(templatePath, projectPath);
// 创建 package.json
const packageJson = {
name: path.basename(projectPath),
version: '0.1.0',
private: true,
dependencies: {
'@pwc-ra/components': '^1.0.18',
'react': '^18.2.0',
'react-dom': '^18.2.0',
'react-router-dom': '^6.22.1',
'antd': '^5.14.1',
'@ant-design/icons': '^5.2.6',
'axios': '^1.6.7',
'keycloak-js': '^26.0.0',
"zustand": "^5.0.3"
},
devDependencies: {
'@types/react': '^18.2.55',
'@types/react-dom': '^18.2.19',
'@typescript-eslint/eslint-plugin': '^6.21.0',
'@typescript-eslint/parser': '^6.21.0',
'@vitejs/plugin-react': '^4.2.1',
'eslint': '^8.56.0',
'eslint-plugin-react-hooks': '^4.6.0',
'eslint-plugin-react-refresh': '^0.4.5',
'typescript': '^5.2.2',
"vite": "^6.2.0"
},
scripts: {
'dev': 'vite',
'build': 'tsc && vite build',
'lint': 'eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0',
'preview': 'vite preview'
}
};
// 写入 package.json
fs.writeFileSync(
path.join(projectPath, 'package.json'),
JSON.stringify(packageJson, null, 2)
);
// 安装依赖
if (!options.skipInstall) {
console.log(chalk.green('\n安装依赖...'));
const useYarn = !options.useNpm && shouldUseYarn();
try {
const command = useYarn ? 'yarn' : 'npm install';
execSync(command, { cwd: projectPath, stdio: 'inherit' });
console.log(chalk.green('\n依赖安装完成!'));
} catch (error) {
console.error(chalk.red('依赖安装失败,请手动安装。'));
console.error(error);
}
}
// 完成提示
console.log(chalk.green('\n项目创建成功!'));
console.log(chalk.blue('\n开始使用:'));
console.log(` cd ${path.basename(projectPath)}`);
if (options.skipInstall) {
console.log(' npm install');
}
console.log(' npm run dev');
console.log('\n祝您开发愉快!');
}
// 检查是否应该使用 Yarn
function shouldUseYarn() {
try {
execSync('yarn --version', { stdio: 'ignore' });
return true;
} catch (error) {
return false;
}
}
// 运行主函数
main().catch((error) => {
console.error(chalk.red('创建项目时出错:'));
console.error(error);
process.exit(1);
});