diff --git a/README.md b/README.md index 54293b8..3bc2dbf 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,14 @@ Allowed values are: - `hashnode` - `medium` +##### Dry Run + +If you want to test out the entire flow without actually posting the article, you can pass the `-d, --dryRun` option: + +```bash +yarn start example.com --dryRun +``` + ## Config By default, this tool will look for configurations under `config/default.json` or `config/local.json`. You can also pass the `-c, --config` option to load configurations from a different JSON file. diff --git a/src/clients/devto-client.ts b/src/clients/devto-client.ts index 9ae5e70..79daea7 100644 --- a/src/clients/devto-client.ts +++ b/src/clients/devto-client.ts @@ -37,7 +37,7 @@ class DevToClient { }) } - async post (url: string) { + async post (url: string, dryRun?: boolean) { //get page id const pageId = this.notion.getPageIdFromURL(url) //get blocks @@ -65,6 +65,11 @@ class DevToClient { article[value] = this.formatValue(value, attributeValue) }) + if (dryRun) { + console.log('No error occurred while preparing article for dev.to.') + return + } + //push to dev.to await this.client.post('articles', { article diff --git a/src/clients/github-client.ts b/src/clients/github-client.ts index d62d1c0..4d68279 100644 --- a/src/clients/github-client.ts +++ b/src/clients/github-client.ts @@ -31,7 +31,7 @@ class GitHubClient { this.notion = new Notion(notion_config) } - async post(url: string) { + async post(url: string, dryRun?: boolean) { //get page id const pageId = this.notion.getPageIdFromURL(url) //get blocks @@ -115,6 +115,11 @@ class GitHubClient { content: this.toBase64(markdown) }) + if (dryRun) { + console.log('No error occurred while preparing article for GitHub.') + return + } + //commit files to GitHub for (const file of files) { await this.octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', { diff --git a/src/clients/hashnode-client.ts b/src/clients/hashnode-client.ts index 686dedc..8ca9be4 100644 --- a/src/clients/hashnode-client.ts +++ b/src/clients/hashnode-client.ts @@ -30,7 +30,7 @@ class HashnodeClient { }) } - async post (url: string) { + async post (url: string, dryRun?: boolean) { //get page id const pageId = this.notion.getPageIdFromURL(url) //get blocks @@ -81,6 +81,11 @@ class HashnodeClient { hideFromHashnodeFeed: this.options.should_hide } + if (dryRun) { + console.log('No error occurred while preparing article for Hashnode.') + return + } + await this.client.request(mutation, data) console.log('Article pushed to Hashnode') diff --git a/src/clients/medium-client.ts b/src/clients/medium-client.ts index f84bded..64757bf 100644 --- a/src/clients/medium-client.ts +++ b/src/clients/medium-client.ts @@ -25,7 +25,7 @@ class MediumClient { }) } - async post (url: string) { + async post (url: string, dryRun?: boolean) { //get page id const pageId = this.notion.getPageIdFromURL(url) //get blocks @@ -55,6 +55,11 @@ class MediumClient { const subtitle = this.notion.getAttributeValue(properties[this.options.properties?.subtitle || MediumProperties.SUBTITLE]) markdown = `# ${title}\r\n\r\n${subtitle ? `${subtitle}\r\n\r\n` : ''}${markdown}` + if (dryRun) { + console.log('No error occurred while preparing article for Medium.') + return + } + await this.client.post(requestPath, { title, contentFormat: 'markdown', diff --git a/src/commands/post.ts b/src/commands/post.ts index 0a98f51..1df5ffb 100644 --- a/src/commands/post.ts +++ b/src/commands/post.ts @@ -5,30 +5,31 @@ import MediumClient from "../clients/medium-client"; import GlobalOptions, { Platforms } from "../types/global-options"; type PostOptions = GlobalOptions & { - platforms: Platforms[] + platforms: Platforms[], + dryRun: boolean } -export default async function post (url: string, { config, platforms }: PostOptions) { +export default async function post (url: string, { config, platforms, dryRun }: PostOptions) { const promises = [] if (platforms.includes(Platforms.GITHUB)) { const github = new GitHubClient(config.github, config.notion) - promises.push(github.post(url)) + promises.push(github.post(url, dryRun)) } if (platforms.includes(Platforms.DEVTO)) { const devto = new DevToClient(config.devto, config.notion) - promises.push(devto.post(url)) + promises.push(devto.post(url, dryRun)) } if (platforms.includes(Platforms.HASHNODE)) { const hashnode = new HashnodeClient(config.hashnode, config.notion) - promises.push(hashnode.post(url)) + promises.push(hashnode.post(url, dryRun)) } if (platforms.includes(Platforms.MEDIUM)) { const medium = new MediumClient(config.medium, config.notion) - promises.push(medium.post(url)) + promises.push(medium.post(url, dryRun)) } await Promise.all(promises) diff --git a/src/index.ts b/src/index.ts index be7dfb6..82a4a3b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,5 +36,10 @@ program .option( '-p, --platforms [platforms...]', 'Platforms to publish the article on.', Object.values(Platforms)) + .option( + '-d, --dryRun', + 'If this option is passed, the entire process runs without actually posting the article. Useful for testing.', + false + ) program.parse() \ No newline at end of file