All files create.ts

100% Statements 41/41
82.14% Branches 23/28
100% Functions 3/3
100% Lines 41/41

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                                      10x 10x 10x 3x               3x   7x 2x     2x   5x 5x 4x 3x 1x 1x         1x   4x 4x 4x     54x 48x       6x     2x   2x   2x 2x 2x 2x 2x   2x 2x 2x 8x 8x     2x 2x 2x 2x   2x 2x   2x         2x      
import fs from 'fs-extra';
import { ParsedArgs } from 'minimist';
import path from 'path';
import download from 'download';
import ora from 'ora';
 
export type CreateOptions = {
  appName?: string;
  f?: boolean;
  force?: boolean;
  e?: string;
  example?: string;
  o?: string;
  output?: string;
  p?: string;
  path?: string;
} & Omit<ParsedArgs, '_'>;
 
export async function create(argv: CreateOptions, exampleHelp: string) {
  const spinner = ora('Downloading Example.');
  try {
    if (!argv.appName || !/^[A-Za-z0-9_\-\.]{1,}$/.test(argv.appName)) {
      console.log(`\n  \x1b[31mPlease specify the project directory name\x1b[0m.
        ${
          !/^[A-Za-z0-9_\-\.]{1,}$/.test(argv.appName)
            ? `\x1b[31mThe name directory name\x1b[0m \x1b[33m${argv.appName}\x1b[0m \x1b[31mcontains special characters.\x1b[0m`
            : ''
        }
        ${exampleHelp || ''}
      `);
      return;
    }
    if (!argv.path || typeof argv.path !== 'string') {
      console.log(
        `\n  Uh oh! \x1b[31mPlease specify download address\x1b[0m. ${exampleHelp || ''}`,
      );
      return;
    }
    const projectPath = path.join(process.cwd(), argv.output, argv.appName);
    if (argv.force) {
      await fs.remove(projectPath);
      await fs.ensureDir(projectPath);
    } else Eif (fs.existsSync(projectPath)) {
      console.log(`
        Uh oh! Looks like there's already a directory called \x1b[31m${argv.appName}\x1b[0m\n
        \x1b[33mPlease try a different name or delete that folder.\x1b[0m\n
          Path: \x1b[33m${projectPath}\x1b[0m\n
      `);
      process.exit(1);
    }
    await fs.ensureDir(projectPath);
    spinner.start(`\nDownloading \x1b[32m${argv.example}.zip\x1b[0m example.`);
    await download(`${argv.path}${argv.example}.zip`, projectPath, {
      extract: true,
    }).on('downloadProgress', (progress) => {
      if (progress.percent !== 1) {
        spinner.text = `The example \x1b[32m${argv.example}.zip\x1b[0m has been downloaded ${(
          progress.percent * 100
        ).toFixed(0)}%.`;
      } else {
        spinner.text = `Unzip the \x1b[32m${argv.example}.zip\x1b[0m file.`;
      }
    });
    spinner.succeed(`Creating a new app in \x1b[32m${projectPath}\x1b[0m! \n`);
 
    const pkgPath = path.resolve(projectPath, 'package.json');
 
    let logstr = `Success! Created \x1b[35m${argv.appName}\x1b[0m at \x1b[32m${projectPath}\x1b[0m\n`;
    Eif (fs.existsSync(pkgPath)) {
      const pkg = require(pkgPath);
      Eif (pkg.version) {
        await fs.writeJSON(pkgPath, { ...pkg, version: '1.0.0' }, { spaces: '  ' });
      }
      logstr += `\nInside that directory, you can run several commands:\n`;
      Eif (pkg.scripts) {
        Object.keys(pkg.scripts).forEach((keyname) => {
          logstr += `   \x1b[36myarn run ${keyname}\x1b[0m\n`;
          logstr += `    └─> ${pkg.scripts[keyname]}\n`;
        });
      }
      logstr += '\nWe suggest that you begin by typing:\n';
      logstr += `   \x1b[36mcd ${argv.appName}\x1b[0m\n`;
      logstr += '   \x1b[36myarn install\x1b[0m\n';
      logstr += '   \x1b[36myarn build\x1b[0m && \x1b[36myarn start\x1b[0m\n';
    }
    logstr += '\nHappy hacking!\n';
    console.log(logstr);
  } catch (error) {
    spinner.fail(`\x1b[31m${error.message}\x1b[0m\n  Error: \x1b[31m${
      error.statusCode || '000'
    }\x1b[0m, The example \x1b[31m${argv.example}.zip\x1b[0m does not exist.
    \n  Download link: \x1b[31m${argv.path}${argv.example}.zip\x1b[0m
    `);
    process.exit(1);
  }
}