- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev-to.js
More file actions
Latest commit
50 lines (44 loc) · 1.21 KB
/
Copy pathdev-to.js
File metadata and controls
50 lines (44 loc) · 1.21 KB
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
const{ execSync }=require('child_process')
constfs=require('fs')
constfetch=require('node-fetch')
constfrontmatter=require('@github-docs/frontmatter')
constpublishArticle=(path)=>{
try{
constmarkdown=fs.readFileSync(`./${path}`,'utf8')
const{ data }=frontmatter(markdown)
if(data.published){
console.log(`Article ${data.title} published. Deploying to dev.to`)
constbody={
article: {
body_markdown: markdown,
},
}
fetch('https://dev.to/api/articles',{
method: 'post',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
'api-key': process.env.DEV_TOKEN,
},
})
.then((response)=>response.json())
.then((json)=>console.log(json))
}else{
console.log(`Article ${data.title} NOT published. Skipping.`)
}
}catch(err){
console.error(err)
}
}
constfiles=execSync(
'git diff --name-only HEAD HEAD~1 -- ./content/articles/'
)
.toString()
.split('\n')
.filter((f)=>f.length>0)
.map((f)=>f.trim())
if(files.length>0){
files.forEach((f)=>publishArticle(f))
}else{
console.log('No Articles.')
}