Skip to content

Commit

Permalink
Run astro check on all examples in CI (withastro#5022)
Browse files Browse the repository at this point in the history
* Run astro check on all examples in CI

* Output stderr

* Build Astro before running checks

* Making things faster + colors

* Fix errors inside examples

* Add congrats message

* Revert unentional change to tsconfigs

* Remove more unneeded changes
  • Loading branch information
Princesseuh committed Oct 12, 2022
1 parent 640ce72 commit f604ef6
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 19 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Examples astro check

on:
push:
branches:
- main
pull_request:
paths:
- 'examples/**'
- '.github/workflows/check.yml'
- 'scripts/smoke/check.js'
- 'packages/astro/src/@types/astro.ts'

env:
ASTRO_TELEMETRY_DISABLED: true
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
FORCE_COLOR: true

jobs:
check:
name: astro check
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Setup PNPM
uses: pnpm/[email protected]

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 16
cache: 'pnpm'

- name: Install dependencies
run: pnpm install

- name: Build
run: pnpm run build

- name: Status
run: git status

- name: astro check
run: pnpm run test:check-examples
7 changes: 6 additions & 1 deletion examples/framework-lit/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import { MyCounter } from '../components/my-counter.js';
<h1>Test app</h1>
<MyCounter client:load />
<Lorem />
<CalcAdd num={33} />

{/**
* Our VS Code extension does not currently properly typecheck attributes on Lit components
* As such, the following code will result in a TypeScript error inside the editor, nonetheless, it works in Astro!
* @ts-expect-error */}
<CalcAdd num={0} />
</body>
</html>
8 changes: 5 additions & 3 deletions examples/non-html-pages/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
// can fetch them directly in the browser.
const response = await fetch(`/about.json`);
const data = await response.json();
document.getElementById(
'result'
).innerHTML = `Load complete!<br/>Built with: <a href="${data.url}">${data.name}!</a>`;
const resultHeader = document.getElementById('result');

if (resultHeader) {
resultHeader.innerHTML = `Load complete!<br/>Built with: <a href="${data.url}">${data.name}!</a>`;
}
</script>
</body>
</html>
7 changes: 7 additions & 0 deletions examples/portfolio/src/components/PortfolioPreview.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
---
import type { MarkdownInstance } from 'astro';
import type { Project } from '../types';
interface Props {
project: MarkdownInstance<Project>
}
const { frontmatter, url } = Astro.props.project;
---

Expand Down
7 changes: 6 additions & 1 deletion examples/portfolio/src/layouts/project.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
import MainHead from '../components/MainHead.astro';
import Footer from '../components/Footer.astro';
import Nav from '../components/Nav.astro';
import type { Project } from '../types';
interface Props {
content: Project;
}
const { content } = Astro.props;
---

<html lang={content.lang || 'en'}>
<html lang="en">
<head>
<MainHead title={content.title} description={content.description} />
<style>
Expand Down
5 changes: 3 additions & 2 deletions examples/portfolio/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import MainHead from '../components/MainHead.astro';
import Nav from '../components/Nav.astro';
import Footer from '../components/Footer.astro';
import PortfolioPreview from '../components/PortfolioPreview.astro';
import type { Project } from '../types';
// Data Fetching: List all Markdown posts in the repo.
const projects = await Astro.glob('./project/**/*.md');
const featuredProject = projects[0];
const projects = await Astro.glob<Project>('./project/**/*.md');
const featuredProject = projects[0]!;
// Full Astro Component Syntax:
// https://docs.astro.build/core-concepts/astro-components/
Expand Down
3 changes: 2 additions & 1 deletion examples/portfolio/src/pages/projects.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import MainHead from '../components/MainHead.astro';
import Footer from '../components/Footer.astro';
import Nav from '../components/Nav.astro';
import PortfolioPreview from '../components/PortfolioPreview.astro';
import type { Project } from '../types';
const projects = (await Astro.glob('./project/**/*.md'))
const projects = (await Astro.glob<Project>('./project/**/*.md'))
.filter(({ frontmatter }) => !!frontmatter.publishDate)
.sort(
(a, b) =>
Expand Down
8 changes: 8 additions & 0 deletions examples/portfolio/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Project {
title: string;
client: string;
description: string;
publishDate: string;
tags: string[];
img: string;
}
2 changes: 1 addition & 1 deletion examples/ssr/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface Product {
export interface Product {
id: number;
name: string;
price: number;
Expand Down
6 changes: 6 additions & 0 deletions examples/ssr/src/components/ProductListing.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
---
import type { Product } from '../api';
interface Props {
products: Product[];
}
const { products } = Astro.props;
---

Expand Down
4 changes: 4 additions & 0 deletions examples/ssr/src/components/TextDecorationSkip.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
---
interface Props {
text: string;
}
const { text } = Astro.props;
const words = text.split(' ');
const last = words.length - 1;
Expand Down
5 changes: 1 addition & 4 deletions examples/ssr/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ const products = await getProducts(Astro.request);
.product-listing-title {
text-align: center;
}

.product-listing {
}
</style>
</head>
<body>
<Header />

<Container tag="main">
<ProductListing products={products} class="product-listing">
<ProductListing products={products}>
<h2 class="product-listing-title" slot="title">Product Listing</h2>
</ProductListing>
</Container>
Expand Down
3 changes: 2 additions & 1 deletion examples/with-tailwindcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"astro": "astro"
},
"dependencies": {
"astro": "^1.4.7",
"@astrojs/tailwind": "^2.0.2",
"@types/canvas-confetti": "^1.4.3",
"astro": "^1.4.7",
"autoprefixer": "^10.4.7",
"canvas-confetti": "^1.5.1",
"postcss": "^8.4.14",
Expand Down
6 changes: 5 additions & 1 deletion examples/with-tailwindcss/src/components/Button.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@

<script>
import confetti from 'canvas-confetti';
document.body.querySelector('button').addEventListener('click', () => confetti());
const button = document.body.querySelector('button');

if (button) {
button.addEventListener('click', () => confetti());
}
</script>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"test": "turbo run test --output-logs=new-only --concurrency=1 --filter=astro --filter=create-astro --filter=\"@astrojs/*\"",
"test:match": "cd packages/astro && pnpm run test:match",
"test:smoke": "turbo run build --filter=\"@example/*\" --filter=\"astro.build\" --filter=\"docs\" --output-logs=new-only --concurrency=1",
"test:check-examples": "node ./scripts/smoke/check.js",
"test:vite-ci": "turbo run test --filter=astro --output-logs=new-only --no-deps --concurrency=1",
"test:e2e": "cd packages/astro && pnpm playwright install && pnpm run test:e2e",
"test:e2e:match": "cd packages/astro && pnpm playwright install && pnpm run test:e2e:match",
Expand Down
13 changes: 9 additions & 4 deletions pnpm-lock.yaml

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

3 changes: 3 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"kleur": "^4.1.4",
"svelte": "^3.48.0",
"tar": "^6.1.11"
},
"devDependencies": {
"tsconfig-resolver": "^3.0.1"
}
}
84 changes: 84 additions & 0 deletions scripts/smoke/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// @ts-check

import { spawn } from 'child_process';
import { readdirSync, readFileSync, writeFileSync } from 'fs';
import * as path from 'path';
import { tsconfigResolverSync } from 'tsconfig-resolver';

function checkExamples() {
let examples = readdirSync('./examples', { withFileTypes: true });
examples = examples.filter((dirent) => dirent.isDirectory());

console.log(`Running astro check on ${examples.length} examples...`);

Promise.all(
examples.map(
(example) =>
new Promise((resolve) => {
const originalConfig = prepareExample(example.name);
let data = '';
const child = spawn('node', ['../../packages/astro/astro.js', 'check'], {
cwd: path.join('./examples', example.name),
env: { ...process.env, FORCE_COLOR: 'true' },
});

child.stdout.on('data', function (buffer) {
data += buffer.toString();
});

child.on('exit', (code) => {
if (code !== 0) {
console.error(data);
}
if (originalConfig) {
resetExample(example.name, originalConfig);
}
resolve(code);
});
})
)
).then((codes) => {
if (codes.some((code) => code !== 0)) {
process.exit(1);
}

console.log("No errors found!");
});
}

/**
* @param {string} examplePath
*/
function prepareExample(examplePath) {
const tsconfigPath = path.join('./examples/', examplePath, 'tsconfig.json');
const tsconfig = tsconfigResolverSync({ filePath: tsconfigPath, cache: false });
let originalConfig = undefined;

if (tsconfig.exists) {
tsconfig.config.extends = 'astro/tsconfigs/strictest';
originalConfig = readFileSync(tsconfigPath).toString();

if (!tsconfig.config.compilerOptions) {
tsconfig.config.compilerOptions = {};
}

tsconfig.config.compilerOptions = Object.assign(tsconfig.config.compilerOptions, {
types: tsconfig.config.compilerOptions.types ?? [], // Speeds up tests
});
}

writeFileSync(tsconfigPath, JSON.stringify(tsconfig.config));

return originalConfig;
}

/**
* @param {string} examplePath
* @param {string} originalConfig
*/
function resetExample(examplePath, originalConfig) {
const tsconfigPath = path.join('./examples/', examplePath, 'tsconfig.json');
writeFileSync(tsconfigPath, originalConfig);
}

checkExamples();

0 comments on commit f604ef6

Please sign in to comment.