chahuadev-framework-en / scripts /copy-user-guides.js
chahuadev
Update README
857cdcf
/**
* Copy User Guides Script
*
* สคริปต์สำหรับคัดลอกไฟล์คู่มือการใช้งานจาก build_assets/plugins
* ไปยังโฟลเดอร์รากของโปรเจค เพื่อรวมในแพ็คเกจขั้นสุดท้าย
*/
const fs = require('fs');
const path = require('path');
class UserGuidesCopier {
constructor() {
this.projectRoot = path.resolve(__dirname, '..');
this.sourceDir = path.join(this.projectRoot, 'build_assets', 'plugins');
this.destinationDir = this.projectRoot;
this.guidesToCopy = [
{
name: 'USER_GUIDE_EN.md',
description: 'English User Guide'
},
{
name: 'USER_GUIDE_TH.md',
description: 'Thai User Guide (คู่มือภาษาไทย)'
}
];
}
/**
* เริ่มกระบวนการคัดลอกไฟล์คู่มือ
*/
async copyUserGuides() {
console.log(' Starting User Guides Copy Process...');
console.log('='.repeat(50));
try {
let successCount = 0;
let failCount = 0;
for (const guide of this.guidesToCopy) {
const result = await this.copyGuide(guide);
if (result) {
successCount++;
} else {
failCount++;
}
}
console.log('\n' + '='.repeat(50));
console.log(' Copy Summary:');
console.log(` Successfully copied: ${successCount} files`);
console.log(` Failed to copy: ${failCount} files`);
if (successCount > 0) {
console.log('\n User guides are now ready for distribution!');
console.log(' These files will be included in your built application.');
}
if (failCount > 0) {
console.log('\n Some files could not be copied. Please check the errors above.');
process.exit(1);
}
} catch (error) {
console.error(' Copy process failed:', error.message);
process.exit(1);
}
}
/**
* คัดลอกไฟล์คู่มือแต่ละไฟล์
*/
async copyGuide(guide) {
const sourcePath = path.join(this.sourceDir, guide.name);
const destPath = path.join(this.destinationDir, guide.name);
console.log(`\n Processing: ${guide.description}`);
console.log(` Source: ${path.relative(this.projectRoot, sourcePath)}`);
console.log(` Destination: ${path.relative(this.projectRoot, destPath)}`);
try {
// ตรวจสอบว่าไฟล์ต้นทางมีอยู่หรือไม่
if (!fs.existsSync(sourcePath)) {
console.log(` Source file not found: ${guide.name}`);
return false;
}
// ตรวจสอบขนาดไฟล์
const stats = fs.statSync(sourcePath);
console.log(` File size: ${this.formatFileSize(stats.size)}`);
// คัดลอกไฟล์
fs.copyFileSync(sourcePath, destPath);
// ตรวจสอบว่าคัดลอกสำเร็จ
if (fs.existsSync(destPath)) {
const destStats = fs.statSync(destPath);
if (stats.size === destStats.size) {
console.log(` Successfully copied: ${guide.name}`);
return true;
} else {
console.log(` File size mismatch after copy: ${guide.name}`);
return false;
}
} else {
console.log(` Destination file not created: ${guide.name}`);
return false;
}
} catch (error) {
console.log(` Error copying ${guide.name}: ${error.message}`);
return false;
}
}
/**
* แปลงขนาดไฟล์เป็นรูปแบบที่อ่านง่าย
*/
formatFileSize(bytes) {
const units = ['B', 'KB', 'MB', 'GB'];
let size = bytes;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(1)} ${units[unitIndex]}`;
}
/**
* ตรวจสอบสถานะไฟล์คู่มือ
*/
async checkStatus() {
console.log(' Checking User Guides Status...');
console.log('='.repeat(50));
for (const guide of this.guidesToCopy) {
const sourcePath = path.join(this.sourceDir, guide.name);
const destPath = path.join(this.destinationDir, guide.name);
console.log(`\n ${guide.description}:`);
// ตรวจสอบไฟล์ต้นทาง
if (fs.existsSync(sourcePath)) {
const sourceStats = fs.statSync(sourcePath);
console.log(` Source: (${this.formatFileSize(sourceStats.size)})`);
} else {
console.log(` Source: Not found`);
}
// ตรวจสอบไฟล์ปลายทาง
if (fs.existsSync(destPath)) {
const destStats = fs.statSync(destPath);
console.log(` Destination: (${this.formatFileSize(destStats.size)})`);
} else {
console.log(` Destination: Not found`);
}
}
}
}
// รองรับการใช้งานผ่าน command line
if (require.main === module) {
const copier = new UserGuidesCopier();
// ตรวจสอบ command line arguments
const args = process.argv.slice(2);
if (args.includes('--status') || args.includes('-s')) {
copier.checkStatus().catch(console.error);
} else if (args.includes('--help') || args.includes('-h')) {
console.log(' User Guides Copier');
console.log('');
console.log('Usage:');
console.log(' node scripts/copy-user-guides.js Copy user guides');
console.log(' node scripts/copy-user-guides.js --status Check current status');
console.log(' node scripts/copy-user-guides.js --help Show this help');
console.log('');
console.log('This script copies user guide files from build_assets/plugins/');
console.log('to the project root for inclusion in the final build package.');
} else {
copier.copyUserGuides().catch(console.error);
}
}
module.exports = UserGuidesCopier;