All files / src/configgen tsProjectConfigGen.ts

100% Statements 81/81
95.23% Branches 20/21
100% Functions 21/21
100% Lines 72/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119    1x       2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x 2x 5x   2x       2x 2x 2x 2x   2x 2x 2x 2x 4x   2x 4x   2x 2x   2x 2x   2x 2x   2x 2x       6x       6x 4x   4x       2x   4x 2x   2x             2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x       2x       2x 2x      
import { ContractConfig, ContractRelation, ProjectConfig, ScopeConfig } from '../types';
 
export class TSProjectConfigGen {
    constructor() { }
 
    gen(projectConfig: ProjectConfig): string {
        const constantName = this.generateConstantName(projectConfig.name);
        
        let out = `import { ContractConfig, ContractRelation, Feature, FunctionConfig, MintConfig, ProjectConfig } from "@patchworkdev/common/types";\n\n`;
        out += `const ${constantName}: ProjectConfig = {\n`;
        out += `    name: "${projectConfig.name}",\n`;
        out += `    scopes: [\n`;
        out += projectConfig.scopes.map(scope => this.genScopeConfig(scope)).join(',\n');
        out += `\n    ],\n`;
        out += `    contracts: {\n`;
        out += this.genContractsMap(projectConfig.contracts);
        out += `\n    },\n`;
        out += `    contractRelations: {\n`;
        out += this.genContractRelationsMap(projectConfig.contractRelations);
        out += `\n    }\n`;
        out += `};\n\n`;
        out += `export default ${constantName};\n`;
        return out;
    }
 
    private generateConstantName(projectName: string): string {
        const words = projectName.split(/\s+/);
        const camelCaseWords = words.map((word, index) => 
            index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
        );
        return camelCaseWords.join('') + 'ProjectConfig';
    }
    
    private genScopeConfig(scopeConfig: ScopeConfig): string {
        let out = `        {\n`;
        out += `            name: "${scopeConfig.name}",\n`;
        if (scopeConfig.owner) {
            out += `            owner: "${scopeConfig.owner}",\n`;
        }
        out += `            whitelist: ${scopeConfig.whitelist},\n`;
        out += `            userAssign: ${scopeConfig.userAssign},\n`;
        out += `            userPatch: ${scopeConfig.userPatch},\n`;
        if (scopeConfig.bankers && scopeConfig.bankers.length > 0) {
            out += `            bankers: [${scopeConfig.bankers.map(banker => `"${banker}"`).join(', ')}],\n`;
        }
        if (scopeConfig.operators && scopeConfig.operators.length > 0) {
            out += `            operators: [${scopeConfig.operators.map(operator => `"${operator}"`).join(', ')}],\n`;
        }
        if (scopeConfig.mintConfigs) {
            out += `            mintConfigs: {${this.genRecordEntries(scopeConfig.mintConfigs)}},\n`;
        }
        if (scopeConfig.patchFees) {
            out += `            patchFees: {${this.genRecordEntries(scopeConfig.patchFees)}},\n`;
        }
        if (scopeConfig.assignFees) {
            out += `            assignFees: {${this.genRecordEntries(scopeConfig.assignFees)}}\n`;
        }
        out += `        }`;
        return out;
    }
 
    private genRecordEntries(map: Record<string, any>): string {
        return `\n                ${Object.entries(map).map(([key, value]) => `"${key}": ${this.stringifyValue(value)}`).join(',\n                ')}\n`;
    }
 
    private stringifyValue(value: any): string {
        if (typeof value === 'object' && value !== null) {
            return `{${Object.entries(value).map(([k, v]) => `${k}: ${v}`).join(', ')}}`;
        }
        return JSON.stringify(value);
    }
 
    private genContractsMap(contracts: Record<string, string | ContractConfig>): string {
        return Object.entries(contracts)
            .map(([key, value]) => {
                if (typeof value === 'string') {
                    return `        "${key}": "${value}"`;
                } else {
                    return `        "${key}": ${this.stringifyContractConfig(value)}`;
                }
            })
            .join(',\n');
    }
 
    private stringifyContractConfig(config: ContractConfig): string {
        let out = '{\n';
        out += `            scopeName: "${config.scopeName}",\n`;
        out += `            name: "${config.name}",\n`;
        out += `            symbol: "${config.symbol}",\n`;
        out += `            baseURI: "${config.baseURI}",\n`;
        out += `            schemaURI: "${config.schemaURI}",\n`;
        out += `            imageURI: "${config.imageURI}",\n`;
        out += `            fields: [\n`;
        out += config.fields.map(field => {
            let fieldStr = '                {\n';
            fieldStr += `                    id: ${field.id},\n`;
            fieldStr += `                    key: "${field.key}",\n`;
            fieldStr += `                    type: "${field.type}",\n`;
            if (field.description) fieldStr += `                    description: "${field.description}",\n`;
            if (field.functionConfig) fieldStr += `                    functionConfig: FunctionConfig.${field.functionConfig},\n`;
            fieldStr += '                }';
            return fieldStr;
        }).join(',\n');
        out += '\n            ],\n';
        out += `            features: [${config.features.map(f => this.formatFeature(f)).join(', ')}]\n`;
        out += '        }';
        return out;
    }
    
    private formatFeature(feature: string): string {
        return feature === "1155PATCH" ? `Feature["${feature}"]` : `Feature.${feature}`;
    }
 
    private genContractRelationsMap(relations: Record<string, ContractRelation>): string {
        return Object.entries(relations)
            .map(([key, value]) => `        "${key}": { fragments: [${value.fragments.map(f => `"${f}"`).join(', ')}] }`)
            .join(',\n');
    }
}