/**
* MSI Builder Script - สร้างไฟล์ติดตั้ง Windows
* Chahua Development Thailand
* CEO: Saharath C.
*
* Purpose: สร้าง Windows Installer (.msi) สำหรับ deployment
*/
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
class MSIBuilder {
constructor() {
this.config = {
productName: 'Chahuadev Framework',
productVersion: '1.0.0',
manufacturer: 'Chahua Development Thailand',
description: 'Universal Execution Framework by CEO Saharath C.',
outputDir: './chahuadev-framework/build',
tempDir: './chahuadev-framework/build/temp',
// Logo and Icon Configuration
iconConfig: {
iconFile: 'icon.png',
logoFile: 'logo.png',
splashIcon: 'splash.html'
}
};
console.log(' MSI Builder initialized');
console.log(' Logo system enabled');
}
async buildMSI() {
try {
console.log(' Starting MSI build process...');
// Step 1: Prepare build directory
await this.prepareBuildDirectory();
// Step 2: Copy application files
await this.copyApplicationFiles();
// Step 3: Generate WiX configuration
await this.generateWiXConfig();
// Step 4: Build MSI (requires WiX Toolset)
await this.compileMSI();
console.log(' MSI build completed successfully!');
} catch (error) {
console.error(' MSI build failed:', error.message);
throw error;
}
}
async prepareBuildDirectory() {
console.log(' Preparing build directories...');
// Create directories
const dirs = [
this.config.outputDir,
this.config.tempDir,
path.join(this.config.tempDir, 'app'),
path.join(this.config.tempDir, 'resources')
];
for (const dir of dirs) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
}
async copyApplicationFiles() {
console.log(' Copying application files...');
const filesToCopy = [
'main.js',
'preload.js',
'index.html',
'splash.html',
'validation_gateway.js',
'package.json',
// Logo and Assets (Essential for Splash Screen)
'icon.png',
'logo.png',
'logo.ico',
// Additional logo formats (optional)
'logo.svg',
'app-icon.png',
'app-icon.ico',
// User Guides
'USER_GUIDE_TH.md',
'USER_GUIDE_EN.md',
'modules/',
'strategies/',
'plugins/',
'config/'
];
// แก้ไขเส้นทางให้ถูกต้อง - ใช้ root directory แทน subdirectory
const sourceBase = '.'; // ใช้ current directory
const destBase = path.join(this.config.tempDir, 'app');
for (const file of filesToCopy) {
const sourcePath = path.join(sourceBase, file);
const destPath = path.join(destBase, file);
if (fs.existsSync(sourcePath)) {
this.copyRecursive(sourcePath, destPath);
console.log(` Copied: ${file}`);
} else if (file.includes('.png') || file.includes('.ico')) {
console.log(` Logo file missing: ${file} - will use fallback`);
}
}
// Copy logo to resources directory
await this.copyLogoAssets();
}
copyRecursive(src, dest) {
const stat = fs.statSync(src);
if (stat.isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
const files = fs.readdirSync(src);
for (const file of files) {
this.copyRecursive(
path.join(src, file),
path.join(dest, file)
);
}
} else {
fs.copyFileSync(src, dest);
}
}
// NEW METHOD: Copy logo assets for installer
async copyLogoAssets() {
console.log(' Copying logo assets...');
const resourcesDir = path.join(this.config.tempDir, 'resources');
const logoAssets = [
{ src: 'icon.png', dest: 'app-icon.png' },
{ src: 'logo.png', dest: 'logo.png' },
{ src: 'logo.ico', dest: 'app-icon.ico' }
];
for (const asset of logoAssets) {
// แก้ไขเส้นทาง source ให้ถูกต้อง
const srcPath = path.join('.', asset.src); // ใช้ current directory
const destPath = path.join(resourcesDir, asset.dest);
if (fs.existsSync(srcPath)) {
fs.copyFileSync(srcPath, destPath);
console.log(` Logo copied: ${asset.src} ${asset.dest}`);
} else {
// Create a simple fallback icon
await this.createFallbackIcon(destPath, asset.dest);
}
}
}
// NEW METHOD: Create fallback logo if original missing
async createFallbackIcon(destPath, filename) {
if (filename.endsWith('.png')) {
// Create a simple SVG-based PNG fallback (text representation)
const fallbackContent = `\n`;
fs.writeFileSync(destPath.replace('.png', '.svg'), fallbackContent);
console.log(` Created fallback logo: ${filename}`);
}
}
async generateWiXConfig() {
console.log(' Generating WiX configuration...');
const wixConfig = `
`;
const wixPath = path.join(this.config.tempDir, 'chahuadev.wxs');
fs.writeFileSync(wixPath, wixConfig);
console.log(' WiX configuration generated');
console.log(' Logo integration included');
}
async compileMSI() {
console.log(' Compiling MSI package...');
return new Promise((resolve, reject) => {
// Check if WiX Toolset is available
exec('candle -?', (error) => {
if (error) {
console.warn(' WiX Toolset not found. Installing npm packages instead...');
this.createPortablePackage();
resolve();
return;
}
// Compile with WiX
const wixPath = path.join(this.config.tempDir, 'chahuadev.wxs');
const objPath = path.join(this.config.tempDir, 'chahuadev.wixobj');
const msiPath = path.join(this.config.outputDir, 'ChahuadevFramework.msi');
// Candle (compile)
exec(`candle "${wixPath}" -out "${objPath}"`, (error) => {
if (error) {
reject(new Error(`Candle compilation failed: ${error.message}`));
return;
}
// Light (link)
exec(`light "${objPath}" -out "${msiPath}"`, (error) => {
if (error) {
reject(new Error(`Light linking failed: ${error.message}`));
return;
}
console.log(` MSI created: ${msiPath}`);
resolve();
});
});
});
});
}
createPortablePackage() {
console.log(' Creating portable package instead...');
const packageInfo = {
name: this.config.productName,
version: this.config.productVersion,
description: this.config.description,
author: this.config.manufacturer,
instructions: [
'1. Extract this package to desired location',
'2. Run: npm install',
'3. Start: node app.js',
'4. CLI: node cli.js --help',
'5. For user instructions, see USER_GUIDE_TH.md (Thai) or USER_GUIDE_EN.md (English)'
],
requirements: [
'Node.js v16.0.0 or higher',
'npm v7.0.0 or higher'
],
userGuides: [
'USER_GUIDE_TH.md - Thai language user manual',
'USER_GUIDE_EN.md - English language user manual'
]
};
const readmePath = path.join(this.config.outputDir, 'INSTALLATION.md');
const readme = `# ${packageInfo.name} v${packageInfo.version}
${packageInfo.description}
## Requirements
${packageInfo.requirements.map(req => `- ${req}`).join('\n')}
## Installation Instructions
${packageInfo.instructions.map((step, i) => `${i + 1}. ${step}`).join('\n')}
## User Guides
${packageInfo.userGuides.map(guide => `- ${guide}`).join('\n')}
## Support
For support, contact: ${packageInfo.author}
`;
fs.writeFileSync(readmePath, readme);
fs.writeFileSync(
path.join(this.config.outputDir, 'package-info.json'),
JSON.stringify(packageInfo, null, 2)
);
console.log(' Portable package created');
}
async clean() {
console.log(' Cleaning temporary files...');
if (fs.existsSync(this.config.tempDir)) {
fs.rmSync(this.config.tempDir, { recursive: true, force: true });
}
console.log(' Cleanup completed');
}
}
// CLI Usage
if (require.main === module) {
const builder = new MSIBuilder();
builder.buildMSI()
.then(() => builder.clean())
.catch((error) => {
console.error('Build failed:', error);
process.exit(1);
});
}
module.exports = MSIBuilder;