forked from lotas/contentful-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.js
executable file
·74 lines (58 loc) · 1.99 KB
/
import.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
#!/usr/bin/env node
/* eslint-disable no-console */
require('dotenv').config();
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs');
const convertApi = require('./src/index');
const spaceId = process.env.CONTENTFUL_SPACE_ID;
const apiToken = process.env.CONTENTFUL_TOKEN;
const managementToken = process.env.CONTENTFUL_MANAGEMENT_TOKEN;
const environmentId = process.env.CONTENTFUL_ENVIRONMENT_ID || 'master';
const cmd = process.argv[1].split('/').pop();
const usageHelp = `
Usage:
Running with distribution token:
CONTENTFUL_TOKEN=xxx CONTENTFUL_SPACE_ID=yyy ${cmd}
Running with management token:
CONTENTFUL_MANAGEMENT_TOKEN=xxx CONTENTFUL_SPACE_ID=yyy ${cmd}
Running in different environment:
CONTENTFUL_ENVIRONMENT_ID=xxx CONTENTFUL_MANAGEMENT_TOKEN=xxx CONTENTFUL_SPACE_ID=yyy ${cmd}
Importing from local json file:
${cmd} ./path/to/model.json
Available options:
--help (-h) Display this help
--dev (-d) Include developer information - field Id's and entity Id's
--no-fields (-n) Hide entity fields information, show only entity names and relationships
`;
if (argv.help || argv.h) {
console.info(usageHelp);
process.exit(0);
}
async function run() {
let contentTypes;
if (argv._.length === 1) {
contentTypes = JSON.parse(fs.readFileSync(argv._[0], 'utf8'));
} else if (managementToken) {
contentTypes = await convertApi
.getContentTypesFromManagementApi(spaceId, managementToken, environmentId);
} else if (apiToken) {
contentTypes = await convertApi
.getContentTypesFromDistributionApi(spaceId, apiToken, environmentId);
} else {
console.log(usageHelp);
return false;
}
const options = {
hideEntityFields: argv.n || argv['no-fields'],
dev: argv.dev || argv.d,
};
const modelsMap = convertApi.contentTypesToModelMap(contentTypes);
const dotStr = convertApi.modelsMapToDot(modelsMap, options);
console.log(dotStr);
return dotStr;
}
try {
run();
} catch (err) {
console.warn(err);
}