diff options
author | 2024-01-24 23:20:47 +0100 | |
---|---|---|
committer | 2024-01-24 16:20:47 -0600 | |
commit | fecba30a1abb7ca65dfb8f506dde77117fa447d1 (patch) | |
tree | 4788143f59c18cb65765d8c9984fa5d3f070f6ba /packages/create-astro/src | |
parent | edb5437058c00a2ed6f1119ace6a50c3a42c10f4 (diff) | |
download | astro-fecba30a1abb7ca65dfb8f506dde77117fa447d1.tar.gz astro-fecba30a1abb7ca65dfb8f506dde77117fa447d1.tar.zst astro-fecba30a1abb7ca65dfb8f506dde77117fa447d1.zip |
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 <natemoo-re@users.noreply.github.com>
* Update packages/create-astro/src/messages.ts
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
* fix: remove useless block
---------
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Diffstat (limited to 'packages/create-astro/src')
-rw-r--r-- | packages/create-astro/src/actions/context.ts | 2 | ||||
-rw-r--r-- | packages/create-astro/src/actions/typescript.ts | 20 | ||||
-rw-r--r-- | packages/create-astro/src/messages.ts | 4 |
3 files changed, 14 insertions, 12 deletions
diff --git a/packages/create-astro/src/actions/context.ts b/packages/create-astro/src/actions/context.ts index 51ee6280b..8c173191d 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<Context> { 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 0ac1c753d..886a005c9 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 e912e757d..c9006b292 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<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: '' }) ); |