-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f8b447
commit fc225b1
Showing
11 changed files
with
265 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ node_modules | |
docs | ||
dist | ||
coverage | ||
bin | ||
.rpt2_cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ArticleConfig, ArticleApi } from './dev-to-git.interface' | ||
import got from 'got' | ||
import fs from 'fs' | ||
|
||
export class Article { | ||
constructor(private articleConfig: ArticleConfig, private token: string) {} | ||
|
||
public readArticleOnDisk(): string { | ||
return fs.readFileSync(this.articleConfig.relativePathToArticle).toString() | ||
} | ||
|
||
public publishArticle(): got.GotPromise<any> { | ||
const body: ArticleApi = { | ||
title: this.articleConfig.title, | ||
description: this.articleConfig.description, | ||
body_markdown: this.readArticleOnDisk(), | ||
published: this.articleConfig.published, | ||
tags: this.articleConfig.tags, | ||
series: this.articleConfig.series, | ||
publish_under_org: this.articleConfig.publishUnderOrg, | ||
main_image: this.articleConfig.urlToMainImage, | ||
canonical_url: this.articleConfig.canonicalUrl | ||
} | ||
|
||
return got(`https://dev.to/api/articles/${this.articleConfig.id}`, { | ||
json: true, | ||
method: 'PUT', | ||
headers: { 'api-key': this.token }, | ||
body | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export interface ArticleConfig { | ||
title: string | ||
description: string | ||
id: string | ||
published: string | ||
urlToMainImage: string | ||
tags: string[] | ||
relativePathToArticle: string | ||
series: string | ||
publishUnderOrg: boolean | ||
canonicalUrl: string | ||
} | ||
|
||
// https://dev.to/api#available-json-parameters | ||
export interface ArticleApi { | ||
title: string | ||
description: string | ||
body_markdown: string | ||
published: string | ||
tags: string[] | ||
series: string | ||
publish_under_org: boolean | ||
main_image?: string | ||
canonical_url?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,52 @@ | ||
// Import here Polyfills if needed. Recommended core-js (npm i -D core-js) | ||
// import "core-js/fn/array.find" | ||
// ... | ||
export default class DummyClass { | ||
import minimist from 'minimist' | ||
import fs from 'fs' | ||
import dotenv from 'dotenv' | ||
import { ArticleConfig, ArticleApi } from './dev-to-git.interface' | ||
import { Article } from './article' | ||
|
||
export const DEFAULT_CONFIG_PATH: string = './dev-to-git.json' | ||
|
||
export class DevToGit { | ||
private configPath: string = DEFAULT_CONFIG_PATH | ||
private token: string = '' | ||
|
||
constructor() { | ||
dotenv.config() | ||
|
||
const { config } = minimist(process.argv.slice(2)) | ||
|
||
if (config && typeof config === 'string') { | ||
this.configPath = config | ||
} | ||
|
||
if (!process.env.DEV_TO_GIT_TOKEN) { | ||
throw new Error('Token is required') | ||
} | ||
|
||
this.token = process.env.DEV_TO_GIT_TOKEN | ||
} | ||
|
||
public getConfigPath(): string { | ||
return this.configPath | ||
} | ||
|
||
public readConfigFile(): ArticleConfig[] { | ||
// @todo check structure of the object | ||
|
||
return JSON.parse( | ||
fs.readFileSync(this.getConfigPath()).toString() | ||
) as ArticleConfig[] | ||
} | ||
|
||
public publishArticles() { | ||
const articles = this.readConfigFile() | ||
articles.forEach(articleConf => { | ||
const article = new Article(articleConf, this.token) | ||
article.publishArticle() | ||
}) | ||
} | ||
} | ||
|
||
// @todo move to main file? | ||
const devToGit = new DevToGit() | ||
devToGit.publishArticles() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
require('./dev-to-git.umd.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# This is my awesome article! | ||
|
||
Hey, some text! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
[ | ||
{ | ||
"title": "", | ||
"description": "", | ||
"published": "", | ||
"urlToMainImage": "", | ||
"tags": [], | ||
"relativePathToArticle": "", | ||
"series": "", | ||
"publishUnderOrg": "", | ||
"canonicalUrl": "" | ||
"id": 132750, | ||
"title": "Updated title", | ||
"description": "Great description", | ||
"published": "false", | ||
"urlToMainImage": null, | ||
"tags": ["typescript"], | ||
"relativePathToArticle": "./test/article.md", | ||
"series": null, | ||
"publishUnderOrg": false, | ||
"canonicalUrl": null | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,82 @@ | ||
import DummyClass from "../src/dev-to-git" | ||
|
||
/** | ||
* Dummy test | ||
*/ | ||
describe("Dummy test", () => { | ||
it("works if true is truthy", () => { | ||
expect(true).toBeTruthy() | ||
import { DevToGit, DEFAULT_CONFIG_PATH } from '../src/dev-to-git' | ||
|
||
describe(`DevToGit`, () => { | ||
beforeEach(() => { | ||
process.argv = ['don-t-care', 'don-t-care'] | ||
process.env.DEV_TO_GIT_TOKEN = 'token' | ||
}) | ||
|
||
it("DummyClass is instantiable", () => { | ||
expect(new DummyClass()).toBeInstanceOf(DummyClass) | ||
describe(`Config`, () => { | ||
describe(`Get config`, () => { | ||
it(`should have by default a path "./dev-to-git.json"`, () => { | ||
const devToGit = new DevToGit() | ||
expect(devToGit.getConfigPath()).toBe(DEFAULT_CONFIG_PATH) | ||
}) | ||
|
||
it(`should accept a "config" argument to change the path to the config`, () => { | ||
const CUSTOM_CONFIG_PATH: string = './custom/dev-to-git.json' | ||
process.argv = [ | ||
'don-t-care', | ||
'don-t-care', | ||
'--config', | ||
CUSTOM_CONFIG_PATH | ||
] | ||
const devToGit = new DevToGit() | ||
expect(devToGit.getConfigPath()).toBe(CUSTOM_CONFIG_PATH) | ||
}) | ||
|
||
it(`should use the default path if the "config" flag is passed without nothing`, () => { | ||
process.argv = ['don-t-care', 'don-t-care', '--config'] | ||
const devToGit = new DevToGit() | ||
expect(devToGit.getConfigPath()).toBe(DEFAULT_CONFIG_PATH) | ||
}) | ||
}) | ||
|
||
describe(`Read config from file`, () => { | ||
it(`test`, () => { | ||
process.argv = [ | ||
'don-t-care', | ||
'don-t-care', | ||
'--config', | ||
'./test/dev-to-git.json' | ||
] | ||
|
||
const devToGit = new DevToGit() | ||
|
||
expect(devToGit.readConfigFile()).toEqual(require('./dev-to-git.json')) | ||
}) | ||
}) | ||
}) | ||
|
||
// describe(`Article`, () => { | ||
// describe(`Read`, () => { | ||
// it(`should read an article from the configuration`, () => { | ||
// const CUSTOM_CONFIG_PATH: string = './test/dev-to-git.json' | ||
// process.argv = [ | ||
// 'don-t-care', | ||
// 'don-t-care', | ||
// '--config', | ||
// CUSTOM_CONFIG_PATH | ||
// ] | ||
// const devToGit = new DevToGit() | ||
// expect(devToGit.readArticleOnDisk()).toContain( | ||
// 'This is my awesome article!' | ||
// ) | ||
// expect(devToGit.readArticleOnDisk()).toContain('Hey, some text!') | ||
// }) | ||
// }) | ||
// describe(`Publish`, () => { | ||
// it(`should publish the article`, () => { | ||
// const CUSTOM_CONFIG_PATH: string = './test/dev-to-git.json' | ||
// process.argv = [ | ||
// 'don-t-care', | ||
// 'don-t-care', | ||
// '--config', | ||
// CUSTOM_CONFIG_PATH | ||
// ] | ||
// const devToGit = new DevToGit() | ||
// devToGit.publishArticle(devToGit.readConfigFile()[0]) | ||
// }) | ||
// }) | ||
// }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.