|
| 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 = 'post'; |
| 11 | +const BASE = `../src/routes/(content-single)/(non-course)/${TYPE}/`; |
| 12 | +const g = new Glob(`${BASE}**/*.md`, {}); |
| 13 | + |
| 14 | +const addArticle = async (data) => { |
| 15 | + return fetch('https://dev.to/api/articles/', { |
| 16 | + method: 'POST', |
| 17 | + headers: { |
| 18 | + 'api-key': process.env.PRIVATE_DEVTO, |
| 19 | + 'Content-Type': 'application/json' |
| 20 | + }, |
| 21 | + body: JSON.stringify(data) |
| 22 | + }); |
| 23 | +}; |
| 24 | + |
| 25 | +for await (const file of g) { |
| 26 | + const mdFile = fs.readFileSync(file, { encoding: 'utf8', flag: 'r' }); |
| 27 | + const { data, content } = await matter(mdFile); // data has frontmatter, code is html |
| 28 | + const fm = data; |
| 29 | + if (!fm) continue; // Skip if missing frontmattter |
| 30 | + if ( |
| 31 | + fm?.slug && |
| 32 | + fm?.slug === 'the-myth-of-just-press-record-in-podcasting' && |
| 33 | + fm?.title && |
| 34 | + fm?.cover && |
| 35 | + fm?.published === 'published' && |
| 36 | + new Date(fm?.start) < new Date() && |
| 37 | + !fm?.devto |
| 38 | + ) { |
| 39 | + console.log('Adding', { slug: fm?.slug, devto: fm?.devto }); |
| 40 | + |
| 41 | + try { |
| 42 | + console.log('addArticle to devto'); |
| 43 | + const response = await addArticle({ |
| 44 | + article: { |
| 45 | + title: fm.title, |
| 46 | + published: false, |
| 47 | + tags: ['podcast', 'webdev', 'javascript', 'beginners'], |
| 48 | + main_image: fm.cover.replace('upload/', 'upload/b_rgb:5e1186,c_pad,w_1000,h_420/'), |
| 49 | + canonical_url: `https://codingcat.dev/${TYPE}/${fm.slug}`, |
| 50 | + description: fm?.excerpt || '', |
| 51 | + organization_id: '1009', |
| 52 | + body_markdown: content |
| 53 | + } |
| 54 | + }); |
| 55 | + console.log('addArticle result:', response.status); |
| 56 | + |
| 57 | + // Get new devto url and update |
| 58 | + if (response.status === 201) { |
| 59 | + const { url } = response.json(); |
| 60 | + if (url) { |
| 61 | + const newMdFile = matter.stringify(content, { |
| 62 | + ...data, |
| 63 | + devto: url |
| 64 | + }); |
| 65 | + console.log('Updating', file, { devto: url }); |
| 66 | + fs.writeFileSync(file, newMdFile, { encoding: 'utf8', flag: 'r' }); |
| 67 | + } |
| 68 | + } |
| 69 | + } catch (error) { |
| 70 | + console.error(error); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments