summaryrefslogtreecommitdiff
path: root/packages/create-astro/src/index.tsx
blob: 631079083d05e50b5978f625db0603bf6fdbd5e3 (plain) (blame)
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
import 'source-map-support/register.js';
import React from 'react';
import App from './components/App';
import Version from './components/Version';
import Exit from './components/Exit';
import { render } from 'ink';
import { getTemplates, addProcessListeners } from './utils';
import { args as argsConfig } from './config';
import arg from 'arg';
import Help from './components/Help';

/** main `create-astro` CLI */
export default async function createAstro() {
  const args = arg(argsConfig);
  const projectName = args._[0];
  if (args['--version']) {
    return render(<Version />);
  }
  const templates = await getTemplates();
  if (args['--help']) {
    return render(<Help context={{ templates }} />);
  }

  const pkgManager = /yarn/.test(process.env.npm_execpath) ? 'yarn' : 'npm';
  const use = (args['--use'] ?? pkgManager) as 'npm' | 'yarn';
  const template = args['--template'];
  const force = args['--force'];
  const skipInstall = args['--skip-install'];

  const app = render(<App context={{ projectName, template, templates, force, skipInstall, use }} />);

  const onError = () => {
    if (app) app.clear();
    render(<Exit didError />);
  };
  const onExit = () => {
    if (app) app.clear();
    render(<Exit />);
  };
  addProcessListeners([
    ['uncaughtException', onError],
    ['exit', onExit],
    ['SIGINT', onExit],
    ['SIGTERM', onExit],
  ]);
}