Skip to content

Commit 8350590

Browse files
authored
Merge pull request #11 from increments/add-include-private-command
qiita.config.jsonを使って、設定を変更できるようにする
2 parents e3ec016 + 1cea071 commit 8350590

File tree

7 files changed

+249
-39
lines changed

7 files changed

+249
-39
lines changed

README.md

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ npm install @qiita/qiita-cli@latest
5555

5656
## Qiita CLI のセットアップ方法について
5757

58+
### init コマンドを実行する
59+
60+
以下のコマンドを実行することで、
61+
62+
- .gitignore
63+
- GitHub Actions のワークフローファイル
64+
- 「GitHub で記事を管理する」の項目を参照
65+
- ユーザー設定ファイル(qiita.config.json)
66+
- 「ユーザー設定ファイルについて」の項目を参照
67+
68+
が生成されます。
69+
70+
```console
71+
npx qiita init
72+
```
73+
5874
### Qiita のトークンを発行する
5975

6076
以下の流れでトークンを発行してください。
@@ -161,17 +177,6 @@ Qiita CLI、Qiita Preview から記事の削除はできません。
161177

162178
## GitHub で記事を管理する
163179

164-
以下のコマンドを実行することで、
165-
166-
- .gitignore
167-
- GitHub Actions のワークフローファイル
168-
169-
が生成されます。
170-
171-
```console
172-
npx qiita init
173-
```
174-
175180
### GitHub の設定について
176181

177182
以下の流れで設定を行うことで、GitHub の特定のブランチにコミットしたタイミングで記事の投稿や更新を行うことが可能になります。
@@ -204,19 +209,37 @@ npx qiita pull
204209

205210
### version
206211

207-
qiita-cli のバージョンを確認できます。
212+
Qiita CLI のバージョンを確認できます。
208213

209214
```console
210215
npx qiita version
211216
```
212217

218+
## ユーザー設定ファイルについて
219+
220+
`npx qiita init`コマンドで生成される`qiita.config.json`について説明します。
221+
このファイルを用いて、Qiita CLI の設定を行うことができます。
222+
設定できるオプションは以下の通りです。
223+
224+
- includePrivate: 限定共有記事を含めるかどうかを選べます。デフォルトは`false`です。
225+
213226
## オプション
214227

228+
### --credential \<credential_dir>
229+
230+
Qiita CLI の認証情報(`credentials.json`)を配置する・しているディレクトリを指定できます。
231+
デフォルトでは`$XDG_CONFIG_HOME/qiita-cli`もしくは`$HOME/.config/qiita-cli`になっています。
232+
233+
```console
234+
npx qiita login ---credential ./my_conf/
235+
npx qiita preview --credential ./my_conf/
236+
```
237+
215238
### --config \<config_dir>
216239

217-
qiita-cli の設定情報(`credentials.json`)を配置する・しているディレクトリを指定できます。
240+
Qiita CLI の設定情報(`qiita.config.json`)を配置する・しているディレクトリを指定できます。
218241

219-
デフォルトでは`$XDG_CONFIG_HOME/qiita-cli`もしくは`$HOME/.config/qiita-cli`になっています
242+
デフォルトでは、カレントディレクトリになります
220243

221244
例)
222245

src/commands/help.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ COMMAND:
99
publish <basename> ... 記事を投稿、更新
1010
publish --all 全ての記事を投稿、更新
1111
pull 記事ファイルをQiitaと同期
12-
version qiita-cliのバージョンを表示
12+
version Qiita CLIのバージョンを表示
1313
help ヘルプを表示
1414
1515
OPTIONS:
16-
--config <config_dir>
17-
qiita-cliの設定情報を配置するディレクトリを指定
16+
--credential <credential_dir>
17+
Qiita CLIの認証情報を配置するディレクトリを指定
1818
1919
--root <root_dir>
2020
記事ファイルをダウンロードするディレクトリを指定
2121
2222
--verbose
2323
詳細表示オプションを有効
2424
25+
--config
26+
設定ファイルを配置するディレクトリを指定
27+
2528
詳細についてはReadme(https://github.com/increments/qiita-cli)をご覧ください
2629
`;
2730

src/commands/init.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "node:fs";
22
import path from "node:path";
3+
import { config } from "../lib/config";
34

45
export const init = async () => {
56
const rootDir = process.cwd();
@@ -48,6 +49,18 @@ jobs:
4849
node_modules
4950
`;
5051
writeFile(gitignoreFilePath, gitignoreFileContent);
52+
53+
const userConfigFilePath = config.getUserConfigFilePath();
54+
const userConfigDir = config.getUserConfigDir();
55+
if (!fs.existsSync(userConfigFilePath)) {
56+
fs.mkdirSync(userConfigDir, { recursive: true });
57+
}
58+
const userConfigFileContent = JSON.stringify(
59+
await config.getUserConfig(),
60+
null,
61+
2
62+
);
63+
writeFile(userConfigFilePath, userConfigFileContent);
5164
};
5265

5366
const writeFile = (path: string, content: string) => {

src/lib/config.test.ts

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,37 @@ jest.mock("node:fs/promises", () => {
5555
mkdir: jest.fn(() => {}),
5656
};
5757
});
58+
jest.mock("node:fs", () => {
59+
return {
60+
existsSync: jest.fn((filePath: string): boolean => {
61+
const text = getFile(filePath);
62+
return !!text;
63+
}),
64+
};
65+
});
5866

5967
describe("config", () => {
60-
describe("#getConfigDir", () => {
68+
describe("#getCredentialDir", () => {
6169
beforeEach(() => {
6270
config.load({});
6371
});
6472

6573
it("returns default path", () => {
66-
expect(config.getConfigDir()).toEqual(
74+
expect(config.getCredentialDir()).toEqual(
6775
"/home/test-user/.config/qiita-cli"
6876
);
6977
});
7078

7179
describe("with options", () => {
7280
beforeEach(() => {
7381
config.load({
74-
configDir: "my-config",
82+
credentialDir: "my-credential",
7583
});
7684
});
7785

7886
it("returns customized path", () => {
79-
expect(config.getConfigDir()).toEqual(
80-
"/home/test-user/qiita-articles/my-config"
87+
expect(config.getCredentialDir()).toEqual(
88+
"/home/test-user/qiita-articles/my-credential"
8189
);
8290
});
8391
});
@@ -111,6 +119,101 @@ describe("config", () => {
111119
});
112120
});
113121

122+
describe("#getUserConfigDir", () => {
123+
describe("paths", () => {
124+
beforeEach(() => {
125+
config.load({});
126+
});
127+
128+
it("returns default path", () => {
129+
expect(config.getUserConfigDir()).toEqual(
130+
"/home/test-user/qiita-articles"
131+
);
132+
});
133+
134+
describe("with options", () => {
135+
beforeEach(() => {
136+
config.load({
137+
userConfigDir: "my-root",
138+
});
139+
});
140+
141+
it("returns customized path", () => {
142+
expect(config.getUserConfigDir()).toEqual(
143+
"/home/test-user/qiita-articles/my-root"
144+
);
145+
});
146+
});
147+
});
148+
});
149+
150+
describe("#getUserConfigFilePath", () => {
151+
describe("paths", () => {
152+
beforeEach(() => {
153+
config.load({});
154+
});
155+
156+
it("returns default path", () => {
157+
expect(config.getUserConfigFilePath()).toEqual(
158+
"/home/test-user/qiita-articles/qiita.config.json"
159+
);
160+
});
161+
162+
describe("with options", () => {
163+
beforeEach(() => {
164+
config.load({
165+
userConfigDir: "my-root",
166+
});
167+
});
168+
169+
it("returns customized path", () => {
170+
expect(config.getUserConfigFilePath()).toEqual(
171+
"/home/test-user/qiita-articles/my-root/qiita.config.json"
172+
);
173+
});
174+
});
175+
});
176+
});
177+
178+
describe("#getUserConfig", () => {
179+
const userConfigFilePath =
180+
"/home/test-user/qiita-articles/qiita.config.json";
181+
182+
beforeEach(() => {
183+
config.load({});
184+
});
185+
186+
describe("when user config file already exists", () => {
187+
beforeEach(() => {
188+
const userConfigData = {
189+
includePrivate: true,
190+
};
191+
resetFiles();
192+
setFile(userConfigFilePath, JSON.stringify(userConfigData, null, 2));
193+
});
194+
195+
it("returns user config", async () => {
196+
const userConfig = await config.getUserConfig();
197+
expect(userConfig).toStrictEqual({
198+
includePrivate: true,
199+
});
200+
});
201+
});
202+
203+
describe("when user config file does not exist", () => {
204+
beforeEach(() => {
205+
resetFiles();
206+
});
207+
208+
it("returns default user config", async () => {
209+
const userConfig = await config.getUserConfig();
210+
expect(userConfig).toStrictEqual({
211+
includePrivate: false,
212+
});
213+
});
214+
});
215+
});
216+
114217
describe("#getCredential", () => {
115218
const credentialFilePath =
116219
"/home/test-user/.config/qiita-cli/credentials.json";

0 commit comments

Comments
 (0)