| |
| |
| |
| |
| |
| |
| |
|
|
| 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', |
| |
| 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...'); |
| |
| |
| await this.prepareBuildDirectory(); |
| |
| |
| await this.copyApplicationFiles(); |
| |
| |
| await this.generateWiXConfig(); |
| |
| |
| 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...'); |
| |
| |
| 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', |
| |
| 'icon.png', |
| 'logo.png', |
| 'logo.ico', |
| |
| 'logo.svg', |
| 'app-icon.png', |
| 'app-icon.ico', |
| |
| 'USER_GUIDE_TH.md', |
| 'USER_GUIDE_EN.md', |
| 'modules/', |
| 'strategies/', |
| 'plugins/', |
| 'config/' |
| ]; |
| |
| |
| const sourceBase = '.'; |
| 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`); |
| } |
| } |
| |
| |
| 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); |
| } |
| } |
|
|
| |
| 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) { |
| |
| const srcPath = path.join('.', asset.src); |
| const destPath = path.join(resourcesDir, asset.dest); |
| |
| if (fs.existsSync(srcPath)) { |
| fs.copyFileSync(srcPath, destPath); |
| console.log(` Logo copied: ${asset.src} ${asset.dest}`); |
| } else { |
| |
| await this.createFallbackIcon(destPath, asset.dest); |
| } |
| } |
| } |
|
|
| |
| async createFallbackIcon(destPath, filename) { |
| if (filename.endsWith('.png')) { |
| |
| const fallbackContent = `<!-- Chahuadev Framework Logo Fallback -->\n<svg width="128" height="128" xmlns="http://www.w3.org/2000/svg">\n <rect width="128" height="128" fill="#1a1a2e" rx="18"/>\n <text x="64" y="75" font-family="Arial" font-size="48" font-weight="bold" text-anchor="middle" fill="#00ff88">C</text>\n</svg>`; |
| fs.writeFileSync(destPath.replace('.png', '.svg'), fallbackContent); |
| console.log(` Created fallback logo: ${filename}`); |
| } |
| } |
|
|
| async generateWiXConfig() { |
| console.log(' Generating WiX configuration...'); |
| |
| const wixConfig = `<?xml version="1.0" encoding="UTF-8"?> |
| <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> |
| <Product Id="*" |
| Name="${this.config.productName}" |
| Language="1033" |
| Version="${this.config.productVersion}" |
| Manufacturer="${this.config.manufacturer}" |
| UpgradeCode="12345678-1234-1234-1234-123456789012"> |
| |
| <Package InstallerVersion="200" |
| Compressed="yes" |
| InstallScope="perMachine" |
| Description="${this.config.description}" /> |
| |
| <MajorUpgrade DowngradeErrorMessage="A newer version is already installed." /> |
| <MediaTemplate EmbedCab="yes" /> |
| |
| <!-- Icon Configuration --> |
| <Icon Id="ProductIcon" SourceFile="$(var.SourceDir)\\resources\\app-icon.ico" /> |
| <Property Id="ARPPRODUCTICON" Value="ProductIcon" /> |
| |
| <Feature Id="ProductFeature" Title="Chahuadev Framework" Level="1"> |
| <ComponentGroupRef Id="ProductComponents" /> |
| <ComponentGroupRef Id="LogoComponents" /> |
| <ComponentGroupRef Id="UserGuideComponents" /> |
| </Feature> |
| </Product> |
| |
| <Fragment> |
| <Directory Id="TARGETDIR" Name="SourceDir"> |
| <Directory Id="ProgramFilesFolder"> |
| <Directory Id="INSTALLFOLDER" Name="Chahuadev Framework" /> |
| </Directory> |
| </Directory> |
| </Fragment> |
| |
| <Fragment> |
| <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER"> |
| <Component Id="MainExecutable" Guid="*"> |
| <File Id="main.js" Source="$(var.SourceDir)\\app\\main.js" KeyPath="yes" /> |
| <File Id="app.js" Source="$(var.SourceDir)\\app\\app.js" /> |
| <File Id="cli.js" Source="$(var.SourceDir)\\app\\cli.js" /> |
| <File Id="package.json" Source="$(var.SourceDir)\\app\\package.json" /> |
| <File Id="index.html" Source="$(var.SourceDir)\\app\\index.html" /> |
| <File Id="splash.html" Source="$(var.SourceDir)\\app\\splash.html" /> |
| </Component> |
| </ComponentGroup> |
| |
| <!-- Logo Components --> |
| <ComponentGroup Id="LogoComponents" Directory="INSTALLFOLDER"> |
| <Component Id="LogoAssets" Guid="*"> |
| <File Id="icon.png" Source="$(var.SourceDir)\\app\\icon.png" /> |
| <File Id="logo.png" Source="$(var.SourceDir)\\app\\logo.png" /> |
| </Component> |
| </ComponentGroup> |
| |
| <!-- User Guide Components --> |
| <ComponentGroup Id="UserGuideComponents" Directory="INSTALLFOLDER"> |
| <Component Id="UserGuides" Guid="*"> |
| <File Id="USER_GUIDE_TH.md" Source="$(var.SourceDir)\\app\\USER_GUIDE_TH.md" /> |
| <File Id="USER_GUIDE_EN.md" Source="$(var.SourceDir)\\app\\USER_GUIDE_EN.md" /> |
| </Component> |
| </ComponentGroup> |
| </Fragment> |
| </Wix>`; |
|
|
| 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) => { |
| |
| exec('candle -?', (error) => { |
| if (error) { |
| console.warn(' WiX Toolset not found. Installing npm packages instead...'); |
| this.createPortablePackage(); |
| resolve(); |
| return; |
| } |
| |
| |
| 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'); |
| |
| |
| exec(`candle "${wixPath}" -out "${objPath}"`, (error) => { |
| if (error) { |
| reject(new Error(`Candle compilation failed: ${error.message}`)); |
| return; |
| } |
| |
| |
| 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'); |
| } |
| } |
|
|
| |
| 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; |