From fecba30a1abb7ca65dfb8f506dde77117fa447d1 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 24 Jan 2024 23:20:47 +0100 Subject: [PATCH] fix(create-astro): @astrojs/check and typescript addition (#9813) * fix(create-astro): @astrojs/check and typescript addition * Update packages/create-astro/src/actions/typescript.ts Co-authored-by: Nate Moore * Update packages/create-astro/src/messages.ts Co-authored-by: Nate Moore * fix: remove useless block --------- Co-authored-by: Nate Moore --- .changeset/seven-kiwis-join.md | 5 +++++ packages/create-astro/src/actions/context.ts | 2 +- .../create-astro/src/actions/typescript.ts | 20 ++++++++++--------- packages/create-astro/src/messages.ts | 4 ++-- 4 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 .changeset/seven-kiwis-join.md diff --git a/.changeset/seven-kiwis-join.md b/.changeset/seven-kiwis-join.md new file mode 100644 index 000000000000..328d9a342a4f --- /dev/null +++ b/.changeset/seven-kiwis-join.md @@ -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 diff --git a/packages/create-astro/src/actions/context.ts b/packages/create-astro/src/actions/context.ts index 51ee6280bc89..8c173191db39 100644 --- a/packages/create-astro/src/actions/context.ts +++ b/packages/create-astro/src/actions/context.ts @@ -93,7 +93,7 @@ export async function getContext(argv: string[]): Promise { prompt, packageManager, username: getName(), - version: getVersion(packageManager), + version: getVersion(packageManager, 'astro'), skipHouston, fancy, dryRun, diff --git a/packages/create-astro/src/actions/typescript.ts b/packages/create-astro/src/actions/typescript.ts index 0ac1c753d50c..886a005c9e17 100644 --- a/packages/create-astro/src/actions/typescript.ts +++ b/packages/create-astro/src/actions/typescript.ts @@ -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< @@ -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'; @@ -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; diff --git a/packages/create-astro/src/messages.ts b/packages/create-astro/src/messages.ts index e912e757d88f..c9006b292e47 100644 --- a/packages/create-astro/src/messages.ts +++ b/packages/create-astro/src/messages.ts @@ -55,11 +55,11 @@ export const getName = () => }); let v: string; -export const getVersion = (packageManager: string) => +export const getVersion = (packageManager: string, packageName: string) => new Promise(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: '' }) );