@jungvonmatt/contentful-migrations 6.2.4 → 6.2.6

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
@@ -112,14 +112,14 @@ const addMigrationEntry = async (data, config) => {
112
112
 
113
113
  let entry;
114
114
  try {
115
- entry = await client.getEntry(version);
115
+ entry = await client.getEntry(`${version}`);
116
116
  } catch {}
117
117
 
118
118
  if (!entry) {
119
- entry = await client.createEntryWithId(migrationContentTypeId, version, {
119
+ entry = await client.createEntryWithId(migrationContentTypeId, `${version}`, {
120
120
  fields: {
121
121
  version: {
122
- [defaultLocale]: version,
122
+ [defaultLocale]: `${version}`,
123
123
  },
124
124
  name: {
125
125
  [defaultLocale]: name,
@@ -358,8 +358,11 @@ const getLatestVersion = async (config) => {
358
358
 
359
359
  const getVersionFromFile = (file) => {
360
360
  const name = path.basename(file);
361
- const [, num] = /^(\d+)-/.exec(name);
362
- return parseInt(num, 10);
361
+ const versionMatch = /^\d+/.exec(name);
362
+ if (!versionMatch) {
363
+ return undefined;
364
+ }
365
+ return Number.parseInt(versionMatch[0], 10);
363
366
  };
364
367
 
365
368
  /**
@@ -372,7 +375,7 @@ const getNewMigrations = async (config) => {
372
375
  const migrations = (await globby([`${directory}/*.js`, `${directory}/*.cjs`])).sort((a, b) => {
373
376
  const numA = getVersionFromFile(a);
374
377
  const numB = getVersionFromFile(b);
375
- return numA - numB;
378
+ return (numA || 0) - (numB || 0);
376
379
  });
377
380
 
378
381
  if (storage === STORAGE_CONTENT) {
@@ -380,7 +383,7 @@ const getNewMigrations = async (config) => {
380
383
  const versions = await getMigrationVersions(config);
381
384
  const result = migrations.filter((file) => {
382
385
  const num = getVersionFromFile(file);
383
- return !(versions || []).includes(num);
386
+ return !!num && !(versions || []).includes(num);
384
387
  });
385
388
 
386
389
  return result;
@@ -420,3 +423,4 @@ module.exports.getMigrationVersionFromTag = getMigrationVersionFromTag;
420
423
  module.exports.getLatestVersion = getLatestVersion;
421
424
  module.exports.storeMigration = storeMigration;
422
425
  module.exports.getNewMigrations = getNewMigrations;
426
+ module.exports.getVersionFromFile = getVersionFromFile;
package/lib/migration.js CHANGED
@@ -8,11 +8,11 @@ const {
8
8
  generateMigrationScript,
9
9
  } = require('contentful-cli/dist/lib/cmds/space_cmds/generate_cmds/migration');
10
10
  const pc = require('picocolors');
11
- const { getEnvironment } = require('./contentful');
11
+ const { getEnvironment, getOrganizationId } = require('./contentful');
12
12
 
13
13
  const { confirm, STATE_SUCCESS, STATE_FAILURE } = require('./config');
14
14
 
15
- const { storeMigration, getNewMigrations } = require('./backend');
15
+ const { storeMigration, getNewMigrations, getVersionFromFile } = require('./backend');
16
16
 
17
17
  const migrationHeader = stripIndent`/* eslint-env node */
18
18
  const { withHelpers } = require('@jungvonmatt/contentful-migrations');
@@ -143,7 +143,11 @@ const executeMigration = async (file, config) => {
143
143
  const environmentId = client.sys.id;
144
144
  const organizationId = await getOrganizationId(config);
145
145
  const name = path.basename(file);
146
- const [, version] = /^(\d+)-/.exec(name);
146
+ const version = getVersionFromFile(file);
147
+ if (!version) {
148
+ console.error(`Invalid migration file name "${name}". Must start with a timestamp.`);
149
+ process.exit(1);
150
+ }
147
151
 
148
152
  const options = {
149
153
  filePath: path.resolve(file),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jungvonmatt/contentful-migrations",
3
- "version": "6.2.4",
3
+ "version": "6.2.6",
4
4
  "description": "Helper to handle migrations in contentful",
5
5
  "main": "index.js",
6
6
  "files": [