Skip to content

Commit 17f2204

Browse files
setup auto publish
1 parent 51acb82 commit 17f2204

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

.github/workflows/publish-to-dev-to.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ jobs:
44
build:
55
runs-on: ubuntu-latest
66
steps:
7-
- uses: actions/checkout@v2
7+
- name: Checkout Code
8+
uses: actions/checkout@v2
9+
with:
10+
fetch-depth: 3
11+
812
- uses: actions/setup-node@v1
913
with:
1014
node-version: '12.x'
1115
registry-url: 'https://registry.npmjs.org'
12-
- run: npm install -g axios
16+
- run: npm install -g @github-docs/frontmatter node-fetch
17+
- run: npm link @github-docs/frontmatter node-fetch
1318
- run: node dev-to.js
1419
env:
1520
DEV_TOKEN: ${{ secrets.DEV_TO }}

dev-to.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
1-
// const axios = require('axios');
2-
console.log('hello world')
3-
console.log(`The Dev token is ${process.env.DEV_TOKEN}`)
1+
const { execSync } = require('child_process')
2+
const fs = require('fs')
3+
const fetch = require('node-fetch')
4+
const frontmatter = require('@github-docs/frontmatter')
5+
6+
const publishArticle = (path) => {
7+
try {
8+
const markdown = fs.readFileSync(`./${path}`, 'utf8')
9+
const { data } = frontmatter(markdown)
10+
11+
if (data.published) {
12+
console.log(`Article ${data.title} published. Deploying to dev.to`)
13+
14+
const body = {
15+
article: {
16+
body_markdown: markdown,
17+
},
18+
}
19+
20+
fetch('https://dev.to/api/articles', {
21+
method: 'post',
22+
body: JSON.stringify(body),
23+
headers: {
24+
'Content-Type': 'application/json',
25+
'api-key': process.env.DEV_TOKEN,
26+
},
27+
})
28+
.then((response) => response.json())
29+
.then((json) => console.log(json))
30+
} else {
31+
console.log(`Article ${data.title} NOT published. Skipping.`)
32+
}
33+
} catch (err) {
34+
console.error(err)
35+
}
36+
}
37+
38+
const files = execSync(
39+
'git diff --name-only HEAD HEAD~1 -- ./content/articles/'
40+
)
41+
.toString()
42+
.split('\n')
43+
.filter((f) => f.length > 0)
44+
.map((f) => f.trim())
45+
46+
if (files.length > 0) {
47+
files.forEach((f) => publishArticle(f))
48+
} else {
49+
console.log('No Articles.')
50+
}

0 commit comments

Comments
 (0)