Skip to content

Commit

Permalink
fix(create-astro): @astrojs/check and typescript addition (#9813)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
florian-lefebvre and natemoo-re authored Jan 24, 2024
1 parent edb5437 commit fecba30
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
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

0 comments on commit fecba30

Please sign in to comment.