All files / platform/cli/src/commands createPackage.js

0% Statements 0/27
0% Branches 0/2
0% Functions 0/7
0% Lines 0/27

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                                                                                                                                                           
import Listr from 'listr';
import chalk from 'chalk';
import fs from 'fs';
 
import {
  createDirectoryContents,
  editPackageJson,
  createLicense,
  createReadme,
  initGit,
} from './utils/index.js';
 
const createPackage = async options => {
  const { packageType } = options; // extension or mode
 
  if (fs.existsSync(options.targetDir)) {
    console.error(
      `%s ${packageType} with the same name already exists in this directory, either delete it or choose a different name`,
      chalk.red.bold('ERROR')
    );
    process.exit(1);
  }
 
  fs.mkdirSync(options.targetDir);
 
  const tasks = new Listr(
    [
      {
        title: 'Copying template files',
        task: () =>
          createDirectoryContents(options.templateDir, options.targetDir, options.prettier),
      },
      {
        title: 'Editing Package.json with provided information',
        task: () => editPackageJson(options),
      },
      {
        title: 'Creating a License file',
        task: () => createLicense(options),
      },
      {
        title: 'Creating a Readme file',
        task: () => createReadme(options),
      },
      {
        title: 'Initializing a Git Repository',
        enabled: () => options.gitRepository,
        task: () => initGit(options),
      },
    ],
    {
      exitOnError: true,
    }
  );
 
  await tasks.run();
  console.log();
  console.log(chalk.green(`Done: ${packageType} is ready at`, options.targetDir));
  console.log();
 
  console.log(chalk.green(`NOTE: In order to use this ${packageType} for development,`));
  console.log(chalk.green(`run the following command inside the root of the OHIF monorepo`));
 
  console.log();
  console.log(chalk.green.bold(`    yarn run cli link-${packageType} ${options.targetDir}`));
  console.log();
  console.log(
    chalk.yellow("and when you don't need it anymore, run the following command to unlink it")
  );
  console.log();
  console.log(chalk.yellow(`    yarn run cli unlink-${packageType} ${options.name}`));
  console.log();
 
  return true;
};
 
export default createPackage;