All files / fabric-shim/lib/cmds/metadata/lib generate.js

100% Statements 16/16
100% Branches 4/4
100% Functions 1/1
100% Lines 16/16

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            2x 2x 2x 2x 2x 2x                           4x 4x 4x 4x 3x 3x 3x 3x   1x       2x  
/*
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const path = require('path');
const fs = require('fs-extra');
const ChaincodeFromContract = require('../../../contract-spi/chaincodefromcontract.js');
const Bootstrap = require('../../../contract-spi/bootstrap.js');
const Logger = require('../../../logger');
const logger = Logger.getLogger('../../../cmds/metadata/lib/generate');
 
/**
 * fabric-chaincode-node "metadata generate" command
 * @private
 */
class Generate {
    /**
     * This is the main entry point for starting the user's chaincode
     * @ignore
     */
    static async handler(opts) {
        // load up the meta data that the user may have specified
        // this will need to passed in and rationalized with the code as implemented
        const fileMetadata = await Bootstrap.getMetadata(opts['module-path']);
        const {contracts, serializers, title, version} = Bootstrap.getInfoFromContract(opts['module-path']);
        const chaincode = new ChaincodeFromContract(contracts, serializers, fileMetadata, title, version);
        if (opts.file) {
            const fileName = path.extname(opts.file) === '' ? opts.file + '.json' : opts.file;
            const filePath = path.resolve(process.cwd(), fileName);
            await fs.writeFile(filePath, JSON.stringify(chaincode.metadata, null, 4));
            logger.info(`File containing metadata has been saved to ${filePath}`);
        } else {
            logger.info(JSON.stringify(chaincode.metadata, null, 4));
        }
    }
}
module.exports = Generate;