@agility/content-sync 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -40
- package/dist/agility-sync-sdk.node.js +148 -93
- package/package-lock.json +6856 -0
- package/package.json +1 -1
- package/src/methods/syncContent.js +9 -5
- package/src/methods/syncPages.js +13 -8
- package/src/store-interface-console.js +45 -4
- package/src/store-interface-filesystem.js +89 -42
- package/src/store-interface.js +8 -9
package/package.json
CHANGED
|
@@ -53,15 +53,19 @@ export default async function (languageCode, token) {
|
|
|
53
53
|
const syncItems = syncRet.items;
|
|
54
54
|
|
|
55
55
|
//if we don't get anything back, kick out
|
|
56
|
-
if (syncItems.length
|
|
57
|
-
|
|
56
|
+
if (syncItems.length > 0 ) {
|
|
57
|
+
|
|
58
|
+
for (let index = 0; index < syncItems.length; index++) {
|
|
59
|
+
await storeInterface.saveContentItem({ contentItem: syncItems[index], languageCode });
|
|
60
|
+
}
|
|
58
61
|
}
|
|
59
62
|
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
if (syncRet.syncToken > token) {
|
|
64
|
+
token = syncRet.syncToken;
|
|
65
|
+
} else {
|
|
66
|
+
break;
|
|
62
67
|
}
|
|
63
68
|
|
|
64
|
-
token = syncRet.syncToken;
|
|
65
69
|
itemCount += syncItems.length;
|
|
66
70
|
|
|
67
71
|
} while (token > 0 || busy === true)
|
package/src/methods/syncPages.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { logInfo, logWarning, sleep } from '../util'
|
|
2
2
|
|
|
3
3
|
export default async function (languageCode, token) {
|
|
4
|
-
|
|
4
|
+
const storeInterface = this.store;
|
|
5
5
|
if (!token) token = 0;
|
|
6
6
|
|
|
7
7
|
let itemCount = 0;
|
|
@@ -29,7 +29,7 @@ export default async function (languageCode, token) {
|
|
|
29
29
|
break
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
if (!
|
|
32
|
+
if (!busy) {
|
|
33
33
|
busy = true
|
|
34
34
|
logInfo("Sync API is busy. Waiting...")
|
|
35
35
|
}
|
|
@@ -46,16 +46,21 @@ export default async function (languageCode, token) {
|
|
|
46
46
|
|
|
47
47
|
const syncItems = syncRet.items;
|
|
48
48
|
|
|
49
|
-
//if we
|
|
50
|
-
if (syncItems.length
|
|
51
|
-
|
|
49
|
+
//if we have something...
|
|
50
|
+
if (syncItems.length > 0) {
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
for (let index = 0; index < syncItems.length; index++) {
|
|
54
|
+
await storeInterface.savePageItem({ pageItem: syncItems[index], languageCode });
|
|
55
|
+
}
|
|
52
56
|
}
|
|
53
57
|
|
|
54
|
-
|
|
55
|
-
|
|
58
|
+
if (syncRet.syncToken > token) {
|
|
59
|
+
token = syncRet.syncToken;
|
|
60
|
+
} else {
|
|
61
|
+
break;
|
|
56
62
|
}
|
|
57
63
|
|
|
58
|
-
token = syncRet.syncToken;
|
|
59
64
|
itemCount += syncItems.length;
|
|
60
65
|
|
|
61
66
|
|
|
@@ -1,23 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The function to handle saving/updating an item to your storage. This could be a Content Item, Page, Url Redirections, Sync State (state), or Sitemap.
|
|
3
|
+
* @param {Object} params - The parameters object
|
|
4
|
+
* @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
|
|
5
|
+
* @param {Object} params.item - The object representing the Content Item, Page, Url Redirections, Sync State (state), or Sitemap that needs to be saved/updated
|
|
6
|
+
* @param {String} params.itemType - The type of item being saved/updated, expected values are `item`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
|
|
7
|
+
* @param {String} params.languageCode - The locale code associated to the item being saved/updated
|
|
8
|
+
* @param {(String|Number)} params.itemID - The ID of the item being saved/updated - this could be a string or number depending on the itemType
|
|
9
|
+
* @returns {Void}
|
|
10
|
+
*/
|
|
1
11
|
const saveItem = async ({ options, item, itemType, languageCode, itemID }) => {
|
|
2
12
|
console.log(`Console Interface: saveItem has been called`);
|
|
3
13
|
return null;
|
|
4
14
|
}
|
|
5
|
-
|
|
15
|
+
/**
|
|
16
|
+
* The function to handle deleting an item to your storage. This could be a Content Item, Page, Url Redirections, Sync State (state), or Sitemap.
|
|
17
|
+
* @param {Object} params - The parameters object
|
|
18
|
+
* @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
|
|
19
|
+
* @param {String} params.itemType - The type of item being deleted, expected values are `item`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
|
|
20
|
+
* @param {String} params.languageCode - The locale code associated to the item being saved/updated
|
|
21
|
+
* @param {(String|Number)} params.itemID - The ID of the item being deleted - this could be a string or number depending on the itemType
|
|
22
|
+
* @returns {Void}
|
|
23
|
+
*/
|
|
6
24
|
const deleteItem = async ({ options, itemType, languageCode, itemID }) => {
|
|
7
25
|
console.log(`Console Interface: deleteItem has been called`);
|
|
8
26
|
return null;
|
|
9
27
|
}
|
|
10
|
-
|
|
28
|
+
/**
|
|
29
|
+
* The function to handle updating and placing a Content Item into a "list" so that you can handle querying a collection of items.
|
|
30
|
+
* @param {Object} params - The parameters object
|
|
31
|
+
* @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
|
|
32
|
+
* @param {Object} params.item - The object representing the Content Item
|
|
33
|
+
* @param {String} params.languageCode - The locale code associated to the item being saved/updated
|
|
34
|
+
* @param {(String|Number)} params.itemID - The ID of the item being updated - this could be a string or number depending on the itemType
|
|
35
|
+
* @param {String} params.referenceName - The reference name of the Content List that this Content Item should be added to
|
|
36
|
+
* @param {String} params.definitionName - The Model name that the Content Item is based on
|
|
37
|
+
* @returns {Void}
|
|
38
|
+
*/
|
|
11
39
|
const mergeItemToList = async ({ options, item, languageCode, itemID, referenceName, definitionName }) => {
|
|
12
40
|
console.log(`Console Interface: mergeItemToList has been called`);
|
|
13
41
|
return null;
|
|
14
42
|
}
|
|
15
|
-
|
|
43
|
+
/**
|
|
44
|
+
* The function to handle retrieving a Content Item, Page, Url Redirections, Sync State (state), or Sitemap
|
|
45
|
+
* @param {Object} params - The parameters object
|
|
46
|
+
* @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
|
|
47
|
+
* @param {String} params.itemType - The type of item being accessed, expected values are `item`, `list`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
|
|
48
|
+
* @param {String} params.languageCode - The locale code associated to the item being accessed
|
|
49
|
+
* @param {(String|Number)} params.itemID - The ID of the item being accessed - this could be a string or number depending on the itemType
|
|
50
|
+
* @returns {Object}
|
|
51
|
+
*/
|
|
16
52
|
const getItem = async ({ options, itemType, languageCode, itemID }) => {
|
|
17
53
|
console.log(`Console Interface: getItem has been called`)
|
|
18
54
|
return null;
|
|
19
55
|
}
|
|
20
|
-
|
|
56
|
+
/**
|
|
57
|
+
* The function to handle clearing the cache of synchronized data from the CMS
|
|
58
|
+
* @param {Object} params - The parameters object
|
|
59
|
+
* @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
|
|
60
|
+
* @returns {Void}
|
|
61
|
+
*/
|
|
21
62
|
const clearItems = async ({ options }) => {
|
|
22
63
|
console.log(`Console Interface: clearItem has been called`)
|
|
23
64
|
return null;
|
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
const fs = require('fs')
|
|
2
2
|
const os = require('os')
|
|
3
3
|
const path = require('path')
|
|
4
|
-
const {
|
|
5
|
-
const { lockSync, unlockSync, checkSync, check }
|
|
4
|
+
const {sleep} = require("./util")
|
|
5
|
+
const { lockSync, unlockSync, checkSync, check } = require("proper-lockfile")
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
require("dotenv").config({
|
|
9
9
|
path: `.env.${process.env.NODE_ENV}`,
|
|
10
10
|
})
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The function to handle saving/updating an item to your storage. This could be a Content Item, Page, Url Redirections, Sync State (state), or Sitemap.
|
|
14
|
+
* @param {Object} params - The parameters object
|
|
15
|
+
* @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
|
|
16
|
+
* @param {String} params.options.rootPath - The path to store/access the content as JSON
|
|
17
|
+
* @param {Object} params.item - The object representing the Content Item, Page, Url Redirections, Sync State (state), or Sitemap that needs to be saved/updated
|
|
18
|
+
* @param {String} params.itemType - The type of item being saved/updated, expected values are `item`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
|
|
19
|
+
* @param {String} params.languageCode - The locale code associated to the item being saved/updated
|
|
20
|
+
* @param {(String|Number)} params.itemID - The ID of the item being saved/updated - this could be a string or number depending on the itemType
|
|
21
|
+
* @returns {Void}
|
|
22
|
+
*/
|
|
19
23
|
const saveItem = async ({ options, item, itemType, languageCode, itemID }) => {
|
|
20
24
|
|
|
21
25
|
let filePath = getFilePath({ options, itemType, languageCode, itemID });
|
|
@@ -30,7 +34,16 @@ const saveItem = async ({ options, item, itemType, languageCode, itemID }) => {
|
|
|
30
34
|
let json = JSON.stringify(item);
|
|
31
35
|
fs.writeFileSync(filePath, json);
|
|
32
36
|
}
|
|
33
|
-
|
|
37
|
+
/**
|
|
38
|
+
* The function to handle deleting an item to your storage. This could be a Content Item, Page, Url Redirections, Sync State (state), or Sitemap.
|
|
39
|
+
* @param {Object} params - The parameters object
|
|
40
|
+
* @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
|
|
41
|
+
* @param {String} params.options.rootPath - The path to store/access the content as JSON
|
|
42
|
+
* @param {String} params.itemType - The type of item being deleted, expected values are `item`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
|
|
43
|
+
* @param {String} params.languageCode - The locale code associated to the item being saved/updated
|
|
44
|
+
* @param {(String|Number)} params.itemID - The ID of the item being deleted - this could be a string or number depending on the itemType
|
|
45
|
+
* @returns {Void}
|
|
46
|
+
*/
|
|
34
47
|
const deleteItem = async ({ options, itemType, languageCode, itemID }) => {
|
|
35
48
|
|
|
36
49
|
let filePath = getFilePath({ options, itemType, languageCode, itemID });
|
|
@@ -40,44 +53,62 @@ const deleteItem = async ({ options, itemType, languageCode, itemID }) => {
|
|
|
40
53
|
}
|
|
41
54
|
|
|
42
55
|
}
|
|
43
|
-
|
|
56
|
+
/**
|
|
57
|
+
* The function to handle updating and placing a Content Item into a "list" so that you can handle querying a collection of items.
|
|
58
|
+
* @param {Object} params - The parameters object
|
|
59
|
+
* @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
|
|
60
|
+
* @param {String} params.options.rootPath - The path to store/access the content as JSON
|
|
61
|
+
* @param {Object} params.item - The object representing the Content Item
|
|
62
|
+
* @param {String} params.languageCode - The locale code associated to the item being saved/updated
|
|
63
|
+
* @param {(String|Number)} params.itemID - The ID of the item being updated - this could be a string or number depending on the itemType
|
|
64
|
+
* @param {String} params.referenceName - The reference name of the Content List that this Content Item should be added to
|
|
65
|
+
* @param {String} params.definitionName - The Model name that the Content Item is based on
|
|
66
|
+
* @returns {Void}
|
|
67
|
+
*/
|
|
44
68
|
const mergeItemToList = async ({ options, item, languageCode, itemID, referenceName, definitionName }) => {
|
|
45
69
|
|
|
46
70
|
let contentList = await getItem({ options, itemType: "list", languageCode, itemID: referenceName });
|
|
47
71
|
|
|
48
72
|
if (contentList == null) {
|
|
49
73
|
//initialize the list
|
|
50
|
-
contentList = [];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
//replace the item...
|
|
55
|
-
const cIndex = contentList.findIndex((ci) => {
|
|
56
|
-
return ci.contentID === itemID;
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
if (item.properties.state === 3) {
|
|
60
|
-
//*** deleted item (remove from the list) ***
|
|
61
|
-
if (cIndex >= 0) {
|
|
62
|
-
//remove the item
|
|
63
|
-
contentList.splice(cIndex, 1);
|
|
64
|
-
}
|
|
65
|
-
|
|
74
|
+
contentList = [item];
|
|
66
75
|
} else {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
76
|
+
//replace the item...
|
|
77
|
+
const cIndex = contentList.findIndex((ci) => {
|
|
78
|
+
return ci.contentID === itemID;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (item.properties.state === 3) {
|
|
82
|
+
//*** deleted item (remove from the list) ***
|
|
83
|
+
if (cIndex >= 0) {
|
|
84
|
+
//remove the item
|
|
85
|
+
contentList.splice(cIndex, 1);
|
|
86
|
+
}
|
|
87
|
+
|
|
71
88
|
} else {
|
|
72
|
-
|
|
73
|
-
|
|
89
|
+
//*** regular item (merge) ***
|
|
90
|
+
if (cIndex >= 0) {
|
|
91
|
+
//replace the existing item
|
|
92
|
+
contentList[cIndex] = item;
|
|
93
|
+
} else {
|
|
94
|
+
//and it to the end of the
|
|
95
|
+
contentList.push(item);
|
|
96
|
+
}
|
|
74
97
|
}
|
|
75
|
-
|
|
76
98
|
}
|
|
77
99
|
|
|
78
100
|
await saveItem({ options, item: contentList, itemType: "list", languageCode, itemID: referenceName });
|
|
79
101
|
}
|
|
80
|
-
|
|
102
|
+
/**
|
|
103
|
+
* The function to handle retrieving a Content Item, Page, Url Redirections, Sync State (state), or Sitemap
|
|
104
|
+
* @param {Object} params - The parameters object
|
|
105
|
+
* @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
|
|
106
|
+
* @param {String} params.options.rootPath - The path to store/access the content as JSON
|
|
107
|
+
* @param {String} params.itemType - The type of item being accessed, expected values are `item`, `list`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
|
|
108
|
+
* @param {String} params.languageCode - The locale code associated to the item being accessed
|
|
109
|
+
* @param {(String|Number)} params.itemID - The ID of the item being accessed - this could be a string or number depending on the itemType
|
|
110
|
+
* @returns {Object}
|
|
111
|
+
*/
|
|
81
112
|
const getItem = async ({ options, itemType, languageCode, itemID }) => {
|
|
82
113
|
let filePath = getFilePath({ options, itemType, languageCode, itemID });
|
|
83
114
|
|
|
@@ -87,24 +118,29 @@ const getItem = async ({ options, itemType, languageCode, itemID }) => {
|
|
|
87
118
|
return JSON.parse(json);
|
|
88
119
|
}
|
|
89
120
|
|
|
121
|
+
/**
|
|
122
|
+
* The function to handle clearing the cache of synchronized data from the CMS
|
|
123
|
+
* @param {Object} params - The parameters object
|
|
124
|
+
* @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
|
|
125
|
+
* @param {String} params.options.rootPath - The path to store/access the content as JSON
|
|
126
|
+
* @returns {Void}
|
|
127
|
+
*/
|
|
90
128
|
const clearItems = async ({ options }) => {
|
|
91
129
|
fs.rmdirSync(options.rootPath, { recursive: true })
|
|
92
130
|
}
|
|
93
131
|
|
|
94
|
-
const waitOnLock = async (lockFile) => {
|
|
95
132
|
|
|
96
|
-
while (await check(lockFile)) {
|
|
97
|
-
await sleep(100)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
}
|
|
101
133
|
|
|
134
|
+
/**
|
|
135
|
+
* The function to handle multi-threaded Syncs that may be happening at the same time. If you need to prevent a sync from happening and let it wait until another sync has finished use this.
|
|
136
|
+
* @returns {Promise}
|
|
137
|
+
*/
|
|
102
138
|
const mutexLock = async () => {
|
|
103
139
|
|
|
104
140
|
|
|
105
141
|
const dir = os.tmpdir();
|
|
106
142
|
const lockFile = `${dir}/${"agility-sync"}.mutex`
|
|
107
|
-
if (!fs.existsSync(lockFile)) {
|
|
143
|
+
if (! fs.existsSync(lockFile)) {
|
|
108
144
|
fs.writeFileSync(lockFile, "agility-sync");
|
|
109
145
|
}
|
|
110
146
|
|
|
@@ -139,7 +175,18 @@ const mutexLock = async () => {
|
|
|
139
175
|
}
|
|
140
176
|
|
|
141
177
|
|
|
178
|
+
//private function to get a wait on a lock file
|
|
179
|
+
const waitOnLock = async (lockFile) => {
|
|
180
|
+
while (await check(lockFile)) {
|
|
181
|
+
await sleep(100)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
142
184
|
|
|
185
|
+
//private function to get path of an item
|
|
186
|
+
const getFilePath = ({ options, itemType, languageCode, itemID }) => {
|
|
187
|
+
const fileName = `${itemID}.json`;
|
|
188
|
+
return path.join(options.rootPath, languageCode, itemType, fileName);
|
|
189
|
+
}
|
|
143
190
|
|
|
144
191
|
|
|
145
192
|
module.exports = {
|
package/src/store-interface.js
CHANGED
|
@@ -82,20 +82,19 @@ const saveContentItem = async ({ contentItem, languageCode }) => {
|
|
|
82
82
|
languageCode,
|
|
83
83
|
itemID: contentItem.contentID,
|
|
84
84
|
});
|
|
85
|
-
|
|
86
85
|
if (currentItem) {
|
|
86
|
+
|
|
87
87
|
//if the item is deleted, we need to grab the def and ref name from the current
|
|
88
88
|
definitionName = sanitizeName(currentItem.properties.definitionName)
|
|
89
89
|
referenceName = currentItem.properties.referenceName
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
await store.deleteItem({
|
|
93
|
-
options,
|
|
94
|
-
itemType: "item",
|
|
95
|
-
languageCode,
|
|
96
|
-
itemID: contentItem.contentID,
|
|
97
|
-
});
|
|
98
90
|
|
|
91
|
+
await store.deleteItem({
|
|
92
|
+
options,
|
|
93
|
+
itemType: "item",
|
|
94
|
+
languageCode,
|
|
95
|
+
itemID: contentItem.contentID,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
99
98
|
} else {
|
|
100
99
|
//regular item
|
|
101
100
|
if (!contentItem.properties.definitionName
|