mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Make path argument optional for snapshot command (#14074)
* make path argument optional for snapshot command * Cleanup branching paths a tad Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
@@ -8,48 +8,56 @@ import { dump as toYaml } from 'js-yaml';
|
||||
import { flushCaches } from '../../../cache';
|
||||
|
||||
export async function snapshot(
|
||||
snapshotPath: string,
|
||||
snapshotPath?: string,
|
||||
options?: { yes: boolean; format: 'json' | 'yaml' }
|
||||
): Promise<void> {
|
||||
const filename = path.resolve(process.cwd(), snapshotPath);
|
||||
|
||||
let snapshotExists: boolean;
|
||||
|
||||
try {
|
||||
await fs.access(filename, fsConstants.F_OK);
|
||||
snapshotExists = true;
|
||||
} catch {
|
||||
snapshotExists = false;
|
||||
}
|
||||
|
||||
if (snapshotExists && options?.yes === false) {
|
||||
const { overwrite } = await inquirer.prompt([
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'overwrite',
|
||||
message: 'Snapshot already exists. Do you want to overwrite the file?',
|
||||
},
|
||||
]);
|
||||
|
||||
if (overwrite === false) {
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
await flushCaches();
|
||||
|
||||
const database = getDatabase();
|
||||
|
||||
const snapshot = await getSnapshot({ database });
|
||||
|
||||
try {
|
||||
const snapshot = await getSnapshot({ database });
|
||||
|
||||
let snapshotString: string;
|
||||
|
||||
if (options?.format === 'yaml') {
|
||||
await fs.writeFile(filename, toYaml(snapshot));
|
||||
snapshotString = toYaml(snapshot);
|
||||
} else {
|
||||
await fs.writeFile(filename, JSON.stringify(snapshot));
|
||||
snapshotString = JSON.stringify(snapshot);
|
||||
}
|
||||
|
||||
logger.info(`Snapshot saved to ${filename}`);
|
||||
if (snapshotPath) {
|
||||
const filename = path.resolve(process.cwd(), snapshotPath);
|
||||
|
||||
let snapshotExists: boolean;
|
||||
|
||||
try {
|
||||
await fs.access(filename, fsConstants.F_OK);
|
||||
snapshotExists = true;
|
||||
} catch {
|
||||
snapshotExists = false;
|
||||
}
|
||||
|
||||
if (snapshotExists && options?.yes === false) {
|
||||
const { overwrite } = await inquirer.prompt([
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'overwrite',
|
||||
message: 'Snapshot already exists. Do you want to overwrite the file?',
|
||||
},
|
||||
]);
|
||||
|
||||
if (overwrite === false) {
|
||||
database.destroy();
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
await fs.writeFile(filename, snapshotString);
|
||||
logger.info(`Snapshot saved to ${filename}`);
|
||||
} else {
|
||||
process.stdout.write(snapshotString);
|
||||
}
|
||||
|
||||
database.destroy();
|
||||
process.exit(0);
|
||||
|
||||
@@ -85,7 +85,7 @@ export async function createCli(): Promise<Command> {
|
||||
.description('Create a new Schema Snapshot')
|
||||
.option('-y, --yes', `Assume "yes" as answer to all prompts and run non-interactively`, false)
|
||||
.addOption(new Option('--format <format>', 'JSON or YAML format').choices(['json', 'yaml']).default('yaml'))
|
||||
.argument('<path>', 'Path to snapshot file')
|
||||
.argument('[path]', 'Path to snapshot file')
|
||||
.action(snapshot);
|
||||
|
||||
schemaCommands
|
||||
|
||||
Reference in New Issue
Block a user