diff --git a/README.md b/README.md index f90a58f..88f9d63 100644 --- a/README.md +++ b/README.md @@ -14,23 +14,67 @@ For every article it compares the content and then: ## Todo -[] Add support for canonical urls -[] Add support for images +[ ] Add support for canonical urls + +[ ] Add support for images ## Supported blog platforms - DevTo +## Quick Start + +1. Make sure you have the correct folder structure + +``` +- blog-articles + - article one + - index.md + - article two + - index.md +``` + +2. Add a masterid to the articles you want to sync + +```markdown +--- +title: "Sample Blog Post" +published: false +description: "This is an example" +tags: discuss, javascript +masterid: 123 +--- + +Lorem Ipsum.. +``` + +3. Run Papertown with a valid api key + +``` +papertown sync --devtoApiKey yourdevtoapikey +``` + ## Configuration -### 1: Use a .env file +| Config | Env | Args | +| ------------- | ------------- | ----------- | +| Root Folder | ROOT_FOLDER | rootFolder | +| API Key DevTo | DEVTO_API_KEY | devtoApiKey | + +### 1: Use args + +``` +papertown sync --devtoApiKey yourdevtoapikey +``` + +### 2: Use a .env file -```` +``` DEVTO_API_KEY="yourdevtoapikey" -```` +``` -### 2: Set env variables before running problogger +### 3: Set env variables before running papertown -```` -DEVTO_API_KEY="yourdevtoapikey" problogger sync -```` +``` +DEVTO_API_KEY="yourdevtoapikey" papertown sync +``` diff --git a/package-lock.json b/package-lock.json index b010da8..e0a4d15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "papertown", - "version": "1.0.4", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b5ecdc0..e1a1eff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "papertown", - "version": "1.0.4", + "version": "1.1.0", "description": "", "main": "dist/index.js", "scripts": { diff --git a/package/LICENSE b/package/LICENSE deleted file mode 100644 index 05787c4..0000000 --- a/package/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2019 Christian Kohler - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/package/README.md b/package/README.md deleted file mode 100644 index f90a58f..0000000 --- a/package/README.md +++ /dev/null @@ -1,36 +0,0 @@ -# Papertown - -Papertown syncs you personal markdown blog with blog platforms like dev.to. - -## How does it work - -Papertown sync only articles with a masterid in the frontmatter. The masterid helps papertown identify articles. - -For every article it compares the content and then: - - - if article doesn't exist -> create - - if article hasn't changed -> do nothing - - if article has changed -> update - -## Todo - -[] Add support for canonical urls -[] Add support for images - -## Supported blog platforms - -- DevTo - -## Configuration - -### 1: Use a .env file - -```` -DEVTO_API_KEY="yourdevtoapikey" -```` - -### 2: Set env variables before running problogger - -```` -DEVTO_API_KEY="yourdevtoapikey" problogger sync -```` diff --git a/package/package.json b/package/package.json deleted file mode 100644 index 52923de..0000000 --- a/package/package.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "papertown", - "version": "1.0.1", - "description": "", - "main": "dist/index.js", - "scripts": { - "start": "ncc run src/index.ts", - "build": "ncc build src/index.ts -m", - "test": "echo \"Error: no test specified\" && exit 0" - }, - "files": ["dist/*"], - "author": "", - "license": "ISC", - "devDependencies": { - "@types/convict": "^4.2.1", - "@types/node": "^12.6.8", - "@zeit/ncc": "^0.20.4", - "axios": "^0.19.0", - "chalk": "^2.4.2", - "convict": "^5.2.0", - "dotenv": "^8.2.0", - "frontmatter": "0.0.3" - }, - "dependencies": {} -} diff --git a/src/config.ts b/src/config.ts index b398324..fc7f025 100644 --- a/src/config.ts +++ b/src/config.ts @@ -4,17 +4,19 @@ import * as dotenv from "dotenv"; dotenv.config(); export const config = convict({ - articlesFolder: { + rootFolder: { doc: "Root folder of articles", format: String, default: "blog-articles", - env: "ARTICLES_FOLDER" + env: "ROOT_FOLDER", + arg: "rootFolder" }, devtoApiKey: { doc: "DevTo API Key", format: String, default: "", - env: "DEVTO_API_KEY" + env: "DEVTO_API_KEY", + arg: "devtoApiKey" } }) .validate() diff --git a/src/index.ts b/src/index.ts index e4513fc..16932d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,18 +1,63 @@ #!/usr/bin/env node -import { getMasterArticlesWithMasterId } from "./masterArticles/master-articles"; -import { MasterArticle } from "./masterArticles/MasterArticle"; +import { + getMasterArticlesWithMasterId, + rootFolderExists, + haveDuplicateMasterIDs +} from "./masterArticles/master-articles"; import { syncBlogPlatformsWithMasters } from "./sync/sync"; import { error } from "./utils/logger"; import { config } from "./config"; +import { log } from "util"; + +const usage = ` +Usage + $ papertown + +Commands + sync Runs a sync + +Options + --rootFolder Change the root folder + --devtoApiKey Set the Api Key for DevTo + +Examples + $ foo sync --devtoApiKey asdf234asdf +`; async function main() { + /** + * Check command + */ + const command = process.argv[2]; + const availableCommands = ["sync"]; + if (availableCommands.indexOf(command) === -1) { + log(usage); + return; + } + + /** + * Stop if root folder does not exist + */ + if (!(await rootFolderExists(config.rootFolder))) { + error(`Root folder not found: ${config.rootFolder}`); + return; + } + /** * Get all articles with an masterid in the frontmatter */ const masterArticles = await getMasterArticlesWithMasterId( - config.articlesFolder + config.rootFolder ); + /** + * Stop if no master articles were found + */ + if (masterArticles.length === 0) { + error("No articles found with masterid"); + return; + } + /** * Stop if a masterid is used twice */ @@ -27,12 +72,4 @@ async function main() { await syncBlogPlatformsWithMasters(masterArticles, config); } -/** - * Check if a masterid is used more than once - */ -function haveDuplicateMasterIDs(masterArticles: MasterArticle[]) { - const allIds = masterArticles.map(article => article.frontmatter.masterid); - return new Set(allIds).size !== allIds.length; -} - (async () => await main())(); diff --git a/src/masterArticles/master-articles.ts b/src/masterArticles/master-articles.ts index 45acdc8..1e9c9c4 100644 --- a/src/masterArticles/master-articles.ts +++ b/src/masterArticles/master-articles.ts @@ -6,6 +6,7 @@ import * as frontmatterParser from "frontmatter"; const readDir = util.promisify(fs.readdir); const readFile = util.promisify(fs.readFile); +const exists = util.promisify(fs.exists); export async function getMasterArticlesWithMasterId( rootFolder: string @@ -14,6 +15,18 @@ export async function getMasterArticlesWithMasterId( return allMasterArticles.filter(hasMasterId); } +export async function rootFolderExists(rootFolder) { + return await exists(rootFolder); +} + +/** + * Check if a masterid is used more than once + */ +export function haveDuplicateMasterIDs(masterArticles: MasterArticle[]) { + const allIds = masterArticles.map(article => article.frontmatter.masterid); + return new Set(allIds).size !== allIds.length; +} + /** * Structure has to be: * - rootFolder diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 7a1a2d2..1d5d078 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -1,5 +1,5 @@ import chalk from "chalk"; -const appName = "ProBlogger:"; +const appName = "Papertown:"; export function error(message: string) { console.log(`${chalk.green(appName)} ${chalk.red(message)}`);