<commit_before>#!/usr/bin/env node
const spawn = require("cross-spawn");
const path = require("path");
const maji_package = require("../package.json");

const parseBoolean = value => value === "true";
const parsePort = value => parseInt(value) || null;

const program = require("commander");
program.version(maji_package.version);

const runYarn = function(args, env_args = {}) {
  return runCmd("yarn", ["--silent", ...Array.from(args)], env_args);
};

const runCmd = function(cmd, args, env_args = {}) {
  let env = Object.create(process.env);
  Object.assign(env, env_args);

  let child = spawn(cmd, args, { env, stdio: "inherit" });

  if (child.stdout != null) {
    child.stdout.on("data", data => process.stdout.write(data));
  }

  if (child.stderr != null) {
    child.stderr.on("data", data => process.stderr.write(data.toString()));
  }

  return child.on("exit", exitCode => process.exit(exitCode));
};

const runScript = function(scriptName, args, env_args = {}) {
  return runCmd(
    path.resolve(__dirname + `/../script/${scriptName}`),
    args,
    env_args
  );
};

const literalArgs = function() {
  // commander.js program.args is broken for this purpose
  // https://github.com/tj/commander.js/issues/582
  if (program.rawArgs.indexOf("--") === -1) {
    return [];
  } else {
    return program.rawArgs.slice(program.rawArgs.indexOf("--") + 1);
  }
};

program
  .command("new <package_name> <path>")
  .description("Create a new Maji app")
  .on("--help", () =>
    console.log("  Example:\n  maji new org.example.my-app ~/Code/my-app")
  )
  .action(function(packageName, projectPath) {
    if (!packageName.match(/.*\..*\..*/)) {
      console.log(
        "Please specify a valid package name, for example org.example.my-app"
      );
      process.exit(1);
    }

    projectPath = path.resolve(projectPath);
    return runScript("create-project", [packageName, projectPath]);
  });

program
  .command("run <platform>")
  .description("Build and run a native app for the specified platform")
  .option("-e, --emulator", "run on emulator instead of an actual device")
  .option(
    "--env --environment [environment]",
    "NODE_ENV to run with [development]"
  )
  .action(function(platform, options) {
    const node_env =
      options.environment || process.env.NODE_ENV || "development";
    const env = {
      NODE_ENV: node_env
    };

    const deviceTypeArg = options.emulator ? "--emulator" : "--device";
    return runScript(
      "run-on-device",
      [platform, deviceTypeArg, ...Array.from(literalArgs())],
      env
    );
  });

program
  .command("build [platform]")
  .description("Build a native app for the specified platform")
  .option("--release", "create a release build")
  .option(
    "--env --environment [environment]",
    "NODE_ENV to build with [production]"
  )
  .action(function(platform, options) {
    const node_env = options.environment || "production";<commit_msg>Let `maji build` command read `NODE_ENV` from environment<commit_after>#!/usr/bin/env node
const spawn = require("cross-spawn");
const path = require("path");
const maji_package = require("../package.json");

const parseBoolean = value => value === "true";
const parsePort = value => parseInt(value) || null;

const program = require("commander");
program.version(maji_package.version);

const runYarn = function(args, env_args = {}) {
  return runCmd("yarn", ["--silent", ...Array.from(args)], env_args);
};

const runCmd = function(cmd, args, env_args = {}) {
  let env = Object.create(process.env);
  Object.assign(env, env_args);

  let child = spawn(cmd, args, { env, stdio: "inherit" });

  if (child.stdout != null) {
    child.stdout.on("data", data => process.stdout.write(data));
  }

  if (child.stderr != null) {
    child.stderr.on("data", data => process.stderr.write(data.toString()));
  }

  return child.on("exit", exitCode => process.exit(exitCode));
};

const runScript = function(scriptName, args, env_args = {}) {
  return runCmd(
    path.resolve(__dirname + `/../script/${scriptName}`),
    args,
    env_args
  );
};

const literalArgs = function() {
  // commander.js program.args is broken for this purpose
  // https://github.com/tj/commander.js/issues/582
  if (program.rawArgs.indexOf("--") === -1) {
    return [];
  } else {
    return program.rawArgs.slice(program.rawArgs.indexOf("--") + 1);
  }
};

program
  .command("new <package_name> <path>")
  .description("Create a new Maji app")
  .on("--help", () =>
    console.log("  Example:\n  maji new org.example.my-app ~/Code/my-app")
  )
  .action(function(packageName, projectPath) {
    if (!packageName.match(/.*\..*\..*/)) {
      console.log(
        "Please specify a valid package name, for example org.example.my-app"
      );
      process.exit(1);
    }

    projectPath = path.resolve(projectPath);
    return runScript("create-project", [packageName, projectPath]);
  });

program
  .command("run <platform>")
  .description("Build and run a native app for the specified platform")
  .option("-e, --emulator", "run on emulator instead of an actual device")
  .option(
    "--env --environment [environment]",
    "NODE_ENV to run with [development]"
  )
  .action(function(platform, options) {
    const node_env =
      options.environment || process.env.NODE_ENV || "development";
    const env = {
      NODE_ENV: node_env
    };

    const deviceTypeArg = options.emulator ? "--emulator" : "--device";
    return runScript(
      "run-on-device",
      [platform, deviceTypeArg, ...Array.from(literalArgs())],
      env
    );
  });

program
  .command("build [platform]")
  .description("Build a native app for the specified platform")
  .option("--release", "create a release build")
  .option(
    "--env --environment [environment]",
    "NODE_ENV to build with [production]"
  )
  .action(function(platform, options) {
    const node_env =
      options.environment || process.env.NODE_ENV || "production";
{'fraction_non_alphanumeric': 0.17417619367854742, 'fraction_numerical': 0.0020174848688634837, 'mean_word_length': 3.4197622585438334, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2034113', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1874, 'n_words': 447}