Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add option to ignore snapshot #54

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,29 @@ These options can be used with any command to configure the operation of `direct
- `--collections-path <collectionPath>`
Specify the path for the collections dump, relative to the dump path. The default is `"collections"`.

- `-o, --only-collections <onlyCollections>`
Comma-separated list of directus collections to include during `pull` `push` or `diff` process.

- `-x, --exclude-collections <excludeCollections>`
Comma-separated list of directus collections to exclude during `pull` `push` or `diff`. Can be used along with `only-collections`.

- `--snapshot-path <snapshotPath>`
Specify the path for the schema snapshot dump, relative to the dump path. The default is `"snapshot"`.

- `--no-snapshot`
Do not pull and push the Directus schema. By default, the schema is pulled and pushed.

- `--no-split`
Indicates whether the schema snapshot should be split into multiple files. By default, snapshots are split.

- `-f, --force`
Force the diff of schema, even if the Directus version is different. The default is `false`.

- `--specs-path <specsPath>`
Specify the path for the specifications dump (GraphQL & OpenAPI), relative to the dump path. The default is `"specs"`.

- `--no-specs`
Do not dump the specifications (GraphQL & OpenAPI). By default, specifications are dumped.

- `-o, --only-collections <onlyCollections>`
Comma-separated list of directus collections to include during `pull` `push` or `diff` process.

- `-x, --exclude-collections <excludeCollections>`
Comma-separated list of directus collections to exclude during `pull` `push` or `diff`. Can be used along with `only-collections`.
- `-f, --force`
Force the diff of schema, even if the Directus version is different. The default is `false`.

- `-h, --help`
Display help information for the `directus-sync` commands.
Expand All @@ -201,14 +204,15 @@ module.exports = {
directusToken: 'my-directus-token',
directusEmail: 'admin@example.com', // ignored if directusToken is provided
directusPassword: 'my-directus-password', // ignored if directusToken is provided
split: true,
dumpPath: './directus-config',
collectionsPath: 'collections',
onlyCollections: ['roles', 'permissions', 'settings'],
excludeCollections: ['settings'],
snapshotPath: 'snapshot',
snapshot: true,
split: true,
specsPath: 'specs',
specs: true,
onlyCollections: ['roles', 'permissions', 'settings'],
excludeCollections: ['settings'],
};
```

Expand Down
15 changes: 13 additions & 2 deletions packages/cli/src/lib/commands/diff.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { Container } from 'typedi';
import { MigrationClient, SnapshotClient } from '../services';
import { ConfigService, MigrationClient, SnapshotClient } from '../services';
import { loadCollections } from '../loader';
import pino from 'pino';
import { LOGGER } from '../constants';

export async function runDiff() {
const logger: pino.Logger = Container.get(LOGGER);
const config = Container.get(ConfigService);
const snapshotConfig = config.getSnapshotConfig();

// Clear the cache
await Container.get(MigrationClient).clearCache();

// Snapshot
await Container.get(SnapshotClient).diff();
if (snapshotConfig.enabled) {
await Container.get(SnapshotClient).diff();
} else {
logger.debug('Snapshot is disabled');
}

// Collections
const collections = loadCollections();
Expand Down
13 changes: 12 additions & 1 deletion packages/cli/src/lib/commands/pull.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import { Container } from 'typedi';
import {
ConfigService,
MigrationClient,
SnapshotClient,
SpecificationsClient,
} from '../services';
import { loadCollections } from '../loader';
import pino from 'pino';
import { LOGGER } from '../constants';

export async function runPull() {
const logger: pino.Logger = Container.get(LOGGER);
const config = Container.get(ConfigService);
const snapshotConfig = config.getSnapshotConfig();

// Clear the cache
await Container.get(MigrationClient).clearCache();

// Snapshot
await Container.get(SnapshotClient).pull();
if (snapshotConfig.enabled) {
await Container.get(SnapshotClient).pull();
} else {
logger.debug('Snapshot is disabled');
}

// Specifications
await Container.get(SpecificationsClient).pull();
Expand Down
12 changes: 9 additions & 3 deletions packages/cli/src/lib/commands/push.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { Container } from 'typedi';
import pino from 'pino';
import { MigrationClient, SnapshotClient } from '../services';
import { ConfigService, MigrationClient, SnapshotClient } from '../services';
import { loadCollections } from '../loader';
import { LOGGER } from '../constants';

export async function runPush() {
const logger: pino.Logger = Container.get(LOGGER);
const config = Container.get(ConfigService);
const snapshotConfig = config.getSnapshotConfig();

// Clear the cache
await Container.get(MigrationClient).clearCache();

// Snapshot
logger.info(`---- Push schema ----`);
await Container.get(SnapshotClient).push();
if (snapshotConfig.enabled) {
logger.info(`---- Push schema ----`);
await Container.get(SnapshotClient).push();
} else {
logger.debug('Snapshot is disabled');
}

// Collections
const collections = loadCollections();
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/lib/loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DictionaryValues } from 'ts-essentials';
import {
CollectionRecord,
CollectionsList,
ConfigService,
DashboardsCollection,
FlowsCollection,
Expand All @@ -18,6 +19,7 @@ import { createDumpFolders, getPinoTransport } from './helpers';
import { Container, Token } from 'typedi';
import Logger from 'pino';
import { LOGGER } from './constants';
import pino from 'pino';

// eslint-disable-next-line @typescript-eslint/require-await
export async function initContext(
Expand Down Expand Up @@ -74,7 +76,11 @@ export function loadCollections() {

// Get the collections to process
const config = Container.get(ConfigService);
const logger: pino.Logger = Container.get(LOGGER);
const collectionsToProcess = config.getCollectionsToProcess();
const excludedCollections = CollectionsList.filter(
(collection) => !collectionsToProcess.includes(collection),
);

// Initialize the collections
const output: CollectionInstance[] = [];
Expand All @@ -89,5 +95,9 @@ export function loadCollections() {
);
}

if (excludedCollections.length > 0) {
logger.debug(`Excluded collections: ${excludedCollections.join(', ')}`);
}

return output;
}
64 changes: 39 additions & 25 deletions packages/cli/src/lib/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ function cleanProgramOptions(programOptions: Record<string, unknown>) {
* Remove some default values from the command options that overrides the config file
*/
function cleanCommandOptions(commandOptions: Record<string, unknown>) {
if (commandOptions.snapshot === true) {
delete commandOptions.snapshot;
}
if (commandOptions.split === true) {
delete commandOptions.split;
}
Expand Down Expand Up @@ -92,26 +95,37 @@ export function createProgram() {
);

// Shared options
const noSplitOption = new Option(
'--no-split',
`should split the schema snapshot into multiple files (default "${DefaultConfig.split}")`,
);
const dumpPathOption = new Option(
'--dump-path <dumpPath>',
`the base path for the dump (default "${DefaultConfig.dumpPath}")`,
);

const collectionsPathOption = new Option(
'--collections-path <collectionPath>',
`the path for the collections dump, relative to the dump path (default "${DefaultConfig.collectionsPath}")`,
);
const excludeCollectionsOption = new Option(
'-x, --exclude-collections <excludeCollections>',
`comma separated list of collections to exclude from the process (default to none)`,
).argParser(commaSeparatedList);
const onlyCollectionsOption = new Option(
'-o, --only-collections <onlyCollections>',
`comma separated list of collections to include in the process (default to all)`,
).argParser(commaSeparatedList);

const snapshotPathOption = new Option(
'--snapshot-path <snapshotPath>',
`the path for the schema snapshot dump, relative to the dump path (default "${DefaultConfig.snapshotPath}")`,
);
const forceOption = new Option(
'-f, --force',
`force the diff of schema, even if the Directus version is different (default "${DefaultConfig.force}")`,
const noSnapshotOption = new Option(
'--no-snapshot',
`should pull and push the Directus schema (default "${DefaultConfig.snapshot}")`,
);
const noSplitOption = new Option(
'--no-split',
`should split the schema snapshot into multiple files (default "${DefaultConfig.split}")`,
);

const specificationsPathOption = new Option(
'--specs-path <specsPath>',
`the path for the specifications dump (GraphQL & OpenAPI), relative to the dump path (default "${DefaultConfig.specsPath}")`,
Expand All @@ -120,14 +134,11 @@ export function createProgram() {
'--no-specs',
`should dump the GraphQL & OpenAPI specifications (default "${DefaultConfig.specs}")`,
);
const excludeCollectionsOption = new Option(
'-x, --exclude-collections <excludeCollections>',
`comma separated list of collections to exclude from the process`,
).argParser(commaSeparatedList);
const onlyCollectionsOption = new Option(
'-o, --only-collections <onlyCollections>',
`comma separated list of collections to include in the process`,
).argParser(commaSeparatedList);

const forceOption = new Option(
'-f, --force',
`force the diff of schema, even if the Directus version is different (default "${DefaultConfig.force}")`,
);

program
.version(getVersion())
Expand All @@ -141,40 +152,43 @@ export function createProgram() {
program
.command('pull')
.description('get the schema and collections and store them locally')
.addOption(noSplitOption)
.addOption(dumpPathOption)
.addOption(collectionsPathOption)
.addOption(snapshotPathOption)
.addOption(noSpecificationsOption)
.addOption(specificationsPathOption)
.addOption(excludeCollectionsOption)
.addOption(onlyCollectionsOption)
.addOption(snapshotPathOption)
.addOption(noSnapshotOption)
.addOption(noSplitOption)
.addOption(specificationsPathOption)
.addOption(noSpecificationsOption)
.action(wrapAction(program, runPull));

program
.command('diff')
.description(
'describe the schema and collections diff. Does not modify the database.',
)
.addOption(noSplitOption)
.addOption(dumpPathOption)
.addOption(collectionsPathOption)
.addOption(snapshotPathOption)
.addOption(forceOption)
.addOption(excludeCollectionsOption)
.addOption(onlyCollectionsOption)
.addOption(snapshotPathOption)
.addOption(noSnapshotOption)
.addOption(noSplitOption)
.addOption(forceOption)
.action(wrapAction(program, runDiff));

program
.command('push')
.description('push the schema and collections')
.addOption(noSplitOption)
.addOption(dumpPathOption)
.addOption(collectionsPathOption)
.addOption(snapshotPathOption)
.addOption(forceOption)
.addOption(excludeCollectionsOption)
.addOption(onlyCollectionsOption)
.addOption(snapshotPathOption)
.addOption(noSnapshotOption)
.addOption(noSplitOption)
.addOption(forceOption)
.action(wrapAction(program, runPush));

program
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/lib/services/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class ConfigService {
dumpPath: snapshotPath,
splitFiles: this.requireOptions('split'),
force: this.requireOptions('force'),
enabled: this.requireOptions('snapshot'),
};
}

Expand Down
19 changes: 11 additions & 8 deletions packages/cli/src/lib/services/config/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,30 @@ export const DefaultConfig: Pick<
| 'debug'
| 'dumpPath'
| 'collectionsPath'
| 'excludeCollections'
| 'onlyCollections'
| 'snapshotPath'
| 'snapshot'
| 'split'
| 'force'
| 'specs'
| 'specsPath'
| 'excludeCollections'
| 'onlyCollections'
| 'specs'
| 'force'
> = {
// Global
debug: false,
// Pull, diff, push
dumpPath: './directus-config',
// Collections
collectionsPath: 'collections',
excludeCollections: [],
onlyCollections: [],
// Snapshot
snapshotPath: 'snapshot',
snapshot: true,
split: true,
// Specifications
specs: true,
specsPath: 'specs',
specs: true,
// Diff, push
force: false,
// Exclusion and Inclusion
excludeCollections: [],
onlyCollections: [],
};
Loading
Loading