Skip to content

Commit

Permalink
feat(cli): check directus-extension-sync is installed before push (#97)
Browse files Browse the repository at this point in the history
* feat(cli): test that directus extension is installed before inserting

* fix(e2e): better log filtering
  • Loading branch information
EdouardDem committed Sep 13, 2024
1 parent 97f4b15 commit f78fac7
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
11 changes: 10 additions & 1 deletion packages/cli/src/lib/commands/push.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Container } from 'typedi';
import pino from 'pino';
import { ConfigService, MigrationClient, SnapshotClient } from '../services';
import {
ConfigService,
MigrationClient,
PingClient,
SnapshotClient,
} from '../services';
import { loadCollections } from '../loader';
import { LOGGER } from '../constants';

Expand All @@ -16,6 +21,10 @@ export async function runPush() {

// Snapshot
if (snapshotConfig.enabled) {
// Test if the directus extension is installed
// At this point, it could be not installed and cause an issue with the snapshot push
await Container.get(PingClient).test();

logger.info(`---- Push schema ----`);
await Container.get(SnapshotClient).push();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MigrationClient } from '../../migration-client';
import { ExtensionClient } from '../../extension-client';
import { ExtensionClient, NO_ID_MAP_MESSAGE } from '../../extension-client';
import pino from 'pino';

export interface IdMap {
Expand Down Expand Up @@ -40,7 +40,7 @@ export abstract class IdMapperClient extends ExtensionClient {
const idMap = await this.fetch<IdMap>(
`/table/${this.table}/sync_id/${syncId}`,
).catch((error) => {
if (error.message === 'No id map found') {
if (error.message === NO_ID_MAP_MESSAGE) {
return undefined;
}
throw error;
Expand All @@ -62,7 +62,7 @@ export abstract class IdMapperClient extends ExtensionClient {
const idMap = await this.fetch<IdMap>(
`/table/${this.table}/local_id/${localId}`,
).catch((error) => {
if (error.message === 'No id map found') {
if (error.message === NO_ID_MAP_MESSAGE) {
return undefined;
}
throw error;
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/lib/services/extension-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import createHttpError from 'http-errors';
import { MigrationClient } from './migration-client';
import { Cacheable } from 'typescript-cacheable';

export const NO_ID_MAP_MESSAGE = 'No id map found';

/**
* This class provides an interface to interact with the Directus extension sync endpoints.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/lib/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './config';
export * from './migration-client';
export * from './extension-client';
export * from './ping-client';
export * from './helpers-client';
export * from './snapshot';
export * from './specifications';
Expand Down
30 changes: 30 additions & 0 deletions packages/cli/src/lib/services/ping-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ExtensionClient, NO_ID_MAP_MESSAGE } from './extension-client';
import { MigrationClient } from './migration-client';
import { Service } from 'typedi';

@Service()
export class PingClient extends ExtensionClient {
constructor(migrationClient: MigrationClient) {
super(migrationClient);
}

/** Try to get a fake id map in order to check if the server is up and the extension is installed */
async test() {
const response = await this.fetch<unknown>(
`/table/__ping_test__/sync_id/__ping_test__`,
'GET',
)
.then(() => ({ success: true }))
.catch((error: Error) => ({ error }));

if ('success' in response) {
return; // Should not happen
}

if (response.error.message === NO_ID_MAP_MESSAGE) {
return;
}

throw response.error;
}
}
21 changes: 10 additions & 11 deletions packages/e2e/spec/config/config-path-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ export const configPathInfo = (context: Context) => {
);

const output = await sync.diff();
const first = output.shift();
const log = output.find((l) => l.msg.includes('No config file found'));

expect(first?.msg).toContain('No config file found');
expect(first?.msg).toContain('wrong-path/directus-sync.config.cjs');
expect(log).toBeDefined();
expect(log?.msg).toContain('wrong-path/directus-sync.config.cjs');
});

it('ensure log shows info about missing config file for default files', async () => {
// Init sync client
const sync = await context.getSync('sources/empty-collections');

const output = await sync.diff();
const first = output.shift();
const log = output.find((l) => l.msg.includes('No config file found'));

expect(first?.msg).toContain('No config file found');
expect(first?.msg).toContain('e2e/directus-sync.config.cjs');
expect(first?.msg).toContain('e2e/directus-sync.config.js');
expect(first?.msg).toContain('e2e/directus-sync.config.json');
expect(log).toBeDefined();
expect(log?.msg).toContain('e2e/directus-sync.config.cjs');
expect(log?.msg).toContain('e2e/directus-sync.config.js');
expect(log?.msg).toContain('e2e/directus-sync.config.json');
});

it('ensure logs show info when config path exists', async () => {
Expand All @@ -36,9 +36,8 @@ export const configPathInfo = (context: Context) => {
);

const output = await sync.diff();
const first = output.shift();
const log = output.find((l) => l.msg.includes('Loaded config file from'));

expect(first?.msg).toContain('Loaded config file from');
expect(first?.msg).toContain('config-path-info/directus-sync.config.cjs');
expect(log?.msg).toContain('config-path-info/directus-sync.config.cjs');
});
};

0 comments on commit f78fac7

Please sign in to comment.