All files / src/generate-code index.ts

0% Statements 0/25
0% Branches 0/14
0% Functions 0/2
0% Lines 0/25

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                                                                                                                                                                                                                     
import * as fs from "fs";
import * as path from "path";
 
import { generateTypeScriptCodeForProject } from "./typescript";
import { Dependencies } from "../dependencies";
import { Plugin } from "../cli";
import { getProjectSetExecutions, printSetHeader } from "../sets";
import { CLI_COLOR_CYAN, CLI_FORMAT_BOLD, colorize } from "../tester/cliFormat";
 
export const ALLOWED_LANGUAGES_FOR_CODE_GENERATION = ["typescript"];
 
export interface GenerateCodeCLIOptions {
  language: string;
  outDir: string;
  tag?: string | string[];
  target?: string | string[];
  react?: boolean;
}
 
export async function generateCodeForProject(
  deps: Dependencies,
  cliOptions: GenerateCodeCLIOptions,
) {
  const { rootDirectoryPath } = deps;
 
  if (!cliOptions.language) {
    throw new Error("Option `--language` is required");
  }
 
  if (!cliOptions.outDir) {
    throw new Error("Option `--out-dir` is required");
  }
 
  const absolutePath = path.resolve(rootDirectoryPath, cliOptions.outDir);
 
  if (!fs.existsSync(absolutePath)) {
    console.log(`Creating output directory: ${colorize(absolutePath, CLI_COLOR_CYAN)}`);
    fs.mkdirSync(absolutePath, { recursive: true });
  } else {
    console.log(`Output directory already exists at: ${colorize(absolutePath, CLI_COLOR_CYAN)}`);
  }
 
  if (!ALLOWED_LANGUAGES_FOR_CODE_GENERATION.includes(cliOptions.language)) {
    console.log(CLI_FORMAT_BOLD, "Unsupported language");
    console.log(
      `Only these languages are supported: ${ALLOWED_LANGUAGES_FOR_CODE_GENERATION.join(", ")}`,
    );
 
    throw new Error(`Language ${cliOptions.language} is not supported for code generation`);
  }
 
  if (cliOptions.language === "typescript") {
    return await generateTypeScriptCodeForProject(deps, absolutePath, {
      tag: cliOptions.tag,
      target: cliOptions.target,
      react: cliOptions.react,
    });
  }
 
  throw new Error(`Language ${cliOptions.language} is not supported`);
}
 
export const generateCodePlugin: Plugin = {
  command: "generate-code",
  handler: async function ({ rootDirectoryPath, projectConfig, datasource, parsed }) {
    if (projectConfig.sets && !parsed.set) {
      throw new Error("Pass --set=<set> when generating code in a project with sets enabled.");
    }
 
    const executions = await getProjectSetExecutions(projectConfig, datasource, parsed.set);
 
    for (const execution of executions) {
      printSetHeader(projectConfig, execution.set);
 
      await generateCodeForProject(
        {
          rootDirectoryPath,
          projectConfig: execution.projectConfig,
          datasource: execution.datasource,
          options: parsed,
        },
        {
          language: parsed.language,
          outDir: parsed.outDir,
          tag: parsed.tag,
          target: parsed.target,
          react: parsed.react,
        },
      );
    }
  },
  examples: [
    {
      command: "generate-code --language typescript --out-dir src/generated",
      description: "Generate TypeScript code for the project",
    },
    {
      command: "generate-code --language typescript --out-dir src/generated --tag web --react",
      description: "Generate TypeScript and React helper code for tagged features",
    },
    {
      command: "generate-code --language typescript --out-dir src/generated --target web",
      description: "Generate TypeScript code for features selected by a target",
    },
  ],
};