@jungvonmatt/contentful-migrations 5.2.3 → 5.2.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/lib/backend.js CHANGED
@@ -2,7 +2,7 @@ const path = require('path');
2
2
  const fs = require('fs/promises');
3
3
  const chalk = require('chalk');
4
4
  const cliProgress = require('cli-progress');
5
- const { getEnvironment, getDefaultLocale } = require('./contentful');
5
+ const { getEnvironment, getDefaultLocale, getMigrationItems } = require('./contentful');
6
6
  const { STORAGE_TAG, STORAGE_CONTENT, STATE_SUCCESS, STATE_FAILURE } = require('./config');
7
7
 
8
8
  /**
@@ -193,8 +193,8 @@ const migrateToContentStorage = async (config) => {
193
193
  const client = await getEnvironment(config);
194
194
  const version = await getMigrationVersionFromTag(config);
195
195
 
196
- const {globby} = await import('globby');
197
- const migrations = await globby([`${directory}/*.js`,`${directory}/*.cjs`]);
196
+ const { globby } = await import('globby');
197
+ const migrations = await globby([`${directory}/*.js`, `${directory}/*.cjs`]);
198
198
  const environmentId = client.sys.id;
199
199
  const filtered = migrations.filter((file) => {
200
200
  const name = path.basename(file);
@@ -292,9 +292,7 @@ const migrateToTagStorage = async (config) => {
292
292
  * @returns {number[]}
293
293
  */
294
294
  const getMigrationVersions = async (config) => {
295
- const { migrationContentTypeId } = config;
296
- const client = await getEnvironment(config);
297
- const { items } = await client.getEntries({ content_type: migrationContentTypeId, limit: 1000 });
295
+ const items = await getMigrationItems(config);
298
296
 
299
297
  return (items || []).map((item) => parseInt(item.sys.id, 10));
300
298
  };
@@ -353,11 +351,11 @@ const getLatestVersion = async (config) => {
353
351
  return getMigrationVersionFromTag(config);
354
352
  };
355
353
 
356
- const getVersionFromFile = file => {
354
+ const getVersionFromFile = (file) => {
357
355
  const name = path.basename(file);
358
356
  const [, num] = /^(\d+)-/.exec(name);
359
- return parseInt(num,10);
360
- }
357
+ return parseInt(num, 10);
358
+ };
361
359
 
362
360
  /**
363
361
  * Get all unexecuted migration files
@@ -365,17 +363,16 @@ const getVersionFromFile = file => {
365
363
  */
366
364
  const getNewMigrations = async (config) => {
367
365
  const { directory, storage, migrationContentTypeId } = config || {};
368
- const {globby} = await import('globby');
369
- const migrations = (await globby([`${directory}/*.js`,`${directory}/*.cjs`])).sort((a,b) => {
366
+ const { globby } = await import('globby');
367
+ const migrations = (await globby([`${directory}/*.js`, `${directory}/*.cjs`])).sort((a, b) => {
370
368
  const numA = getVersionFromFile(a);
371
369
  const numB = getVersionFromFile(b);
372
- return numA-numB;
370
+ return numA - numB;
373
371
  });
374
372
 
375
373
  if (storage === STORAGE_CONTENT) {
376
374
  try {
377
375
  const versions = await getMigrationVersions(config);
378
-
379
376
  const result = migrations.filter((file) => {
380
377
  const num = getVersionFromFile(file);
381
378
  return !(versions || []).includes(num);
@@ -384,10 +381,14 @@ const getNewMigrations = async (config) => {
384
381
  return result;
385
382
  } catch (error) {
386
383
  // check if we have a migration scheduled which adds the initial content-type
387
- const regexp = new RegExp(`createContentType\\(['"]${migrationContentTypeId}['"]\\)`,'mg');
388
- const initial = (await Promise.all(migrations.map(async file => {
389
- return fs.readFile(file, 'utf8');
390
- }))).some(content => regexp.test(content));
384
+ const regexp = new RegExp(`createContentType\\(['"]${migrationContentTypeId}['"]\\)`, 'mg');
385
+ const initial = (
386
+ await Promise.all(
387
+ migrations.map(async (file) => {
388
+ return fs.readFile(file, 'utf8');
389
+ })
390
+ )
391
+ ).some((content) => regexp.test(content));
391
392
 
392
393
  if (initial) {
393
394
  return migrations;
package/lib/contentful.js CHANGED
@@ -215,11 +215,9 @@ const pagedGet = async (environment, { method, skip = 0, aggregatedResponse = nu
215
215
  } else {
216
216
  aggregatedResponse.items = aggregatedResponse.items.concat(response.items);
217
217
  }
218
- // const page = Math.ceil(skip / MAX_ALLOWED_LIMIT) + 1;
219
- // const pages = Math.ceil(response.total / MAX_ALLOWED_LIMIT);
220
218
 
221
- if (skip + MAX_ALLOWED_LIMIT <= response.total) {
222
- return pagedGet(environment, { method, skip: skip + MAX_ALLOWED_LIMIT, aggregatedResponse, query });
219
+ if (skip + fullQuery.limit <= response.total) {
220
+ return pagedGet(environment, { method, skip: skip + fullQuery.limit, aggregatedResponse, query });
223
221
  }
224
222
  return aggregatedResponse;
225
223
  };
@@ -316,6 +314,17 @@ const getContent = async (options) => {
316
314
  return { entries, assets, contentTypes };
317
315
  };
318
316
 
317
+ const getMigrationItems = async (options) => {
318
+ const { migrationContentTypeId } = options;
319
+ const environment = await getEnvironment(options);
320
+ const { items } = await pagedGet(environment, {
321
+ method: 'getEntries',
322
+ query: { content_type: migrationContentTypeId },
323
+ });
324
+
325
+ return items;
326
+ };
327
+
319
328
  const getContentTypes = async (options) => {
320
329
  const { contentType } = options;
321
330
  const environment = await getEnvironment(options);
@@ -354,6 +363,7 @@ exports.getNodeName = getNodeName;
354
363
  exports.getEnvironmentId = getEnvironmentId;
355
364
  exports.getEditorInterfaces = getEditorInterfaces;
356
365
  exports.getApiKeys = getApiKeys;
366
+ exports.getMigrationItems = getMigrationItems;
357
367
 
358
368
  // Constants
359
369
  exports.TYPE_SYMBOL = TYPE_SYMBOL;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jungvonmatt/contentful-migrations",
3
- "version": "5.2.3",
3
+ "version": "5.2.4",
4
4
  "description": "Helper to handle migrations in contentful",
5
5
  "main": "index.js",
6
6
  "files": [