diff --git a/README.md b/README.md index e5400f3..d060abc 100644 --- a/README.md +++ b/README.md @@ -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); +``` diff --git a/package.json b/package.json index fd0b4c6..ed5b1a5 100644 --- a/package.json +++ b/package.json @@ -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" @@ -14,9 +19,6 @@ "files": [ "dist/*" ], - "bin": { - "papertown": "dist/index.js" - }, "author": "", "license": "ISC", "devDependencies": { diff --git a/src/api.ts b/src/api.ts new file mode 100644 index 0000000..cc2da2c --- /dev/null +++ b/src/api.ts @@ -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) { + await command_sync({ + ...default_config, + ...config, + }); +} diff --git a/src/commands/sync.ts b/src/commands/sync.ts new file mode 100644 index 0000000..ed2de28 --- /dev/null +++ b/src/commands/sync.ts @@ -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); +} diff --git a/src/config.ts b/src/config.ts index b547106..f0564ae 100644 --- a/src/config.ts +++ b/src/config.ts @@ -3,20 +3,27 @@ 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: @@ -24,14 +31,14 @@ export const config = convict({ 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(); diff --git a/src/index.ts b/src/index.ts index 991162a..ab78d37 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -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())();