All files / scripts/gen-script index.js

2.27% Statements 1/44
0% Branches 0/7
0% Functions 0/5
2.33% Lines 1/43

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 951x                                                                                                                                                                                            
module.exports = async function(files = [], options) {
  const path = require('path');
  const _ = require('lodash');
  const fs = require('fs-extra');
 
  const { getAllFiles, printOrSilent } = require('@haechi-labs/vvisp-utils');
  const { getJsApis, render, rollingUp } = require('./utils');
  const generateApis = require('./generateApis');
 
  options = require('../utils/injectConfig')(options);
 
  const TEMPLATE = {
    backScript: path.join(__dirname, './template/script.mustache'),
    backIndex: path.join(__dirname, './template/index.mustache'),
    frontScript: path.join(__dirname, './template/front.mustache'),
    frontIndex: path.join(__dirname, './template/frontIndex.mustache'),
    exampleApiUser: path.join(__dirname, './template/exampleApiUser.js')
  };
 
  if (files.length === 0) {
    files.push(path.join('./', 'contracts'));
  }
 
  for (let i = 0; i < files.length; i++) {
    const file = files[i];
    if (fs.statSync(file).isDirectory()) {
      files = _.without(files, file);
      files = _.union(
        files,
        getAllFiles(file, filePath => {
          return path.parse(filePath).ext === '.sol';
        })
      );
    }
  }
 
  if (options.front) {
    await atFront(files, options);
  } else {
    await atBack(files, options);
  }
 
  async function atBack(files, options) {
    const { rootDir, abiDir, jsDir } = setDir('back');
 
    await generateApis(files, abiDir, jsDir, TEMPLATE.backScript, options);
    render(
      getJsApis(jsDir),
      path.join(rootDir, 'index.js'),
      TEMPLATE.backIndex
    );
 
    fs.copySync(TEMPLATE.exampleApiUser, path.join('./exampleApiUser.js'));
    printOrSilent('\nGenerate Finished!\n', options);
 
    printOrSilent('Api files are generated at contractApis/');
    printOrSilent(
      'To use generated apis, see generated exampleApiUser.js file in root dir.\n'
    );
  }
 
  async function atFront(files, options) {
    const { rootDir, abiDir, jsDir } = setDir('front');
 
    const name = options.front;
 
    await generateApis(files, abiDir, jsDir, TEMPLATE.frontScript, options);
 
    const apis = {
      name: name,
      ...getJsApis(jsDir)
    };
 
    render(apis, path.join(jsDir, name + '.js'), TEMPLATE.frontIndex);
 
    printOrSilent('\nGenerate Finished!\n', options);
 
    printOrSilent('Now Rollup Modules...', options);
    await rollingUp(rootDir, jsDir, name);
    printOrSilent('Rollup Finished!', options);
  }
 
  function setDir(dirName) {
    const rootDir = path.join(`./contractApis/${dirName}`);
    const abiDir = path.join(rootDir, 'abi');
    const jsDir = path.join(rootDir, 'js');
 
    fs.ensureDirSync(rootDir);
    fs.ensureDirSync(abiDir);
    fs.ensureDirSync(jsDir);
 
    return { rootDir, abiDir, jsDir };
  }
};