-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (61 loc) · 1.92 KB
/
index.js
File metadata and controls
71 lines (61 loc) · 1.92 KB
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
#!/usr/bin/env node
const contentful = require('contentful-management');
const fs = require('fs');
const argv = require('yargs')
.default('interval', 5000)
.default('maxAmount', 10)
.describe('managementToken', 'Mangment token to space API')
.describe('space', 'Space id to work with')
.describe('maxAmount', 'Contentul API limit')
.describe('interval', 'Contentful API calls timeout')
.describe('config', 'Configuration json file path')
.usage('Usage: $0 --config [file]')
.demandOption(['config'])
.config('config', configPath => JSON.parse(fs.readFileSync(configPath, 'utf-8')))
.argv;
const config = {
managementToken: argv.managementToken,
space: argv.space,
interval: argv.interval,
maxAmount: argv.maxAmount,
};
console.log('Connecting to Contentful');
const client = contentful.createClient({
accessToken: config.managementToken,
});
client.getSpace(config.space)
.then((space) => space.getEntries({
limit: config.maxAmount,
}))
.then((response) => {
console.log('Extracted: ', response.limit);
console.log('Total entries: ', response.total);
return deleteEntry(response.items.shift(), response.items);
})
.then(() => console.log('Your space is clean!'))
.catch(console.error);
function deleteEntry(entry, list) {
const identifier = entry.sys.id;
console.log('---Deleting entry:', identifier);
return entry.delete()
.then((err) => {
console.log('list: ', list);
console.log('Entry deleted: ', identifier, err || '');
if (err) {
return Promise.reject();
}
if (list.length === 0) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(deleteEntry(list.shift(), list));
}, config.interval);
});
})
.catch((error) => {
console.log('Failed to delete entry: ', identifier);
console.log(error);
return Promise.reject();
});
}