forked from imrans110/contentful-migration-poc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
246 lines (215 loc) · 7.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env node
(async () => {
try {
require("dotenv").config();
const { promisify } = require("util");
const { readdir } = require("fs");
const readdirAsync = promisify(readdir);
const path = require("path");
const { createClient } = require("contentful-management");
const {
default: runMigration,
} = require("contentful-migration/built/bin/cli");
// utility fns
const getVersionOfFile = (file) =>
file.replace(".js", "").replace(/_/g, ".");
const getFileOfVersion = (version) => version.replace(/\./g, "_") + ".js";
//
// Configuration variables
//
const {
CONTENTFUL_SPACE_ID,
CONTENTFUL_MANAGEMENT_ACCESS_TOKEN,
CONTENTFUL_ENV_ID,
} = process.env;
const MIGRATIONS_DIR = path.join(__dirname, './migrations')
const client = createClient({
accessToken: CONTENTFUL_MANAGEMENT_ACCESS_TOKEN,
});
const space = await client.getSpace(CONTENTFUL_SPACE_ID);
let ENVIRONMENT_ID = "";
let environment;
console.log("Running with the following configuration");
console.log(`CONTENTFUL_SPACE_ID: ${CONTENTFUL_SPACE_ID}`);
// ---------------------------------------------------------------------------
if (
CONTENTFUL_ENV_ID == "master" ||
CONTENTFUL_ENV_ID == "staging" ||
CONTENTFUL_ENV_ID == "qa"
) {
console.log(`Running on ${CONTENTFUL_ENV_ID}.`);
ENVIRONMENT_ID = `${CONTENTFUL_ENV_ID}-`.concat(getStringDate());
} else {
console.log("Running on feature branch");
ENVIRONMENT_ID = CONTENTFUL_ENV_ID;
}
console.log(`ENVIRONMENT_ID: ${ENVIRONMENT_ID}`);
// ---------------------------------------------------------------------------
console.log(
`Checking for existing versions of environment: ${ENVIRONMENT_ID}`
);
try {
environment = await space.getEnvironment(ENVIRONMENT_ID);
if (
ENVIRONMENT_ID != "master" ||
ENVIRONMENT_ID != "staging" ||
ENVIRONMENT_ID != "qa"
) {
await environment.delete();
console.log("Environment deleted");
}
} catch (e) {
console.log("Environment not found");
}
// ---------------------------------------------------------------------------
if (
ENVIRONMENT_ID != "master" ||
ENVIRONMENT_ID != "staging" ||
ENVIRONMENT_ID != "qa"
) {
console.log(`Creating environment ${ENVIRONMENT_ID}`);
environment = await space.createEnvironmentWithId(ENVIRONMENT_ID, {
name: ENVIRONMENT_ID,
});
}
// ---------------------------------------------------------------------------
const DELAY = 3000;
const MAX_NUMBER_OF_TRIES = 10;
let count = 0;
console.log("Waiting for environment processing...");
while (count < MAX_NUMBER_OF_TRIES) {
const status = (await space.getEnvironment(environment.sys.id)).sys.status
.sys.id;
if (status === "ready" || status === "failed") {
if (status === "ready") {
console.log(
`Successfully processed new environment (${ENVIRONMENT_ID})`
);
} else {
console.log("Environment creation failed");
}
break;
}
await new Promise((resolve) => setTimeout(resolve, DELAY));
count++;
}
// ---------------------------------------------------------------------------
console.log("Update API Keys to allow access to new environment");
const newEnv = {
sys: {
type: "Link",
linkType: "Environment",
id: ENVIRONMENT_ID,
},
};
const { items: keys } = await space.getApiKeys();
await Promise.all(
keys.map((key) => {
console.log(`Updating - ${key.sys.id}`);
key.environments.push(newEnv);
return key.update();
})
);
// ---------------------------------------------------------------------------
console.log("Set default locale to new environment");
const defaultLocale = (await environment.getLocales()).items.find(
(locale) => locale.default
).code;
// ---------------------------------------------------------------------------
console.log("Read all the available migrations from the file system");
const availableMigrations = (await readdirAsync(MIGRATIONS_DIR))
.filter((file) => /^\d+?\.js$/.test(file))
.map((file) => getVersionOfFile(file));
// ---------------------------------------------------------------------------
console.log("Figure out latest ran migration of the contentful space");
const { items: versions } = await environment.getEntries({
content_type: "versionTracker",
});
if (!versions.length || versions.length > 1) {
throw new Error(
"There should only be one entry of type 'versionTracking'"
);
}
let storedVersionEntry = versions[0];
const currentVersionString =
storedVersionEntry.fields.lastPublished[defaultLocale];
// ---------------------------------------------------------------------------
console.log("Evaluate which migrations to run");
const currentMigrationIndex = availableMigrations.indexOf(
currentVersionString
);
console.log({ currentVersionString, availableMigrations });
if (currentMigrationIndex === -1) {
throw new Error(
`Version ${currentVersionString} is not matching with any known migration`
);
}
const migrationsToRun = availableMigrations.slice(
currentMigrationIndex + 1
);
const migrationOptions = {
spaceId: CONTENTFUL_SPACE_ID,
environmentId: ENVIRONMENT_ID,
accessToken: CONTENTFUL_MANAGEMENT_ACCESS_TOKEN,
yes: true,
};
// ---------------------------------------------------------------------------
console.log("Run migrations and update version entry");
while ((migrationToRun = migrationsToRun.shift())) {
const filePath = path.join(
__dirname,
"migrations",
getFileOfVersion(migrationToRun)
);
console.log(`Running ${filePath}`);
await runMigration(
Object.assign(migrationOptions, {
filePath,
})
);
console.log(`${migrationToRun} succeeded`);
storedVersionEntry.fields.lastPublished[defaultLocale] = migrationToRun;
storedVersionEntry = await storedVersionEntry.update();
storedVersionEntry = await storedVersionEntry.publish();
console.log(`Updated version entry to ${migrationToRun}`);
}
// ---------------------------------------------------------------------------
console.log("Checking if we need to an alias");
if (
CONTENTFUL_ENV_ID == "master" ||
CONTENTFUL_ENV_ID == "staging" ||
CONTENTFUL_ENV_ID == "qa"
) {
console.log(`Running on ${CONTENTFUL_ENV_ID}.`);
console.log(`Updating ${CONTENTFUL_ENV_ID} alias.`);
await space
.getEnvironmentAlias(CONTENTFUL_ENV_ID)
.then((alias) => {
alias.environment.sys.id = ENVIRONMENT_ID;
return alias.update();
})
.then((alias) => console.log(`alias ${alias.sys.id} updated.`))
.catch(console.error);
console.log(`${CONTENTFUL_ENV_ID} alias updated.`);
} else {
console.log("Running on feature branch");
console.log("No alias changes required");
}
console.log("All done!");
} catch (e) {
console.error(e);
process.exit(1);
}
})();
function getStringDate() {
var d = new Date();
function pad(n) {
return n < 10 ? "0" + n : n;
}
return (
d.toISOString().substring(0, 10) +
"-" +
pad(d.getUTCHours()) +
pad(d.getUTCMinutes())
);
}