zp-figma-converter
Version:
Convert Figma designs to various code formats
126 lines (123 loc) • 4.87 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const figma_converter_1 = require("./figma/figma-converter");
// Help text
const helpText = `
FIGMA - Convert Figma design to various code formats
Usage:
figma <output-format> <figma-file-key> [figma-node-id] [options]
Supported formats:
csd Convert to Cocos Studio Document format
Options:
-o, --output <path> Output file path or directory (default is ./output/)
-v, --verbose Show detailed information during conversion
-h, --help Show help
Examples:
figma csd Abc123XyZ # Convert entire Figma file to CSD
figma csd Abc123XyZ 456:789 # Convert specific Figma node to CSD
figma csd Abc123XyZ -o my-output/ # Specify output directory
`;
// Supported formats
const SUPPORTED_FORMATS = ['csd'];
// Parse command line arguments
function parseArgs() {
const args = process.argv.slice(2);
// Show help for empty command or help command
if (args.length === 0 || args[0] === 'help' || args[0] === '-h' || args[0] === '--help') {
console.log(helpText);
process.exit(0);
}
// Initialize options with defaults
let options = {
format: '',
fileKey: '',
nodeId: undefined,
output: './output',
verbose: false
};
// First argument is output format (required)
options.format = args[0];
// Validate format
if (!SUPPORTED_FORMATS.includes(options.format)) {
console.error(`Error: Unsupported format '${options.format}'. Supported formats: ${SUPPORTED_FORMATS.join(', ')}`);
console.log(helpText);
process.exit(1);
}
// Second argument is file key (required)
if (args.length > 1) {
options.fileKey = args[1];
}
else {
console.error(`Error: Missing required Figma file key`);
console.log(helpText);
process.exit(1);
}
// Parse remaining arguments
for (let i = 2; i < args.length; i++) {
const arg = args[i];
if (arg === '-o' || arg === '--output') {
options.output = args[++i] || './output';
}
else if (arg === '-v' || arg === '--verbose') {
options.verbose = true;
}
else if (arg === '-h' || arg === '--help') {
console.log(helpText);
process.exit(0);
}
else if (!arg.startsWith('-') && options.nodeId === undefined) {
// If not an option and nodeId is not set, assume it's the nodeId
options.nodeId = arg;
}
}
return options;
}
// Convert Figma file/node to specified format
function convertFigma(options) {
return __awaiter(this, void 0, void 0, function* () {
if (options.verbose) {
console.log(`Converting Figma file ${options.fileKey}${options.nodeId ? `, node ${options.nodeId}` : ''} to ${options.format.toUpperCase()} format`);
console.log(`Output: ${options.output}`);
}
else {
console.log(`Converting Figma to ${options.format.toUpperCase()}...`);
}
try {
// Create Figma controller
const figmaConverter = new figma_converter_1.FigmaConverter(process.env.FIGMA_API_KEY || '', options.output);
// Convert Figma to selected format
const outputPath = yield figmaConverter.convertNode(options.fileKey, options.nodeId || '', options.format, undefined);
console.log(`Conversion successful! Output saved to: ${outputPath}`);
}
catch (error) {
console.error('Conversion failed:', error.message);
if (options.verbose) {
console.error(error);
}
process.exit(1);
}
});
}
// Main function
function main() {
return __awaiter(this, void 0, void 0, function* () {
const options = parseArgs();
yield convertFigma(options);
});
}
// Run program
main().catch(error => {
console.error('Unhandled error:', error);
process.exit(1);
});
//# sourceMappingURL=cli.js.map