All files / src/modules parseArgs.ts

60% Statements 33/55
51.61% Branches 32/62
100% Functions 2/2
58.49% Lines 31/53

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 1861x 1x 1x               1x 3x   3x 12x 12x 12x 24x 24x 24x                   12x     3x                                                                                                       3x   3x     3x                                 3x                                                                           3x       3x     3x     3x       3x       3x       3x 3x             3x 6x 6x         3x   3x    
import * as fs from 'fs';
import * as path from 'path';
import * as commander from 'commander';
import Args from '../interface/Args';
import SceneExporterPlugin from '../interface/SceneExporterPlugin';
import AssetExporterPlugin from '../interface/AssetExporterPlugin';
 
/**
 * Parses CLI argument via process.env and convert to Args type.
 */
export default function parseArgs(): Args {
  const packageJson = require('../../package.json');
 
  const spaceSeparatedPaths = (value: string): string[] => {
    const parts = [];
    const frags = value.split(' ');
    for (let i = 0; i < frags.length; i++) {
      const frag = frags[i];
      Eif (fs.existsSync(frag)) {
        parts.push(frag);
      } else {
        const nextFrag = frags[i + 1];
        if (!nextFrag) {
          break;
        }
        frags[i + 1] = `${frag} ${nextFrag}`;
      }
    }
 
    return parts;
  };
 
  commander
    .version(packageJson.version)
    .option(
      '-c, --config [value]',
      'config file path'
    )
    .option(
      '-r, --runtime [value]',
      'runtime identifier'
    )
    .option(
      '-a, --assetRoot [value]',
      'root directory for assets'
    )
    .option(
      '-s, --sceneFiles [value]',
      'exporting scene files (space separated)',
      spaceSeparatedPaths
    )
    .option(
      '-d, --destDir [value]',
      "destination directory           default './scene-graph'"
    )
    .option(
      '--assetDestDir [value]',
      'asset destination directory     default \${DEST}/\${ASSET_NAME_SPACE}'
    )
    .option(
      '--assetNameSpace [value]',
      "asset directory name            default 'assets'"
    )
    .option(
      '-p, --plugins [value]',
      "plugin names (space separated)  default ''",
      spaceSeparatedPaths
    ).option(
      '--listRuntimes',
      'list available runtimes'
    )
    .parse(process.argv);
 
  // passing option via process.env is deprecated
 
  let config: {
    runtime?: string;
    assetRoot?: string;
    sceneFiles?: string | string[];
    destDir?: string;
    assetDestDir?: string;
    assetNameSpace?: string;
    plugins?: string | SceneExporterPlugin | AssetExporterPlugin |
    string[] | SceneExporterPlugin[] | AssetExporterPlugin[];
  } = {};
 
  let configFilePath = '';
 
  // using -c option
  Iif (commander.config) {
    const configPath = path.resolve(process.cwd(), commander.config);
    const userConfigFactory = require(configPath);
    config = userConfigFactory();
    if (config.sceneFiles && !Array.isArray(config.sceneFiles)) {
      config.sceneFiles = [config.sceneFiles];
    }
    if (config.plugins && !Array.isArray(config.plugins)) {
      const plugin = config.plugins as string | SceneExporterPlugin | AssetExporterPlugin;
      config.plugins = [plugin] as string[] | SceneExporterPlugin[] | AssetExporterPlugin[];
    }
 
    configFilePath = path.dirname(commander.config);
  }
 
  // priority
  // cli option > user config > env
  const args: Args = {
    runtime:
      commander.runtime
      || config.runtime
      || process.env.RUNTIME
      || '',
    assetRoot:
      commander.assetRoot
      || config.assetRoot
      || process.env.ASSET_ROOT
      || '',
    sceneFiles:
      commander.sceneFiles
      || config.sceneFiles
      || (process.env.SCENE_FILE ? spaceSeparatedPaths(process.env.SCENE_FILE) : []),
    destDir:
      commander.destDir
      || config.destDir
      || process.env.DEST
      || path.resolve(process.cwd(), 'scene-graph'),
    assetDestDir:
      commander.assetDestDir
      || config.assetDestDir
      || process.env.ASSET_DEST
      || '',
    assetNameSpace:
      commander.assetNameSpace
      || config.assetNameSpace
      || process.env.ASSET_NAME_SPACE
      || 'assets',
    plugins:
      commander.plugins
      || config.plugins
      || (process.env.PLUGINS ? spaceSeparatedPaths(process.env.PLUGINS) : []),
    listRuntimes:
      commander.listRuntimes
  };
 
  Iif (args.listRuntimes) {
    return args;
  }
 
  Iif (!args.runtime) {
    throw new Error('runtime option is required');
  }
  Iif (!args.assetRoot) {
    throw new Error('assetRoot option is required');
  }
  Iif (args.sceneFiles.length === 0) {
    throw new Error('sceneFiles option is required');
  }
 
  Iif (!path.isAbsolute(args.assetRoot)) {
    args.assetRoot = path.resolve(process.cwd(), configFilePath, args.assetRoot);
  }
 
  Iif (!path.isAbsolute(args.destDir)) {
    args.destDir = path.resolve(process.cwd(), args.destDir);
  }
 
  Eif (args.assetDestDir) {
    Iif (!path.isAbsolute(args.assetDestDir)) {
      args.assetDestDir = path.resolve(args.destDir, args.assetNameSpace, args.assetDestDir);
    }
  } else {
    args.assetDestDir = path.resolve(args.destDir, args.assetNameSpace);
  }
 
  for (let i = 0; i < args.sceneFiles.length; i++) {
    const sceneFile = args.sceneFiles[i];
    Iif (!path.isAbsolute(sceneFile)) {
      args.sceneFiles[i] = path.resolve(process.cwd(), configFilePath, sceneFile);
    }
  }
 
  args.assetRoot = args.assetRoot.replace(/\/$/, '');
 
  return args;
}