Skip to content

Commit

Permalink
feat: add api (beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianKohler committed Aug 24, 2020
1 parent 1542cab commit 8d31910
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 61 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,22 @@ Can be a relative image, papertown will resolve it.
```
cover_image: direct_url_to_image.jpg
```

## Papertown API (BETA)

Papertown can be used within your node script.

### Example

```javascript
const { sync } = require("papertown");

const config = {
rootFolder: "my-root-folder",
devtoApiKey: "my-key",
imageRootUrlGithub: "",
dryRun: false,
};

await sync(config);
```
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{
"name": "papertown",
"version": "1.2.2",
"version": "1.3.2",
"description": "",
"main": "dist/index.js",
"main": "dist/lib/index.js",
"bin": {
"papertown": "dist/bin/index.js"
},
"scripts": {
"start": "ncc run src/index.ts",
"build": "ncc build src/index.ts -m",
"build": "npm run build:bin && npm run build:lib",
"build:bin": "ncc build src/index.ts -o dist/bin -m",
"build:lib": "ncc build src/api.ts -o dist/lib -m",
"test": "jest",
"test:watch": "jest --watch",
"link": "cd dist && npm link papertown"
Expand All @@ -14,9 +19,6 @@
"files": [
"dist/*"
],
"bin": {
"papertown": "dist/index.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SyncConfig, config as default_config } from "./config";
import { sync as command_sync } from "./commands/sync";

export async function sync(config: Partial<SyncConfig>) {
await command_sync({
...default_config,
...config,
});
}
56 changes: 56 additions & 0 deletions src/commands/sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Config } from "convict";

import { SyncConfig } from "../config";
import {
rootFolderExists,
getMasterArticlesWithMasterId,
haveDuplicateMasterIDs,
} from "../masterArticles/master-articles";
import { error } from "console";
import { syncBlogPlatformsWithMasters } from "../sync/sync";

export async function sync(config: SyncConfig) {
/**
* Stop if root folder does not exist
*/
if (!(await rootFolderExists(config.rootFolder))) {
error(`
Root folder not found: ${config.rootFolder}
Run $ papertown sync --rootFolder yourrootfolder
`);
return;
}

/**
* Get all articles with an masterid in the frontmatter
*/
const masterArticles = await getMasterArticlesWithMasterId(
config.rootFolder,
config.imageRootUrlGithub
);

/**
* Stop if no master articles were found
*/
if (masterArticles.length === 0) {
error(`
No articles found with masterid.
Add a masterid to the frontmatter of post
you want to sync
`);
return;
}

/**
* Stop if a masterid is used twice
*/
if (haveDuplicateMasterIDs(masterArticles)) {
error("Articles have duplicate master ids");
return;
}

/**
* Sync all master articles
*/
await syncBlogPlatformsWithMasters(masterArticles, config);
}
19 changes: 13 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,42 @@ import * as dotenv from "dotenv";

dotenv.config();

export const config = convict({
export interface SyncConfig {
rootFolder: string;
devtoApiKey: string;
imageRootUrlGithub: string;
dryRun: boolean;
}

export const config: SyncConfig = convict({
rootFolder: {
doc: "Root folder of articles",
format: String,
default: "blog-articles",
env: "ROOT_FOLDER",
arg: "rootFolder"
arg: "rootFolder",
},
devtoApiKey: {
doc: "DevTo API Key",
format: String,
default: "",
env: "DEVTO_API_KEY",
arg: "devtoApiKey"
arg: "devtoApiKey",
},
imageRootUrlGithub: {
doc:
"Root Url which is used to replace relative urls. E.g. https://raw.githubusercontent.com/ChristianKohler/Homepage/master",
format: String,
default: "",
env: "IMAGE_ROOT_URL_GITHUB",
arg: "imageRootUrlGithub"
arg: "imageRootUrlGithub",
},
dryRun: {
doc: "Only outputs the changes and doesn't run create or update",
format: Boolean,
default: false,
env: "DRY_RUN"
}
env: "DRY_RUN",
},
})
.validate()
.getProperties();
54 changes: 5 additions & 49 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#!/usr/bin/env node
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";
import { sync } from "./commands/sync";
import { config } from "./config";

const usage = `
Usage
Expand Down Expand Up @@ -38,48 +33,9 @@ async function main() {
}

/**
* Stop if root folder does not exist
*/
if (!(await rootFolderExists(config.rootFolder))) {
error(`
Root folder not found: ${config.rootFolder}
Run $ papertown sync --rootFolder yourrootfolder
`);
return;
}

/**
* Get all articles with an masterid in the frontmatter
*/
const masterArticles = await getMasterArticlesWithMasterId(
config.rootFolder,
config.imageRootUrlGithub
);

/**
* Stop if no master articles were found
*/
if (masterArticles.length === 0) {
error(`
No articles found with masterid.
Add a masterid to the frontmatter of post
you want to sync
`);
return;
}

/**
* Stop if a masterid is used twice
*/
if (haveDuplicateMasterIDs(masterArticles)) {
error("Articles have duplicate master ids");
return;
}

/**
* Sync all master articles
* Start sync command
*/
await syncBlogPlatformsWithMasters(masterArticles, config);
await sync(config);
}

(async () => await main())();

0 comments on commit 8d31910

Please sign in to comment.