Skip to content

Commit 9d2bf80

Browse files
feat(create-studiocms): implement latest version check in the verify step (#1358)
* feat(verify): implement latest version check in the verify step * fix(index): replace readJson with direct import of package.json * fix(dependencies): remove internal_helpers from package.json and pnpm-lock.yaml * fix(verify): handle latest version check more gracefully and improve fetch error handling * fix(verify): simplify update confirmation check in version logging * fix(verify): enhance version comparison by validating semantic versioning * fix(verify): improve update confirmation check to handle symbol type
1 parent 54d5a3e commit 9d2bf80

5 files changed

Lines changed: 71 additions & 11 deletions

File tree

.changeset/brown-planets-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-studiocms": minor
3+
---
4+
5+
Implement latest version check in the verify step

packages/create-studiocms/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
],
2929
"devDependencies": {
3030
"@bluwy/giget-core": "^0.1.6",
31+
"@types/semver": "catalog:",
3132
"@withstudiocms/cli-kit": "workspace:^",
3233
"@withstudiocms/effect": "workspace:^",
33-
"@withstudiocms/internal_helpers": "workspace:^",
3434
"effect": "catalog:effect",
3535
"package-json": "^10.0.1",
36+
"semver": "catalog:",
3637
"tsdown": "catalog:"
3738
},
3839
"engines": {

packages/create-studiocms/src/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { StudioCMSColorwayBg } from '@withstudiocms/cli-kit/colors';
22
import { label } from '@withstudiocms/cli-kit/messages';
33
import { intro, tasks } from '@withstudiocms/effect/clack';
44
import { Cli, Effect, Layer, PlatformNode } from '@withstudiocms/effect/effect';
5-
import { readJson } from '@withstudiocms/internal_helpers/utils';
5+
import pkgJson from '../package.json';
66
import { CommandConfig } from './args.ts';
77
import { type EffectStepFn, getContext } from './context.ts';
88
import { dependencies } from './steps/dependencies.ts';
@@ -13,11 +13,6 @@ import { projectName } from './steps/projectName.ts';
1313
import { template } from './steps/template.ts';
1414
import { verify } from './steps/verify.ts';
1515

16-
/**
17-
* Get the package.json data for this package
18-
*/
19-
const pkgJson = readJson<{ version: string }>(new URL('../package.json', import.meta.url));
20-
2116
/**
2217
* Define the CLI command for the create-studiocms application
2318
*/

packages/create-studiocms/src/steps/verify.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import dns from 'node:dns/promises';
22
import { styleText } from 'node:util';
33
import { verifyTemplate } from '@bluwy/giget-core';
44
import { StudioCMSColorwayError, StudioCMSColorwayInfo } from '@withstudiocms/cli-kit/colors';
5-
import { log } from '@withstudiocms/effect/clack';
5+
import { confirm, log } from '@withstudiocms/effect/clack';
66
import { Effect } from 'effect';
7+
import { compare as semCompare, valid as semValid } from 'semver';
8+
import pkg from '../../package.json';
79
import { CLIError, type Context, type EffectStepFn } from '../context.ts';
810
import { getTemplateTarget } from './template.ts';
911

12+
const name = pkg.name;
13+
const version = pkg.version;
14+
1015
/**
1116
* Verify internet connection and template availability
1217
*/
@@ -20,6 +25,39 @@ export const verify: EffectStepFn = Effect.fn('verify')(
2025
ctx.exit(1);
2126
}
2227
if (ctx.debug) yield* Effect.log('Internet connection verified');
28+
29+
// Check if we are on the latest version of the CLI
30+
if (ctx.debug) yield* Effect.log('Checking for updates...');
31+
32+
const latestVersion = yield* getLatestVersionEffect().pipe(Effect.orElseSucceed(() => null));
33+
34+
if (latestVersion) {
35+
const comparison =
36+
semValid(version) && semValid(latestVersion) ? semCompare(version, latestVersion) : null;
37+
switch (comparison) {
38+
case -1: {
39+
const updateConfirmed = yield* confirm({
40+
message: StudioCMSColorwayInfo(
41+
`${styleText('bold', 'A new version of the CLI is available!')} You are using version ${styleText('reset', version)} but the latest version is ${styleText('reset', latestVersion)}. It is recommended to restart the CLI with the latest version to get new features and bug fixes!\n\nDo you want to exit?`
42+
),
43+
initialValue: true,
44+
});
45+
46+
if (typeof updateConfirmed === 'symbol' || updateConfirmed) {
47+
yield* log.info(
48+
StudioCMSColorwayInfo(
49+
'Check https://www.npmjs.com/package/create-studiocms for the latest version.'
50+
)
51+
);
52+
ctx.exit(0);
53+
}
54+
break;
55+
}
56+
default:
57+
if (ctx.debug) yield* Effect.log('You are using the latest version of the CLI');
58+
break;
59+
}
60+
}
2361
}
2462

2563
if (ctx.template) {
@@ -61,3 +99,21 @@ const isOnline = Effect.fn('isOnline')(() =>
6199
catch: (cause) => new CLIError({ cause }),
62100
})
63101
);
102+
103+
/**
104+
* Get the latest version of the CLI from npm registry
105+
*/
106+
const getLatestVersionEffect = Effect.fn('getLatestVersion')(() =>
107+
Effect.tryPromise({
108+
try: () =>
109+
fetch(`https://registry.npmjs.org/${name}/latest`, {
110+
signal: AbortSignal.timeout(5000),
111+
})
112+
.then((res) => {
113+
if (!res.ok) return null;
114+
return res.json() as Promise<{ version?: string }>;
115+
})
116+
.then((data) => data?.version ?? null),
117+
catch: (cause) => new CLIError({ cause }),
118+
})
119+
);

pnpm-lock.yaml

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)