Skip to content

Commit

Permalink
feat(create-astro): improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Sep 7, 2023
1 parent b29f144 commit beaba06
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-kings-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': minor
---

Improve startup performance by lazily initializing async contextual values
2 changes: 1 addition & 1 deletion packages/create-astro/create-astro.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const currentVersion = process.versions.node;
const requiredMajorVersion = parseInt(currentVersion.split('.')[0], 10);
const minimumMajorVersion = 14;
const minimumMajorVersion = 16;

if (requiredMajorVersion < minimumMajorVersion) {
console.error(`Node.js v${currentVersion} is out of date and unsupported!`);
Expand Down
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.2.3",
"@astrojs/cli-kit": "^0.2.5",
"execa": "^8.0.1",
"giget": "1.1.2",
"node-fetch-native": "^1.4.0",
Expand Down
12 changes: 7 additions & 5 deletions packages/create-astro/src/actions/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { prompt } from '@astrojs/cli-kit';
import { random } from '@astrojs/cli-kit/utils';
import arg from 'arg';
import os from 'node:os';
import detectPackageManager from 'which-pm-runs';
Expand All @@ -10,8 +11,8 @@ export interface Context {
prompt: typeof prompt;
cwd: string;
packageManager: string;
username: string;
version: string;
username: Promise<string>;
version: Promise<string>;
skipHouston: boolean;
fancy?: boolean;
dryRun?: boolean;
Expand All @@ -25,6 +26,7 @@ export interface Context {
stdin?: typeof process.stdin;
stdout?: typeof process.stdout;
exit(code: number): never;
hat?: string;
}

export async function getContext(argv: string[]): Promise<Context> {
Expand Down Expand Up @@ -52,7 +54,6 @@ export async function getContext(argv: string[]): Promise<Context> {
);

const packageManager = detectPackageManager()?.name ?? 'npm';
const [username, version] = await Promise.all([getName(), getVersion()]);
let cwd = flags['_'][0];
let {
'--help': help = false,
Expand Down Expand Up @@ -86,14 +87,15 @@ export async function getContext(argv: string[]): Promise<Context> {
help,
prompt,
packageManager,
username,
version,
username: getName(),
version: getVersion(),
skipHouston,
fancy,
dryRun,
projectName,
template,
ref: ref ?? 'latest',
hat: fancy ? random(['🎩', '🎩', '🎩', '🎩', '🎓', '👑', '🧢', '🍦']) : undefined,
yes,
install: install ?? (noInstall ? false : undefined),
git: git ?? (noGit ? false : undefined),
Expand Down
14 changes: 6 additions & 8 deletions packages/create-astro/src/actions/intro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ import { color, label } from '@astrojs/cli-kit';
import { random } from '@astrojs/cli-kit/utils';
import { banner, say, welcome } from '../messages.js';

export async function intro(ctx: Pick<Context, 'skipHouston' | 'version' | 'username' | 'fancy'>) {
export async function intro(ctx: Pick<Context, 'hat' | 'skipHouston' | 'version' | 'username' | 'fancy'>) {
banner();

if (!ctx.skipHouston) {
const hat = ctx.fancy ? random(['🎩', '🎩', '👑', '🧢', '🍦']) : undefined;
await say(
[
[
'Welcome',
'to',
label('astro', color.bgGreen, color.black),
(ctx.version ? color.green(`v${ctx.version}`) : '') + ',',
`${ctx.username}!`,
ctx.version.then(version => ((version ? color.green(`v${version}`) : '') + ',')),
ctx.username.then(username => `${username}!`),
],
random(welcome),
],
{ hat }
{ clear: true, hat: ctx.hat }
);
await banner(ctx.version);
} else {
await banner(ctx.version);
}
}
4 changes: 2 additions & 2 deletions packages/create-astro/src/actions/next-steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Context } from './context';

import { nextSteps, say } from '../messages.js';

export async function next(ctx: Pick<Context, 'cwd' | 'packageManager' | 'skipHouston'>) {
export async function next(ctx: Pick<Context, 'hat' | 'cwd' | 'packageManager' | 'skipHouston'>) {
let projectDir = path.relative(process.cwd(), ctx.cwd);

const commandMap: { [key: string]: string } = {
Expand All @@ -17,7 +17,7 @@ export async function next(ctx: Pick<Context, 'cwd' | 'packageManager' | 'skipHo
await nextSteps({ projectDir, devCmd });

if (!ctx.skipHouston) {
await say(['Good luck out there, astronaut! 🚀']);
await say(['Good luck out there, astronaut! 🚀'], { hat: ctx.hat });
}
return;
}
2 changes: 2 additions & 0 deletions packages/create-astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ process.on('SIGTERM', exit);
// https://github.com/withastro/docs/blob/main/src/pages/en/install/auto.md
// if you make any changes to the flow or wording here.
export async function main() {
// Clear console because PNPM startup is super ugly
console.clear();
// NOTE: In the v7.x version of npm, the default behavior of `npm init` was changed
// to no longer require `--` to pass args and instead pass `--` directly to us. This
// broke our arg parser, since `--` is a special kind of flag. Filtering for `--` here
Expand Down
11 changes: 5 additions & 6 deletions packages/create-astro/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ export const getVersion = () =>
});

export const log = (message: string) => stdout.write(message + '\n');
export const banner = async (version: string) =>
log(
`\n${label('astro', color.bgGreen, color.black)}${
version ? ' ' + color.green(color.bold(`v${version}`)) : ''
} ${color.bold('Launch sequence initiated.')}`
);
export const banner = () => {
const prefix = `astro`;
const suffix = `Launch sequence initiated.`;
log(`${label(prefix, color.bgGreen, color.black)} ${suffix}`);
}

export const bannerAbort = () =>
log(`\n${label('astro', color.bgRed)} ${color.bold('Launch sequence aborted.')}`);
Expand Down
27 changes: 17 additions & 10 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 beaba06

Please sign in to comment.