Skip to content

Commit

Permalink
feat(cli): validate Directus version
Browse files Browse the repository at this point in the history
  • Loading branch information
EdouardDem committed Sep 5, 2024
1 parent 884af4f commit 8a35af2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 44 deletions.
6 changes: 4 additions & 2 deletions packages/cli/src/lib/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export async function runDiff() {
const config = Container.get(ConfigService);
const snapshotConfig = config.getSnapshotConfig();

// Clear the cache
await Container.get(MigrationClient).clearCache();
// Check and prepare instance
const migrationClient = Container.get(MigrationClient);
await migrationClient.validateDirectusVersion();
await migrationClient.clearCache();

// Snapshot
if (snapshotConfig.enabled) {
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/lib/commands/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export async function runPull() {
const config = Container.get(ConfigService);
const snapshotConfig = config.getSnapshotConfig();

// Clear the cache
await Container.get(MigrationClient).clearCache();
// Check and prepare instance
const migrationClient = Container.get(MigrationClient);
await migrationClient.validateDirectusVersion();
await migrationClient.clearCache();

// Snapshot
if (snapshotConfig.enabled) {
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/lib/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export async function runPush() {
const config = Container.get(ConfigService);
const snapshotConfig = config.getSnapshotConfig();

// Clear the cache
await Container.get(MigrationClient).clearCache();
// Check and prepare instance
const migrationClient = Container.get(MigrationClient);
await migrationClient.validateDirectusVersion();
await migrationClient.clearCache();

// Snapshot
if (snapshotConfig.enabled) {
Expand Down
51 changes: 51 additions & 0 deletions packages/cli/src/lib/services/migration-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import {
rest,
RestClient,
RestConfig,
serverInfo,
} from '@directus/sdk';
import { Inject, Service } from 'typedi';
import pino from 'pino';
import { LOGGER } from '../constants';
import { ConfigService, isDirectusConfigWithToken } from './config';
import { getChildLogger } from '../helpers';
import { compareVersions } from 'compare-versions';
import { Cacheable } from 'typescript-cacheable';

@Service()
export class MigrationClient {
Expand Down Expand Up @@ -79,4 +82,52 @@ export class MigrationClient {
await client.setToken(token);
return client;
}

/**
* Get the server version
*/
@Cacheable()
protected async getDirectusVersion() {
const client = await this.get();
const info = await client.request<{ version?: string }>(serverInfo());
if (!info?.version) {
throw new Error('Could not get the Directus version');
}
return info.version;
}

/**
* This method compares the Directus instance version with the given one.
*/
protected async compareWithDirectusVersion(
version: string,
): Promise<'equal' | 'greater' | 'smaller'> {
const directusVersion = await this.getDirectusVersion();
const diff = compareVersions(version, directusVersion);
if (diff === 0) {
return 'equal';
} else if (diff > 0) {
return 'greater';
} else {
return 'smaller';
}
}

/**
* This method validate that the Directus instance version is compatible with the current CLI version.
*/
async validateDirectusVersion() {
const directusVersion = await this.getDirectusVersion();
if ((await this.compareWithDirectusVersion('10.0.0')) === 'greater') {
throw new Error(
`This CLI is not compatible with Directus ${directusVersion}. Please upgrade Directus to 10.0.0 (or higher).`,
);
}
if ((await this.compareWithDirectusVersion('11.0.0')) === 'greater') {
throw new Error(
`This CLI is not compatible with Directus ${directusVersion}. Please use \`npx directus-sync@2.2.0 [command]\` or upgrade Directus to 11.0.0 (or higher).`,
);
}
this.logger.debug(`Directus ${directusVersion} is compatible`);
}
}
42 changes: 4 additions & 38 deletions packages/cli/src/lib/services/snapshot/snapshot-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { LOGGER } from '../../constants';
import pino from 'pino';
import { getChildLogger, loadJsonFilesRecursively } from '../../helpers';
import { ConfigService, SnapshotHooks } from '../config';
import { compareVersions } from 'compare-versions';

const SNAPSHOT_JSON = 'snapshot.json';
const INFO_JSON = 'info.json';
Expand All @@ -35,8 +34,6 @@ export class SnapshotClient {

protected readonly hooks: SnapshotHooks;

protected snapshot: Snapshot | undefined;

constructor(
config: ConfigService,
@Inject(LOGGER) baseLogger: pino.Logger,
Expand Down Expand Up @@ -126,17 +123,11 @@ export class SnapshotClient {
}

/**
* Get the snapshot from cache or from the Directus instance.
* This ensures to get the snapshot only once.
* If forceRefresh is true, the snapshot is fetched from the Directus instance.
* @protected
* Get the snapshot from the Directus instance.
*/
protected async getSnapshot(forceRefresh = false): Promise<Snapshot> {
if (!this.snapshot || forceRefresh) {
const directus = await this.migrationClient.get();
this.snapshot = await directus.request<Snapshot>(schemaSnapshot()); // Get better types
}
return this.snapshot;
protected async getSnapshot(): Promise<Snapshot> {
const directus = await this.migrationClient.get();
return await directus.request<Snapshot>(schemaSnapshot()); // Get better types
}

/**
Expand Down Expand Up @@ -240,29 +231,4 @@ export class SnapshotClient {
return readJsonSync(filePath, 'utf-8') as Snapshot;
}
}

/**
* Get the server version
*/
async getDirectusVersion() {
const snapshot = await this.getSnapshot();
return snapshot.directus;
}

/**
* This method compares the Directus instance version with the given one.
*/
async compareWithDirectusVersion(
version: string,
): Promise<'equal' | 'greater' | 'smaller'> {
const directusVersion = await this.getDirectusVersion();
const diff = compareVersions(version, directusVersion);
if (diff === 0) {
return 'equal';
} else if (diff > 0) {
return 'greater';
} else {
return 'smaller';
}
}
}

0 comments on commit 8a35af2

Please sign in to comment.