0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

refactor(cli): fallback to latest when current version is unavailable (#3779)

refactor(cli): fallback to latest when current version number is unavailable
This commit is contained in:
Gao Sun 2023-04-28 09:54:55 +08:00 committed by GitHub
parent 4bba658d7b
commit 11db88a77a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,6 +6,7 @@ import path from 'node:path';
import { assert } from '@silverhand/essentials';
import chalk from 'chalk';
import { got, RequestError } from 'got';
import inquirer from 'inquirer';
import * as semver from 'semver';
import tar from 'tar';
@ -102,11 +103,31 @@ export const validateDatabase = async () => {
}
};
const fetchDownloadUrl = async (url?: string) => {
if (url) {
return url;
}
const defaultUrl = `https://github.com/logto-io/logto/releases/download/v${packageJson.version}/logto.tar.gz`;
try {
await got.head(defaultUrl);
} catch (error) {
if (error instanceof RequestError && error.response?.statusCode === 404) {
consoleLog.warn(
`Current version "v${packageJson.version}" not found in GitHub Releases, fallback to "latest".\n` +
'If you want to download the latest version, please wait a few moments and try again.'
);
return 'https://github.com/logto-io/logto/releases/latest/download/logto.tar.gz';
}
}
return defaultUrl;
};
export const downloadRelease = async (url?: string) => {
const tarFilePath = path.resolve(os.tmpdir(), './logto.tar.gz');
const from =
url ??
`https://github.com/logto-io/logto/releases/download/v${packageJson.version}/logto.tar.gz`;
const from = await fetchDownloadUrl(url);
consoleLog.info(`Download Logto from ${from}`);
consoleLog.info(`Target ${tarFilePath}`);