Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"commander": "^2.20.0",
"dotenv": "8.0.0",
"front-matter": "3.0.2",
"got": "9.6.0"
"got": "9.6.0",
"throttled-queue": "^2.1.4"
}
}
12 changes: 10 additions & 2 deletions src/dev-to-git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chalk from 'chalk';
import program from 'commander';
import dotenv from 'dotenv';
import fs from 'fs';
import throttledQueue from 'throttled-queue';
import { Article } from './article';
import {
ArticleConfig,
Expand All @@ -16,9 +17,11 @@ import { Logger, formatArticlePublishedStatuses, logBuilder } from './helpers';
export const DEFAULT_CONFIG_PATH: string = './dev-to-git.json';

const repositoryRe: RegExp = /.*\/(.*)\/(.*)\.git/;
const ARTICLE_UPLOAD_INTERVAL = 3000;

export class DevToGit {
private configuration: ConfigurationOptions;
private queue: ReturnType<typeof throttledQueue>;

public logger: Logger;

Expand Down Expand Up @@ -47,6 +50,8 @@ export class DevToGit {
this.logger(chalk.red('DEV_TO_GIT_TOKEN environment variable, or --dev-to-token argument is required'));
process.exit(1);
}

this.queue = throttledQueue(1, ARTICLE_UPLOAD_INTERVAL);
}

private parseRepository(repo: string | null): Repository | null {
Expand Down Expand Up @@ -107,14 +112,17 @@ export class DevToGit {
public async publishArticles(): Promise<ArticlePublishedStatus[]> {
const articles = this.readConfigFile();

const articlePublishedStatuses = [];
const articlePublishedStatuses: ArticlePublishedStatus[] = [];

// instead of using Promise.all we use a for with await
// to run the updates one by one to avoid hammering dev.to API
// and have more risks of being rate limited
for (const articleConf of articles) {
const article = new Article(articleConf, this.configuration.devToToken);
articlePublishedStatuses.push(await article.publishArticle());

await this.queue(async () => {
articlePublishedStatuses.push(await article.publishArticle());
});
}

return articlePublishedStatuses;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7684,6 +7684,11 @@ throat@^4.0.0:
resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a"
integrity sha1-iQN8vJLFarGJJua6TLsgDhVnKmo=

throttled-queue@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/throttled-queue/-/throttled-queue-2.1.4.tgz#4e2008c73ab3f72ba1bb09496c3cc9c5b745dbee"
integrity sha512-YGdk8sdmr4ge3g+doFj/7RLF5kLM+Mi7DEciu9PHxnMJZMeVuZeTj31g4VE7ekUffx/IdbvrtOCiz62afg0mkg==

through2@^2.0.0, through2@^2.0.2, through2@~2.0.0:
version "2.0.5"
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.5.tgz#01c1e39eb31d07cb7d03a96a70823260b23132cd"
Expand Down