mirror of
https://github.com/withastro/astro.git
synced 2025-02-17 22:44:24 -05:00
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>
This commit is contained in:
parent
edb5437058
commit
fecba30a1a
4 changed files with 19 additions and 12 deletions
5
.changeset/seven-kiwis-join.md
Normal file
5
.changeset/seven-kiwis-join.md
Normal file
|
@ -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
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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: '' })
|
||||
);
|
||||
|
|
Loading…
Add table
Reference in a new issue