Skip to content

Commit

Permalink
added dryRun option
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser committed Feb 24, 2023
1 parent 76478f1 commit 0a89aef
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 10 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion src/clients/devto-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/clients/github-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}', {
Expand Down
7 changes: 6 additions & 1 deletion src/clients/hashnode-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
7 changes: 6 additions & 1 deletion src/clients/medium-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down
13 changes: 7 additions & 6 deletions src/commands/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 0a89aef

Please sign in to comment.