@jungvonmatt/contentful-migrations 5.1.0 → 5.1.1

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.
Files changed (2) hide show
  1. package/lib/backend.js +26 -7
  2. package/package.json +1 -1
package/lib/backend.js CHANGED
@@ -352,26 +352,45 @@ const getLatestVersion = async (config) => {
352
352
  return getMigrationVersionFromTag(config);
353
353
  };
354
354
 
355
+ const getVersionFromFile = file => {
356
+ const name = path.basename(file);
357
+ const [, num] = /^(\d+)-/.exec(name);
358
+ return parseInt(num,10);
359
+ }
360
+
355
361
  /**
356
362
  * Get all unexecuted migration files
357
363
  * @param {Object} config The config object including all required data
358
364
  */
359
365
  const getNewMigrations = async (config) => {
360
- const { directory, storage } = config || {};
366
+ const { directory, storage, migrationContentTypeId } = config || {};
361
367
  const {globby} = await import('globby');
362
- const migrations = await globby([`${directory}/*.js`,`${directory}/*.cjs`]);
368
+ const migrations = (await globby([`${directory}/*.js`,`${directory}/*.cjs`])).sort((a,b) => {
369
+ const numA = getVersionFromFile(a);
370
+ const numB = getVersionFromFile(b);
371
+ return numA-numB;
372
+ });
363
373
 
364
374
  if (storage === STORAGE_CONTENT) {
365
375
  try {
366
376
  const versions = await getMigrationVersions(config);
367
377
 
368
- return migrations.filter((file) => {
369
- const name = path.basename(file);
370
- const [, num] = /^(\d+)-/.exec(name);
371
-
372
- return !(versions || []).includes(parseInt(num, 10));
378
+ const result = migrations.filter((file) => {
379
+ const num = getVersionFromFile(file);
380
+ return !(versions || []).includes(num);
373
381
  });
382
+
383
+ return result;
374
384
  } catch (error) {
385
+ // check if we have a migration scheduled which adds the initial content-type
386
+ const regexp = new RegExp(`createContentType\\(['"]${migrationContentTypeId}['"]\\)`,'mg');
387
+ const initial = (await Promise.all(migrations.map(async file => {
388
+ return fs.readFile(file, 'utf8');
389
+ }))).some(content => regexp.test(content));
390
+
391
+ if (initial) {
392
+ return migrations;
393
+ }
375
394
  console.error(chalk.red('\nError:'), `Missing migration content type. Run ${chalk.cyan('npx migrations init')}`);
376
395
  process.exit(1);
377
396
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jungvonmatt/contentful-migrations",
3
- "version": "5.1.0",
3
+ "version": "5.1.1",
4
4
  "description": "Helper to handle migrations in contentful",
5
5
  "main": "index.js",
6
6
  "files": [