Skip to content

Commit

Permalink
Move create-astro async tasks to end (#9470)
Browse files Browse the repository at this point in the history
* Upgrade @astrojs/cli-kit

* Add tasks to context

* Change steps to use tasks

* Do tasks at end

* Add changeset

* Make labels more consistent

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>

* Update .changeset/chatty-penguins-sin.md

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
  • Loading branch information
3 people authored Jan 3, 2024
1 parent b8a6fa8 commit 607303b
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-penguins-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': minor
---

Improves the `create astro` CLI experience by asking all the questions upfront, then creating your new Astro project based on your responses.
2 changes: 1 addition & 1 deletion packages/create-astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"//a": "MOST PACKAGES SHOULD GO IN DEV_DEPENDENCIES! THEY WILL BE BUNDLED.",
"//b": "DEPENDENCIES IS FOR UNBUNDLED PACKAGES",
"dependencies": {
"@astrojs/cli-kit": "^0.3.1",
"@astrojs/cli-kit": "^0.4.0",
"giget": "1.1.3"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/create-astro/src/actions/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prompt } from '@astrojs/cli-kit';
import { prompt, type Task } from '@astrojs/cli-kit';
import { random } from '@astrojs/cli-kit/utils';
import arg from 'arg';
import os from 'node:os';
Expand Down Expand Up @@ -26,6 +26,7 @@ export interface Context {
stdout?: typeof process.stdout;
exit(code: number): never;
hat?: string;
tasks: Task[];
}

export async function getContext(argv: string[]): Promise<Context> {
Expand Down Expand Up @@ -103,6 +104,7 @@ export async function getContext(argv: string[]): Promise<Context> {
exit(code) {
process.exit(code);
},
tasks: [],
};
return context;
}
Expand Down
9 changes: 5 additions & 4 deletions packages/create-astro/src/actions/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { color } from '@astrojs/cli-kit';
import fs from 'node:fs';
import path from 'node:path';
import { error, info, spinner, title } from '../messages.js';
import { error, info, title } from '../messages.js';
import { shell } from '../shell.js';
import type { Context } from './context.js';

export async function dependencies(
ctx: Pick<Context, 'install' | 'yes' | 'prompt' | 'packageManager' | 'cwd' | 'dryRun'>
ctx: Pick<Context, 'install' | 'yes' | 'prompt' | 'packageManager' | 'cwd' | 'dryRun' | 'tasks'>
) {
let deps = ctx.install ?? ctx.yes;
if (deps === undefined) {
Expand All @@ -24,8 +24,9 @@ export async function dependencies(
if (ctx.dryRun) {
await info('--dry-run', `Skipping dependency installation`);
} else if (deps) {
await spinner({
start: `Installing dependencies with ${ctx.packageManager}...`,
ctx.tasks.push({
pending: 'Dependencies',
start: `Dependencies installing with ${ctx.packageManager}...`,
end: 'Dependencies installed',
onError: (e) => {
error('error', e);
Expand Down
9 changes: 6 additions & 3 deletions packages/create-astro/src/actions/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import path from 'node:path';
import type { Context } from './context.js';

import { color } from '@astrojs/cli-kit';
import { error, info, spinner, title } from '../messages.js';
import { error, info, title } from '../messages.js';
import { shell } from '../shell.js';

export async function git(ctx: Pick<Context, 'cwd' | 'git' | 'yes' | 'prompt' | 'dryRun'>) {
export async function git(
ctx: Pick<Context, 'cwd' | 'git' | 'yes' | 'prompt' | 'dryRun' | 'tasks'>
) {
if (fs.existsSync(path.join(ctx.cwd, '.git'))) {
await info('Nice!', `Git has already been initialized`);
return;
Expand All @@ -26,7 +28,8 @@ export async function git(ctx: Pick<Context, 'cwd' | 'git' | 'yes' | 'prompt' |
if (ctx.dryRun) {
await info('--dry-run', `Skipping Git initialization`);
} else if (_git) {
await spinner({
ctx.tasks.push({
pending: 'Git',
start: 'Git initializing...',
end: 'Git initialized',
while: () =>
Expand Down
7 changes: 4 additions & 3 deletions packages/create-astro/src/actions/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { color } from '@astrojs/cli-kit';
import { downloadTemplate } from 'giget';
import fs from 'node:fs';
import path from 'node:path';
import { error, info, spinner, title } from '../messages.js';
import { error, info, title } from '../messages.js';

export async function template(
ctx: Pick<Context, 'template' | 'prompt' | 'yes' | 'dryRun' | 'exit'>
ctx: Pick<Context, 'template' | 'prompt' | 'yes' | 'dryRun' | 'exit' | 'tasks'>
) {
if (!ctx.template && ctx.yes) ctx.template = 'basics';

Expand All @@ -32,7 +32,8 @@ export async function template(
if (ctx.dryRun) {
await info('--dry-run', `Skipping template copying`);
} else if (ctx.template) {
await spinner({
ctx.tasks.push({
pending: 'Template',
start: 'Template copying...',
end: 'Template copied',
while: () =>
Expand Down
15 changes: 12 additions & 3 deletions packages/create-astro/src/actions/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import { color } from '@astrojs/cli-kit';
import { readFile, rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
import stripJsonComments from 'strip-json-comments';
import { error, info, spinner, title, typescriptByDefault } from '../messages.js';
import { error, info, title, typescriptByDefault } from '../messages.js';
import { shell } from '../shell.js';

type PickedTypeScriptContext = Pick<
Context,
'typescript' | 'yes' | 'prompt' | 'dryRun' | 'cwd' | 'exit' | 'packageManager' | 'install'
| 'typescript'
| 'yes'
| 'prompt'
| 'dryRun'
| 'cwd'
| 'exit'
| 'packageManager'
| 'install'
| 'tasks'
>;

export async function typescript(ctx: PickedTypeScriptContext) {
Expand Down Expand Up @@ -61,7 +69,8 @@ export async function typescript(ctx: PickedTypeScriptContext) {
if (ts === 'relaxed' || ts === 'default') {
ts = 'base';
}
await spinner({
ctx.tasks.push({
pending: 'TypeScript',
start: 'TypeScript customizing...',
end: 'TypeScript customized',
while: () =>
Expand Down
14 changes: 13 additions & 1 deletion packages/create-astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { template } from './actions/template.js';
import { setupTypeScript, typescript } from './actions/typescript.js';
import { verify } from './actions/verify.js';
import { setStdout } from './messages.js';
import { tasks } from '@astrojs/cli-kit';

const exit = () => process.exit(0);
process.on('SIGINT', exit);
Expand Down Expand Up @@ -43,12 +44,23 @@ export async function main() {

// Steps which write to files need to go above git
git,
next,
];

for (const step of steps) {
await step(ctx);
}

// eslint-disable-next-line no-console
console.log('');

const labels = {
start: 'Project initializing...',
end: 'Project initialized!',
};
await tasks(labels, ctx.tasks);

await next(ctx);

process.exit(0);
}

Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 607303b

Please sign in to comment.