|
| 1 | +/* |
| 2 | + * You can test this using act |
| 3 | + * run act -s PRIVATE_DEVTO=yourapikey |
| 4 | + */ |
| 5 | + |
| 6 | +import { Glob } from 'glob'; |
| 7 | +import matter from 'gray-matter'; |
| 8 | +import fs from 'fs'; |
| 9 | + |
| 10 | +const TYPE = 'podcast'; |
| 11 | +const BASE = `../src/routes/(content-single)/(non-course)/${TYPE}/`; |
| 12 | +const g = new Glob(`${BASE}**/*.md`, {}); |
| 13 | + |
| 14 | +const delay = async (ms) => new Promise((res) => setTimeout(res, ms)); |
| 15 | + |
| 16 | +const addArticle = async (input) => { |
| 17 | + return fetch('https://api.hashnode.com/', { |
| 18 | + method: 'POST', |
| 19 | + headers: { |
| 20 | + authorization: process.env.PRIVATE_HASHNODE, |
| 21 | + 'Content-Type': 'application/json' |
| 22 | + }, |
| 23 | + body: JSON.stringify({ |
| 24 | + operationName: 'createPublication', |
| 25 | + query: `mutation createPublication($input: CreateStoryInput!) { |
| 26 | + createPublicationStory( |
| 27 | + publicationId: "60242f8180da6c44eadf775b" |
| 28 | + input: $input |
| 29 | + ) { |
| 30 | + message |
| 31 | + post { |
| 32 | + _id |
| 33 | + title |
| 34 | + slug |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + `, |
| 39 | + variables: { |
| 40 | + input: { |
| 41 | + isPartOfPublication: { |
| 42 | + publicationId: '60242f8180da6c44eadf775b' |
| 43 | + }, |
| 44 | + ...input |
| 45 | + } |
| 46 | + } |
| 47 | + }) |
| 48 | + }); |
| 49 | +}; |
| 50 | + |
| 51 | +for await (const file of g) { |
| 52 | + const mdFile = fs.readFileSync(file, { encoding: 'utf8', flag: 'r' }); |
| 53 | + const { data, content } = await matter(mdFile); // data has frontmatter, code is html |
| 54 | + const fm = data; |
| 55 | + if (!fm) continue; |
| 56 | + // TODO: We might need to add a check on cononical if this page is already in dev.to |
| 57 | + if ( |
| 58 | + fm?.slug && |
| 59 | + fm.slug === 'angular-17-cypress-testing' && |
| 60 | + fm?.title && |
| 61 | + fm?.cover && |
| 62 | + fm?.published === 'published' && |
| 63 | + new Date(fm?.start) < new Date() && |
| 64 | + !fm?.hasnode |
| 65 | + ) { |
| 66 | + console.log('Adding', { slug: fm?.slug, hashnode: fm?.hasnode }); |
| 67 | + |
| 68 | + try { |
| 69 | + console.log('addArticle to hasnode'); |
| 70 | + |
| 71 | + // const response = await addArticle( |
| 72 | + |
| 73 | + const finalContent = ` |
| 74 | +Original: https://codingcat.dev/${TYPE}/${fm.slug} |
| 75 | +${fm?.spotify ? '%[' + fm.spotify + ']' : ''} |
| 76 | +${fm?.youtube ? '%[' + fm.youtube + ']' : ''} |
| 77 | +${content}`; |
| 78 | + const response = await addArticle({ |
| 79 | + title: fm.title, |
| 80 | + subtitle: fm?.excerpt || '', |
| 81 | + slug: `${TYPE}-${fm.slug}`, |
| 82 | + contentMarkdown: finalContent, |
| 83 | + coverImageURL: fm.cover, |
| 84 | + isRepublished: { |
| 85 | + originalArticleURL: `https://codingcat.dev/${TYPE}/${fm.slug}` |
| 86 | + }, |
| 87 | + tags: [ |
| 88 | + { |
| 89 | + _id: '56744722958ef13879b950d3', |
| 90 | + name: 'podcast', |
| 91 | + slug: 'podcast' |
| 92 | + }, |
| 93 | + { |
| 94 | + _id: '56744721958ef13879b94cad', |
| 95 | + name: 'JavaScript', |
| 96 | + slug: 'javascript' |
| 97 | + }, |
| 98 | + { |
| 99 | + _id: '56744722958ef13879b94f1b', |
| 100 | + name: 'Web Development', |
| 101 | + slug: 'web-development' |
| 102 | + }, |
| 103 | + { |
| 104 | + _id: '56744723958ef13879b955a9', |
| 105 | + name: 'Beginner Developers', |
| 106 | + slug: 'beginners' |
| 107 | + } |
| 108 | + ] |
| 109 | + }); |
| 110 | + |
| 111 | + console.log('addArticle result:', response.status); |
| 112 | + if (response?.error) console.error('error', response.error); |
| 113 | + // Get new devto url and update |
| 114 | + if (response.status === 200) { |
| 115 | + const json = await response.json(); |
| 116 | + const hashnodeSlug = json?.data?.createPublicationStory?.post?.slug; |
| 117 | + |
| 118 | + if (!hashnodeSlug) { |
| 119 | + console.error('hasnode url missing'); |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + if (hashnodeSlug) { |
| 124 | + console.log('Updating', file, { hashnode: json.url }); |
| 125 | + const newMdFile = matter.stringify(content, { |
| 126 | + ...data, |
| 127 | + hashnode: hashnodeSlug |
| 128 | + }); |
| 129 | + fs.writeFileSync(file, newMdFile, { encoding: 'utf8' }); |
| 130 | + } |
| 131 | + } |
| 132 | + // Avoid 429 |
| 133 | + await delay(process.env?.SYNDICATE_DELAY ? Integer(process.env.SYNDICATE_DELAY) : 10000); |
| 134 | + } catch (error) { |
| 135 | + console.error(error); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments