diff options
Diffstat (limited to '')
-rw-r--r-- | packages/create-astro/src/index.ts | 87 | ||||
-rw-r--r-- | packages/create-astro/src/index.tsx | 46 |
2 files changed, 87 insertions, 46 deletions
diff --git a/packages/create-astro/src/index.ts b/packages/create-astro/src/index.ts new file mode 100644 index 000000000..5bf16e490 --- /dev/null +++ b/packages/create-astro/src/index.ts @@ -0,0 +1,87 @@ +import fs from 'fs'; +import path from 'path'; +import { bold, cyan, gray, green, red } from 'kleur/colors'; +import prompts from 'prompts'; +import degit from 'degit'; +import yargs from 'yargs-parser'; +import { TEMPLATES } from './templates'; +const args = yargs(process.argv); +prompts.override(args); + +export function mkdirp(dir: string) { + try { + fs.mkdirSync(dir, { recursive: true }); + } catch (e) { + if (e.code === 'EEXIST') return; + throw e; + } +} + + +const { version } = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8')); + +export async function main() { + console.log('\n' + bold('Welcome to Astro!') + gray(` (create-astro v${version})`)); + console.log(`If you encounter a problem, visit ${cyan('https://github.com/snowpack/astro/issues')} to search or file a new issue.\n`); + + console.log(green(`>`) + gray(` Prepare for liftoff.`)); + console.log(green(`>`) + gray(` Gathering mission details...`)); + + const cwd = args['_'][2] || '.'; + if (fs.existsSync(cwd)) { + if (fs.readdirSync(cwd).length > 0) { + const response = await prompts({ + type: 'confirm', + name: 'forceOverwrite', + message: 'Directory not empty. Continue?', + initial: false + }); + if (!response.forceOverwrite) { + process.exit(1); + } + } + } else { + mkdirp(cwd); + } + + const options = /** @type {import('./types/internal').Options} */ (await prompts([ + { + type: 'select', + name: 'template', + message: 'Which app template would you like to use?', + choices: TEMPLATES + }, + ])); + + const emitter = degit(`snowpackjs/astro/examples/${options.template}`, { + cache: false, + force: true, + verbose: false, + }); + + try { + // emitter.on('info', info => { console.log(info.message) }); + await emitter.clone(cwd); + } catch (err) { + // degit is compiled, so the stacktrace is pretty noisy. Just report the message. + console.error(red(err.message)); + process.exit(1); + } + + console.log(bold(green('✔ Copied project files'))); + + console.log('\nNext steps:'); + let i = 1; + + const relative = path.relative(process.cwd(), cwd); + if (relative !== '') { + console.log(` ${i++}: ${bold(cyan(`cd ${relative}`))}`); + } + + console.log(` ${i++}: ${bold(cyan('npm install'))} (or pnpm install, yarn, etc)`); + console.log(` ${i++}: ${bold(cyan('git init && git add -A && git commit -m "Initial commit"'))} (optional step)`); + console.log(` ${i++}: ${bold(cyan('npm start'))} (or pnpm, yarn, etc)`); + + console.log(`\nTo close the dev server, hit ${bold(cyan('Ctrl-C'))}`); + console.log('\nStuck? Visit us at https://astro.build/chat\n'); +}
\ No newline at end of file diff --git a/packages/create-astro/src/index.tsx b/packages/create-astro/src/index.tsx deleted file mode 100644 index 631079083..000000000 --- a/packages/create-astro/src/index.tsx +++ /dev/null @@ -1,46 +0,0 @@ -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], - ]); -} |