-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d6c789
commit 8cbe7c1
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* You can test this using act | ||
* run act -s PRIVATE_DEVTO=yourapikey | ||
*/ | ||
|
||
import { Glob } from 'glob'; | ||
import matter from 'gray-matter'; | ||
import fs from 'fs'; | ||
|
||
const TYPE = 'post'; | ||
const BASE = `../src/routes/(content-single)/(non-course)/${TYPE}/`; | ||
const g = new Glob(`${BASE}**/*.md`, {}); | ||
|
||
const delay = async (ms) => new Promise((res) => setTimeout(res, ms)); | ||
|
||
const addArticle = async (input) => { | ||
return fetch('https://api.hashnode.com/', { | ||
method: 'POST', | ||
headers: { | ||
authorization: '2b02d999-9e2d-409a-a2b4-7a1bcbbc58d5', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
operationName: 'createPublication', | ||
query: `mutation createPublication($input: CreateStoryInput!) { | ||
createPublicationStory( | ||
publicationId: "60242f8180da6c44eadf775b" | ||
input: $input | ||
) { | ||
message | ||
post { | ||
_id | ||
title | ||
slug | ||
} | ||
} | ||
} | ||
`, | ||
variables: { | ||
input: { | ||
isPartOfPublication: { | ||
publicationId: '60242f8180da6c44eadf775b' | ||
}, | ||
...input | ||
} | ||
} | ||
}) | ||
}); | ||
}; | ||
|
||
for await (const file of g) { | ||
const mdFile = fs.readFileSync(file, { encoding: 'utf8', flag: 'r' }); | ||
const { data, content } = await matter(mdFile); // data has frontmatter, code is html | ||
const fm = data; | ||
if (!fm) continue; | ||
// TODO: We might need to add a check on cononical if this page is already in dev.to | ||
if ( | ||
fm?.slug && | ||
fm?.title && | ||
fm?.cover && | ||
fm?.published === 'published' && | ||
new Date(fm?.start) < new Date() && | ||
!fm?.hasnode | ||
) { | ||
console.log('Adding', { slug: fm?.slug, hashnode: fm?.hasnode }); | ||
|
||
try { | ||
console.log('addArticle to hasnode'); | ||
|
||
// const response = await addArticle( | ||
|
||
const finalContent = ` | ||
Original: https://codingcat.dev/${TYPE}/${fm.slug} | ||
${fm?.spotify ? '%[' + fm.spotify + ']' : ''} | ||
${fm?.youtube ? '%[' + fm.youtube + ']' : ''} | ||
${content}`; | ||
const response = await addArticle({ | ||
title: fm.title, | ||
subtitle: fm?.excerpt || '', | ||
slug: `${TYPE}-${fm.slug}`, | ||
contentMarkdown: finalContent, | ||
coverImageURL: fm.cover, | ||
isRepublished: { | ||
originalArticleURL: `https://codingcat.dev/${TYPE}/${fm.slug}` | ||
}, | ||
tags: [ | ||
{ | ||
_id: '56744721958ef13879b94cad', | ||
name: 'JavaScript', | ||
slug: 'javascript' | ||
}, | ||
{ | ||
_id: '56744722958ef13879b94f1b', | ||
name: 'Web Development', | ||
slug: 'web-development' | ||
}, | ||
{ | ||
_id: '56744723958ef13879b955a9', | ||
name: 'Beginner Developers', | ||
slug: 'beginners' | ||
} | ||
] | ||
}); | ||
|
||
console.log('addArticle result:', response.status); | ||
if (response?.error) console.error('error', response.error); | ||
// Get new devto url and update | ||
if (response.status === 201) { | ||
const json = await response.json(); | ||
if (json?.url) { | ||
console.log('Updating', file, { devto: json.url }); | ||
const newMdFile = matter.stringify(content, { | ||
...data, | ||
devto: json.url | ||
}); | ||
fs.writeFileSync(file, newMdFile, { encoding: 'utf8' }); | ||
} | ||
} | ||
// Avoid 429 | ||
await delay(process.env?.SYNDICATE_DELAY ? Integer(process.env.SYNDICATE_DELAY) : 10000); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8cbe7c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
codingcat-dev-sveltefirebase-com – ./apps/sveltefirebase-com
codingcat-dev-sveltefirebase-com.vercel.app
sveltefirebase.com
codingcat-dev-sveltefirebase-com-git-main-coding-cat-dev.vercel.app
codingcat-dev-sveltefirebase-com-coding-cat-dev.vercel.app
8cbe7c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
codingcat-dev – ./apps/codingcatdev
codingcat-dev-git-main-coding-cat-dev.vercel.app
codingcat-dev-coding-cat-dev.vercel.app
codingcat.dev