Skip to content

Commit 0f54756

Browse files
Merge pull request appwrite#931 from appwrite/fix-cli-bugs
Fix cli bugs
2 parents 8265f1a + 2c115a4 commit 0f54756

File tree

3 files changed

+55
-42
lines changed

3 files changed

+55
-42
lines changed

templates/cli/lib/commands/push.js.twig

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const chalk = require('chalk');
22
const inquirer = require("inquirer");
33
const JSONbig = require("json-bigint")({ storeAsString: false });
44
const { Command } = require("commander");
5-
const { localConfig, globalConfig, KeysAttributes, KeysFunction, whitelistKeys, KeysTopics, KeysStorage, KeysTeams, } = require("../config");
5+
const { localConfig, globalConfig, KeysAttributes, KeysFunction, whitelistKeys, KeysTopics, KeysStorage, KeysTeams, KeysCollection } = require("../config");
66
const { Spinner, SPINNER_ARC, SPINNER_DOTS } = require('../spinner');
77
const { paginate } = require('../paginate');
88
const { questionsPushBuckets, questionsPushTeams, questionsPushFunctions, questionsGetEntrypoint, questionsPushCollections, questionPushChanges, questionPushChangesConfirmation, questionsPushMessagingTopics, questionsPushResources } = require("../questions");
@@ -341,42 +341,50 @@ const awaitPools = {
341341
}
342342

343343
const getConfirmation = async () => {
344-
if (!cliConfig.force) {
345-
async function fixConfirmation() {
346-
const answers = await inquirer.prompt(questionPushChangesConfirmation);
347-
if(answers.changes !== 'YES' && answers.changes !== 'NO') {
348-
return await fixConfirmation();
349-
}
344+
if (cliConfig.force) {
345+
return true;
346+
}
350347

351-
return answers.changes;
348+
async function fixConfirmation() {
349+
const answers = await inquirer.prompt(questionPushChangesConfirmation);
350+
if (answers.changes !== 'YES' && answers.changes !== 'NO') {
351+
return await fixConfirmation();
352352
}
353353

354-
let answers = await inquirer.prompt(questionPushChanges);
355-
if(answers.changes !== 'YES' && answers.changes !== 'NO') {
356-
answers.changes = await fixConfirmation();
357-
}
354+
return answers.changes;
355+
}
358356

359-
if (answers.changes === 'YES') {
360-
return true;
361-
}
357+
let answers = await inquirer.prompt(questionPushChanges);
362358

363-
warn('Skipping push action. Changes were not applied.');
364-
return false;
359+
if (answers.changes !== 'YES' && answers.changes !== 'NO') {
360+
answers.changes = await fixConfirmation();
365361
}
366362

367-
return true;
363+
if (answers.changes === 'YES') {
364+
return true;
365+
}
366+
367+
warn('Skipping push action. Changes were not applied.');
368+
return false;
369+
368370
};
369371

370-
const approveChanges = async (resource, resourceGetFunction, keys, resourceName, resourcePlural, skipKeys = []) => {
372+
const approveChanges = async (resource, resourceGetFunction, keys, resourceName, resourcePlural, skipKeys = [], secondId = '', secondResourceName = '') => {
371373
log('Checking for changes ...');
372374
const changes = [];
373375

374376
await Promise.all(resource.map(async (localResource) => {
375377
try {
376-
const remoteResource = await resourceGetFunction({
378+
const options = {
377379
[resourceName]: localResource['$id'],
378380
parseOutput: false,
379-
});
381+
};
382+
383+
if (secondId !== '' && secondResourceName !== '') {
384+
options[secondResourceName] = localResource[secondId]
385+
}
386+
387+
const remoteResource = await resourceGetFunction(options);
380388

381389
for (let [key, value] of Object.entries(whitelistKeys(remoteResource, keys))) {
382390
if (skipKeys.includes(key)) {
@@ -412,7 +420,7 @@ const approveChanges = async (resource, resourceGetFunction, keys, resourceName,
412420
}
413421

414422
drawTable(changes);
415-
if((await getConfirmation()) === true) {
423+
if ((await getConfirmation()) === true) {
416424
return true;
417425
}
418426

@@ -688,6 +696,20 @@ const deleteAttribute = async (collection, attribute, isIndex = false) => {
688696
});
689697
}
690698

699+
const compareAttribute = (remote, local, reason, key) => {
700+
if (Array.isArray(remote) && Array.isArray(local)) {
701+
if (JSON.stringify(remote) !== JSON.stringify(local)) {
702+
const bol = reason === '' ? '' : '\n';
703+
reason += `${bol}${key} changed from ${chalk.red(remote)} to ${chalk.green(local)}`;
704+
}
705+
} else if (remote !== local) {
706+
const bol = reason === '' ? '' : '\n';
707+
reason += `${bol}${key} changed from ${chalk.red(remote)} to ${chalk.green(local)}`;
708+
}
709+
710+
return reason
711+
}
712+
691713

692714
/**
693715
* Check if attribute non-changeable fields has been changed
@@ -715,11 +737,7 @@ const checkAttributeChanges = (remote, local, collection, recraeting = true) =>
715737

716738
if (changeableKeys.includes(key)) {
717739
if (!recraeting) {
718-
if (remote[key] !== local[key]) {
719-
const bol = reason === '' ? '' : '\n';
720-
reason += `${bol}${key} changed from ${chalk.red(remote[key])} to ${chalk.green(local[key])}`;
721-
attribute = local;
722-
}
740+
reason += compareAttribute(remote[key], local[key], reason, key)
723741
}
724742
continue;
725743
}
@@ -728,15 +746,7 @@ const checkAttributeChanges = (remote, local, collection, recraeting = true) =>
728746
continue;
729747
}
730748

731-
if (Array.isArray(remote[key]) && Array.isArray(local[key])) {
732-
if (JSON.stringify(remote[key]) !== JSON.stringify(local[key])) {
733-
const bol = reason === '' ? '' : '\n';
734-
reason += `${bol}${key} changed from ${chalk.red(remote[key])} to ${chalk.green(local[key])}`;
735-
}
736-
} else if (remote[key] !== local[key]) {
737-
const bol = reason === '' ? '' : '\n';
738-
reason += `${bol}${key} changed from ${chalk.red(remote[key])} to ${chalk.green(local[key])}`;
739-
}
749+
reason += compareAttribute(remote[key], local[key], reason, key)
740750
}
741751

742752
return reason === '' ? undefined : { key: keyName, attribute, reason, action };
@@ -793,7 +803,7 @@ const attributesToCreate = async (remoteAttributes, localAttributes, collection,
793803
log(`Attribute recreation will cause ${chalk.red('loss of data')}`);
794804
}
795805

796-
if((await getConfirmation()) !== true) {
806+
if ((await getConfirmation()) !== true) {
797807
return changedAttributes;
798808
}
799809
}
@@ -915,7 +925,7 @@ const pushSettings = async () => {
915925

916926
if (changes.length > 0) {
917927
drawTable(changes);
918-
if((await getConfirmation()) !== true) {
928+
if ((await getConfirmation()) !== true) {
919929
success(`Successfully pushed 0 project settings.`);
920930
return;
921931
}
@@ -1308,8 +1318,6 @@ const pushCollection = async ({ returnOnZero, attempts } = { returnOnZero: false
13081318

13091319
const databases = Array.from(new Set(collections.map(collection => collection['databaseId'])));
13101320

1311-
log('Checking for changes ...');
1312-
13131321
// Parallel db actions
13141322
await Promise.all(databases.map(async (databaseId) => {
13151323
const localDatabase = localConfig.getDatabase(databaseId);
@@ -1340,6 +1348,10 @@ const pushCollection = async ({ returnOnZero, attempts } = { returnOnZero: false
13401348
}
13411349
}));
13421350

1351+
1352+
if (!(await approveChanges(collections, databasesGetCollection, KeysCollection, 'collectionId', 'collections', ['attributes', 'indexes'], 'databaseId', 'databaseId',))) {
1353+
return;
1354+
}
13431355
// Parallel collection actions
13441356
await Promise.all(collections.map(async (collection) => {
13451357
try {

templates/cli/lib/config.js.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,5 +618,6 @@ module.exports = {
618618
KeysTopics,
619619
KeysStorage,
620620
KeysTeams,
621+
KeysCollection,
621622
whitelistKeys
622623
};

templates/cli/lib/questions.js.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ const questionPushChanges = [
662662
}
663663
];
664664

665-
const questionPushChangesFix = [
665+
const questionPushChangesConfirmation = [
666666
{
667667
type: "input",
668668
name: "changes",
@@ -847,5 +847,5 @@ module.exports = {
847847
questionsInitResources,
848848
questionsCreateTeam,
849849
questionPushChanges,
850-
questionPushChangesFix
850+
questionPushChangesConfirmation
851851
};

0 commit comments

Comments
 (0)