<|start_filename|>index.js<|end_filename|>
const codeCreate = require('./commands/code_create.js');

(function () {
  'use strict';

  exports.topics = [{
    name: 'code',
    description: 'commands for source'
  }];

  exports.namespace = {
    name: 'waw',
    description: 'Various commands from <NAME>'
  };

  exports.commands = [
    codeCreate
  ];

}());

<|start_filename|>commands/code_create.js<|end_filename|>
const path = require('path');
const fse = require('fs-extra');
const fs = require('fs');
const os = require('os');

const codeCreate = require('../lib/code_create.js');

(function () {
  'use strict';

  module.exports = {
    topic: 'code',
    command: 'create',
    description: 'create source from a template',
    help: 'help text for waw:code:create',
    flags: [{
      name: 'name',
      char: 'n',
      description: 'file or bundle name',
      hasValue: true,
      required: true
    }, {
      name: 'template',
      char: 't',
      description: 'code template name',
      hasValue: true,
      required: true
    }, {
      name: 'outputdir',
      char: 'd',
      description: 'folder for saving the created files',
      hasValue: true,
      required: false
    }, {
      name: 'vars',
      char: 'v',
      description: 'variables required by the template',
      hasValue: true,
      required: false
    }],
    run(context) {

      const template = context.flags.template;
      const name = context.flags.name;
      const outputdir = context.flags.outputdir;
      const vars = context.flags.vars;

      let templateFolder = path.join(os.homedir(), '.sfdx-templates', template);
      if (!fse.existsSync(templateFolder)) {
        templateFolder = path.join(__dirname, '../templates', template);
      }

      codeCreate.createFiles(templateFolder, name, template, vars, outputdir, (err, success) => {

        if (err) {
          console.error('ERROR:', err);
          process.exit(1);
        }

        console.log(success);
      });
    }
  };
}());

<|start_filename|>test/test.js<|end_filename|>
const assert = require('chai').assert;

(function () {
  'use strict';

  before(() => {

  });

  describe('files', () => {

    it('initial test', (done) => {

        assert(true, true);
        done();

    });

  });

  after(() => {

  });

}());

<|start_filename|>lib/code_create.js<|end_filename|>
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');

function updateContent(content, values) {
  const splitValues = values.split('=');

  const varName = splitValues[0];
  const varValue = splitValues[1];

  content = content.replace(new RegExp(`{{${varName}}}`, 'g'), varValue);
  return content;
}

function createFiles(templateFolder, name, template, vars, outputdir, done) {

  if (!outputdir) {
    outputdir = '.';
  }

  if (!fse.existsSync(templateFolder)) {
    done(`specified template '${template}' doesn't exist`, null);
  }

  const defJsonPath = path.join(templateFolder, 'def.json');

  if (!fse.existsSync(defJsonPath)) {
    done('def.json not found', null);
  }

  const defJson = JSON.parse(fs.readFileSync(defJsonPath).toString());
  const defJsonVars = defJson.vars;
  const defJsonBundle = defJson.bundle;
  const defJsonFiles = defJson.files;

  if (!vars) {
    done(`The following variables are required: ${defJsonVars}. Specify them like: -v className=myclass,apiName=40.0`, null);
  }

  const filesCreated = [];

  defJsonFiles.forEach((row) => {
    const fileName = row[0];
    const fileExtension = row[1];

    if (fileName !== 'def.json') {

      const templateFilePath = path.join(templateFolder, fileName);
      let content = fs.readFileSync(templateFilePath).toString();

      if (vars.includes(',')) {
        const splitVars = vars.split(',');
        splitVars.forEach((value) => {
          content = updateContent(content, value);
        });
      } else {
        content = updateContent(content, vars);
      }

      let newFile = path.join(outputdir, `${name}.${fileExtension}`);
      // if bundle flagged as true, add all the files in a folder with the same name
      if (defJsonBundle) {
        newFile = path.join(outputdir, name, `${name}.${fileExtension}`);
      } 

      const newFilePath = path.dirname(newFile);

      fse.ensureDirSync(newFilePath);
      fs.writeFileSync(newFile, content);
      filesCreated.push(newFile);
    }
  });

  let result = 'The following files were created:';
  for (let i = 0; i < filesCreated.length; i++) {
    result += `\n  ${filesCreated[i]}`;
  }

  done(null, result);
}

module.exports = {
  createFiles
};