mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
refactor(core): reuse connector reading function in cli (#2332)
This commit is contained in:
parent
b137daeb12
commit
183ebfd856
3 changed files with 45 additions and 39 deletions
|
@ -1,6 +1,6 @@
|
|||
import { exec } from 'child_process';
|
||||
import { existsSync } from 'fs';
|
||||
import { readFile, mkdir, unlink, readdir } from 'fs/promises';
|
||||
import { readFile, mkdir, unlink } from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { promisify } from 'util';
|
||||
|
||||
|
@ -13,7 +13,7 @@ import tar from 'tar';
|
|||
import { z } from 'zod';
|
||||
|
||||
import { connectorDirectory } from '../../constants';
|
||||
import { isTty, log, oraPromise } from '../../utilities';
|
||||
import { getConnectorPackagesFromDirectory, isTty, log, oraPromise } from '../../utilities';
|
||||
import { defaultPath } from '../install/utils';
|
||||
|
||||
const coreDirectory = 'packages/core';
|
||||
|
@ -102,40 +102,10 @@ const getConnectorDirectory = (instancePath: string) =>
|
|||
export const isOfficialConnector = (packageName: string) =>
|
||||
packageName.startsWith('@logto/connector-');
|
||||
|
||||
const getConnectorPackageName = async (directory: string) => {
|
||||
const filePath = path.join(directory, 'package.json');
|
||||
|
||||
if (!existsSync(filePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const json = await readFile(filePath, 'utf8');
|
||||
const { name } = z.object({ name: z.string() }).parse(JSON.parse(json));
|
||||
|
||||
if (name.startsWith('connector-') || Boolean(name.split('/')[1]?.startsWith('connector-'))) {
|
||||
return name;
|
||||
}
|
||||
};
|
||||
|
||||
export type ConnectorPackage = {
|
||||
name: string;
|
||||
path: string;
|
||||
};
|
||||
|
||||
export const getConnectorPackagesFrom = async (instancePath?: string) => {
|
||||
const directory = getConnectorDirectory(await inquireInstancePath(instancePath));
|
||||
const content = await readdir(directory, 'utf8');
|
||||
const rawPackages = await Promise.all(
|
||||
content.map(async (value) => {
|
||||
const currentDirectory = path.join(directory, value);
|
||||
|
||||
return { name: await getConnectorPackageName(currentDirectory), path: currentDirectory };
|
||||
})
|
||||
);
|
||||
|
||||
return rawPackages.filter(
|
||||
(packageInfo): packageInfo is ConnectorPackage => typeof packageInfo.name === 'string'
|
||||
);
|
||||
return getConnectorPackagesFromDirectory(directory);
|
||||
};
|
||||
|
||||
export const addConnectors = async (instancePath: string, packageNames: string[]) => {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { execSync } from 'child_process';
|
||||
import { createWriteStream } from 'fs';
|
||||
import { createWriteStream, existsSync } from 'fs';
|
||||
import { readdir, readFile } from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
import type { Optional } from '@silverhand/essentials';
|
||||
|
@ -10,6 +11,7 @@ import got from 'got';
|
|||
import { HttpsProxyAgent } from 'hpagent';
|
||||
import inquirer from 'inquirer';
|
||||
import ora from 'ora';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const safeExecSync = (command: string) => {
|
||||
try {
|
||||
|
@ -173,3 +175,38 @@ export function findLastIndex<T>(
|
|||
|
||||
return -1;
|
||||
}
|
||||
|
||||
const getConnectorPackageName = async (directory: string) => {
|
||||
const filePath = path.join(directory, 'package.json');
|
||||
|
||||
if (!existsSync(filePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const json = await readFile(filePath, 'utf8');
|
||||
const { name } = z.object({ name: z.string() }).parse(JSON.parse(json));
|
||||
|
||||
if (name.startsWith('connector-') || Boolean(name.split('/')[1]?.startsWith('connector-'))) {
|
||||
return name;
|
||||
}
|
||||
};
|
||||
|
||||
export type ConnectorPackage = {
|
||||
name: string;
|
||||
path: string;
|
||||
};
|
||||
|
||||
export const getConnectorPackagesFromDirectory = async (directory: string) => {
|
||||
const content = await readdir(directory, 'utf8');
|
||||
const rawPackages = await Promise.all(
|
||||
content.map(async (value) => {
|
||||
const currentDirectory = path.join(directory, value);
|
||||
|
||||
return { name: await getConnectorPackageName(currentDirectory), path: currentDirectory };
|
||||
})
|
||||
);
|
||||
|
||||
return rawPackages.filter(
|
||||
(packageInfo): packageInfo is ConnectorPackage => typeof packageInfo.name === 'string'
|
||||
);
|
||||
};
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { existsSync } from 'fs';
|
||||
import { readdir } from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
import { connectorDirectory } from '@logto/cli/lib/constants';
|
||||
import { getConnectorPackagesFromDirectory } from '@logto/cli/lib/utilities';
|
||||
import type { AllConnector, CreateConnector } from '@logto/connector-kit';
|
||||
import { validateConfig } from '@logto/connector-kit';
|
||||
import { findPackage } from '@logto/shared';
|
||||
|
@ -32,12 +32,11 @@ const loadConnectors = async () => {
|
|||
return [];
|
||||
}
|
||||
|
||||
const connectorFolders = await readdir(directory);
|
||||
const connectorPackages = await getConnectorPackagesFromDirectory(directory);
|
||||
|
||||
const connectors = await Promise.all(
|
||||
connectorFolders.map(async (folder) => {
|
||||
connectorPackages.map(async ({ path: packagePath, name }) => {
|
||||
try {
|
||||
const packagePath = path.join(directory, folder);
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
const { default: createConnector } = (await import(packagePath)) as {
|
||||
default: CreateConnector<AllConnector>;
|
||||
|
@ -71,7 +70,7 @@ const loadConnectors = async () => {
|
|||
if (error instanceof Error) {
|
||||
console.log(
|
||||
`${chalk.red(
|
||||
`[load-connector] skip ${chalk.bold(folder)} due to error: ${error.message}`
|
||||
`[load-connector] skip ${chalk.bold(name)} due to error: ${error.message}`
|
||||
)}`
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in a new issue