From 5dda0a6dd0e04468c078e3581e68a614ce23404c Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Fri, 14 Oct 2022 17:37:53 +0800 Subject: [PATCH] feat(cli): add `download-url` option for install --- packages/cli/src/commands/connector/index.ts | 2 +- packages/cli/src/commands/install/index.ts | 18 ++++++++++++++---- packages/cli/src/commands/install/utils.ts | 16 ++++++++++++++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/commands/connector/index.ts b/packages/cli/src/commands/connector/index.ts index 56ae4bcef..a5ff36788 100644 --- a/packages/cli/src/commands/connector/index.ts +++ b/packages/cli/src/commands/connector/index.ts @@ -6,7 +6,7 @@ import list from './list'; import remove from './remove'; const connector: CommandModule = { - command: ['connector', 'c'], + command: ['connector', 'c', 'connectors'], describe: 'Command for Logto connectors', builder: (yargs) => yargs diff --git a/packages/cli/src/commands/install/index.ts b/packages/cli/src/commands/install/index.ts index 1844309dd..b0cb27fd3 100644 --- a/packages/cli/src/commands/install/index.ts +++ b/packages/cli/src/commands/install/index.ts @@ -14,15 +14,17 @@ import { logFinale, decompress, inquireOfficialConnectors, + isUrl, } from './utils'; export type InstallArgs = { path?: string; skipSeed: boolean; officialConnectors?: boolean; + downloadUrl?: string; }; -const installLogto = async ({ path, skipSeed, officialConnectors }: InstallArgs) => { +const installLogto = async ({ path, skipSeed, officialConnectors, downloadUrl }: InstallArgs) => { validateNodeVersion(); // Get instance path @@ -32,7 +34,8 @@ const installLogto = async ({ path, skipSeed, officialConnectors }: InstallArgs) await validateDatabase(); // Download and decompress - const tarPath = await downloadRelease(); + const tarPath = + !downloadUrl || isUrl(downloadUrl) ? await downloadRelease(downloadUrl) : downloadUrl; await decompress(instancePath, tarPath); // Seed database @@ -69,6 +72,7 @@ const install: CommandModule< p?: string; ss: boolean; oc?: boolean; + du?: string; } > = { command: ['init', 'i', 'install'], @@ -91,9 +95,15 @@ const install: CommandModule< describe: 'Add official connectors after downloading Logto', type: 'boolean', }, + du: { + alias: 'download-url', + describe: 'URL for downloading Logto, can be a local path to tar', + type: 'string', + hidden: true, + }, }), - handler: async ({ p, ss, oc }) => { - await installLogto({ path: p, skipSeed: ss, officialConnectors: oc }); + handler: async ({ p, ss, oc, du }) => { + await installLogto({ path: p, skipSeed: ss, officialConnectors: oc, downloadUrl: du }); }, }; diff --git a/packages/cli/src/commands/install/utils.ts b/packages/cli/src/commands/install/utils.ts index 5e295b11d..9776eec63 100644 --- a/packages/cli/src/commands/install/utils.ts +++ b/packages/cli/src/commands/install/utils.ts @@ -84,12 +84,12 @@ export const validateDatabase = async () => { } }; -export const downloadRelease = async () => { +export const downloadRelease = async (url?: string) => { const tarFilePath = path.resolve(os.tmpdir(), './logto.tar.gz'); log.info(`Download Logto to ${tarFilePath}`); await downloadFile( - 'https://github.com/logto-io/logto/releases/latest/download/logto.tar.gz', + url ?? 'https://github.com/logto-io/logto/releases/latest/download/logto.tar.gz', tarFilePath ); @@ -166,3 +166,15 @@ export const inquireOfficialConnectors = async (initialAnswer?: boolean) => { return value; }; + +export const isUrl = (string: string) => { + try { + // On purpose to test + // eslint-disable-next-line no-new + new URL(string); + + return true; + } catch { + return false; + } +};