Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(create-astro): @astrojs/check and typescript addition #9813

Merged
5 changes: 5 additions & 0 deletions .changeset/seven-kiwis-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-astro": patch
---

Fixes `@astrojs/check` and `typescript` addition to `package.json` dependencies when the user has decided not to auto-install dependencies
2 changes: 1 addition & 1 deletion packages/create-astro/src/actions/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function getContext(argv: string[]): Promise<Context> {
prompt,
packageManager,
username: getName(),
version: getVersion(packageManager),
version: getVersion(packageManager, 'astro'),
skipHouston,
fancy,
dryRun,
Expand Down
20 changes: 11 additions & 9 deletions packages/create-astro/src/actions/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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, title, typescriptByDefault } from '../messages.js';
import { error, getVersion, info, title, typescriptByDefault } from '../messages.js';
import { shell } from '../shell.js';

type PickedTypeScriptContext = Pick<
Expand Down Expand Up @@ -89,13 +89,6 @@ const FILES_TO_UPDATE = {
options: { value: string; ctx: PickedTypeScriptContext }
) => {
try {
// add required dependencies for astro check
if (options.ctx.install)
await shell(options.ctx.packageManager, ['add', '@astrojs/check', 'typescript'], {
cwd: path.dirname(file),
stdio: 'ignore',
});

// inject additional command to build script
const data = await readFile(file, { encoding: 'utf-8' });
const indent = /(^\s+)/m.exec(data)?.[1] ?? '\t';
Expand All @@ -107,8 +100,17 @@ const FILES_TO_UPDATE = {
if (typeof buildScript === 'string' && !buildScript.includes('astro check')) {
// Mutate the existing object to avoid changing user-defined script order
parsedPackageJson.scripts.build = `astro check && ${buildScript}`;
await writeFile(file, JSON.stringify(parsedPackageJson, null, indent), 'utf-8');
}

const [astroCheckVersion, typescriptVersion] = await Promise.all([
getVersion(options.ctx.packageManager, '@astrojs/check'),
getVersion(options.ctx.packageManager, 'typescript'),
]);
parsedPackageJson.dependencies ??= {};
parsedPackageJson.dependencies['@astrojs/check'] = `^${astroCheckVersion}`;
parsedPackageJson.dependencies.typescript = `^${typescriptVersion}`;

await writeFile(file, JSON.stringify(parsedPackageJson, null, indent), 'utf-8');
} catch (err) {
// if there's no package.json (which is very unlikely), then do nothing
if (err && (err as any).code === 'ENOENT') return;
Expand Down
4 changes: 2 additions & 2 deletions packages/create-astro/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export const getName = () =>
});

let v: string;
export const getVersion = (packageManager: string) =>
export const getVersion = (packageManager: string, packageName: string) =>
new Promise<string>(async (resolve) => {
if (v) return resolve(v);
let registry = await getRegistry(packageManager);
const { version } = await fetch(`${registry}/astro/latest`, { redirect: 'follow' }).then(
const { version } = await fetch(`${registry}/${packageName}/latest`, { redirect: 'follow' }).then(
(res) => res.json(),
() => ({ version: '' })
);
Expand Down