All files / src/bin gb-studio-cli.ts

0% Statements 0/85
0% Branches 0/21
0% Functions 0/9
0% Lines 0/84

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                                                                                                                                                                                                                                                                                                                                                                   
import { copy, readFile, writeFile } from "fs-extra";
import Path from "path";
import os from "os";
import rimraf from "rimraf";
import { promisify } from "util";
import { program } from "commander";
import { binjgbRoot } from "consts";
import initElectronL10N from "lib/lang/initElectronL10N";
import { loadEngineFields } from "lib/project/engineFields";
import { loadSceneTypes } from "lib/project/sceneTypes";
import loadProject from "lib/project/loadProjectData";
import { decompressProjectResources } from "shared/lib/resources/compression";
import { buildRunner } from "lib/compiler/buildRunner";
import { BuildType } from "lib/compiler/buildWorker";
 
const rmdir = promisify(rimraf);
 
declare const VERSION: string;
 
type Command = "export" | "make:rom" | "make:pocket" | "make:web";
 
const buildTypeForCommand = (command: Command): BuildType => {
  Iif (command === "make:web") {
    return "web";
  }
  Iif (command === "make:pocket") {
    return "pocket";
  }
  return "rom";
};
 
const main = async (
  command: Command,
  projectFile: string,
  destination: string
) => {
  initElectronL10N();
 
  // Load project file
  const projectRoot = Path.resolve(Path.dirname(projectFile));
  const loadedProject = await loadProject(projectFile);
  const project = decompressProjectResources(loadedProject.resources);
  const buildType = buildTypeForCommand(command);
 
  // Load engine fields
  const engineFields = await loadEngineFields(projectRoot);
  const sceneTypes = await loadSceneTypes(projectRoot);
 
  // Use OS default tmp
  const tmpPath = os.tmpdir();
  const tmpBuildDir = Path.join(tmpPath, "_gbsbuild");
  const outputRoot = tmpBuildDir;
 
  const progress = (message: string) => {
    Iif (program.verbose) {
      console.log(message);
    }
  };
 
  const warnings = (message: string) => {
    Iif (program.verbose) {
      console.warn(message);
    }
  };
 
  const { result } = buildRunner({
    project,
    buildType,
    projectRoot,
    engineFields,
    sceneTypes,
    tmpPath,
    outputRoot,
    debugEnabled: project.settings.debuggerEnabled,
    make: command !== "export",
    progress,
    warnings,
  });
 
  await result;
 
  const colorOnly = project.settings.colorMode === "color";
  const gameFile = colorOnly ? "game.gbc" : "game.gb";
 
  if (command === "export") {
    if (program.onlyData) {
      // Export src/data and include/data to destination
      const dataSrcTmpPath = Path.join(tmpBuildDir, "src", "data");
      const dataSrcOutPath = Path.join(destination, "src", "data");
      const dataIncludeTmpPath = Path.join(tmpBuildDir, "include", "data");
      const dataIncludeOutPath = Path.join(destination, "include", "data");
      await rmdir(dataSrcOutPath);
      await rmdir(dataIncludeOutPath);
      await copy(dataSrcTmpPath, dataSrcOutPath);
      await copy(dataIncludeTmpPath, dataIncludeOutPath);
    } else {
      // Export GBDK project to destination
      await copy(tmpBuildDir, destination);
    }
  } else if (command === "make:rom") {
    const romTmpPath = Path.join(tmpBuildDir, "build", "rom", gameFile);
    await copy(romTmpPath, destination);
  } else if (command === "make:pocket") {
    const romTmpPath = Path.join(tmpBuildDir, "build", "rom", "game.pocket");
    await copy(romTmpPath, destination);
  } else Iif (command === "make:web") {
    const romTmpPath = Path.join(tmpBuildDir, "build", "rom", gameFile);
    await copy(binjgbRoot, destination);
    await copy(romTmpPath, `${destination}/rom/${gameFile}`);
    const sanitize = (s: string) => String(s || "").replace(/["<>]/g, "");
    const projectName = sanitize(project.metadata.name);
    const author = sanitize(project.metadata.author);
    const colorsHead =
      project.settings.colorMode !== "mono"
        ? `<style type="text/css"> body { background-color:#${project.settings.customColorsBlack}; }</style>`
        : "";
    const customHead = project.settings.customHead || "";
    const customControls = JSON.stringify({
      up: project.settings.customControlsUp,
      down: project.settings.customControlsDown,
      left: project.settings.customControlsLeft,
      right: project.settings.customControlsRight,
      a: project.settings.customControlsA,
      b: project.settings.customControlsB,
      start: project.settings.customControlsStart,
      select: project.settings.customControlsSelect,
    });
    const html = (await readFile(`${destination}/index.html`, "utf8"))
      .replace(/___PROJECT_NAME___/g, projectName)
      .replace(/___AUTHOR___/g, author)
      .replace(/___COLORS_HEAD___/g, colorsHead)
      .replace(/___PROJECT_HEAD___/g, customHead)
      .replace(/___CUSTOM_CONTROLS___/g, customControls);
 
    const scriptJs = (
      await readFile(`${destination}/js/script.js`, "utf8")
    ).replace(/ROM_FILENAME = "[^"]*"/g, `ROM_FILENAME = "rom/${gameFile}"`);
 
    await writeFile(`${destination}/index.html`, html);
    await writeFile(`${destination}/js/script.js`, scriptJs);
  }
};
 
program.version(VERSION);
 
program
  .command("export <projectFile> <destination>")
  .description("Export a project file to a GBDK project with engine and data")
  .action((source, destination) => {
    main("export", source, destination);
  });
 
program
  .command("make:rom <projectFile> <destination.gb>")
  .description("Build a ROM from project file")
  .action((source, destination) => {
    main("make:rom", source, destination);
  });
 
program
  .command("make:pocket <projectFile> <destination.pocket>")
  .description("Build a Pocket from project file")
  .action((source, destination) => {
    main("make:pocket", source, destination);
  });
 
program
  .command("make:web <projectFile> <destination>")
  .description("Build for web from project file")
  .action((source, destination) => {
    main("make:web", source, destination);
  });
 
program.option("-d, --onlyData", "Only replace data folder in destination");
program.option("-v, --verbose", "Verbose output");
 
program.parse(process.argv);