-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
74 lines (70 loc) · 1.99 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
require('dotenv').config();
const contentful = require('contentful');
const { exec } = require('child_process');
const client = contentful.createClient({
space: process.env.space_id,
accessToken: process.env.access_token,
environment: process.env.space_env
});
function execMe(cmd, name) {
return new Promise((resolve, reject) => {
exec(cmd, (err, stdout, stderr) => {
if (err) {
console.log(err);
process.exit(-1);
}
console.log(stdout);
});
setTimeout(function() {
resolve(true);
}, 2000);
});
}
client
.getContentTypes()
.then(async contentTypes => {
const generateMigrations = async () => {
console.log('=====Generating content model migrations=====');
for (const contentType of contentTypes.items) {
let contentTypeId = contentType.sys.id;
try {
await execMe(
`contentful space generate migration --space-id ${
process.env.space_id
} --environment-id ${
process.env.space_env
} --content-type-id ${contentTypeId} --filename migrations/${contentTypeId}-${
process.env.space_env
}.js`,
contentTypeId
);
} catch (err) {
throw err;
process.exit(-1);
}
}
};
const generateContentExport = async () => {
console.log('=====Generating content export=====');
await execMe(
`contentful space export --space-id ${process.env.space_id} --environment-id ${
process.env.space_env
} --content-file ${
process.env.space_env
}-content-export.json --export-dir content_export/ --use-verbose-renderer`
);
};
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
try {
await generateMigrations();
await wait(3000);
await generateContentExport();
} catch (err) {
process.exit(-1);
}
})
.catch(err => {
process.exit(1);
});