penframe
Version:
A lightweight DSL-based wireframe and UI structure visualization tool.
86 lines (71 loc) • 2.64 kB
JavaScript
const fs = require('fs');
const path = require('path');
const { astToPngFile } = require('./svg/astToPng');
async function main() {
const inputFile = process.argv[2];
const outputFile = process.argv[3];
if (!inputFile) {
console.error('Usage: node src/ast2png.js <input.json> [output.png] [options]');
console.error('');
console.error('Options:');
console.error(' --width <number> Viewport width');
console.error(' --height <number> Viewport height');
console.error(' --scale <number> Device scale factor (default: 2)');
console.error(' --background <color> Background color (default: white)');
console.error('');
console.error('Examples:');
console.error(' node src/ast2png.js examples/ast_sample.json output.png');
console.error(' node src/ast2png.js examples/ast_sample.json output.png --width 800 --height 600');
console.error(' node src/ast2png.js examples/ast_sample.json output.png --scale 3 --background transparent');
process.exit(1);
}
// Parse command line arguments
const args = process.argv.slice(4);
const options = {};
for (let i = 0; i < args.length; i += 2) {
const flag = args[i];
const value = args[i + 1];
switch (flag) {
case '--width':
options.width = parseInt(value);
break;
case '--height':
options.height = parseInt(value);
break;
case '--scale':
options.deviceScaleFactor = parseFloat(value);
break;
case '--background':
options.background = value;
break;
}
}
// Default output file
const output = outputFile || inputFile.replace(/\.json$/, '.png');
// Check if input file exists
if (!fs.existsSync(inputFile)) {
console.error(`Error: Input file not found: ${inputFile}`);
process.exit(1);
}
try {
// Read and parse AST JSON
const astContent = fs.readFileSync(inputFile, 'utf8');
const ast = JSON.parse(astContent);
console.log(`Converting ${inputFile} to PNG using Puppeteer...`);
console.log(`Options:`, options);
// Convert AST to PNG
await astToPngFile(ast, output, options);
console.log(`✓ PNG saved to: ${output}`);
// Show file size
const stats = fs.statSync(output);
console.log(`File size: ${(stats.size / 1024).toFixed(1)} KB`);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
}
main().catch(error => {
console.error('Unexpected error:', error);
process.exit(1);
});