Skip to content

Commit

Permalink
feat: first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime1992 committed Jul 5, 2019
1 parent 3f8b447 commit fc225b1
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules
docs
dist
coverage
bin
.rpt2_cache
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
"version": "0.0.0",
"description": "",
"keywords": [],
"main": "dist/dev-to-git.umd.js",
"module": "dist/dev-to-git.es5.js",
"typings": "dist/types/dev-to-git.d.ts",
"main": "bin/dev-to-git.umd.js",
"module": "bin/dev-to-git.es5.js",
"typings": "bin/types/dev-to-git.d.ts",
"files": [
"dist"
"bin"
],
"bin": {
"dev-to-git": "bin/index.js"
},
"author": "Maxime Robert <maxime.robert1992@gmail.com>",
"repository": {
"type": "git",
Expand All @@ -19,11 +22,12 @@
"node": ">=6.0.0"
},
"scripts": {
"copy-index": "cp ./src/index.js ./bin",
"prettier:base": "yarn run prettier \"./{src,test}/**/*.ts\" \"./**/*.{yml,md,json}\"",
"prettier:fix": "yarn run prettier:base --write",
"lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'",
"prebuild": "rimraf dist",
"build": "tsc --module commonjs && rollup -c rollup.config.ts && typedoc --out docs --target es6 --theme minimal --mode file src",
"prebuild": "rimraf bin",
"build": "tsc --module commonjs && rollup -c rollup.config.ts && typedoc --out docs --target es6 --theme minimal --mode file src && yarn run copy-index",
"start": "rollup -c rollup.config.ts -w",
"test": "jest --coverage",
"test:watch": "jest --coverage --watch",
Expand Down Expand Up @@ -86,7 +90,10 @@
"devDependencies": {
"@commitlint/cli": "^7.1.2",
"@commitlint/config-conventional": "^7.1.2",
"@types/dotenv": "6.1.1",
"@types/got": "9.6.0",
"@types/jest": "^23.3.2",
"@types/minimist": "1.2.0",
"@types/node": "^10.11.0",
"colors": "^1.3.2",
"commitizen": "^3.0.0",
Expand Down Expand Up @@ -118,5 +125,10 @@
"tslint-config-standard": "^8.0.1",
"typedoc": "^0.12.0",
"typescript": "^3.0.3"
},
"dependencies": {
"dotenv": "8.0.0",
"got": "9.6.0",
"minimist": "1.2.0"
}
}
32 changes: 32 additions & 0 deletions src/article.ts
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
})
}
}
25 changes: 25 additions & 0 deletions src/dev-to-git.interface.ts
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
}
54 changes: 50 additions & 4 deletions src/dev-to-git.ts
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()
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('./dev-to-git.umd.js');
3 changes: 3 additions & 0 deletions test/article.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is my awesome article!

Hey, some text!
19 changes: 10 additions & 9 deletions test/dev-to-git.json
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
}
]
88 changes: 78 additions & 10 deletions test/dev-to-git.test.ts
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])
// })
// })
// })
})
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declarationDir": "dist/types",
"outDir": "dist/lib",
"declarationDir": "bin/types",
"outDir": "bin/lib",
"typeRoots": ["node_modules/@types"]
},
"include": ["src"]
Expand Down
Loading

0 comments on commit fc225b1

Please sign in to comment.