Search code examples
jsonyeomanhyperledgeryeoman-generator

Injecting variables with Yeoman into a JSON file with <%= variable %> but not outputting the variable


I am setting this up as stated in the docs for replacing values within files:

prompting() {
      return this.prompt([{
        type    : 'input',
        name    : 'name',
        message : 'Your project name',
        default : this.appname // Default to current folder name
      }, {
        type    : 'input',
        name    : 'chaincodeTitle',
        message : 'Chaincode folder name'
      }]).then((answers) => {
        this.log('app name', answers.name);
        this.props = answers;
      });
    }


// Creates all the files and directories from a hyperledger project template source
    writing() {  

      this.fs.copyTpl(
        this.templatePath('sdm/artifacts/local/config.json'),
        this.destinationPath('./' + this.props.name + 'hyperledger/local' + '/config.json'),
        {props: this.props.name}
      );     
    }

Here's where I put the tag in my JSON file:

{
    "host": "localhost",
    "port": "3000",
    "logLevel": "INFO",
    "channelCfgTxn": "../../../artifacts/local/channel/mychannel.tx",
    "chaincodeId": "MikeGcc",
    "CC_SRC_PATH": "../../../app/chaincode",
    "chaincodePath": "<%= props %>",
    "chaincodeVersion": "v0",
    "queryFunction": "getVersion",
    "keyValueStore": "/sim/fabric-client-kvs-local",
    "chaincodeName": "<%= props %>",
    "jwt_expiretime": "360000",
    "client": "http://localhost:4200/",
    "registerSupplierRoute": "register-supplier/"
}

But when I run my yeoman generator, yeoman doesnt detect its own tag it seems like and is outputting the string: <%= props %>

So I get the following error when it finishes:

info: [packager/Golang.js]: packaging GOLANG from <%= props %>
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: ENOENT: no such file or directory, lstat '/home/mike/mikehyperledger/app/chaincode/src/<%= props %>'

This has been killing me for two days now. Anyone familiar with Yeoman who might know whats happening here?


Solution

  • The problem was that this command was in the writing block which is a promise to write and all the commands in that block happen asynchronously.

    To fix this problem, I had to create a block outside of writing called "end()" and insert my fsCopyTpl in there like this:

       end() {  
    
          this.fs.copyTpl(
            this.templatePath('sdm/artifacts/local/config.json'),
            this.destinationPath('./' + this.props.name + 'hyperledger/local' + '/config.json'),
            {props: this.props.name}
          );     
        }