Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-checks-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improves the `astro check` error shown for TypeScript 7. The command now explains that TypeScript 7 is not currently supported and provides instructions for experimentally type-checking Astro files with TypeScript 7.1 and `@astrojs/ts-content-mapper`.
15 changes: 14 additions & 1 deletion packages/astro/src/cli/check/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from 'node:path';
import { getMajor } from 'verkit';
import { ensureProcessNodeEnv } from '../../core/util.js';
import { createLoggerFromFlags, type Flags, flagsToAstroInlineConfig } from '../flags.js';
import { getPackage } from '../install-package.js';
import { getPackage, getPackageVersion } from '../install-package.js';

export async function check(flags: Flags): Promise<boolean | void> {
ensureProcessNodeEnv('production');
Expand All @@ -10,6 +11,18 @@ export async function check(flags: Flags): Promise<boolean | void> {
skipAsk: !!flags.yes || !!flags.y,
cwd: flags.root,
};
const typescriptVersion = await getPackageVersion('typescript', flags.root);

if (typescriptVersion && getMajor(typescriptVersion) >= 7) {
logger.error(
'check',
'astro check does not currently support TypeScript 7.0. To continue using astro check, install TypeScript 6 instead.\n\n' +
'astro check will be deprecated in a future Astro release. Experimental support for type-checking Astro files with TypeScript 7.1+ is available through @astrojs/ts-content-mapper. See its README for setup instructions:\n' +
'https://github.com/withastro/astro/tree/main/packages/language-tools/ts-content-mapper#usage',
);
return true;
}

// @ts-ignore For some unknown reason, in CI TS isn't able to get the type here even though it works locally.
const checkPackage = await getPackage<typeof import('@astrojs/check')>(
'@astrojs/check',
Expand Down
6 changes: 4 additions & 2 deletions packages/astro/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,12 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
case 'check': {
const { check } = await import('./check/index.js');
const checkServer = await check(flags);
if (flags.watch) {
if (typeof checkServer === 'boolean') {
return process.exit(checkServer ? 1 : 0);
} else if (flags.watch) {
return await new Promise(() => {}); // lives forever
} else {
return process.exit(typeof checkServer === 'boolean' && checkServer ? 1 : 0);
return process.exit(0);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/astro/src/cli/install-package.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { pathToFileURL } from 'node:url';
import * as clack from '@clack/prompts';
Expand All @@ -16,6 +17,19 @@ type GetPackageOptions = {
cwd?: string;
};

export async function getPackageVersion(
packageName: string,
cwd = process.cwd(),
): Promise<string | undefined> {
try {
const packageJsonPath = require.resolve(`${packageName}/package.json`, { paths: [cwd] });
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf-8'));
return typeof packageJson.version === 'string' ? packageJson.version : undefined;
} catch {
return undefined;
}
}

export async function getPackage<T>(
packageName: string,
logger: AstroLogger,
Expand Down
25 changes: 25 additions & 0 deletions packages/astro/test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';
import { promises as fs, readFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { Writable } from 'node:stream';
import { describe, it } from 'node:test';
Expand Down Expand Up @@ -103,4 +104,28 @@ describe('astro cli', () => {

assert.equal(result.stdout.includes('1 error'), true);
});

it('astro check explains TypeScript 7 support', async () => {
const projectRoot = await fs.mkdtemp(join(tmpdir(), 'astro-check-ts7-'));
const typescriptRoot = join(projectRoot, 'node_modules', 'typescript');

try {
await fs.mkdir(typescriptRoot, { recursive: true });
await fs.writeFile(
join(typescriptRoot, 'package.json'),
JSON.stringify({
name: 'typescript',
version: '7.0.2',
type: 'module',
exports: { './package.json': './package.json' },
}),
);

const result = await cli('check', '--root', projectRoot, '--noSync').getResult();

assert.match(result.stderr, /does not currently support TypeScript 7\.0/);
} finally {
await fs.rm(projectRoot, { recursive: true, force: true });
}
});
});
Loading