contentful-import 8.5.1 → 8.5.3
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/dist/index.js +2 -30
- package/dist/parseOptions.js +6 -31
- package/dist/tasks/get-destination-data.js +5 -23
- package/dist/tasks/init-client.js +0 -10
- package/dist/tasks/push-to-space/assets.js +0 -14
- package/dist/tasks/push-to-space/creation.js +11 -43
- package/dist/tasks/push-to-space/publishing.js +2 -35
- package/dist/tasks/push-to-space/push-to-space.js +6 -31
- package/dist/transform/transform-space.js +1 -10
- package/dist/transform/transformers.js +3 -17
- package/dist/usageParams.js +0 -7
- package/dist/utils/headers.js +2 -7
- package/dist/utils/schema.js +1 -6
- package/dist/utils/sort-entries.js +1 -22
- package/dist/utils/sort-locales.js +0 -6
- package/dist/utils/validations.js +0 -14
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -4,56 +4,35 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = runContentfulImport;
|
|
7
|
-
|
|
8
7
|
var _cliTable = _interopRequireDefault(require("cli-table3"));
|
|
9
|
-
|
|
10
8
|
var _differenceInSeconds = _interopRequireDefault(require("date-fns/differenceInSeconds"));
|
|
11
|
-
|
|
12
9
|
var _formatDistance = _interopRequireDefault(require("date-fns/formatDistance"));
|
|
13
|
-
|
|
14
10
|
var _listr = _interopRequireDefault(require("listr"));
|
|
15
|
-
|
|
16
11
|
var _listrUpdateRenderer = _interopRequireDefault(require("listr-update-renderer"));
|
|
17
|
-
|
|
18
12
|
var _listrVerboseRenderer = _interopRequireDefault(require("listr-verbose-renderer"));
|
|
19
|
-
|
|
20
13
|
var _lodash = require("lodash");
|
|
21
|
-
|
|
22
14
|
var _pQueue = _interopRequireDefault(require("p-queue"));
|
|
23
|
-
|
|
24
15
|
var _logging = require("contentful-batch-libs/dist/logging");
|
|
25
|
-
|
|
26
16
|
var _listr2 = require("contentful-batch-libs/dist/listr");
|
|
27
|
-
|
|
28
17
|
var _initClient = _interopRequireDefault(require("./tasks/init-client"));
|
|
29
|
-
|
|
30
18
|
var _getDestinationData = _interopRequireDefault(require("./tasks/get-destination-data"));
|
|
31
|
-
|
|
32
19
|
var _pushToSpace = _interopRequireDefault(require("./tasks/push-to-space/push-to-space"));
|
|
33
|
-
|
|
34
20
|
var _transformSpace = _interopRequireDefault(require("./transform/transform-space"));
|
|
35
|
-
|
|
36
21
|
var _validations = require("./utils/validations");
|
|
37
|
-
|
|
38
22
|
var _parseOptions = _interopRequireDefault(require("./parseOptions"));
|
|
39
|
-
|
|
40
23
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
41
|
-
|
|
42
24
|
const ONE_SECOND = 1000;
|
|
43
|
-
|
|
44
25
|
function createListrOptions(options) {
|
|
45
26
|
if (options.useVerboseRenderer) {
|
|
46
27
|
return {
|
|
47
28
|
renderer: _listrVerboseRenderer.default
|
|
48
29
|
};
|
|
49
30
|
}
|
|
50
|
-
|
|
51
31
|
return {
|
|
52
32
|
renderer: _listrUpdateRenderer.default,
|
|
53
33
|
collapse: false
|
|
54
34
|
};
|
|
55
35
|
}
|
|
56
|
-
|
|
57
36
|
function runContentfulImport(params) {
|
|
58
37
|
const log = [];
|
|
59
38
|
const options = (0, _parseOptions.default)(params);
|
|
@@ -62,8 +41,9 @@ function runContentfulImport(params) {
|
|
|
62
41
|
interval: ONE_SECOND,
|
|
63
42
|
intervalCap: options.rateLimit,
|
|
64
43
|
carryoverConcurrencyCount: true
|
|
65
|
-
});
|
|
44
|
+
});
|
|
66
45
|
|
|
46
|
+
// Setup custom log listener to store log messages for later
|
|
67
47
|
(0, _logging.setupLogging)(log);
|
|
68
48
|
const infoTable = new _cliTable.default();
|
|
69
49
|
infoTable.push([{
|
|
@@ -74,15 +54,12 @@ function runContentfulImport(params) {
|
|
|
74
54
|
if (options.skipLocales && type === 'locales') {
|
|
75
55
|
return;
|
|
76
56
|
}
|
|
77
|
-
|
|
78
57
|
if (options.skipContentModel && ['contentTypes', 'editorInterfaces'].indexOf(type) >= 0) {
|
|
79
58
|
return;
|
|
80
59
|
}
|
|
81
|
-
|
|
82
60
|
if (options.contentModelOnly && !(['contentTypes', 'editorInterfaces', 'locales'].indexOf(type) >= 0)) {
|
|
83
61
|
return;
|
|
84
62
|
}
|
|
85
|
-
|
|
86
63
|
infoTable.push([(0, _lodash.startCase)(type), options.content[type].length]);
|
|
87
64
|
});
|
|
88
65
|
console.log(infoTable.toString());
|
|
@@ -145,7 +122,6 @@ function runContentfulImport(params) {
|
|
|
145
122
|
}).then(ctx => {
|
|
146
123
|
console.log('Finished importing all data');
|
|
147
124
|
const resultTypes = Object.keys(ctx.data);
|
|
148
|
-
|
|
149
125
|
if (resultTypes.length) {
|
|
150
126
|
const resultTable = new _cliTable.default();
|
|
151
127
|
resultTable.push([{
|
|
@@ -159,7 +135,6 @@ function runContentfulImport(params) {
|
|
|
159
135
|
} else {
|
|
160
136
|
console.log('No data was imported');
|
|
161
137
|
}
|
|
162
|
-
|
|
163
138
|
const endTime = new Date();
|
|
164
139
|
const durationHuman = (0, _formatDistance.default)(endTime, options.startTime);
|
|
165
140
|
const durationSeconds = (0, _differenceInSeconds.default)(endTime, options.startTime);
|
|
@@ -175,7 +150,6 @@ function runContentfulImport(params) {
|
|
|
175
150
|
const errorLog = log.filter(logMessage => logMessage.level !== 'info' && logMessage.level !== 'warning');
|
|
176
151
|
const displayLog = log.filter(logMessage => logMessage.level !== 'info');
|
|
177
152
|
(0, _logging.displayErrorLog)(displayLog);
|
|
178
|
-
|
|
179
153
|
if (errorLog.length) {
|
|
180
154
|
return (0, _logging.writeErrorLogFile)(options.errorLogFile, errorLog).then(() => {
|
|
181
155
|
const multiError = new Error('Errors occurred');
|
|
@@ -184,10 +158,8 @@ function runContentfulImport(params) {
|
|
|
184
158
|
throw multiError;
|
|
185
159
|
});
|
|
186
160
|
}
|
|
187
|
-
|
|
188
161
|
console.log('The import was successful.');
|
|
189
162
|
return data;
|
|
190
163
|
});
|
|
191
164
|
}
|
|
192
|
-
|
|
193
165
|
module.exports = exports.default;
|
package/dist/parseOptions.js
CHANGED
|
@@ -4,31 +4,18 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = parseOptions;
|
|
7
|
-
|
|
8
7
|
var _fs = _interopRequireDefault(require("fs"));
|
|
9
|
-
|
|
10
8
|
var _path = require("path");
|
|
11
|
-
|
|
12
9
|
var _format = _interopRequireDefault(require("date-fns/format"));
|
|
13
|
-
|
|
14
10
|
var _package = require("../package");
|
|
15
|
-
|
|
16
11
|
var _headers = require("./utils/headers");
|
|
17
|
-
|
|
18
12
|
var _proxy = require("contentful-batch-libs/dist/proxy");
|
|
19
|
-
|
|
20
13
|
var _addSequenceHeader = _interopRequireDefault(require("contentful-batch-libs/dist/add-sequence-header"));
|
|
21
|
-
|
|
22
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
-
|
|
24
15
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
25
|
-
|
|
26
16
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
27
|
-
|
|
28
17
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
29
|
-
|
|
30
18
|
const SUPPORTED_ENTITY_TYPES = ['contentTypes', 'tags', 'entries', 'assets', 'locales', 'webhooks', 'editorInterfaces'];
|
|
31
|
-
|
|
32
19
|
function parseOptions(params) {
|
|
33
20
|
const defaultOptions = {
|
|
34
21
|
skipContentModel: false,
|
|
@@ -41,51 +28,43 @@ function parseOptions(params) {
|
|
|
41
28
|
rateLimit: 7
|
|
42
29
|
};
|
|
43
30
|
const configFile = params.config ? require((0, _path.resolve)(process.cwd(), params.config)) : {};
|
|
44
|
-
|
|
45
31
|
const options = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, defaultOptions), configFile), params), {}, {
|
|
46
32
|
headers: (0, _addSequenceHeader.default)(params.headers || (0, _headers.getHeadersConfig)(params.header))
|
|
47
|
-
});
|
|
48
|
-
|
|
33
|
+
});
|
|
49
34
|
|
|
35
|
+
// Validation
|
|
50
36
|
if (!options.spaceId) {
|
|
51
37
|
throw new Error('The `spaceId` option is required.');
|
|
52
38
|
}
|
|
53
|
-
|
|
54
39
|
if (!options.managementToken) {
|
|
55
40
|
throw new Error('The `managementToken` option is required.');
|
|
56
41
|
}
|
|
57
|
-
|
|
58
42
|
if (!options.contentFile && !options.content) {
|
|
59
43
|
throw new Error('Either the `contentFile` or `content` option are required.');
|
|
60
44
|
}
|
|
61
|
-
|
|
62
45
|
if (options.contentModelOnly && options.skipContentModel) {
|
|
63
46
|
throw new Error('`contentModelOnly` and `skipContentModel` cannot be used together');
|
|
64
47
|
}
|
|
65
|
-
|
|
66
48
|
if (options.skipLocales && !options.contentModelOnly) {
|
|
67
49
|
throw new Error('`skipLocales` can only be used together with `contentModelOnly`');
|
|
68
50
|
}
|
|
69
|
-
|
|
70
51
|
const proxySimpleExp = /.+:\d+/;
|
|
71
52
|
const proxyAuthExp = /.+:.+@.+:\d+/;
|
|
72
|
-
|
|
73
53
|
if (typeof options.proxy === 'string' && options.proxy && !(proxySimpleExp.test(options.proxy) || proxyAuthExp.test(options.proxy))) {
|
|
74
54
|
throw new Error('Please provide the proxy config in the following format:\nhost:port or user:password@host:port');
|
|
75
55
|
}
|
|
76
|
-
|
|
77
56
|
options.startTime = new Date();
|
|
78
|
-
|
|
79
57
|
if (!options.errorLogFile) {
|
|
80
58
|
options.errorLogFile = (0, _path.resolve)(process.cwd(), `contentful-import-error-log-${options.spaceId}-${(0, _format.default)(options.startTime, "yyyy-MM-dd'T'HH-mm-ss")}.json`);
|
|
81
59
|
} else {
|
|
82
60
|
options.errorLogFile = (0, _path.resolve)(process.cwd(), options.errorLogFile);
|
|
83
|
-
}
|
|
84
|
-
|
|
61
|
+
}
|
|
85
62
|
|
|
63
|
+
// Further processing
|
|
86
64
|
options.accessToken = options.managementToken;
|
|
87
|
-
options.content = options.content || JSON.parse(_fs.default.readFileSync(options.contentFile));
|
|
65
|
+
options.content = options.content || JSON.parse(_fs.default.readFileSync(options.contentFile));
|
|
88
66
|
|
|
67
|
+
// Clean up content to only include supported entity types
|
|
89
68
|
Object.keys(options.content).forEach(type => {
|
|
90
69
|
if (SUPPORTED_ENTITY_TYPES.indexOf(type) === -1) {
|
|
91
70
|
delete options.content[type];
|
|
@@ -94,19 +73,15 @@ function parseOptions(params) {
|
|
|
94
73
|
SUPPORTED_ENTITY_TYPES.forEach(type => {
|
|
95
74
|
options.content[type] = options.content[type] || [];
|
|
96
75
|
});
|
|
97
|
-
|
|
98
76
|
if (typeof options.proxy === 'string') {
|
|
99
77
|
options.proxy = (0, _proxy.proxyStringToObject)(options.proxy);
|
|
100
78
|
}
|
|
101
|
-
|
|
102
79
|
if (!options.rawProxy && options.proxy) {
|
|
103
80
|
options.httpsAgent = (0, _proxy.agentFromProxy)(options.proxy);
|
|
104
81
|
delete options.proxy;
|
|
105
82
|
}
|
|
106
|
-
|
|
107
83
|
options.application = options.managementApplication || `contentful.import/${_package.version}`;
|
|
108
84
|
options.feature = options.managementFeature || 'library-import';
|
|
109
85
|
return options;
|
|
110
86
|
}
|
|
111
|
-
|
|
112
87
|
module.exports = exports.default;
|
|
@@ -4,19 +4,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = getDestinationData;
|
|
7
|
-
|
|
8
7
|
var _bluebird = _interopRequireDefault(require("bluebird"));
|
|
9
|
-
|
|
10
8
|
var _logging = require("contentful-batch-libs/dist/logging");
|
|
11
|
-
|
|
12
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
-
|
|
14
10
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
15
|
-
|
|
16
11
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
17
|
-
|
|
18
12
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
19
|
-
|
|
20
13
|
const BATCH_CHAR_LIMIT = 1990;
|
|
21
14
|
const BATCH_SIZE_LIMIT = 100;
|
|
22
15
|
const METHODS = {
|
|
@@ -37,7 +30,6 @@ const METHODS = {
|
|
|
37
30
|
method: 'getAssets'
|
|
38
31
|
}
|
|
39
32
|
};
|
|
40
|
-
|
|
41
33
|
async function batchedIdQuery({
|
|
42
34
|
environment,
|
|
43
35
|
type,
|
|
@@ -55,26 +47,21 @@ async function batchedIdQuery({
|
|
|
55
47
|
limit: idBatch.split(',').length
|
|
56
48
|
});
|
|
57
49
|
totalFetched = totalFetched + response.items.length;
|
|
58
|
-
|
|
59
50
|
_logging.logEmitter.emit('info', `Fetched ${totalFetched} of ${response.total} ${entityTypeName}`);
|
|
60
|
-
|
|
61
51
|
return response.items;
|
|
62
52
|
});
|
|
63
53
|
});
|
|
64
54
|
const responses = await _bluebird.default.all(allPendingResponses);
|
|
65
55
|
return responses.flat();
|
|
66
56
|
}
|
|
67
|
-
|
|
68
57
|
function getIdBatches(ids) {
|
|
69
58
|
const batches = [];
|
|
70
59
|
let currentBatch = '';
|
|
71
60
|
let currentSize = 0;
|
|
72
|
-
|
|
73
61
|
while (ids.length > 0) {
|
|
74
62
|
const id = ids.splice(0, 1);
|
|
75
63
|
currentBatch += id;
|
|
76
64
|
currentSize = currentSize + 1;
|
|
77
|
-
|
|
78
65
|
if (currentSize === BATCH_SIZE_LIMIT || currentBatch.length > BATCH_CHAR_LIMIT || ids.length === 0) {
|
|
79
66
|
batches.push(currentBatch);
|
|
80
67
|
currentBatch = '';
|
|
@@ -83,9 +70,9 @@ function getIdBatches(ids) {
|
|
|
83
70
|
currentBatch += ',';
|
|
84
71
|
}
|
|
85
72
|
}
|
|
86
|
-
|
|
87
73
|
return batches;
|
|
88
74
|
}
|
|
75
|
+
|
|
89
76
|
/**
|
|
90
77
|
* Gets content from a space which will have content copied to it, based on a
|
|
91
78
|
* collection of existing content.
|
|
@@ -95,7 +82,6 @@ function getIdBatches(ids) {
|
|
|
95
82
|
*
|
|
96
83
|
*/
|
|
97
84
|
|
|
98
|
-
|
|
99
85
|
async function getDestinationData({
|
|
100
86
|
client,
|
|
101
87
|
spaceId,
|
|
@@ -114,10 +100,10 @@ async function getDestinationData({
|
|
|
114
100
|
locales: [],
|
|
115
101
|
entries: [],
|
|
116
102
|
assets: []
|
|
117
|
-
};
|
|
103
|
+
};
|
|
118
104
|
|
|
105
|
+
// Make sure all required properties are available and at least an empty array
|
|
119
106
|
sourceData = _objectSpread(_objectSpread({}, result), sourceData);
|
|
120
|
-
|
|
121
107
|
if (!skipContentModel) {
|
|
122
108
|
const contentTypeIds = sourceData.contentTypes.map(e => e.sys.id);
|
|
123
109
|
result.contentTypes = batchedIdQuery({
|
|
@@ -126,7 +112,6 @@ async function getDestinationData({
|
|
|
126
112
|
ids: contentTypeIds,
|
|
127
113
|
requestQueue
|
|
128
114
|
});
|
|
129
|
-
|
|
130
115
|
if (!skipLocales) {
|
|
131
116
|
const localeIds = sourceData.locales.map(e => e.sys.id);
|
|
132
117
|
result.locales = batchedIdQuery({
|
|
@@ -136,19 +121,17 @@ async function getDestinationData({
|
|
|
136
121
|
requestQueue
|
|
137
122
|
});
|
|
138
123
|
}
|
|
139
|
-
}
|
|
140
|
-
|
|
124
|
+
}
|
|
141
125
|
|
|
126
|
+
// include tags even if contentModelOnly = true
|
|
142
127
|
result.tags = environment.getTags().then(response => response.items).catch(e => {
|
|
143
128
|
// users without access to Tags will get 404
|
|
144
129
|
// if they dont have access, remove tags array so they're not handled in future steps
|
|
145
130
|
delete result.tags;
|
|
146
131
|
});
|
|
147
|
-
|
|
148
132
|
if (contentModelOnly) {
|
|
149
133
|
return _bluebird.default.props(result);
|
|
150
134
|
}
|
|
151
|
-
|
|
152
135
|
const entryIds = sourceData.entries.map(e => e.sys.id);
|
|
153
136
|
const assetIds = sourceData.assets.map(e => e.sys.id);
|
|
154
137
|
result.entries = batchedIdQuery({
|
|
@@ -166,5 +149,4 @@ async function getDestinationData({
|
|
|
166
149
|
result.webhooks = [];
|
|
167
150
|
return _bluebird.default.props(result);
|
|
168
151
|
}
|
|
169
|
-
|
|
170
152
|
module.exports = exports.default;
|
|
@@ -4,30 +4,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = initClient;
|
|
7
|
-
|
|
8
7
|
var _contentfulManagement = require("contentful-management");
|
|
9
|
-
|
|
10
8
|
var _logging = require("contentful-batch-libs/dist/logging");
|
|
11
|
-
|
|
12
9
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
13
|
-
|
|
14
10
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
15
|
-
|
|
16
11
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
17
|
-
|
|
18
12
|
function logHandler(level, data) {
|
|
19
13
|
_logging.logEmitter.emit(level, data);
|
|
20
14
|
}
|
|
21
|
-
|
|
22
15
|
function initClient(opts) {
|
|
23
16
|
const defaultOpts = {
|
|
24
17
|
timeout: 30000,
|
|
25
18
|
logHandler
|
|
26
19
|
};
|
|
27
|
-
|
|
28
20
|
const config = _objectSpread(_objectSpread({}, defaultOpts), opts);
|
|
29
|
-
|
|
30
21
|
return (0, _contentfulManagement.createClient)(config);
|
|
31
22
|
}
|
|
32
|
-
|
|
33
23
|
module.exports = exports.default;
|
|
@@ -5,25 +5,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.getAssetStreamForURL = getAssetStreamForURL;
|
|
7
7
|
exports.processAssets = processAssets;
|
|
8
|
-
|
|
9
8
|
var _fs = _interopRequireDefault(require("fs"));
|
|
10
|
-
|
|
11
9
|
var _path = require("path");
|
|
12
|
-
|
|
13
10
|
var _util = require("util");
|
|
14
|
-
|
|
15
11
|
var _getEntityName = _interopRequireDefault(require("contentful-batch-libs/dist/get-entity-name"));
|
|
16
|
-
|
|
17
12
|
var _logging = require("contentful-batch-libs/dist/logging");
|
|
18
|
-
|
|
19
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
20
|
-
|
|
21
14
|
const stat = (0, _util.promisify)(_fs.default.stat);
|
|
22
|
-
|
|
23
15
|
async function getAssetStreamForURL(url, assetsDirectory) {
|
|
24
16
|
const [, assetPath] = url.split('//');
|
|
25
17
|
const filePath = (0, _path.join)(assetsDirectory, assetPath);
|
|
26
|
-
|
|
27
18
|
try {
|
|
28
19
|
await stat(filePath);
|
|
29
20
|
return _fs.default.createReadStream(filePath);
|
|
@@ -33,7 +24,6 @@ async function getAssetStreamForURL(url, assetsDirectory) {
|
|
|
33
24
|
throw error;
|
|
34
25
|
}
|
|
35
26
|
}
|
|
36
|
-
|
|
37
27
|
async function processAssets({
|
|
38
28
|
assets,
|
|
39
29
|
timeout,
|
|
@@ -43,21 +33,17 @@ async function processAssets({
|
|
|
43
33
|
const pendingProcessingAssets = assets.map(asset => {
|
|
44
34
|
return requestQueue.add(async () => {
|
|
45
35
|
_logging.logEmitter.emit('info', `Processing Asset ${(0, _getEntityName.default)(asset)}`);
|
|
46
|
-
|
|
47
36
|
const processingOptions = Object.assign({}, timeout && {
|
|
48
37
|
processingCheckWait: timeout
|
|
49
38
|
}, retryLimit && {
|
|
50
39
|
processingCheckRetry: retryLimit
|
|
51
40
|
});
|
|
52
|
-
|
|
53
41
|
try {
|
|
54
42
|
const processedAsset = await asset.processForAllLocales(processingOptions);
|
|
55
43
|
return processedAsset;
|
|
56
44
|
} catch (err) {
|
|
57
45
|
err.entity = asset;
|
|
58
|
-
|
|
59
46
|
_logging.logEmitter.emit('error', err);
|
|
60
|
-
|
|
61
47
|
return null;
|
|
62
48
|
}
|
|
63
49
|
});
|
|
@@ -6,17 +6,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.createEntities = createEntities;
|
|
7
7
|
exports.createEntries = createEntries;
|
|
8
8
|
exports.createLocales = createLocales;
|
|
9
|
-
|
|
10
9
|
var _collection = require("lodash/collection");
|
|
11
|
-
|
|
12
10
|
var _object = require("lodash/object");
|
|
13
|
-
|
|
14
11
|
var _getEntityName = _interopRequireDefault(require("contentful-batch-libs/dist/get-entity-name"));
|
|
15
|
-
|
|
16
12
|
var _logging = require("contentful-batch-libs/dist/logging");
|
|
17
|
-
|
|
18
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
-
|
|
20
14
|
/**
|
|
21
15
|
* Creates a list of entities
|
|
22
16
|
* Applies to all entities except Entries, as the CMA API for those is slightly different
|
|
@@ -34,10 +28,10 @@ function createEntities({
|
|
|
34
28
|
destinationEntitiesById,
|
|
35
29
|
requestQueue
|
|
36
30
|
});
|
|
37
|
-
}
|
|
38
|
-
// Locales need to be created in series
|
|
39
|
-
|
|
31
|
+
}
|
|
40
32
|
|
|
33
|
+
// TODO
|
|
34
|
+
// Locales need to be created in series
|
|
41
35
|
function createLocales({
|
|
42
36
|
context,
|
|
43
37
|
entities,
|
|
@@ -51,7 +45,6 @@ function createLocales({
|
|
|
51
45
|
requestQueue
|
|
52
46
|
});
|
|
53
47
|
}
|
|
54
|
-
|
|
55
48
|
async function createEntitiesWithConcurrency({
|
|
56
49
|
context,
|
|
57
50
|
entities,
|
|
@@ -71,11 +64,11 @@ async function createEntitiesWithConcurrency({
|
|
|
71
64
|
}
|
|
72
65
|
});
|
|
73
66
|
});
|
|
74
|
-
const createdEntities = await Promise.all(pendingCreatedEntities);
|
|
67
|
+
const createdEntities = await Promise.all(pendingCreatedEntities);
|
|
75
68
|
|
|
69
|
+
// Filter null values in case of errors
|
|
76
70
|
return createdEntities.filter(entity => entity);
|
|
77
71
|
}
|
|
78
|
-
|
|
79
72
|
async function createEntitiesInSequence({
|
|
80
73
|
context,
|
|
81
74
|
entities,
|
|
@@ -83,11 +76,9 @@ async function createEntitiesInSequence({
|
|
|
83
76
|
requestQueue
|
|
84
77
|
}) {
|
|
85
78
|
const createdEntities = [];
|
|
86
|
-
|
|
87
79
|
for (const entity of entities) {
|
|
88
80
|
const destinationEntity = getDestinationEntityForSourceEntity(destinationEntitiesById, entity.transformed);
|
|
89
81
|
const operation = destinationEntity ? 'update' : 'create';
|
|
90
|
-
|
|
91
82
|
try {
|
|
92
83
|
// Even though we run things in sequence here,
|
|
93
84
|
// we still want to go through the normal rate limiting queue
|
|
@@ -99,20 +90,17 @@ async function createEntitiesInSequence({
|
|
|
99
90
|
createdEntities.push(createdEntity);
|
|
100
91
|
} catch (err) {
|
|
101
92
|
const maybeSubstituteEntity = handleCreationErrors(entity, err);
|
|
102
|
-
|
|
103
93
|
if (maybeSubstituteEntity) {
|
|
104
94
|
createdEntities.push(maybeSubstituteEntity);
|
|
105
95
|
}
|
|
106
96
|
}
|
|
107
97
|
}
|
|
108
|
-
|
|
109
98
|
return createdEntities;
|
|
110
99
|
}
|
|
100
|
+
|
|
111
101
|
/**
|
|
112
102
|
* Creates a list of entries
|
|
113
103
|
*/
|
|
114
|
-
|
|
115
|
-
|
|
116
104
|
async function createEntries({
|
|
117
105
|
context,
|
|
118
106
|
entities,
|
|
@@ -130,7 +118,6 @@ async function createEntries({
|
|
|
130
118
|
}));
|
|
131
119
|
return createdEntries.filter(entry => entry);
|
|
132
120
|
}
|
|
133
|
-
|
|
134
121
|
async function createEntry({
|
|
135
122
|
entry,
|
|
136
123
|
target,
|
|
@@ -141,7 +128,6 @@ async function createEntry({
|
|
|
141
128
|
const contentTypeId = entry.original.sys.contentType.sys.id;
|
|
142
129
|
const destinationEntry = getDestinationEntityForSourceEntity(destinationEntitiesById, entry.transformed);
|
|
143
130
|
const operation = destinationEntry ? 'update' : 'create';
|
|
144
|
-
|
|
145
131
|
try {
|
|
146
132
|
const createdOrUpdatedEntry = await requestQueue.add(() => {
|
|
147
133
|
return destinationEntry ? updateDestinationWithSourceData(destinationEntry, entry.transformed) : createEntryInDestination(target, contentTypeId, entry.transformed);
|
|
@@ -163,75 +149,62 @@ async function createEntry({
|
|
|
163
149
|
requestQueue
|
|
164
150
|
});
|
|
165
151
|
}
|
|
166
|
-
|
|
167
152
|
err.entity = entry;
|
|
153
|
+
_logging.logEmitter.emit('error', err);
|
|
168
154
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
155
|
+
// No need to pass this entry down to publishing if it wasn't created
|
|
172
156
|
return null;
|
|
173
157
|
}
|
|
174
158
|
}
|
|
175
|
-
|
|
176
159
|
function updateDestinationWithSourceData(destinationEntity, sourceEntity) {
|
|
177
160
|
const plainData = getPlainData(sourceEntity);
|
|
178
161
|
(0, _object.assign)(destinationEntity, plainData);
|
|
179
162
|
return destinationEntity.update();
|
|
180
163
|
}
|
|
181
|
-
|
|
182
164
|
function createInDestination(context, sourceEntity) {
|
|
183
165
|
const {
|
|
184
166
|
type,
|
|
185
167
|
target
|
|
186
168
|
} = context;
|
|
187
|
-
|
|
188
169
|
if (type === 'Tag') {
|
|
189
170
|
// tags are created with a different signature
|
|
190
171
|
return createTagInDestination(context, sourceEntity);
|
|
191
172
|
}
|
|
192
|
-
|
|
193
173
|
const id = (0, _object.get)(sourceEntity, 'sys.id');
|
|
194
174
|
const plainData = getPlainData(sourceEntity);
|
|
195
175
|
return id ? target[`create${type}WithId`](id, plainData) : target[`create${type}`](plainData);
|
|
196
176
|
}
|
|
197
|
-
|
|
198
177
|
function createEntryInDestination(space, contentTypeId, sourceEntity) {
|
|
199
178
|
const id = sourceEntity.sys.id;
|
|
200
179
|
const plainData = getPlainData(sourceEntity);
|
|
201
180
|
return id ? space.createEntryWithId(contentTypeId, id, plainData) : space.createEntry(contentTypeId, plainData);
|
|
202
181
|
}
|
|
203
|
-
|
|
204
182
|
function createTagInDestination(context, sourceEntity) {
|
|
205
183
|
const id = sourceEntity.sys.id;
|
|
206
184
|
const visibility = sourceEntity.sys.visibility || 'private';
|
|
207
185
|
const name = sourceEntity.name;
|
|
208
186
|
return context.target.createTag(id, name, visibility);
|
|
209
187
|
}
|
|
188
|
+
|
|
210
189
|
/**
|
|
211
190
|
* Handles entity creation errors.
|
|
212
191
|
* If the error is a VersionMismatch the error is thrown and a message is returned
|
|
213
192
|
* instructing the user on what this situation probably means.
|
|
214
193
|
*/
|
|
215
|
-
|
|
216
|
-
|
|
217
194
|
function handleCreationErrors(entity, err) {
|
|
218
195
|
// Handle the case where a locale already exists and skip it
|
|
219
196
|
if ((0, _object.get)(err, 'error.sys.id') === 'ValidationFailed') {
|
|
220
197
|
const errors = (0, _object.get)(err, 'error.details.errors');
|
|
221
|
-
|
|
222
198
|
if (errors && errors.length > 0 && errors[0].name === 'taken') {
|
|
223
199
|
return entity;
|
|
224
200
|
}
|
|
225
201
|
}
|
|
226
|
-
|
|
227
202
|
err.entity = entity.original;
|
|
203
|
+
_logging.logEmitter.emit('error', err);
|
|
228
204
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
205
|
+
// No need to pass this entity down to publishing if it wasn't created
|
|
232
206
|
return null;
|
|
233
207
|
}
|
|
234
|
-
|
|
235
208
|
function cleanupUnknownFields(fields, errors) {
|
|
236
209
|
return (0, _object.omitBy)(fields, (field, fieldId) => {
|
|
237
210
|
return (0, _collection.find)(errors, error => {
|
|
@@ -240,19 +213,14 @@ function cleanupUnknownFields(fields, errors) {
|
|
|
240
213
|
});
|
|
241
214
|
});
|
|
242
215
|
}
|
|
243
|
-
|
|
244
216
|
function getDestinationEntityForSourceEntity(destinationEntitiesById, sourceEntity) {
|
|
245
217
|
return destinationEntitiesById.get((0, _object.get)(sourceEntity, 'sys.id')) || null;
|
|
246
218
|
}
|
|
247
|
-
|
|
248
219
|
function creationSuccessNotifier(method, createdEntity) {
|
|
249
220
|
const verb = method[0].toUpperCase() + method.substr(1, method.length) + 'd';
|
|
250
|
-
|
|
251
221
|
_logging.logEmitter.emit('info', `${verb} ${createdEntity.sys.type} ${(0, _getEntityName.default)(createdEntity)}`);
|
|
252
|
-
|
|
253
222
|
return createdEntity;
|
|
254
223
|
}
|
|
255
|
-
|
|
256
224
|
function getPlainData(entity) {
|
|
257
225
|
const data = entity.toPlainObject ? entity.toPlainObject() : entity;
|
|
258
226
|
return (0, _object.omit)(data, 'sys');
|