Skip to content

Commit

Permalink
Feat: [create astro] git step (#3227)
Browse files Browse the repository at this point in the history
* feat: add git init step

* fix: update unit tests

* feat: simplify next steps for copy pasteability

* docs: add clarifying comment on test stdin spoofing

* docs: remove "empty" from git repo message

* fix: update git step text for test

* fix: remove redundant --dryrun flag

* refactor: simplify next steps with &&

* chore: changeset
  • Loading branch information
bholmesdev committed Apr 29, 2022
1 parent 6cd9ef5 commit c8f5fa3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-pens-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': minor
---

Add "initialize git repository" step to simplify our next steps suggestion. We now give you a one-liner to easily paste in your terminal and start the dev server!
54 changes: 31 additions & 23 deletions packages/create-astro/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import { bold, cyan, gray, green, red, yellow } from 'kleur/colors';
import { bgCyan, black, bold, cyan, gray, green, red, yellow } from 'kleur/colors';
import prompts from 'prompts';
import degit from 'degit';
import yargs from 'yargs-parser';
Expand Down Expand Up @@ -181,19 +181,17 @@ export async function main() {
process.exit(0);
}

if (installResponse.install) {
if (installResponse.install && !args.dryrun) {
const installExec = execa(pkgManager, ['install'], { cwd });
const installingPackagesMsg = `Installing packages${emojiWithFallback(' 📦', '...')}`;
spinner = ora({ color: 'green', text: installingPackagesMsg }).start();
if (!args.dryrun) {
await new Promise<void>((resolve, reject) => {
installExec.stdout?.on('data', function (data) {
spinner.text = `${installingPackagesMsg}\n${bold(`[${pkgManager}]`)} ${data}`;
});
installExec.on('error', (error) => reject(error));
installExec.on('close', () => resolve());
await new Promise<void>((resolve, reject) => {
installExec.stdout?.on('data', function (data) {
spinner.text = `${installingPackagesMsg}\n${bold(`[${pkgManager}]`)} ${data}`;
});
}
installExec.on('error', (error) => reject(error));
installExec.on('close', () => resolve());
});
spinner.succeed();
}

Expand Down Expand Up @@ -227,26 +225,36 @@ export async function main() {
);
}

console.log('\nNext steps:');
let i = 1;
const gitResponse = await prompts({
type: 'confirm',
name: 'git',
message: 'Initialize a git repository?',
initial: true,
});

if (!gitResponse) {
process.exit(0);
}

if (gitResponse.git && !args.dryrun) {
await execaCommand('git init', { cwd });
}

console.log(`\n${bgCyan(black(' Next steps '))}\n`);

const relative = path.relative(process.cwd(), cwd);
const startCommand = [];
if (relative !== '') {
console.log(` ${i++}: ${bold(cyan(`cd ${relative}`))}`);
startCommand.push(bold(cyan(`cd ${relative}`)));
}

if (!installResponse.install) {
console.log(` ${i++}: ${bold(cyan(`${pkgManager} install`))}`);
startCommand.push(bold(cyan(`${pkgManager} install`)));
}
console.log(
` ${i++}: ${bold(
cyan('git init && git add -A && git commit -m "Initial commit"')
)} (optional step)`
);
const runCommand = pkgManager === 'npm' ? 'npm run dev' : `${pkgManager} dev`;
console.log(` ${i++}: ${bold(cyan(runCommand))}`);
startCommand.push(bold(cyan(pkgManager === 'npm' ? 'npm run dev' : `${pkgManager} dev`)));
console.log(startCommand.join(' && '));

console.log(`\nTo close the dev server, hit ${bold(cyan('Ctrl-C'))}`);
console.log(`\nStuck? Visit us at ${cyan('https://astro.build/chat')}\n`);
console.log(`Stuck? Visit us at ${cyan('https://astro.build/chat')}\n`);
}

function emojiWithFallback(char: string, fallback: string) {
Expand Down
7 changes: 5 additions & 2 deletions packages/create-astro/test/astro-add-step.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ describe('[create-astro] astro add', function () {
});

it('should use "astro add" when user has installed dependencies', function () {
const { stdout, stdin } = setup([tempDir, '--dryrun']);
const { stdout, stdin } = setup([tempDir]);
return promiseWithTimeout((resolve) => {
const seen = new Set();
const installPrompt = PROMPT_MESSAGES.install('npm');
stdout.on('data', (chunk) => {
if (!seen.has(PROMPT_MESSAGES.template) && chunk.includes(PROMPT_MESSAGES.template)) {
seen.add(PROMPT_MESSAGES.template);
// respond with "enter key"
stdin.write('\x0D');
}
if (!seen.has(installPrompt) && chunk.includes(installPrompt)) {
Expand All @@ -44,17 +45,19 @@ describe('[create-astro] astro add', function () {
});

it('should use "npx astro@latest add" when use has NOT installed dependencies', function () {
const { stdout, stdin } = setup([tempDir, '--dryrun']);
const { stdout, stdin } = setup([tempDir]);
return promiseWithTimeout((resolve) => {
const seen = new Set();
const installPrompt = PROMPT_MESSAGES.install('npm');
stdout.on('data', (chunk) => {
if (!seen.has(PROMPT_MESSAGES.template) && chunk.includes(PROMPT_MESSAGES.template)) {
seen.add(PROMPT_MESSAGES.template);
// respond with "enter key"
stdin.write('\x0D');
}
if (!seen.has(installPrompt) && chunk.includes(installPrompt)) {
seen.add(installPrompt);
// respond with "no, then enter key"
stdin.write('n\x0D');
}
if (chunk.includes(PROMPT_MESSAGES.astroAdd('npx astro@latest add --yes'))) {
Expand Down
11 changes: 9 additions & 2 deletions packages/create-astro/test/install-step.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ describe('[create-astro] install', function () {
});

it('should respect package manager in prompt', function () {
const { stdout, stdin } = setup([tempDir, '--dryrun']);
const { stdout, stdin } = setup([tempDir]);
return promiseWithTimeout((resolve) => {
const seen = new Set();
const installPrompt = PROMPT_MESSAGES.install(FAKE_PACKAGE_MANAGER);
stdout.on('data', (chunk) => {
if (!seen.has(PROMPT_MESSAGES.template) && chunk.includes(PROMPT_MESSAGES.template)) {
seen.add(PROMPT_MESSAGES.template);
// respond with "enter key"
stdin.write('\x0D');
}
if (!seen.has(installPrompt) && chunk.includes(installPrompt)) {
Expand All @@ -39,22 +40,28 @@ describe('[create-astro] install', function () {
});

it('should respect package manager in next steps', function () {
const { stdout, stdin } = setup([tempDir, '--dryrun']);
const { stdout, stdin } = setup([tempDir]);
return promiseWithTimeout((resolve) => {
const seen = new Set();
const installPrompt = PROMPT_MESSAGES.install(FAKE_PACKAGE_MANAGER);
const astroAddPrompt = PROMPT_MESSAGES.astroAdd();
stdout.on('data', (chunk) => {
if (!seen.has(PROMPT_MESSAGES.template) && chunk.includes(PROMPT_MESSAGES.template)) {
seen.add(PROMPT_MESSAGES.template);
// respond with "enter key"
stdin.write('\x0D');
}
if (!seen.has(installPrompt) && chunk.includes(installPrompt)) {
seen.add(installPrompt);
// respond with "no, then enter key"
stdin.write('n\x0D');
}
if (!seen.has(astroAddPrompt) && chunk.includes(astroAddPrompt)) {
seen.add(astroAddPrompt);
stdin.write('n\x0D');
}
if (!seen.has(PROMPT_MESSAGES.git) && chunk.includes(PROMPT_MESSAGES.git)) {
seen.add(PROMPT_MESSAGES.git);
stdin.write('\x0D');
}
if (chunk.includes('banana dev')) {
Expand Down
3 changes: 2 additions & 1 deletion packages/create-astro/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ export const PROMPT_MESSAGES = {
install: (pkgManager) => `Would you like us to run "${pkgManager} install?"`,
astroAdd: (astroAddCommand = 'npx astro@latest add --yes') =>
`Run "${astroAddCommand}?" This lets you optionally add component frameworks (ex. React), CSS frameworks (ex. Tailwind), and more.`,
git: 'Initialize a git repository?',
};

export function setup(args = []) {
const { stdout, stdin } = execa('../create-astro.mjs', args, { cwd: testDir });
const { stdout, stdin } = execa('../create-astro.mjs', [...args, '--dryrun'], { cwd: testDir });
return {
stdin,
stdout,
Expand Down

0 comments on commit c8f5fa3

Please sign in to comment.