File size: 7,125 Bytes
857cdcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 *  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;