|
| 1 | +// These lines are the required packages we need. These let us do things like make network requests to the Public API |
| 2 | +// and interact with the frontmatter |
| 3 | +const axios = require('axios'); |
| 4 | +const path = require('path'); |
| 5 | +const fs = require('fs'); |
| 6 | +const fm = require('front-matter'); |
| 7 | +const yaml = require('js-yaml'); |
| 8 | +const { |
| 9 | + type |
| 10 | +} = require('os'); |
| 11 | + |
| 12 | +require('dotenv').config(); |
| 13 | + |
| 14 | +// Here, global variables are set |
| 15 | +const PAPI_URL = "https://api.segmentapis.com" |
| 16 | +const slugOverrides = yaml.load(fs.readFileSync(path.resolve(__dirname, `../src/_data/catalog/slugs.yml`))) |
| 17 | + |
| 18 | +// This function connects with the Public API. It looks for the endpoint URL and a page token value. |
| 19 | +// The function is called in the updateSources and update Destination functions. |
| 20 | +// Functions let us reuse code easily. Instead of needing to write this out multiple times, I can define it once |
| 21 | +// and pass in the necessary details when I call it. |
| 22 | +const getCatalog = async (url, page_token = "MA==") => { |
| 23 | + try { |
| 24 | + const res = await axios.get(url, { |
| 25 | + headers: { |
| 26 | + 'Content-Type': 'application/json', |
| 27 | + 'Authorization': `Bearer ${process.env.PAPI_TOKEN}` |
| 28 | + }, |
| 29 | + data: { |
| 30 | + "pagination": { |
| 31 | + "count": 200, |
| 32 | + "cursor": page_token |
| 33 | + } |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + return res.data |
| 38 | + } catch (error) { |
| 39 | + console.log(error) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// This function, again called by the two update functions, is what generates the slug values for each integration. |
| 44 | +// It takes the integration's Display Name and converts it to a slug. |
| 45 | +const slugify = (displayName) => { |
| 46 | + let slug = displayName |
| 47 | + .toLowerCase() |
| 48 | + .replace(/\s+/g, '-') |
| 49 | + .replace('-&-', '-') |
| 50 | + .replace('/', '-') |
| 51 | + .replace(/[\(\)]/g, '') |
| 52 | + .replace('.', '-') |
| 53 | + |
| 54 | +// This is how we handle manual slug overrides right now. |
| 55 | +// If a slug appears in the slugOverrides file, we want to use the 'override' value instead. |
| 56 | + for (key in slugOverrides) { |
| 57 | + let original = slugOverrides[key].original |
| 58 | + let override = slugOverrides[key].override |
| 59 | + |
| 60 | + if (slug == original) { |
| 61 | + console.log(original + " -> " + override) |
| 62 | + slug = override |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return slug |
| 67 | +} |
| 68 | + |
| 69 | +// This function does the actual work of adding the id value to the source and destination |
| 70 | +// Notice that the write to file step is commented out. This is to verify that the updated frontmatter |
| 71 | +// is correct before we write a whole bunch of files. |
| 72 | +// Uncomment that line and remove the line above it to run it for real. |
| 73 | +const addIdToExisting = (integration) => { |
| 74 | + let itemURL = integration.url |
| 75 | + try { |
| 76 | + const catalogPath = path.resolve('src', itemURL, 'index.md') |
| 77 | + if (fs.existsSync(catalogPath)) { |
| 78 | + const f = fm(fs.readFileSync(catalogPath, 'utf8')); |
| 79 | + const attr = `---\n${f.frontmatter}\nid: ${integration.id}\n---\n` |
| 80 | + const body = f.body |
| 81 | + const content = attr + body |
| 82 | + console.log(attr) |
| 83 | + //fs.writeFileSync(catalogPath, content) |
| 84 | + } |
| 85 | + } catch (e) { |
| 86 | + console.log(error) |
| 87 | + return false |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +// This is just a stripped down version of the updateSources() script from the catalog script. |
| 93 | +// We're retrieving less information overall, because all we care about here is the id. |
| 94 | +const updateSources = async () => { |
| 95 | + |
| 96 | + let sources = [] |
| 97 | + let nextPageToken = "MA==" |
| 98 | + |
| 99 | + while (nextPageToken !== null) { |
| 100 | + const res = await getCatalog(`${PAPI_URL}/catalog/sources/`, nextPageToken) |
| 101 | + sources = sources.concat(res.data.sourcesCatalog) |
| 102 | + nextPageToken = res.data.pagination.next |
| 103 | + } |
| 104 | + |
| 105 | + const libraryCategories = [ |
| 106 | + 'server', |
| 107 | + 'mobile', |
| 108 | + 'ott', |
| 109 | + 'roku', |
| 110 | + 'website' |
| 111 | + ] |
| 112 | + sources.forEach(source => { |
| 113 | + let slug = slugify(source.name) |
| 114 | + let mainCategory = source.categories[0] ? source.categories[0].toLowerCase() : '' |
| 115 | + |
| 116 | + if (libraryCategories.includes(mainCategory)) { |
| 117 | + url = `connections/sources/catalog/libraries/${mainCategory}/${slug}` |
| 118 | + } else { |
| 119 | + url = `connections/sources/catalog/cloud-apps/${slug}` |
| 120 | + mainCategory = 'cloud-app' |
| 121 | + } |
| 122 | +// So, we retrieve and store only the id and the URL, which is defined in the if statement on line 116. |
| 123 | + let updatedSource = { |
| 124 | + id: source.id, |
| 125 | + url, |
| 126 | + } |
| 127 | + addIdToExisting(updatedSource) |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | +// Similar to the sources script, only for destinations. |
| 132 | +const updateDestinations = async () => { |
| 133 | + let destinations = [] |
| 134 | + let nextPageToken = "MA==" |
| 135 | + |
| 136 | + while (nextPageToken !== null) { |
| 137 | + const res = await getCatalog(`${PAPI_URL}/catalog/destinations/`, nextPageToken) |
| 138 | + destinations = destinations.concat(res.data.destinationsCatalog) |
| 139 | + nextPageToken = res.data.pagination.next |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + destinations.forEach(destination => { |
| 144 | + let slug = slugify(destination.name) |
| 145 | + |
| 146 | + let url = `connections/destinations/catalog/${slug}` |
| 147 | + |
| 148 | + let updatedDestination = { |
| 149 | + id: destination.id, |
| 150 | + url |
| 151 | + } |
| 152 | + addIdToExisting(updatedDestination) |
| 153 | + |
| 154 | + }) |
| 155 | + |
| 156 | +} |
| 157 | +updateDestinations() |
| 158 | +updateSources() |
0 commit comments