Skip to content

Commit 0a89aef

Browse files
committed
added dryRun option
1 parent 76478f1 commit 0a89aef

File tree

7 files changed

+44
-10
lines changed

7 files changed

+44
-10
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ Allowed values are:
140140
- `hashnode`
141141
- `medium`
142142

143+
##### Dry Run
144+
145+
If you want to test out the entire flow without actually posting the article, you can pass the `-d, --dryRun` option:
146+
147+
```bash
148+
yarn start example.com --dryRun
149+
```
150+
143151
## Config
144152

145153
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.

src/clients/devto-client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class DevToClient {
3737
})
3838
}
3939

40-
async post (url: string) {
40+
async post (url: string, dryRun?: boolean) {
4141
//get page id
4242
const pageId = this.notion.getPageIdFromURL(url)
4343
//get blocks
@@ -65,6 +65,11 @@ class DevToClient {
6565
article[value] = this.formatValue(value, attributeValue)
6666
})
6767

68+
if (dryRun) {
69+
console.log('No error occurred while preparing article for dev.to.')
70+
return
71+
}
72+
6873
//push to dev.to
6974
await this.client.post('articles', {
7075
article

src/clients/github-client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class GitHubClient {
3131
this.notion = new Notion(notion_config)
3232
}
3333

34-
async post(url: string) {
34+
async post(url: string, dryRun?: boolean) {
3535
//get page id
3636
const pageId = this.notion.getPageIdFromURL(url)
3737
//get blocks
@@ -115,6 +115,11 @@ class GitHubClient {
115115
content: this.toBase64(markdown)
116116
})
117117

118+
if (dryRun) {
119+
console.log('No error occurred while preparing article for GitHub.')
120+
return
121+
}
122+
118123
//commit files to GitHub
119124
for (const file of files) {
120125
await this.octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', {

src/clients/hashnode-client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class HashnodeClient {
3030
})
3131
}
3232

33-
async post (url: string) {
33+
async post (url: string, dryRun?: boolean) {
3434
//get page id
3535
const pageId = this.notion.getPageIdFromURL(url)
3636
//get blocks
@@ -81,6 +81,11 @@ class HashnodeClient {
8181
hideFromHashnodeFeed: this.options.should_hide
8282
}
8383

84+
if (dryRun) {
85+
console.log('No error occurred while preparing article for Hashnode.')
86+
return
87+
}
88+
8489
await this.client.request(mutation, data)
8590

8691
console.log('Article pushed to Hashnode')

src/clients/medium-client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MediumClient {
2525
})
2626
}
2727

28-
async post (url: string) {
28+
async post (url: string, dryRun?: boolean) {
2929
//get page id
3030
const pageId = this.notion.getPageIdFromURL(url)
3131
//get blocks
@@ -55,6 +55,11 @@ class MediumClient {
5555
const subtitle = this.notion.getAttributeValue(properties[this.options.properties?.subtitle || MediumProperties.SUBTITLE])
5656
markdown = `# ${title}\r\n\r\n${subtitle ? `${subtitle}\r\n\r\n` : ''}${markdown}`
5757

58+
if (dryRun) {
59+
console.log('No error occurred while preparing article for Medium.')
60+
return
61+
}
62+
5863
await this.client.post(requestPath, {
5964
title,
6065
contentFormat: 'markdown',

src/commands/post.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,31 @@ import MediumClient from "../clients/medium-client";
55
import GlobalOptions, { Platforms } from "../types/global-options";
66

77
type PostOptions = GlobalOptions & {
8-
platforms: Platforms[]
8+
platforms: Platforms[],
9+
dryRun: boolean
910
}
1011

11-
export default async function post (url: string, { config, platforms }: PostOptions) {
12+
export default async function post (url: string, { config, platforms, dryRun }: PostOptions) {
1213
const promises = []
1314

1415
if (platforms.includes(Platforms.GITHUB)) {
1516
const github = new GitHubClient(config.github, config.notion)
16-
promises.push(github.post(url))
17+
promises.push(github.post(url, dryRun))
1718
}
1819

1920
if (platforms.includes(Platforms.DEVTO)) {
2021
const devto = new DevToClient(config.devto, config.notion)
21-
promises.push(devto.post(url))
22+
promises.push(devto.post(url, dryRun))
2223
}
2324

2425
if (platforms.includes(Platforms.HASHNODE)) {
2526
const hashnode = new HashnodeClient(config.hashnode, config.notion)
26-
promises.push(hashnode.post(url))
27+
promises.push(hashnode.post(url, dryRun))
2728
}
2829

2930
if (platforms.includes(Platforms.MEDIUM)) {
3031
const medium = new MediumClient(config.medium, config.notion)
31-
promises.push(medium.post(url))
32+
promises.push(medium.post(url, dryRun))
3233
}
3334

3435
await Promise.all(promises)

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,10 @@ program
3636
.option(
3737
'-p, --platforms [platforms...]',
3838
'Platforms to publish the article on.', Object.values(Platforms))
39+
.option(
40+
'-d, --dryRun',
41+
'If this option is passed, the entire process runs without actually posting the article. Useful for testing.',
42+
false
43+
)
3944

4045
program.parse()

0 commit comments

Comments
 (0)