Skip to content

Commit

Permalink
feat: improve doc and cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kohler authored and Christian Kohler committed Nov 8, 2019
1 parent 9c360f9 commit e31f3fd
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 108 deletions.
62 changes: 53 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "papertown",
"version": "1.0.4",
"version": "1.1.0",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down
21 changes: 0 additions & 21 deletions package/LICENSE

This file was deleted.

36 changes: 0 additions & 36 deletions package/README.md

This file was deleted.

25 changes: 0 additions & 25 deletions package/package.json

This file was deleted.

8 changes: 5 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
59 changes: 48 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 <command>
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
*/
Expand All @@ -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())();
13 changes: 13 additions & 0 deletions src/masterArticles/master-articles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -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)}`);
Expand Down

0 comments on commit e31f3fd

Please sign in to comment.