Skip to content

Commit 6d71d5e

Browse files
authored
Merge pull request #18 from iterative/auto-generate-commands
2 parents 8964300 + 1faaf70 commit 6d71d5e

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Update tool commands list for Prismjs
2+
on:
3+
schedule:
4+
- cron: '0 10 * * 1-5'
5+
6+
jobs:
7+
fetch-commands:
8+
name: Run script to fetch commands
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-node@v3
13+
with:
14+
cache: 'yarn'
15+
- uses: iterative/setup-cml@v1
16+
17+
- name: Install required package
18+
run: yarn workspace @dvcorg/gatsby-theme-iterative add isomorphic-fetch
19+
20+
- name: Fetch commands
21+
run: yarn get-commands all
22+
23+
- name: Check
24+
run: |
25+
if [[ `git status packages/gatsby-theme-iterative/config/prismjs --porcelain` ]]; then
26+
cml pr create packages/gatsby-theme-iterative/config/prismjs --title "Update tool commands"
27+
else
28+
echo "No changes!"
29+
fi
30+
env:
31+
REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"build": "yarn workspace example build",
1616
"serve": "yarn workspace example serve",
1717
"develop": "yarn workspace example develop",
18+
"get-commands": "yarn workspace @dvcorg/gatsby-theme-iterative get-commands",
1819
"prepare": "husky install"
1920
},
2021
"workspaces": [
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const fs = require('fs')
2+
require('isomorphic-fetch')
3+
4+
const args = process.argv.slice(2)
5+
const tool = args[0] || 'dvc'
6+
7+
const repoList = {
8+
dvc: { repo: 'dvc.org', branch: 'main' },
9+
cml: { repo: 'cml.dev', branch: 'master' },
10+
mlem: { repo: 'mlem.ai', branch: 'main' }
11+
}
12+
13+
const paths = ['command-reference', 'cli-reference', 'ref']
14+
15+
const getUrl = (repo, branch = 'main') => {
16+
return `https://raw.githubusercontent.com/iterative/${repo}/${branch}/content/docs/sidebar.json`
17+
}
18+
19+
const writeCommandsToFile = async (commands, tool) => {
20+
const file = `${__dirname}/${tool}-commands.js`
21+
22+
const start = 'module.exports = [\n'
23+
const end = ']\n'
24+
const content =
25+
start + `${commands.map(cmd => ` '${cmd}'`).join(',\n')}\n` + end
26+
27+
fs.writeFileSync(file, content)
28+
}
29+
30+
const getCommands = async tool => {
31+
const url = getUrl(repoList[tool].repo, repoList[tool].branch)
32+
const res = await fetch(url)
33+
const sidebar = await res.json()
34+
const cmdRef = sidebar.find(item => paths.includes(item.slug))
35+
if (!cmdRef) {
36+
throw new Error(`Could not find command reference in sidebar.json`)
37+
}
38+
const commands = []
39+
cmdRef.children.forEach(item => {
40+
const { label, children } = item
41+
if (Array.isArray(children)) {
42+
children.forEach(subitem => {
43+
const { label } = subitem
44+
commands.push(label)
45+
})
46+
}
47+
commands.push(label)
48+
})
49+
50+
writeCommandsToFile(commands, tool)
51+
}
52+
53+
switch (tool) {
54+
case 'dvc':
55+
case 'cml':
56+
case 'mlem':
57+
{
58+
getCommands(tool).catch(err => {
59+
console.error(err)
60+
})
61+
}
62+
break
63+
case 'all': {
64+
// dvc
65+
getCommands('dvc').catch(err => {
66+
console.error(err)
67+
})
68+
// cml
69+
getCommands('cml').catch(err => {
70+
console.error(err)
71+
})
72+
// mlem
73+
getCommands('mlem').catch(err => {
74+
console.error(err)
75+
})
76+
}
77+
default:
78+
break
79+
}

packages/gatsby-theme-iterative/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"types": "src/typings.d.ts",
77
"author": "Roger Parent (@rogermparent)",
88
"license": "MIT",
9+
"scripts": {
10+
"get-commands": "node config/prismjs/get-commands.js"
11+
},
912
"dependencies": {
1013
"@reach/portal": "^0.17.0",
1114
"@reach/router": "^1.3.4",

0 commit comments

Comments
 (0)