All files generate.ts

84.12% Statements 53/63
66.66% Branches 10/15
80% Functions 4/5
84.12% Lines 53/63

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  1x 3x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                               1x 1x 3x 3x 2x     1x     1x 3x 3x 2x   1x 1x                       1x 1x 1x 1x         1x 1x   1x 1x     1x 1x 1x 1x         1x             1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x   1x                
import chalk from "chalk";
import inquirer from "inquirer";
import { command, Options } from "execa";
import ora from "ora";
import { join, resolve } from "path";
import { replaceInFile } from "replace-in-file";
 
export const generateProject = async (): Promise<void> => {
  const teal = chalk.rgb(9, 228, 163);
  const blue = chalk.rgb(32, 98, 250);
 
  console.info(blue.bold("Neoxia open-source"));
  console.info(teal.bold("\nCreate a new library 🧙\n"));
 
  const answers = await inquirer.prompt([
    {
      message: teal("What kind of library do you want to create ?"),
      name: "type",
      type: "list",
      choices: [
        { name: "NodeJS Library", value: "node" },
        { name: "Angular Library", value: "angular" },
      ],
    },
    {
      message: teal("How do you want to call it ?"),
      default: "my-awesome-nx-lib",
      name: "name",
      type: "input",
      validate: (input: string) => {
        if (
          !input.match(/^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/)
        ) {
          return "Please provide a valid npm package name";
        }
        return true;
      },
    },
  ]);
 
  let selectedPackageManager: string;
  const installedPackageManagers: string[] = [];
  const hasCommand = async (cmd: string): Promise<boolean> => {
    try {
      await command(cmd);
      return true;
    } catch (_e) {
      return false;
    }
  };
  await Promise.all(
    ["npm", "yarn", "pnpm"].map(async (packageManager) => {
      const isInstalled = await hasCommand(`${packageManager} -v`);
      if (isInstalled) installedPackageManagers.push(packageManager);
    }),
  );
  if (installedPackageManagers.length > 1) {
    selectedPackageManager = (
      await inquirer.prompt([
        {
          message: teal("Which package manager do you want to use ?"),
          name: "packageManager",
          type: "list",
          choices: installedPackageManagers,
        },
      ])
    ).pacEkageManager;
  } else {
    selectedPackageManager = installedPackageManagers[0];
  }
  process.stdout.write("\n");
 
  const cloning = ora();
  const dest = resolve(join(process.cwd(), answers.name));
  const execaOptions: Options = {
    cwd: dest,
    stdio: process.env.DEBUG_CREATE_NX_LIB ? "inherit" : "ignore",
    shell: true,
  };
  try {
    cloning.start(teal.bold(`Creating project in ${dest} ✨`));
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const degit = require("tiged");
    const cloner = degit(`neoxia/create-nx-lib/starters/${answers.type}`, {
      mode: "git",
    });
    await cloner.clone(dest);
    await command("git init", execaOptions);
    await command(
      'npm pkg set scripts.prepare="npx\\ husky\\ install"',
      execaOptions,
    );
    await replaceInFile({
      files: `${dest}/**`,
      from: /my-awesome-nx-lib/g,
      to: answers.name,
    });
    cloning.succeed(teal.bold("Project initialized ✨"));
  } catch (e) {
    cloning.fail(chalk.red.bold("Failed to create project"));
    console.error(e);
    process.exit(1);
  }
 
  const installing = ora();
  installing.start(teal.bold("Installing dependencies 📦"));
  try {
    await command(`${selectedPackageManager} install`, execaOptions);
    installing.succeed(teal.bold("Dependencies installed 📦"));
  } catch (e) {
    installing.fail(
      chalk.red.bold("Failed to install dependencies, you must do it yourself"),
    );
    console.error(e);
    process.exit(1);
  }
  process.stdout.write("\n");
  console.info(
    `cd ./${answers.name} ${chalk.grey("# you'll find your project here")}`,
  );
  console.info(`npm build ${chalk.grey("# build your library")}`);
  console.info(`npm test ${chalk.grey("# run unit tests")}`);
  process.stdout.write("\n");
  console.info(teal.bold("We are all set 🚀 Happy coding !"));
};