From 10ab7feac06887039ee5f08224f50cffd1abec04 Mon Sep 17 00:00:00 2001 From: fussy113 Date: Mon, 17 Jul 2023 17:31:59 +0900 Subject: [PATCH] Add force option to pull command --- README.md | 8 ++++++++ src/commands/pull.ts | 11 +++++++++-- src/lib/file-system-repo.ts | 4 ++-- src/lib/sync-articles-from-qiita.ts | 4 +++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 69c42b0..790c5f9 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,14 @@ Qiita 上で更新を行い、手元で変更を行っていない記事ファ npx qiita pull ``` +`--force`オプションを用いることで、記事ファイルをを全て強制的に Qiita と同期します。 + +```console +npx qiita pull --force +# -f は --force のエイリアスとして使用できます。 +npx qiita pull -f +``` + ### version Qiita CLI のバージョンを確認できます。 diff --git a/src/commands/pull.ts b/src/commands/pull.ts index 24a3a73..f22aeca 100644 --- a/src/commands/pull.ts +++ b/src/commands/pull.ts @@ -7,12 +7,19 @@ import { QiitaApi } from "../qiita-api"; import { startLocalChangeWatcher, startServer } from "../server/app"; export const pull = async (argv: string[]) => { - const args = arg({}, { argv }); + const args = arg( + { + "--force": Boolean, + "-f": "--force", + }, + { argv } + ); const qiitaApi = await getQiitaApiInstance(); const fileSystemRepo = await getFileSystemRepo(); + const isLocalUpdate = args["--force"]; - await syncArticlesFromQiita({ fileSystemRepo, qiitaApi }); + await syncArticlesFromQiita({ fileSystemRepo, qiitaApi, isLocalUpdate }); console.log("Sync local articles from Qiita"); console.log("Successful!"); }; diff --git a/src/lib/file-system-repo.ts b/src/lib/file-system-repo.ts index 68a5061..9b4a015 100644 --- a/src/lib/file-system-repo.ts +++ b/src/lib/file-system-repo.ts @@ -276,9 +276,9 @@ export class FileSystemRepo { } } - async saveItems(items: Item[]) { + async saveItems(items: Item[], isLocalUpdate: boolean = false) { const promises = items.map(async (item) => { - await this.syncItem(item); + await this.syncItem(item, false, isLocalUpdate); }); await Promise.all(promises); diff --git a/src/lib/sync-articles-from-qiita.ts b/src/lib/sync-articles-from-qiita.ts index 3c99cce..8bd7054 100644 --- a/src/lib/sync-articles-from-qiita.ts +++ b/src/lib/sync-articles-from-qiita.ts @@ -5,9 +5,11 @@ import { config } from "./config"; export const syncArticlesFromQiita = async ({ fileSystemRepo, qiitaApi, + isLocalUpdate = false, }: { fileSystemRepo: FileSystemRepo; qiitaApi: QiitaApi; + isLocalUpdate?: boolean; }) => { const per = 100; const userConfig = await config.getUserConfig(); @@ -20,6 +22,6 @@ export const syncArticlesFromQiita = async ({ const result = userConfig.includePrivate ? items : items.filter((item) => !item.private); - await fileSystemRepo.saveItems(result); + await fileSystemRepo.saveItems(result, isLocalUpdate); } };