@jungvonmatt/contentful-migrations 5.1.2 → 5.2.2
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/LICENSE +21 -0
- package/README.md +48 -4
- package/cli.js +286 -0
- package/index.d.ts +18 -0
- package/index.js +26 -287
- package/lib/helpers/locale.d.ts +9 -0
- package/lib/helpers/locale.js +42 -0
- package/lib/helpers/validation.d.ts +10 -0
- package/lib/helpers/validation.js +95 -0
- package/lib/migration.js +39 -10
- package/package.json +6 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Jung von Matt TECH (https://www.jvm.com/)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# JvM Contentful Migrations
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[![NPM Version][npm-image]][npm-url] [![Sonarcloud Status][sonarcloud-image]][sonarcloud-url]
|
|
4
|
+
|
|
5
|
+
JvM Contentful Migrations offers additional functionality on top of the existing migration functionality of the [Contentful CLI](https://github.com/contentful/contentful-cli). It makes it easy and safe to deploy changes to your content model in a way that can be reviewed and tested before being deployed to production. With migrations you can do almost everything with your content and your content model. See the [official documentation](https://github.com/contentful/contentful-migration) for more information.
|
|
4
6
|
|
|
5
7
|
## Getting started
|
|
6
8
|
|
|
@@ -37,19 +39,30 @@ This initializes migrations and stores the config values in the `package.json` o
|
|
|
37
39
|
<br/>
|
|
38
40
|
<br/>
|
|
39
41
|
|
|
42
|
+
## Showing help
|
|
43
|
+
|
|
44
|
+
Whenever you get stuck, you can output the help to your terminal:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npx migrations help
|
|
48
|
+
|
|
49
|
+
# Help for a specific command
|
|
50
|
+
npx migrations help <command>
|
|
51
|
+
```
|
|
52
|
+
|
|
40
53
|
## Handling contentful environments
|
|
41
54
|
|
|
42
55
|
It is recommended that you develop and test your migrations in a separate environment before executing them on production content. You can handle environments using the `environment` command:
|
|
43
56
|
|
|
44
57
|
```bash
|
|
45
58
|
# Add a new environment and activate it for API usage
|
|
46
|
-
npx migrations environment <environment-id> --create
|
|
59
|
+
npx migrations environment <environment-id> --create [--source-environment-id <source-environment-id>]
|
|
47
60
|
|
|
48
61
|
# Remove an environment
|
|
49
|
-
npx migrations environment <environment-id> --remove
|
|
62
|
+
npx migrations environment <environment-id> --remove [--source-environment-id <source-environment-id>]
|
|
50
63
|
|
|
51
64
|
# Reset an environment
|
|
52
|
-
npx migrations environment <environment-id> --reset
|
|
65
|
+
npx migrations environment <environment-id> --reset [--source-environment-id <source-environment-id>]
|
|
53
66
|
```
|
|
54
67
|
|
|
55
68
|
## Generating blank migrations
|
|
@@ -155,8 +168,39 @@ npx migrations doc -e <environment> -p <path/to/docs>
|
|
|
155
168
|
`--template`: Use a custom template for docs. `.js` with default export or `.mustache` is allowed<br/>
|
|
156
169
|
`--extension`: Use a custom file extension (default is `.md`)<br/>
|
|
157
170
|
|
|
171
|
+
|
|
172
|
+
## Migration helpers
|
|
173
|
+
We provide you with a few smaller migration helpers. There aren't many at the moment, but there may be more in the future.
|
|
174
|
+
|
|
175
|
+
To use the helpers you just need to wrap your migration with the provided `withHelpers` function which makes the helpers available as 3rd parameter
|
|
176
|
+
in your migration function:
|
|
177
|
+
|
|
178
|
+
```js
|
|
179
|
+
const { withHelpers } = require('@jungvonmatt/contentful-migrations');
|
|
180
|
+
|
|
181
|
+
module.exports = withHelpers(async (migration, context, helpers) => {
|
|
182
|
+
// Get all locales
|
|
183
|
+
await helpers.locale.getLocales();
|
|
184
|
+
// Get default locale
|
|
185
|
+
await helpers.locale.getDefaultLocale();
|
|
186
|
+
|
|
187
|
+
// Add or remove values from "linkContentType" validations without affecting the other elements in the array
|
|
188
|
+
await helpers.validation.addLinkContentTypeValues('contentTypeId', 'fieldId', ['value']);
|
|
189
|
+
await helpers.validation.removeLinkContentTypeValues('contentTypeId', 'fieldId', ['value']);
|
|
190
|
+
|
|
191
|
+
// Add or remove values from "in" validations without affecting the other elements in the array
|
|
192
|
+
await helpers.validation.addInValues('contentTypeId', 'fieldId', ['value']);
|
|
193
|
+
await helpers.validation.removeInValues('contentTypeId', 'fieldId', ['value']);
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
158
197
|
## Can I contribute?
|
|
159
198
|
|
|
160
199
|
Of course. We appreciate all of our [contributors](https://github.com/jungvonmatt/contentful-migrations/graphs/contributors) and
|
|
161
200
|
welcome contributions to improve the project further. If you're uncertain whether an addition should be made, feel
|
|
162
201
|
free to open up an issue and we can discuss it.
|
|
202
|
+
|
|
203
|
+
[npm-url]: https://www.npmjs.com/package/@jungvonmatt/contentful-migrations
|
|
204
|
+
[npm-image]: https://img.shields.io/npm/v/@jungvonmatt/contentful-migrations.svg
|
|
205
|
+
[sonarcloud-url]: https://sonarcloud.io/dashboard?id=jungvonmatt_contentful-migrations
|
|
206
|
+
[sonarcloud-image]: https://sonarcloud.io/api/project_badges/measure?project=jungvonmatt_contentful-migrations&metric=alert_status
|
package/cli.js
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/* eslint-disable no-console */
|
|
4
|
+
/* eslint-env node */
|
|
5
|
+
const fs = require('fs-extra');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const chalk = require('chalk');
|
|
8
|
+
const { Command } = require('commander');
|
|
9
|
+
|
|
10
|
+
const { initializeContentModel, migrateToContentStorage, migrateToTagStorage } = require('./lib/backend');
|
|
11
|
+
const { createMigration, runMigrations, fetchMigration, executeMigration } = require('./lib/migration');
|
|
12
|
+
const { versionDelete, versionAdd } = require('./lib/version');
|
|
13
|
+
const { transferContent } = require('./lib/content');
|
|
14
|
+
const { createOfflineDocs } = require('./lib/doc');
|
|
15
|
+
const { createEnvironment, removeEnvironment, resetEnvironment } = require('./lib/environment');
|
|
16
|
+
const { getConfig, askAll, askMissing, STORAGE_CONTENT, STORAGE_TAG } = require('./lib/config');
|
|
17
|
+
const pkg = require('./package.json');
|
|
18
|
+
|
|
19
|
+
require('dotenv').config();
|
|
20
|
+
|
|
21
|
+
const parseArgs = (cmd) => {
|
|
22
|
+
const { parent = {} } = cmd || {};
|
|
23
|
+
const directory = cmd.path || parent.path;
|
|
24
|
+
return {
|
|
25
|
+
...cmd,
|
|
26
|
+
environment: cmd.env || parent.env,
|
|
27
|
+
directory: directory ? path.resolve(directory) : undefined,
|
|
28
|
+
sourceEnvironmentId: cmd.sourceEnvironmentId || parent.sourceEnvironmentId,
|
|
29
|
+
destEnvironmentId: cmd.destEnvironmentId || parent.destEnvironmentId,
|
|
30
|
+
verbose: cmd.verbose || parent.verbose,
|
|
31
|
+
template: cmd.template || parent.template,
|
|
32
|
+
yes: cmd.yes || parent.yes,
|
|
33
|
+
extension: cmd.extension || parent.extension,
|
|
34
|
+
bail: cmd.bail || parent.bail,
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const errorHandler = (error, log) => {
|
|
39
|
+
if (log) {
|
|
40
|
+
const { errors, message } = error;
|
|
41
|
+
console.error(chalk.red('\nError:'), message);
|
|
42
|
+
(errors || []).forEach((err) => {
|
|
43
|
+
console.error(chalk.red('Error:'), err.message);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
process.exit(1);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const actionRunner = (fn, log = true) => {
|
|
50
|
+
return (...args) => {
|
|
51
|
+
const verbose = args.some((arg) => arg.verbose);
|
|
52
|
+
return fn(...args).catch((error) => errorHandler(error, verbose || log));
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const program = new Command();
|
|
57
|
+
|
|
58
|
+
program.version(pkg.version);
|
|
59
|
+
program
|
|
60
|
+
.command('init')
|
|
61
|
+
.description('Initialize contentful-migrations')
|
|
62
|
+
.action(
|
|
63
|
+
actionRunner(async (cmd) => {
|
|
64
|
+
const config = await getConfig(parseArgs(cmd || {}));
|
|
65
|
+
const verified = await askAll(config);
|
|
66
|
+
const { managementToken, accessToken, environmentId, spaceId, ...rest } = verified;
|
|
67
|
+
|
|
68
|
+
if (verified.storage === STORAGE_CONTENT) {
|
|
69
|
+
await initializeContentModel({ ...config, ...verified });
|
|
70
|
+
await migrateToContentStorage({ ...config, ...verified });
|
|
71
|
+
}
|
|
72
|
+
if (verified.storage === STORAGE_TAG) {
|
|
73
|
+
await migrateToTagStorage({ ...config, ...verified });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!process.env.CONTENTFUL_SPACE_ID) {
|
|
77
|
+
rest.spaceId = spaceId;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// try to store in package.json
|
|
81
|
+
const { pkgUp } = await import('pkg-up');
|
|
82
|
+
const localPkg = await pkgUp();
|
|
83
|
+
if (localPkg) {
|
|
84
|
+
const packageJson = await fs.readJson(localPkg);
|
|
85
|
+
rest.directory = path.relative(path.dirname(localPkg), rest.directory);
|
|
86
|
+
packageJson.migrations = rest;
|
|
87
|
+
await fs.outputJson(localPkg, packageJson, { spaces: 2 });
|
|
88
|
+
} else {
|
|
89
|
+
// store in .migrationsrc if no package.json is available
|
|
90
|
+
await fs.outputJson(path.join(process.cwd(), '.migrationsrc'), rest, { spaces: 2 });
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
program
|
|
96
|
+
.command('fetch')
|
|
97
|
+
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
98
|
+
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
99
|
+
.option('-c, --content-type <content-type...>', 'Specify content-types')
|
|
100
|
+
.option('-p, --path <path/to/migrations>', 'Change the path where the migrations are saved')
|
|
101
|
+
.option('-v, --verbose', 'Verbosity')
|
|
102
|
+
.description('Generated new Contentful migration from content type')
|
|
103
|
+
.action(
|
|
104
|
+
actionRunner(async (cmd) => {
|
|
105
|
+
const config = await getConfig(parseArgs(cmd || {}));
|
|
106
|
+
const verified = await askMissing(config);
|
|
107
|
+
await fetchMigration({ ...verified, contentType: cmd.contentType });
|
|
108
|
+
})
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
program
|
|
112
|
+
.command('generate')
|
|
113
|
+
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
114
|
+
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
115
|
+
.option('-p, --path <path/to/migrations>', 'Change the path where the migrations are saved')
|
|
116
|
+
.option('-v, --verbose', 'Verbosity')
|
|
117
|
+
.description('Generated new Contentful migration')
|
|
118
|
+
.action(
|
|
119
|
+
actionRunner(async (cmd) => {
|
|
120
|
+
const config = await getConfig(parseArgs(cmd || {}));
|
|
121
|
+
const verified = await askMissing(config);
|
|
122
|
+
await createMigration(verified);
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
program
|
|
127
|
+
.command('migrate')
|
|
128
|
+
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
129
|
+
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
130
|
+
.option('-p, --path <path/to/migrations>', 'Change the path where the migrations are stored')
|
|
131
|
+
.option('-v, --verbose', 'Verbosity')
|
|
132
|
+
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
|
|
133
|
+
.option('--bail', 'Abort execution after first failed migration (default: true)', true)
|
|
134
|
+
.option('--no-bail', 'Ignore failed migrations')
|
|
135
|
+
.description('Execute all unexecuted migrations available.')
|
|
136
|
+
.action(
|
|
137
|
+
actionRunner(async (cmd) => {
|
|
138
|
+
const config = await getConfig(parseArgs(cmd || {}));
|
|
139
|
+
const verified = await askMissing(config);
|
|
140
|
+
|
|
141
|
+
const { missingStorageModel } = verified;
|
|
142
|
+
if (missingStorageModel) {
|
|
143
|
+
console.error(
|
|
144
|
+
chalk.red('\nError:'),
|
|
145
|
+
`Missing migration content type. Run ${chalk.cyan('npx migrations init')}`
|
|
146
|
+
);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
await runMigrations(verified);
|
|
151
|
+
}, false)
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
program
|
|
155
|
+
.command('execute <file>')
|
|
156
|
+
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
157
|
+
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
158
|
+
.option('-v, --verbose', 'Verbosity')
|
|
159
|
+
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
|
|
160
|
+
.description('Execute a single migration.')
|
|
161
|
+
.action(
|
|
162
|
+
actionRunner(async (file, options) => {
|
|
163
|
+
const config = await getConfig(parseArgs(options || {}));
|
|
164
|
+
const verified = await askMissing(config);
|
|
165
|
+
|
|
166
|
+
const { missingStorageModel } = verified;
|
|
167
|
+
if (missingStorageModel) {
|
|
168
|
+
console.error(
|
|
169
|
+
chalk.red('\nError:'),
|
|
170
|
+
`Missing migration content type. Run ${chalk.cyan('npx migrations init')}`
|
|
171
|
+
);
|
|
172
|
+
process.exit(1);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
await executeMigration(path.resolve(file), verified);
|
|
176
|
+
}, false)
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
program
|
|
180
|
+
.command('version <file>')
|
|
181
|
+
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
182
|
+
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
183
|
+
.option('-v, --verbose', 'Verbosity')
|
|
184
|
+
.option('--add', 'Mark migration as migrated')
|
|
185
|
+
.option('--remove', 'Delete migration entry in Contentful')
|
|
186
|
+
.description('Manually mark a migration as migrated or not. (Only available with the Content-model storage)')
|
|
187
|
+
.action(
|
|
188
|
+
actionRunner(async (file, options) => {
|
|
189
|
+
const { remove, add } = options;
|
|
190
|
+
const config = await getConfig(parseArgs(options || {}));
|
|
191
|
+
const verified = await askMissing(config);
|
|
192
|
+
|
|
193
|
+
const { missingStorageModel } = verified;
|
|
194
|
+
if (missingStorageModel) {
|
|
195
|
+
console.error(
|
|
196
|
+
chalk.red('\nError:'),
|
|
197
|
+
`Missing migration content type. Run ${chalk.cyan('npx migrations init')}`
|
|
198
|
+
);
|
|
199
|
+
process.exit(1);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const { storage } = verified || {};
|
|
203
|
+
if (storage === STORAGE_TAG) {
|
|
204
|
+
throw new Error('The version command is not available for the "tag" storage');
|
|
205
|
+
}
|
|
206
|
+
if (remove) {
|
|
207
|
+
await versionDelete(file, verified);
|
|
208
|
+
} else if (add) {
|
|
209
|
+
await versionAdd(file, verified);
|
|
210
|
+
}
|
|
211
|
+
}, true)
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
program
|
|
215
|
+
.command('environment <environment-id>')
|
|
216
|
+
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
217
|
+
.option('-v, --verbose', 'Verbosity')
|
|
218
|
+
.option('--create', 'Create new contentful environment')
|
|
219
|
+
.option('--remove', 'Delete contentful environment')
|
|
220
|
+
.option('--reset', 'Reset contentful environment')
|
|
221
|
+
.option('--source-environment-id <environment-id>', 'Set the source environment to clone new environment from')
|
|
222
|
+
.description('Add or remove a contentful environment for migrations')
|
|
223
|
+
.action(
|
|
224
|
+
actionRunner(async (environmentId, options) => {
|
|
225
|
+
const { remove, create, reset } = options;
|
|
226
|
+
const config = await getConfig(parseArgs({ ...(options || {}), environmentId }));
|
|
227
|
+
const verified = await askMissing(config, ['accessToken', 'spaceId', 'environmentId']);
|
|
228
|
+
|
|
229
|
+
if (create) {
|
|
230
|
+
return createEnvironment(environmentId, verified);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (remove) {
|
|
234
|
+
return removeEnvironment(environmentId, verified);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (reset) {
|
|
238
|
+
return resetEnvironment(environmentId, verified);
|
|
239
|
+
}
|
|
240
|
+
}, true)
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
program
|
|
244
|
+
.command('doc')
|
|
245
|
+
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
246
|
+
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
247
|
+
.option('-p, --path <path/to/docs>', 'Change the path where the docs are stored')
|
|
248
|
+
.option('-v, --verbose', 'Verbosity')
|
|
249
|
+
.option('-t, --template <path/to/template>', 'Use custom template for docs')
|
|
250
|
+
.option('--extension <file-extension>', 'Use custom file extension (default is `md`)')
|
|
251
|
+
.description('Generate offline docs from content-types')
|
|
252
|
+
.action(
|
|
253
|
+
actionRunner(async (cmd) => {
|
|
254
|
+
const config = await getConfig(parseArgs(cmd || {}));
|
|
255
|
+
const verified = await askMissing(config, ['accessToken', 'spaceId', 'environmentId']);
|
|
256
|
+
await createOfflineDocs(verified);
|
|
257
|
+
}, true)
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
program
|
|
261
|
+
.command('content')
|
|
262
|
+
.requiredOption('--source-environment-id <environment-id>', 'Set the Contentful source environment (from)')
|
|
263
|
+
.requiredOption('--dest-environment-id <environment-id>', 'Set the Contentful destination environment (to)')
|
|
264
|
+
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
265
|
+
.option('-c, --content-type <content-type>', 'Specify content-type')
|
|
266
|
+
.option('-v, --verbose', 'Verbosity')
|
|
267
|
+
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
|
|
268
|
+
.option('--diff', 'Manually choose skip/overwrite for every conflict')
|
|
269
|
+
.option('--force', 'No manual diffing. Overwrites all conflicting entries/assets')
|
|
270
|
+
.description('Transfer content from source environment to destination environment')
|
|
271
|
+
.action(
|
|
272
|
+
actionRunner(async (cmd) => {
|
|
273
|
+
const config = await getConfig(parseArgs(cmd || {}));
|
|
274
|
+
const verified = await askMissing({ ...config, environmentId: 'not-used' });
|
|
275
|
+
|
|
276
|
+
// run migrations on destination environment
|
|
277
|
+
await transferContent({
|
|
278
|
+
...verified,
|
|
279
|
+
contentType: cmd.contentType || '',
|
|
280
|
+
forceOverwrite: cmd.force || false,
|
|
281
|
+
diffConflicts: cmd.diff || false,
|
|
282
|
+
});
|
|
283
|
+
})
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
program.parse(process.argv);
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type Migration, { MigrationContext, MigrationFunction } from "contentful-migration";
|
|
2
|
+
import type { LocaleHelpers } from "./lib/helpers/locale";
|
|
3
|
+
import type { ValidationHelpers } from "./lib/helpers/validation";
|
|
4
|
+
|
|
5
|
+
export interface MigrationHelpers {
|
|
6
|
+
locale: LocaleHelpers,
|
|
7
|
+
validation: ValidationHelpers
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type EnhancedMigrationFunction = (
|
|
11
|
+
migration: Migration,
|
|
12
|
+
context?: MigrationContext,
|
|
13
|
+
helpers?: MigrationHelpers
|
|
14
|
+
) => void;
|
|
15
|
+
|
|
16
|
+
export function withHelpers(cb: EnhancedMigrationFunction): MigrationFunction;
|
|
17
|
+
export { getLocaleHelpers } from "./lib/helpers/locale";
|
|
18
|
+
export { getValidationHelpers } from "./lib/helpers/validation";
|
package/index.js
CHANGED
|
@@ -1,287 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const {
|
|
15
|
-
const {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
directory: directory ? path.resolve(directory) : undefined,
|
|
28
|
-
sourceEnvironmentId: cmd.sourceEnvironmentId || parent.sourceEnvironmentId,
|
|
29
|
-
destEnvironmentId: cmd.destEnvironmentId || parent.destEnvironmentId,
|
|
30
|
-
verbose: cmd.verbose || parent.verbose,
|
|
31
|
-
template: cmd.template || parent.template,
|
|
32
|
-
yes: cmd.yes || parent.yes,
|
|
33
|
-
extension: cmd.extension || parent.extension,
|
|
34
|
-
bail: cmd.bail || parent.bail,
|
|
35
|
-
};
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const errorHandler = (error, log) => {
|
|
39
|
-
if (log) {
|
|
40
|
-
const { errors, message } = error;
|
|
41
|
-
console.error(chalk.red('\nError:'), message);
|
|
42
|
-
(errors || []).forEach((error) => {
|
|
43
|
-
console.error(chalk.red('Error:'), error.message);
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
process.exit(1);
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const actionRunner = (fn, log = true) => {
|
|
50
|
-
return (...args) => {
|
|
51
|
-
const verbose = args.some((arg) => arg.verbose);
|
|
52
|
-
return fn(...args).catch((error) => errorHandler(error, verbose || log));
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const program = new Command();
|
|
57
|
-
|
|
58
|
-
program.version(pkg.version);
|
|
59
|
-
program
|
|
60
|
-
.command('init')
|
|
61
|
-
.description('Initialize contentful-migrations')
|
|
62
|
-
.action(
|
|
63
|
-
actionRunner(async (cmd) => {
|
|
64
|
-
const config = await getConfig(parseArgs(cmd || {}));
|
|
65
|
-
const verified = await askAll(config);
|
|
66
|
-
const { managementToken, accessToken, environmentId, spaceId, ...rest } = verified;
|
|
67
|
-
|
|
68
|
-
if (verified.storage === STORAGE_CONTENT) {
|
|
69
|
-
await initializeContentModel({ ...config, ...verified });
|
|
70
|
-
await migrateToContentStorage({ ...config, ...verified });
|
|
71
|
-
}
|
|
72
|
-
if (verified.storage === STORAGE_TAG) {
|
|
73
|
-
await migrateToTagStorage({ ...config, ...verified });
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (!process.env.CONTENTFUL_SPACE_ID) {
|
|
77
|
-
rest.spaceId = spaceId;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// try to store in package.json
|
|
81
|
-
|
|
82
|
-
const {pkgUp} = await import('pkg-up');
|
|
83
|
-
const localPkg = await pkgUp();
|
|
84
|
-
if (localPkg) {
|
|
85
|
-
const packageJson = await fs.readJson(localPkg);
|
|
86
|
-
rest.directory = path.relative(path.dirname(localPkg), rest.directory);
|
|
87
|
-
packageJson.migrations = rest;
|
|
88
|
-
await fs.outputJson(localPkg, packageJson, { spaces: 2 });
|
|
89
|
-
} else {
|
|
90
|
-
// store in .migrationsrc if no package.json is available
|
|
91
|
-
await fs.outputJson(path.join(process.cwd(), '.migrationsrc'), rest, { spaces: 2 });
|
|
92
|
-
}
|
|
93
|
-
})
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
program
|
|
97
|
-
.command('fetch')
|
|
98
|
-
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
99
|
-
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
100
|
-
.option('-c, --content-type <content-type...>', 'Specify content-types')
|
|
101
|
-
.option('-p, --path <path/to/migrations>', 'Change the path where the migrations are saved')
|
|
102
|
-
.option('-v, --verbose', 'Verbosity')
|
|
103
|
-
.description('Generated new Contentful migration from content type')
|
|
104
|
-
.action(
|
|
105
|
-
actionRunner(async (cmd) => {
|
|
106
|
-
const config = await getConfig(parseArgs(cmd || {}));
|
|
107
|
-
const verified = await askMissing(config);
|
|
108
|
-
await fetchMigration({ ...verified, contentType: cmd.contentType });
|
|
109
|
-
})
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
program
|
|
113
|
-
.command('generate')
|
|
114
|
-
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
115
|
-
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
116
|
-
.option('-p, --path <path/to/migrations>', 'Change the path where the migrations are saved')
|
|
117
|
-
.option('-v, --verbose', 'Verbosity')
|
|
118
|
-
.description('Generated new Contentful migration')
|
|
119
|
-
.action(
|
|
120
|
-
actionRunner(async (cmd) => {
|
|
121
|
-
const config = await getConfig(parseArgs(cmd || {}));
|
|
122
|
-
const verified = await askMissing(config);
|
|
123
|
-
await createMigration(verified);
|
|
124
|
-
})
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
program
|
|
128
|
-
.command('migrate')
|
|
129
|
-
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
130
|
-
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
131
|
-
.option('-p, --path <path/to/migrations>', 'Change the path where the migrations are stored')
|
|
132
|
-
.option('-v, --verbose', 'Verbosity')
|
|
133
|
-
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
|
|
134
|
-
.option('--bail', 'Abort execution after first failed migration (default: true)', true)
|
|
135
|
-
.option('--no-bail', 'Ignore failed migrations')
|
|
136
|
-
.description('Execute all unexecuted migrations available.')
|
|
137
|
-
.action(
|
|
138
|
-
actionRunner(async (cmd) => {
|
|
139
|
-
const config = await getConfig(parseArgs(cmd || {}));
|
|
140
|
-
const verified = await askMissing(config);
|
|
141
|
-
|
|
142
|
-
const { missingStorageModel } = verified;
|
|
143
|
-
if (missingStorageModel) {
|
|
144
|
-
console.error(
|
|
145
|
-
chalk.red('\nError:'),
|
|
146
|
-
`Missing migration content type. Run ${chalk.cyan('npx migrations init')}`
|
|
147
|
-
);
|
|
148
|
-
process.exit(1);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
await runMigrations(verified);
|
|
152
|
-
}, false)
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
program
|
|
156
|
-
.command('execute <file>')
|
|
157
|
-
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
158
|
-
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
159
|
-
.option('-v, --verbose', 'Verbosity')
|
|
160
|
-
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
|
|
161
|
-
.description('Execute a single migration.')
|
|
162
|
-
.action(
|
|
163
|
-
actionRunner(async (file, options) => {
|
|
164
|
-
const config = await getConfig(parseArgs(options || {}));
|
|
165
|
-
const verified = await askMissing(config);
|
|
166
|
-
|
|
167
|
-
const { missingStorageModel } = verified;
|
|
168
|
-
if (missingStorageModel) {
|
|
169
|
-
console.error(
|
|
170
|
-
chalk.red('\nError:'),
|
|
171
|
-
`Missing migration content type. Run ${chalk.cyan('npx migrations init')}`
|
|
172
|
-
);
|
|
173
|
-
process.exit(1);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
await executeMigration(path.resolve(file), verified);
|
|
177
|
-
}, false)
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
program
|
|
181
|
-
.command('version <file>')
|
|
182
|
-
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
183
|
-
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
184
|
-
.option('-v, --verbose', 'Verbosity')
|
|
185
|
-
.option('--add', 'Mark migration as migrated')
|
|
186
|
-
.option('--remove', 'Delete migration entry in Contentful')
|
|
187
|
-
.description('Manually mark a migration as migrated or not. (Only available with the Content-model storage)')
|
|
188
|
-
.action(
|
|
189
|
-
actionRunner(async (file, options) => {
|
|
190
|
-
const { remove, add } = options;
|
|
191
|
-
const config = await getConfig(parseArgs(options || {}));
|
|
192
|
-
const verified = await askMissing(config);
|
|
193
|
-
|
|
194
|
-
const { missingStorageModel } = verified;
|
|
195
|
-
if (missingStorageModel) {
|
|
196
|
-
console.error(
|
|
197
|
-
chalk.red('\nError:'),
|
|
198
|
-
`Missing migration content type. Run ${chalk.cyan('npx migrations init')}`
|
|
199
|
-
);
|
|
200
|
-
process.exit(1);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const { storage } = verified || {};
|
|
204
|
-
if (storage === STORAGE_TAG) {
|
|
205
|
-
throw new Error('The version command is not available for the "tag" storage');
|
|
206
|
-
}
|
|
207
|
-
if (remove) {
|
|
208
|
-
await versionDelete(file, verified);
|
|
209
|
-
} else if (add) {
|
|
210
|
-
await versionAdd(file, verified);
|
|
211
|
-
}
|
|
212
|
-
}, true)
|
|
213
|
-
);
|
|
214
|
-
|
|
215
|
-
program
|
|
216
|
-
.command('environment <environment-id>')
|
|
217
|
-
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
218
|
-
.option('-v, --verbose', 'Verbosity')
|
|
219
|
-
.option('--create', 'Create new contentful environment')
|
|
220
|
-
.option('--remove', 'Delete contentful environment')
|
|
221
|
-
.option('--reset', 'Reset contentful environment')
|
|
222
|
-
.option('--source-environment-id <environment-id>', 'Set the source environment to clone new environment from')
|
|
223
|
-
.description('Add or remove a contentful environment for migrations')
|
|
224
|
-
.action(
|
|
225
|
-
actionRunner(async (environmentId, options) => {
|
|
226
|
-
const { remove, create, reset } = options;
|
|
227
|
-
const config = await getConfig(parseArgs({ ...(options || {}), environmentId }));
|
|
228
|
-
const verified = await askMissing(config, ['accessToken', 'spaceId', 'environmentId']);
|
|
229
|
-
|
|
230
|
-
if (create) {
|
|
231
|
-
return createEnvironment(environmentId, verified);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
if (remove) {
|
|
235
|
-
return removeEnvironment(environmentId, verified);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (reset) {
|
|
239
|
-
return resetEnvironment(environmentId, verified);
|
|
240
|
-
}
|
|
241
|
-
}, true)
|
|
242
|
-
);
|
|
243
|
-
|
|
244
|
-
program
|
|
245
|
-
.command('doc')
|
|
246
|
-
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
247
|
-
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
|
|
248
|
-
.option('-p, --path <path/to/docs>', 'Change the path where the docs are stored')
|
|
249
|
-
.option('-v, --verbose', 'Verbosity')
|
|
250
|
-
.option('-t, --template <path/to/template>', 'Use custom template for docs')
|
|
251
|
-
.option('--extension <file-extension>', 'Use custom file extension (default is `md`)')
|
|
252
|
-
.description('Generate offline docs from content-types')
|
|
253
|
-
.action(
|
|
254
|
-
actionRunner(async (cmd) => {
|
|
255
|
-
const config = await getConfig(parseArgs(cmd || {}));
|
|
256
|
-
const verified = await askMissing(config, ['accessToken', 'spaceId', 'environmentId']);
|
|
257
|
-
await createOfflineDocs(verified);
|
|
258
|
-
}, true)
|
|
259
|
-
);
|
|
260
|
-
|
|
261
|
-
program
|
|
262
|
-
.command('content')
|
|
263
|
-
.requiredOption('--source-environment-id <environment-id>', 'Set the Contentful source environment (from)')
|
|
264
|
-
.requiredOption('--dest-environment-id <environment-id>', 'Set the Contentful destination environment (to)')
|
|
265
|
-
.option('-s, --space-id <space-id>', 'Contentful space id')
|
|
266
|
-
.option('-c, --content-type <content-type>', 'Specify content-type')
|
|
267
|
-
.option('-v, --verbose', 'Verbosity')
|
|
268
|
-
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
|
|
269
|
-
.option('--diff', 'Manually choose skip/overwrite for every conflict')
|
|
270
|
-
.option('--force', 'No manual diffing. Overwrites all conflicting entries/assets')
|
|
271
|
-
.description('Transfer content from source environment to destination environment')
|
|
272
|
-
.action(
|
|
273
|
-
actionRunner(async (cmd) => {
|
|
274
|
-
const config = await getConfig(parseArgs(cmd || {}));
|
|
275
|
-
const verified = await askMissing({ ...config, environmentId: 'not-used' });
|
|
276
|
-
|
|
277
|
-
// run migrations on destination environment
|
|
278
|
-
await transferContent({
|
|
279
|
-
...verified,
|
|
280
|
-
contentType: cmd.contentType || '',
|
|
281
|
-
forceOverwrite: cmd.force || false,
|
|
282
|
-
diffConflicts: cmd.diff || false,
|
|
283
|
-
});
|
|
284
|
-
})
|
|
285
|
-
);
|
|
286
|
-
|
|
287
|
-
program.parse(process.argv);
|
|
1
|
+
/**
|
|
2
|
+
* Adds helpers for the migration
|
|
3
|
+
*
|
|
4
|
+
* Example:
|
|
5
|
+
* const { withHelpers } = require('@jungvonmatt/contentful-migrations');
|
|
6
|
+
*
|
|
7
|
+
* module.exports = withHelpers(async (migration, context, helpers) => {
|
|
8
|
+
*
|
|
9
|
+
* ...
|
|
10
|
+
*
|
|
11
|
+
* });
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
const { getValidationHelpers } = require('./lib/helpers/validation');
|
|
15
|
+
const { getLocaleHelpers } = require('./lib/helpers/locale');
|
|
16
|
+
|
|
17
|
+
// Export wrapper
|
|
18
|
+
module.exports.withHelpers = (cb) => (migration, context) => {
|
|
19
|
+
const locale = getLocaleHelpers(migration, context);
|
|
20
|
+
const validation = getValidationHelpers(migration, context);
|
|
21
|
+
|
|
22
|
+
return cb(migration, context, { locale, validation });
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
module.exports.getValidationHelpers = getValidationHelpers;
|
|
26
|
+
module.exports.getLocaleHelpers = getLocaleHelpers;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Locale } from "contentful-management/dist/typings/export-types";
|
|
2
|
+
import type Migration, { MigrationContext } from "contentful-migration";
|
|
3
|
+
|
|
4
|
+
export interface LocaleHelpers {
|
|
5
|
+
getLocales(): Promise<[Locale]>;
|
|
6
|
+
getDefaultLocale(): Promise<Locale>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function getLocaleHelpers(migration: Migration, context: MigrationContext): LocaleHelpers;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adds utils for the migration
|
|
3
|
+
*
|
|
4
|
+
* Example:
|
|
5
|
+
* const { getLocaleHelpers } = require('@jungvonmatt/contentful-migrations');
|
|
6
|
+
*
|
|
7
|
+
* module.exports = async function (migration, context) {
|
|
8
|
+
* const localeHelper = getLocaleHelpers(migration, context);
|
|
9
|
+
* ...
|
|
10
|
+
*
|
|
11
|
+
* await localeHelper.getLocales();
|
|
12
|
+
* await localeHelper.getDefaultLocale();
|
|
13
|
+
*
|
|
14
|
+
* };
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
const getLocaleHelpers = (migration, context) => {
|
|
18
|
+
const { makeRequest } = context;
|
|
19
|
+
|
|
20
|
+
const getLocales = async () => {
|
|
21
|
+
const { items: locales } = await makeRequest({
|
|
22
|
+
method: 'GET',
|
|
23
|
+
url: '/locales',
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return locales;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const getDefaultLocale = async () => {
|
|
30
|
+
// Fetch locale
|
|
31
|
+
const locales = await getLocales();
|
|
32
|
+
|
|
33
|
+
return locales.find((locale) => locale.default);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
getLocales,
|
|
38
|
+
getDefaultLocale,
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
module.exports.getLocaleHelpers = getLocaleHelpers;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type Migration, { MigrationContext } from "contentful-migration";
|
|
2
|
+
|
|
3
|
+
export interface ValidationHelpers {
|
|
4
|
+
addLinkContentTypeValues(contentTypeId: string, fieldId: string, values: string | [string]): Promise<void>;
|
|
5
|
+
addInValues(contentTypeId: string, fieldId: string, values: string | [string]): Promise<void>;
|
|
6
|
+
removeLinkContentTypeValues(contentTypeId: string, fieldId: string, values: string | [string]): Promise<void>;
|
|
7
|
+
removeInValues(contentTypeId: string, fieldId: string, values: string | [string]): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function getValidationHelpers(migration: Migration, context: MigrationContext): ValidationHelpers;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const { TYPE_LINK, TYPE_ARRAY } = require('../contentful');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Adds utils for the migration
|
|
5
|
+
*
|
|
6
|
+
* Example:
|
|
7
|
+
* const { getValidationHelpers } = require('@jungvonmatt/contentful-migrations');
|
|
8
|
+
*
|
|
9
|
+
* module.exports = async function (migration, context) {
|
|
10
|
+
* const validationHelper = getValidationHelpers(migration, context);
|
|
11
|
+
* ...
|
|
12
|
+
*
|
|
13
|
+
* await validationHelper.addLinkContentTypeValues('contentTypeId', 'fieldId', ['value']);
|
|
14
|
+
* await validationHelper.addInValues('contentTypeId', 'fieldId', ['value']);
|
|
15
|
+
* await validationHelper.removeLinkContentTypeValues('contentTypeId', 'fieldId', ['value']);
|
|
16
|
+
* await validationHelper.removeInValues('contentTypeId', 'fieldId', ['value']);
|
|
17
|
+
*
|
|
18
|
+
* };
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
const getValidationHelpers = (migration, context) => {
|
|
22
|
+
const { makeRequest } = context;
|
|
23
|
+
|
|
24
|
+
const addValidationValues = (validations, key, values = []) =>
|
|
25
|
+
validations.map((validation) => {
|
|
26
|
+
if (validation?.[key]) {
|
|
27
|
+
if (!Array.isArray(values)) {
|
|
28
|
+
values = [values];
|
|
29
|
+
}
|
|
30
|
+
if (!Array.isArray(validation[key])) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`addValidationValues can only be used on arrays. validation.${key} is typeof ${typeof validation[key]}`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
validation[key] = [...new Set([...validation[key], ...values])];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return validation;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const removeValidationValues = (validations, key, values = []) =>
|
|
42
|
+
validations.map((validation) => {
|
|
43
|
+
if (validation?.[key]) {
|
|
44
|
+
if (!Array.isArray(values)) {
|
|
45
|
+
values = [values];
|
|
46
|
+
}
|
|
47
|
+
if (!Array.isArray(validation[key])) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`removeValidationValues can only be used on arrays. validation.${key} is typeof ${typeof validation[key]}`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
validation[key] = validation[key].filter((x) => !values.includes(x));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return validation;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const modifyValidationValuesForType = async (validationKey, method, contentTypeId, fieldId, typeIds) => {
|
|
59
|
+
// Fetch content type
|
|
60
|
+
const { fields } = await makeRequest({
|
|
61
|
+
method: 'GET',
|
|
62
|
+
url: `/content_types/${contentTypeId}`,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const { type, items = {}, validations = [] } = fields?.find((field) => field.id === fieldId) ?? {};
|
|
66
|
+
|
|
67
|
+
if (type === TYPE_ARRAY) {
|
|
68
|
+
const ct = migration.editContentType(contentTypeId);
|
|
69
|
+
ct.editField(fieldId).items({ ...items, validations: method(items?.validations ?? [], validationKey, typeIds) });
|
|
70
|
+
} else {
|
|
71
|
+
const ct = migration.editContentType(contentTypeId);
|
|
72
|
+
ct.editField(fieldId).validations(method(validations ?? [], validationKey, typeIds));
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
async addLinkContentTypeValues(contentTypeId, fieldId, values) {
|
|
78
|
+
await modifyValidationValuesForType('linkContentType', addValidationValues, contentTypeId, fieldId, values);
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
async addInValues(contentTypeId, fieldId, values) {
|
|
82
|
+
await modifyValidationValuesForType('in', addValidationValues, contentTypeId, fieldId, values);
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
async removeLinkContentTypeValues(contentTypeId, fieldId, values) {
|
|
86
|
+
await modifyValidationValuesForType('linkContentType', removeValidationValues, contentTypeId, fieldId, values);
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
async removeInValues(contentTypeId, fieldId, values) {
|
|
90
|
+
await modifyValidationValuesForType('in', removeValidationValues, contentTypeId, fieldId, values);
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
module.exports.getValidationHelpers = getValidationHelpers;
|
package/lib/migration.js
CHANGED
|
@@ -14,6 +14,17 @@ const { confirm, STATE_SUCCESS, STATE_FAILURE } = require('./config');
|
|
|
14
14
|
|
|
15
15
|
const { storeMigration, getNewMigrations } = require('./backend');
|
|
16
16
|
|
|
17
|
+
const migrationHeader = stripIndent`/* eslint-env node */
|
|
18
|
+
const { withHelpers } = require('@jungvonmatt/contentful-migrations');
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Contentful migration
|
|
22
|
+
* API: https://github.com/contentful/contentful-migration
|
|
23
|
+
* Editor Interfaces: https://www.contentful.com/developers/docs/extensibility/app-framework/editor-interfaces/
|
|
24
|
+
*/
|
|
25
|
+
module.exports = withHelpers(async function (migration, context, helpers) {
|
|
26
|
+
`;
|
|
27
|
+
|
|
17
28
|
/**
|
|
18
29
|
* Create new migration file.
|
|
19
30
|
* Adds initial migration file adding the migration field in the content type
|
|
@@ -32,16 +43,9 @@ const createMigration = async (config) => {
|
|
|
32
43
|
const { directory } = config || {};
|
|
33
44
|
const timestamp = Date.now();
|
|
34
45
|
const filename = path.join(directory, `${timestamp}-migration.${module ? 'cjs' : 'js'}`);
|
|
35
|
-
const content = stripIndent
|
|
36
|
-
/* eslint-env node */
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Contentful migration
|
|
40
|
-
*/
|
|
41
|
-
module.exports = function(migration /*, context */) {
|
|
46
|
+
const content = stripIndent`${migrationHeader}
|
|
42
47
|
// Add your migration code here
|
|
43
|
-
|
|
44
|
-
}`;
|
|
48
|
+
})`;
|
|
45
49
|
|
|
46
50
|
await fs.outputFile(filename, await format(filename, content));
|
|
47
51
|
console.log(`Generated new migration file to ${chalk.green(filename)}`);
|
|
@@ -80,8 +84,33 @@ const fetchMigration = async (config) => {
|
|
|
80
84
|
const filename = path.join(directory, `${timestamp++}-create-${entry.sys.id}-migration.${module ? 'cjs' : 'js'}`);
|
|
81
85
|
|
|
82
86
|
const content = await generateMigrationScript(client, [entry]);
|
|
87
|
+
// Fetch migration script generated by contentful
|
|
88
|
+
let modifiedContent = content.toString();
|
|
89
|
+
// Modify migration script to use our helpers
|
|
90
|
+
const testDefaultValueRegex = /(defaultValue\({\s*)[^\:]+([^\)]+)/g;
|
|
91
|
+
if (testDefaultValueRegex.test(modifiedContent)) {
|
|
92
|
+
modifiedContent = content
|
|
93
|
+
.toString()
|
|
94
|
+
// Add call to utils.getDefaultLocale() to the top
|
|
95
|
+
.replace(
|
|
96
|
+
'module.exports = function (migration) {',`
|
|
97
|
+
${migrationHeader}
|
|
98
|
+
const defaultLocale = helpers.locale.getDefaultLocale();
|
|
99
|
+
`)
|
|
100
|
+
// Replace the default locale with defaultLocale.code so that the migration
|
|
101
|
+
// still works as expected when the locale is changed in contentful
|
|
102
|
+
.replace(testDefaultValueRegex, '$1[defaultLocale.code]$2')
|
|
103
|
+
// Add a closing parentheses as we wrap the migration function.
|
|
104
|
+
.replace(/};\s*$/g, '});');
|
|
105
|
+
} else {
|
|
106
|
+
// If we don't have a default value we just wrap the migration function with our withHelpers wrapper
|
|
107
|
+
modifiedContent = content
|
|
108
|
+
.toString()
|
|
109
|
+
.replace('module.exports = function (migration) {', migrationHeader)
|
|
110
|
+
.replace(/};\s*$/g, '});');
|
|
111
|
+
}
|
|
83
112
|
|
|
84
|
-
await fs.outputFile(filename, await format(filename,
|
|
113
|
+
await fs.outputFile(filename, await format(filename, modifiedContent));
|
|
85
114
|
console.log(`Generated new migration file to ${chalk.green(filename)}`);
|
|
86
115
|
});
|
|
87
116
|
|
package/package.json
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jungvonmatt/contentful-migrations",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.2",
|
|
4
4
|
"description": "Helper to handle migrations in contentful",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
7
|
+
"index.d.ts",
|
|
7
8
|
"index.js",
|
|
9
|
+
"cli.js",
|
|
8
10
|
"lib"
|
|
9
11
|
],
|
|
12
|
+
"typings": "index.d.ts",
|
|
10
13
|
"bin": {
|
|
11
|
-
"contentful-migrations": "
|
|
12
|
-
"migrations": "
|
|
14
|
+
"contentful-migrations": "cli.js",
|
|
15
|
+
"migrations": "cli.js"
|
|
13
16
|
},
|
|
14
17
|
"scripts": {
|
|
15
18
|
"test": "npm run lint",
|