diff options
Diffstat (limited to 'packages/create-astro/src/index.tsx')
-rw-r--r-- | packages/create-astro/src/index.tsx | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/packages/create-astro/src/index.tsx b/packages/create-astro/src/index.tsx new file mode 100644 index 000000000..0927eaae9 --- /dev/null +++ b/packages/create-astro/src/index.tsx @@ -0,0 +1,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 run = args['--run'] ?? true; + + const app = render(<App context={{ projectName, template, templates, force, run, 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], + ]) +} |