wechat-page-generator
Version:
🚀 Automatic page generator for WeChat Mini Program with ready-made templates
163 lines (134 loc) • 4.22 kB
JavaScript
const fs = require('fs');
const path = require('path');
// Import the createPage function
let createPage;
try {
// Try importing from local lib path
const libPath = path.join(__dirname, '../lib/index.js');
if (fs.existsSync(libPath)) {
({ createPage } = require(libPath));
} else {
// Fallback to globally installed module
({ createPage } = require('wechat-page-generator'));
}
} catch (error) {
console.error('❌ Error: Failed to load module wechat-page-generator');
console.error('Details:', error.message);
process.exit(1);
}
// Terminal colors
const colors = {
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
reset: '\x1b[0m',
bold: '\x1b[1m'
};
/**
* Display help
*/
function showHelp() {
console.log(`
${colors.bold}WeChat Mini Program Page Generator${colors.reset}
${colors.blue}Usage:${colors.reset}
wx:create <page-name> [options]
${colors.blue}Examples:${colors.reset}
wx:create profile
wx:create user-settings
wx:create "My Page"
${colors.blue}Options:${colors.reset}
-h, --help Show this help
-v, --version Show version
${colors.blue}Description:${colors.reset}
Create a new WeChat Mini Program page with all required files:
• .wxml (template)
• .js (logic)
• .wxss (styles)
• .json (configuration)
`);
}
/**
* Display version
*/
function showVersion() {
try {
const packagePath = path.join(__dirname, '../package.json');
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
console.log(`v${packageJson.version}`);
} catch (error) {
console.log('v1.0.2'); // Default version fallback
}
}
/**
* Main function
*/
function main() {
const args = process.argv.slice(2);
// No argument provided
if (args.length === 0) {
console.log(`${colors.red}❌ Error: Page name is required${colors.reset}`);
showHelp();
process.exit(1);
}
const firstArg = args[0];
// Help
if (firstArg === '-h' || firstArg === '--help') {
showHelp();
process.exit(0);
}
// Version
if (firstArg === '-v' || firstArg === '--version') {
showVersion();
process.exit(0);
}
const pageName = firstArg;
try {
console.log(`${colors.blue}🔄 Creating page "${pageName}"...${colors.reset}`);
const result = createPage(pageName);
console.log(`${colors.green}${colors.bold}✅ Page "${result.pageName}" successfully created!${colors.reset}`);
console.log(`${colors.blue}📁 Location:${colors.reset} ${result.path}`);
console.log(`${colors.blue}📄 Files created:${colors.reset}`);
result.files.forEach(file => {
console.log(` • ${file}`);
});
// Display app.json update status
console.log(`\n${colors.blue}📱 app.json:${colors.reset}`);
if (result.appJsonUpdated.updated) {
console.log(`${colors.green} ✅ Page automatically added to app.json${colors.reset}`);
console.log(` 📄 Path: pages/${result.pageName}/${result.pageName}`);
console.log(` 📊 Total pages: ${result.appJsonUpdated.totalPages}`);
} else {
console.log(`${colors.yellow} ⚠️ app.json not updated: ${result.appJsonUpdated.reason}${colors.reset}`);
}
console.log(`\n${colors.yellow}💡 Next steps:${colors.reset}`);
if (!result.appJsonUpdated.updated) {
console.log(`1. Manually add this page to app.json:
"pages": [
"pages/${result.pageName}/${result.pageName}"
]
2. Navigate to this page using:`);
} else {
console.log(`1. Navigate to this page using:`);
}
console.log(` wx.navigateTo({
url: '/pages/${result.pageName}/${result.pageName}'
})
`);
} catch (error) {
console.error(`${colors.red}❌ Error: ${error.message}${colors.reset}`);
process.exit(1);
}
}
// Handle uncaught errors
process.on('uncaughtException', (error) => {
console.error(`${colors.red}❌ Uncaught error: ${error.message}${colors.reset}`);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error(`${colors.red}❌ Unhandled promise rejection: ${reason}${colors.reset}`);
process.exit(1);
});
// Run the main program
main();