@diagramers/admin
Version:
Diagramers Admin Template - React starter for admin dashboards.
163 lines (135 loc) • 5.9 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const { execSync } = require('child_process');
const projectName = process.argv[2];
const options = process.argv.slice(3);
if (!projectName) {
console.error('❌ Project name is required');
console.log('Usage: diagramers-admin-init <project-name> [options]');
process.exit(1);
}
console.log(`🚀 Initializing React admin application: ${projectName}`);
try {
// Find template directory
console.log('🔍 Searching for template directory...');
const templateDir = path.join(__dirname, '..', 'templates', 'react-admin-template');
if (!fs.existsSync(templateDir)) {
// If no templates directory, use the current admin package as template
console.log('📁 Using current admin package as template');
const sourceDir = path.join(__dirname, '..');
const targetDir = path.join(process.cwd(), projectName);
// Create target directory
console.log(`📁 Creating target directory: ${targetDir}`);
fs.ensureDirSync(targetDir);
// Copy files (excluding node_modules, .git, etc.)
console.log('📁 Copying template files...');
console.log(` From: ${sourceDir}`);
console.log(` To: ${targetDir}`);
const filesToCopy = [
'src', 'public', 'package.json', 'jsconfig.json',
'README.md', 'sass', 'routing', 'views', 'hooks',
'components', 'auth', 'cs-line-icons', 'lang',
'layout', 'settings', 'App.js', 'config.js',
'constants.js', 'index.js', 'reportWebVitals.js',
'routes.js', 'store.js'
];
filesToCopy.forEach(file => {
const sourcePath = path.join(sourceDir, file);
const targetPath = path.join(targetDir, file);
if (fs.existsSync(sourcePath)) {
fs.copySync(sourcePath, targetPath);
}
});
console.log('✅ Template files copied successfully');
// Update package.json
console.log('📄 Verifying copied package.json...');
const packageJsonPath = path.join(targetDir, 'package.json');
if (fs.existsSync(packageJsonPath)) {
console.log('✅ Package.json copied successfully');
// Update package.json with project name
console.log('📝 Updating package.json...');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.name = projectName;
packageJson.private = true;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log('✅ Package.json updated successfully');
}
// Create .env file
console.log('📄 Creating .env file...');
const envContent = `REACT_APP_API_URL=http://localhost:5000
REACT_APP_PROJECT_NAME=${projectName}
`;
fs.writeFileSync(path.join(targetDir, '.env'), envContent);
console.log('✅ .env file created successfully');
// Install dependencies
console.log('📦 Installing dependencies...');
execSync('npm install --legacy-peer-deps', {
cwd: targetDir,
stdio: 'inherit'
});
// Initialize git
console.log('🔧 Initializing git repository...');
execSync('git init', {
cwd: targetDir,
stdio: 'inherit'
});
console.log('✅ React admin application initialized successfully!');
console.log(`📁 Project created at: ${targetDir}`);
console.log(`🚀 To start development: cd ${projectName} && npm start`);
} else {
// Use existing template directory
console.log(`📁 Found template at: ${templateDir}`);
const packageJsonPath = path.join(templateDir, 'package.json');
if (fs.existsSync(packageJsonPath)) {
console.log('📄 Checking template package.json...');
console.log(`📄 Checking template package.json at: ${packageJsonPath}`);
console.log('✅ Template package.json found');
}
const targetDir = path.join(process.cwd(), projectName);
console.log(`📁 Creating target directory: ${targetDir}`);
fs.ensureDirSync(targetDir);
console.log('📁 Copying template files...');
console.log(` From: ${templateDir}`);
console.log(` To: ${targetDir}`);
fs.copySync(templateDir, targetDir);
console.log('✅ Template files copied successfully');
// Update package.json
console.log('📄 Verifying copied package.json...');
const targetPackageJsonPath = path.join(targetDir, 'package.json');
if (fs.existsSync(targetPackageJsonPath)) {
console.log('✅ Package.json copied successfully');
console.log('📝 Updating package.json...');
const packageJson = JSON.parse(fs.readFileSync(targetPackageJsonPath, 'utf8'));
packageJson.name = projectName;
packageJson.private = true;
fs.writeFileSync(targetPackageJsonPath, JSON.stringify(packageJson, null, 2));
console.log('✅ Package.json updated successfully');
}
// Create .env file
console.log('📄 Creating .env file...');
const envContent = `REACT_APP_API_URL=http://localhost:5000
REACT_APP_PROJECT_NAME=${projectName}
`;
fs.writeFileSync(path.join(targetDir, '.env'), envContent);
console.log('✅ .env file created successfully');
// Install dependencies
console.log('📦 Installing dependencies...');
execSync('npm install --legacy-peer-deps', {
cwd: targetDir,
stdio: 'inherit'
});
// Initialize git
console.log('🔧 Initializing git repository...');
execSync('git init', {
cwd: targetDir,
stdio: 'inherit'
});
console.log('✅ React admin application initialized successfully!');
console.log(`📁 Project created at: ${targetDir}`);
console.log(`🚀 To start development: cd ${projectName} && npm start`);
}
} catch (error) {
console.error(`❌ Failed to initialize admin project: ${error.message}`);
process.exit(1);
}