From 0da978959040b6337bc9f889100943d2c89b10dd Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 27 Feb 2022 14:43:03 +0000 Subject: [PATCH] chore: move scripts to mjs --- .github/workflows/sync.yml | 2 +- api/{build.js => build.mjs} | 6 ++-- api/metadata.json | 2 +- api/package.json | 2 +- api/parser.ts | 2 +- scripts/{global.js => global.mjs} | 16 ++++------- scripts/{publish.js => publish.mjs} | 43 ++++++++++++----------------- 7 files changed, 30 insertions(+), 43 deletions(-) rename api/{build.js => build.mjs} (86%) rename scripts/{global.js => global.mjs} (76%) rename scripts/{publish.js => publish.mjs} (76%) diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml index f71961db..c7fd2026 100644 --- a/.github/workflows/sync.yml +++ b/.github/workflows/sync.yml @@ -22,6 +22,6 @@ jobs: run: yarn - name: Update blogs on dev.to - run: node scripts/publish.js + run: node scripts/publish.mjs env: DEVTO_TOKEN: ${{ secrets.DEVTO_TOKEN }} diff --git a/api/build.js b/api/build.mjs similarity index 86% rename from api/build.js rename to api/build.mjs index 3893c1c9..65cfd27d 100644 --- a/api/build.js +++ b/api/build.mjs @@ -1,10 +1,10 @@ -const grayMatter = require('gray-matter') +import grayMatter from 'gray-matter' -const { +import { iterateOnDirectory, getMatchOrReturn, writeFile, -} = require('../scripts/global') +} from '../scripts/global.mjs' const metadata = {} diff --git a/api/metadata.json b/api/metadata.json index 1e9d49b9..82bc1e71 100644 --- a/api/metadata.json +++ b/api/metadata.json @@ -1 +1 @@ -{"metadata":{"good-ux-and-giving-birth":{"title":"Good UX and giving birth","date":"2019-12-16","tags":["personal","ux","brand"],"description":"A review of Freya, an app that's head-and-shoulders above the rest."},"introduction":{"title":"A brief introduction","date":"2019-12-12","tags":["personal"],"description":"After a long time without a blog, it's time to put pen to paper again."},"serverless-functions-nuxt-zeit-now":{"title":"Using serverless functions in Nuxt on ZEIT Now","date":"2019-12-19","tags":["nuxt","now","serverless","zeit"],"description":"If you are using ZEIT Now to host your Nuxt application, you can use the same entrypoints for Nuxt server middleware and your Now serverless functions. Here's how."}}} \ No newline at end of file +{"metadata":{"building-your-own-vue-rich-text-component":{"title":"Building your own Vue rich text component","date":"2020-3-1","tags":["vue","components","rich text"],"description":"One way you might build your own Vue rich text component."},"good-ux-and-giving-birth":{"title":"Good UX and giving birth","date":"2019-12-16","tags":["personal","ux","brand"],"description":"A review of Freya, an app that's head-and-shoulders above the rest."},"introduction":{"title":"A brief introduction","date":"2019-12-12","tags":["personal"],"description":"After a long time without a blog, it's time to put pen to paper again."},"serverless-functions-nuxt-zeit-now":{"title":"Using serverless functions in Nuxt on ZEIT Now","date":"2019-12-19","tags":["nuxt","now","serverless","zeit"],"description":"If you are using ZEIT Now to host your Nuxt application, you can use the same entrypoints for Nuxt server middleware and your Now serverless functions. Here's how."}}} \ No newline at end of file diff --git a/api/package.json b/api/package.json index 4cb69b54..278fb22d 100644 --- a/api/package.json +++ b/api/package.json @@ -4,7 +4,7 @@ "main": "index.js", "license": "MIT", "scripts": { - "now-build": "node build.js" + "now-build": "node build.mjs" }, "dependencies": { "chrome-aws-lambda": "10.1.0", diff --git a/api/parser.ts b/api/parser.ts index d9479298..49e7a644 100644 --- a/api/parser.ts +++ b/api/parser.ts @@ -1,6 +1,6 @@ import { IncomingMessage } from 'http' -import { getMatchOrReturn } from '../scripts/global' +import { getMatchOrReturn } from '../scripts/global.mjs' import { metadata } from './metadata.json' diff --git a/scripts/global.js b/scripts/global.mjs similarity index 76% rename from scripts/global.js rename to scripts/global.mjs index 201a49b4..0fddfef5 100644 --- a/scripts/global.js +++ b/scripts/global.mjs @@ -1,5 +1,5 @@ -const path = require('path') -const fs = require('fs') +import path from 'path' +import fs from 'fs' /** * @type {(dir: string, callback: (path: string) => any)} walkDir @@ -15,7 +15,7 @@ function walkDir(dir, callback) { /** * @type {(directory: string, callback: (path: string, contents: string) => any)} iterateOnDirectory */ -function iterateOnDirectory(directory, callback) { +export function iterateOnDirectory(directory, callback) { walkDir(directory, path => callback(path, fs.existsSync(path) && fs.readFileSync(path, 'utf8')) ) @@ -24,7 +24,7 @@ function iterateOnDirectory(directory, callback) { /** * @type {(haystack: string, needle: string | RegExp, index: number ) => string} getMatchOrReturn */ -function getMatchOrReturn(haystack, needle, index = 0) { +export function getMatchOrReturn(haystack, needle, index = 0) { const matches = haystack.match(needle) if (!matches || !matches.length || matches.length < index + 1) return '' return matches[index] @@ -33,14 +33,8 @@ function getMatchOrReturn(haystack, needle, index = 0) { /** * @type {(message: string, filename: string, contents: string) => void} writeFile */ -function writeFile(_message, filename, contents) { +export function writeFile(_message, filename, contents) { if (fs && fs.writeFileSync) { return fs.writeFileSync(filename, contents) } } - -module.exports = { - iterateOnDirectory, - getMatchOrReturn, - writeFile, -} diff --git a/scripts/publish.js b/scripts/publish.mjs similarity index 76% rename from scripts/publish.js rename to scripts/publish.mjs index 9baefcf6..b950158c 100644 --- a/scripts/publish.js +++ b/scripts/publish.mjs @@ -1,8 +1,8 @@ /* eslint-disable camelcase */ -const axios = require('axios') +import { $fetch } from 'ohmyfetch' -const { iterateOnDirectory, getMatchOrReturn } = require('./global') +import { iterateOnDirectory, getMatchOrReturn } from './global.mjs' const url = 'https://dev.to/api' const token = process.env.DEVTO_TOKEN || 'CYgR6zbcVgtKDRkawFYZKrCT' @@ -24,21 +24,20 @@ async function getMarkdownArticles() { return articles } -async function getArticles() { - const { data } = await axios.get(`${url}/articles/me`, { +function getArticles() { + return $fetch(`${url}/articles/me`, { headers: { 'api-key': token, }, }) - return data } // eslint-disable-next-line async function postArticle({ title, body_markdown, canonical_url }) { try { - const { data } = await axios.post( - `${url}/articles`, - { + return await $fetch(`${url}/articles`, { + method: 'POST', + body: { article: { published: true, title, @@ -46,13 +45,10 @@ async function postArticle({ title, body_markdown, canonical_url }) { body_markdown, }, }, - { - headers: { - 'api-key': token, - }, - } - ) - return data + headers: { + 'api-key': token, + }, + }) } catch (e) { console.log(e) } @@ -61,9 +57,9 @@ async function postArticle({ title, body_markdown, canonical_url }) { // eslint-disable-next-line async function updateArticle(id, { title, body_markdown, canonical_url }) { try { - const { data } = await axios.put( - `${url}/articles/${id}`, - { + return await $fetch(`${url}/articles/${id}`, { + method: 'PUT', + body: { article: { published: true, title, @@ -71,13 +67,10 @@ async function updateArticle(id, { title, body_markdown, canonical_url }) { canonical_url, }, }, - { - headers: { - 'api-key': token, - }, - } - ) - return data + headers: { + 'api-key': token, + }, + }) } catch (e) { console.log(e) } -- knotmirror 0.0.1