Skip to content

Commit b90f7aa

Browse files
committed
code refactoring in add()
1 parent 1a1cd22 commit b90f7aa

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/commands.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,18 +328,22 @@ export async function setup(contest_id: string, options: { choice: Choices, forc
328328
}
329329

330330
export async function add(options: { choice?: Choices | boolean, force?: boolean, taskDirnameFormat?: string, template?: string, tests?: boolean }) {
331-
let flg_default_choice = false;
332-
if (options.choice === undefined) {
333-
options.choice = (await getConfig()).get("default-task-choice");
334-
flg_default_choice = true;
335-
}
336331
try {
337-
if (!checkValidChoiceOption(options.choice)) {
338-
throw new Error(`Invalid option is given with --choice. (inquire/all/none/rest/next)${flg_default_choice ? "\nIf you did not set the --choice option, check the \"default-task-choice\" option in global config by using `acc config`." : ""}`);
339-
}
340332
const {path, data} = await findProjectJSON();
341333
const {contest, tasks} = data;
342-
const choices = await selectTasks(tasks, options.choice, options.force);
334+
const choices = await (async (c) => {
335+
// choiceオプションが設定されていない場合はコンフィグからデフォルト値を取得
336+
let flg_default_choice = false;
337+
if (c === undefined) {
338+
c = (await getConfig()).get("default-task-choice");
339+
flg_default_choice = true;
340+
}
341+
// 有効な値が与えられているかどうかバリデーションを行う
342+
if (!checkValidChoiceOption(c)) {
343+
throw new Error(`Invalid option is given with --choice. (inquire/all/none/rest/next)${flg_default_choice ? "\nIf you did not set the --choice option, check the \"default-task-choice\" option in global config by using `acc config`." : ""}`);
344+
}
345+
return await selectTasks(tasks, c, options.force);
346+
})(options.choice);
343347
const template = options.template !== undefined ? (await getTemplate(options.template).catch((e) => {
344348
throw new Error(`Failed to load template "${options.template}".\n ${e}`);
345349
})) : undefined;

0 commit comments

Comments
 (0)