All files / src cli.ts

20.69% Statements 6/29
0% Branches 0/2
0% Functions 0/5
20.69% Lines 6/29

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 701x 1x     1x 1x 1x         1x                                                                                                                    
import * as fs from 'fs';
import * as path from 'path';
 
import Args from './interface/Args';
import ExportManager from './exporter/ExportManager';
import parseArgs from './modules/parseArgs';
import mkdirp from './modules/mkdirp';
 
/**
 * entry point for export CLI
 */
export default function cli(): void {
  let args: Args;
  try {
    args = parseArgs();
  } catch (e) {
    console.log(e.message);
    return;
  }
 
  if (args.listRuntimes) {
    const runtimes = ExportManager.getRegisteredExporterRuntimes();
    console.log('available runtime identifiers:');
    console.log(runtimes.sort().join('\n'));
    return;
  }
 
  /**
   * Instantiate exporter implement
   */
  const manager = new ExportManager();
  manager.loadPlugins(args.plugins);
 
  /**
   * Execute export
   */
  const sceneGraphs = manager.exportScene(args.runtime, args.sceneFiles, args.assetRoot);
 
  // scene graph file manages assets
  const assetExportMap = manager.exportAsset(
    sceneGraphs,
    args.runtime,
    args.assetRoot,
    args.assetDestDir,
    args.assetNameSpace
  );
 
  const newDirRoot = args.assetDestDir;
 
  mkdirp(newDirRoot);
 
  // write scene graph file
  sceneGraphs.forEach((sceneGraph, sceneFilePath) => {
    const destFileName = `${path.basename(sceneFilePath)}.json`;
    const dest = path.join(args.assetDestDir, destFileName);
    // if (debug) {
    fs.writeFile(dest, JSON.stringify(sceneGraph, null, 2), () => {});
    // } else {
    //   fs.writeFile(dest, JSON.stringify(sceneGraph), () => {});
    // }
  });
 
  // copy assets
  assetExportMap.forEach((entity) => {
    const targetDir = path.dirname(entity.localDestPath);
    mkdirp(targetDir);
    fs.copyFile(entity.localSrcPath, entity.localDestPath, () => {});
  });
}