Skip to content

Commit bb80ece

Browse files
committed
refactor(cli): Review fixing
1 parent ea9f2fb commit bb80ece

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

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

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -692,14 +692,23 @@ const deleteAttribute = async (collection, attribute) => {
692692
});
693693
}
694694

695-
const deepSimilar = (remote, local, collection) => {
695+
/**
696+
* Check if attribute non-changeable fields has been changed
697+
* If so return the differences as an object.
698+
* @param remote
699+
* @param local
700+
* @param collection
701+
* @returns {undefined|{reason: string, action: *, attribute, key: string}}
702+
*/
703+
const checkAttributeChanges = (remote, local, collection) => {
696704
if (local === undefined) {
697705
return undefined;
698706
}
699707

700708
const keyName = `${chalk.yellow(local.key)} in ${collection.name} (${collection['$id']})`;
701709
const action = chalk.cyan('recreating');
702710
let reason = '';
711+
703712
for (let key of Object.keys(remote)) {
704713
if (changeableKeys.includes(key)) {
705714
continue;
@@ -714,23 +723,38 @@ const deepSimilar = (remote, local, collection) => {
714723
return reason === '' ? undefined : { key: keyName, attribute: remote, reason, action };
715724
}
716725

717-
const findMatch = (attribute, attributes) => attributes.find((attr) => attr.key === attribute.key);
726+
/**
727+
* Check if attributes contain the given attribute
728+
* @param attribute
729+
* @param attributes
730+
* @returns {*}
731+
*/
732+
const attributesContains = (attribute, attributes) => attributes.find((attr) => attr.key === attribute.key);
733+
718734

719-
const mapChangeAttribute = (attribute, collection, isAdding) => {
735+
const generateChangesObject = (attribute, collection, isAdding) => {
720736
return {
721737
key: `${chalk.yellow(attribute.key)} in ${collection.name} (${collection['$id']})`,
722-
attribute,
738+
attribute: attribute,
723739
reason: isAdding ? 'Field doesn\'t exist on the remote server' : 'Field doesn\'t exist in appwrite.json file',
724740
action: isAdding ? chalk.green('adding') : chalk.red('deleting')
725741
};
726742

727743
};
728744

729-
const updatedList = async (remoteAttributes, localAttributes, collection) => {
745+
/**
746+
* Filter deleted and recreated attributes,
747+
* return list of attributes to create
748+
* @param remoteAttributes
749+
* @param localAttributes
750+
* @param collection
751+
* @returns {Promise<*|*[]>}
752+
*/
753+
const attributesToCreate = async (remoteAttributes, localAttributes, collection) => {
730754

731-
const deleting = remoteAttributes.filter((attribute) => !findMatch(attribute, localAttributes)).map((attr) => mapChangeAttribute(attr, collection, false));
732-
const adding = localAttributes.filter((attribute) => !findMatch(attribute, remoteAttributes)).map((attr) => mapChangeAttribute(attr, collection, true));
733-
const conflicts = remoteAttributes.map((attribute) => deepSimilar(attribute, findMatch(attribute, localAttributes), collection)).filter(attribute => attribute !== undefined);
755+
const deleting = remoteAttributes.filter((attribute) => !attributesContains(attribute, localAttributes)).map((attr) => generateChangesObject(attr, collection, false));
756+
const adding = localAttributes.filter((attribute) => !attributesContains(attribute, remoteAttributes)).map((attr) => generateChangesObject(attr, collection, true));
757+
const conflicts = remoteAttributes.map((attribute) => checkAttributeChanges(attribute, attributesContains(attribute, localAttributes), collection)).filter(attribute => attribute !== undefined);
734758

735759
let changedAttributes = [];
736760
const changing = [...deleting, ...adding, ...conflicts]
@@ -751,7 +775,7 @@ const updatedList = async (remoteAttributes, localAttributes, collection) => {
751775
if (conflicts.length > 0) {
752776
changedAttributes = conflicts.map((change) => change.attribute);
753777
await Promise.all(changedAttributes.map((changed) => deleteAttribute(collection, changed)));
754-
remoteAttributes = remoteAttributes.filter((attribute) => !findMatch(attribute, changedAttributes))
778+
remoteAttributes = remoteAttributes.filter((attribute) => !attributesContains(attribute, changedAttributes))
755779
}
756780

757781
const deletingAttributes = deleting.map((change) => change.attribute);
@@ -766,7 +790,7 @@ const updatedList = async (remoteAttributes, localAttributes, collection) => {
766790
}
767791
}
768792

769-
return localAttributes.filter((attribute) => !findMatch(attribute, remoteAttributes));
793+
return localAttributes.filter((attribute) => !attributesContains(attribute, remoteAttributes));
770794
}
771795

772796
const pushCollection = async ({ all, yes } = {}) => {
@@ -864,7 +888,7 @@ const pushCollection = async ({ all, yes } = {}) => {
864888
let attributes = collection.attributes;
865889

866890
if (collection.isExisted) {
867-
attributes = await updatedList(collection.remoteVersion.attributes, collection.attributes, collection);
891+
attributes = await attributesToCreate(collection.remoteVersion.attributes, collection.attributes, collection);
868892

869893
if (Array.isArray(attributes) && attributes.length <= 0) {
870894
log(`No changes has been detected. Skipping ${collection.name} ( ${collection['$id']} )`);

templates/cli/lib/questions.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ const questionsPushCollections = [
370370
{
371371
type: "input",
372372
name: "changes",
373-
message: `Changes above will cause recreation of an attribute. All existing documents will lose data in those attributes. Type "YES" to confirm`
373+
message: `Are you sure you want to override this collection? This can lead to loss of data! Type "YES" to confirm.`
374374
}
375375
]
376376

0 commit comments

Comments
 (0)