forked from segmentio/segment-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_id.js
179 lines (145 loc) · 4.34 KB
/
add_id.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
// Purpose: Add id values to integrations that don't have them
// Why it's important: We look up integration metadata by ID, rather than slug
// Instructions: run `make add-id`, select the integration type, enter the slug
// The script:
// 1. Get's the list of public integrations from the API
// 2. Checks the slug you entered for an id
// 3. If there is no ID, it adds the ID retrieved from the API to the file.
const axios = require('axios');
const path = require('path');
const fs = require('fs');
const fm = require('front-matter');
const yaml = require('js-yaml');
const {
Select
} = require('enquirer');
const {
AutoComplete
} = require('enquirer');
const {
type
} = require('os');
const {
autocomplete
} = require('@algolia/autocomplete-js');
require('dotenv').config();
// Here, global variables are set
const PAPI_URL = "https://api.segmentapis.com"
const slugOverrides = yaml.load(fs.readFileSync(path.resolve(__dirname, `../src/_data/catalog/slugs.yml`)))
const slugify = (displayName) => {
let slug = displayName
.toLowerCase()
.replace(/\s+/g, '-')
.replace('-&-', '-')
.replace('/', '-')
.replace(/[\(\)]/g, '')
.replace('.', '-')
for (key in slugOverrides) {
let original = slugOverrides[key].original
let override = slugOverrides[key].override
if (slug == original) {
slug = override
}
}
return slug
}
const getCatalog = async (url, page_token = "MA==") => {
try {
const res = await axios.get(url, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.PAPI_TOKEN}`
},
data: {
"pagination": {
"count": 200,
"cursor": page_token
}
}
});
return res.data
} catch (error) {
console.log(error)
}
}
const updateId = async () => {
const type_select = new Select({
name: 'int_type',
message: 'What type of integration?',
choices: ['sources', 'destinations']
})
let int_type = await type_select.run()
let nextPageToken = "MA=="
let integrations = []
let paredIntegrations = []
while (nextPageToken !== undefined) {
const res = await getCatalog(`${PAPI_URL}/catalog/${int_type}/`, nextPageToken)
if (int_type == "sources") {
integrations = integrations.concat(res.data.sourcesCatalog)
} else {
integrations = integrations.concat(res.data.destinationsCatalog)
}
nextPageToken = res.data.pagination.next
}
integrations.forEach(integration => {
const urlParse = (slug, type) => {
let url = ""
if (type == "destinations") {
url = `connections/destinations/catalog/${slug}`
} else {
const libraryCategories = [
'server',
'mobile',
'ott',
'roku',
'website'
]
let mainCategory = integration.categories[0] ? integration.categories[0].toLowerCase() : ''
if (libraryCategories.includes(mainCategory)) {
url = `connections/sources/catalog/libraries/${mainCategory}/${slug}`
} else {
url = `connections/sources/catalog/cloud-apps/${slug}`
mainCategory = 'cloud-app'
}
}
return url
}
let slug = slugify(integration.name)
let url = urlParse(slug, int_type)
let updatedIntegration = {
id: integration.id,
slug,
url
}
paredIntegrations.push(updatedIntegration)
})
let integrationSlugs = paredIntegrations.map(a => a.slug)
const slug_select = new AutoComplete({
name: "slug",
message: "Enter the integration slug",
limit: 10,
initial: 2,
choices: integrationSlugs
})
let slug_value = await slug_select.run()
let final = paredIntegrations.find(x => x.slug == slug_value)
let itemURL = final.url
const catalogPath = path.resolve('src/', itemURL, 'index.md')
if (fs.existsSync(catalogPath)) {
const f = fm(fs.readFileSync(catalogPath, 'utf8'));
const fmatter = f.frontmatter
const re_id = new RegExp("(id: )\S*")
if (!re_id.test(fmatter)) {
const attr = `---\n${f.frontmatter}\nid: ${final.id}\n---\n`
const body = f.body
const content = attr + body
fs.writeFileSync(catalogPath, content)
console.log(`${final.slug} updated`)
} else {
console.log("integration already has an ID")
}
} else {
console.log("can't find that integration")
}
}
updateId()