Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: build static datasources #45

Merged
merged 5 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {
'prettier',
],
rules: {
camelcase: 'error',
'react/prop-types': 'off',
'@docusaurus/string-literal-i18n-messages': 'error',
'@docusaurus/no-untranslated-text': 'warn',
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ versioned_docs/*

versioned_sidebars/*
!versioned_sidebars/.keep

static/generated/*.json
88 changes: 54 additions & 34 deletions scripts/build-plugin-list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@
'use strict'

const { promises: fs } = require('fs')
const path = require('path')

const log = require('pino')({
level: process.env.LOG_LEVEL || 'debug',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
},
},
})

const LATEST_ECOSYSTEM_FILE = path.join(__dirname, '../versioned_docs/version-latest/Guides/Ecosystem.md')
const OUTPUT_FILE = path.join(__dirname, '../static/generated/plugins.json')

generateEcosystemJson({
ecosystemFile: LATEST_ECOSYSTEM_FILE,
outputFile: OUTPUT_FILE,
})

async function generateEcosystemJson({ ecosystemFile, outputFile }) {
log.info('Generating ecosystem data file from source %s', ecosystemFile)
const plugins = await extractEcosystemFromFile(ecosystemFile)
log.debug('Read the ecosystem file')

await fs.writeFile(outputFile, JSON.stringify(plugins, null, 2))
log.info('Wrote the ecosystem plugin file to %s', outputFile)
}

async function extractEcosystemFromFile(file) {
const content = await fs.readFile(file, 'utf8')

const extractPlugins = (pluginContent) => {
const [, pluginText] = content.split('#### [Core](#core)\n')

const [
corePluginsContent, //
communityPluginsContent, //
] = pluginText.split('#### [Community](#community)')

return {
corePlugins: extractPlugins(corePluginsContent),
communityPlugins: extractPlugins(communityPluginsContent),
}
}

function extractPlugins(pluginContent) {
const lines = pluginContent.split('\n').filter(Boolean) // remove empty lines

// if a line doesn't start with "-" merge it back with the previous item
Expand All @@ -12,6 +58,7 @@ const extractPlugins = (pluginContent) => {
}
return acc
}, [])

const re = /\[`([-a-zA-Z\d./@]+)`\]\(([^)]+)\)(\s*(.+))?/
const plugins = mergedLines.map((line) => {
const match = re.exec(line)
Expand All @@ -23,40 +70,13 @@ const extractPlugins = (pluginContent) => {

const name = match[1]
const url = match[2]
const description = match[3] ? match[3].trim() : ''
const description = match[3] ? match[3].trim().replace(/ {2,}/g, '') : ''

return { name, url, description }
return {
name,
url,
description: description.charAt(0).toUpperCase() + description.slice(1),
}
})
return plugins
}

async function extractEcosystemFromFile(file) {
let data
try {
data = await fs.readFile(file, 'utf8')
} catch (e) {
if (e.code === 'ENOENT') {
const legacyEcosystemFile = file.replace('Guides', '')
data = await fs.readFile(legacyEcosystemFile, 'utf8')
}
}

const content = data.toString()
const corePluginsContent = content.split('#### [Core](#core)\n')[1].split('#### [Community](#community)')[0]

const communityPluginsContent = content.split('#### [Core](#core)\n')[1].split('#### [Community](#community)')[1]

const plugins = {
corePlugins: extractPlugins(corePluginsContent),
communityPlugins: extractPlugins(communityPluginsContent),
}

return { plugins }
}

async function process() {
const plugins = await extractEcosystemFromFile('Ecosystem.md')
console.log(JSON.stringify(plugins['plugins']['communityPlugins']))
}

process()
73 changes: 73 additions & 0 deletions scripts/build-static-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const { promises: fs } = require('fs')
const path = require('path')
const yaml = require('yaml')

const log = require('pino')({
level: process.env.LOG_LEVEL || 'debug',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
},
},
})

execute([
// ##### Team
{
staticDataFile: path.join(__dirname, '../static/data/team.yml'),
outputFile: path.join(__dirname, '../static/generated/team.json'),
sideEffect: (data) => {
// Sort alphabetically by `sortname`
data.forEach((group) => {
group.people.sort((a, b) => {
return a.sortname.localeCompare(b.sortname)
})
})
},
},

// ##### Organizations
{
staticDataFile: path.join(__dirname, '../static/data/organisations.yml'),
outputFile: path.join(__dirname, '../static/generated/organisations.json'),
sideEffect: (data) => {
// Sort alphabetically by `name` lowercase
data.sort((a, b) => {
return a.name.toLowerCase().localeCompare(b.name.toLowerCase())
})
},
},

// ##### Acknowledgements
{
staticDataFile: path.join(__dirname, '../static/data/acknowledgements.yml'),
outputFile: path.join(__dirname, '../static/generated/acknowledgements.json'),
sideEffect: () => {
// As-is, no sorting
},
},
])

async function execute(filePipeline) {
for (const file of filePipeline) {
await processFile(file)
}
}

async function processFile({ staticDataFile, outputFile, sideEffect }) {
const dataFile = await yamlToJSON(staticDataFile)
log.debug('Read the file: %s', staticDataFile)

sideEffect(dataFile)

await fs.writeFile(outputFile, JSON.stringify(dataFile, null, 2))
log.info('Wrote the file to %s', outputFile)
}

async function yamlToJSON(yamlFile) {
const content = await fs.readFile(yamlFile, 'utf8')
return yaml.parse(content)
}
4 changes: 3 additions & 1 deletion scripts/build-website.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ node ./scripts/process-releases.js

####### Data Generation Phase

# TODO node build-plugin-list.js
node ./scripts/build-plugin-list.js
node ./scripts/download-benchmarks.js
node ./scripts/build-static-data.js

####### Build Phase

Expand Down
Loading