From b15a96be51159ac874fb2c21e0264a8afc233e99 Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Tue, 19 Sep 2023 03:40:39 +0900 Subject: [PATCH] Move createConfigFile to utils.ts --- src/commands/init.ts | 71 ++++---------------------------------------- src/utils.ts | 65 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 67 deletions(-) diff --git a/src/commands/init.ts b/src/commands/init.ts index c571487..d708e3a 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -1,14 +1,15 @@ // init command // Import the necessary modules -import inquirer, { QuestionCollection } from 'inquirer'; +import inquirer from 'inquirer'; import fs from 'fs'; import path from 'path'; import { isAuthorized } from '../auth'; -import { Config, CONFIG_FILE_NAME, DEFAULT_CONFIG } from '../constants'; +import { CONFIG_FILE_NAME } from '../constants'; import login from './login'; import { MESSAGES } from '../messages'; +import * as utils from '../utils'; interface InquirerInitOverwriteResponse { overwrite: boolean; @@ -18,68 +19,6 @@ interface InitCommandOptions { readonly login?: boolean; } -/** - * Creates a config file in the current directory based on user input - */ -async function createConfigFile(): Promise { - // Define the questions to be asked - const questions: QuestionCollection = [ - { - name: 'sourceDir', - type: 'input', - message: MESSAGES.prompt.enterSourceDir, - default: DEFAULT_CONFIG.sourceDir, - validate: (value: string) => { - if (fs.existsSync(value)) { - return true; - } else { - return MESSAGES.prompt.enterValidPath; - } - }, - }, - { - name: 'targetDriveFolderId', - type: 'input', - message: MESSAGES.prompt.enterTargetDriveFolderId, - default: DEFAULT_CONFIG.targetDriveFolderId, - validate: (value: string) => { - if (value.length) { - return true; - } else { - return MESSAGES.prompt.enterValidId; - } - }, - }, - { - name: 'targetIsSharedDrive', - type: 'confirm', - message: MESSAGES.prompt.targetIsSharedDriveYN, - default: DEFAULT_CONFIG.targetIsSharedDrive, - }, - { - name: 'updateExistingGoogleSheets', - type: 'confirm', - message: MESSAGES.prompt.updateExistingGoogleSheetsYN, - default: DEFAULT_CONFIG.updateExistingGoogleSheets, - }, - { - name: 'saveOriginalFilesToDrive', - type: 'confirm', - message: MESSAGES.prompt.saveOriginalFilesToDriveYN, - default: DEFAULT_CONFIG.saveOriginalFilesToDrive, - }, - ]; - - // Prompt the user for input - const answers = (await inquirer.prompt(questions)) as Config; - - // Write the answers to a config file - fs.writeFileSync( - path.join(process.cwd(), CONFIG_FILE_NAME), - JSON.stringify(answers, null, 2), - ); -} - /** * Create a config file `c2g.config.json` in the current directory. * If a config file already exists, prompt the user to overwrite it. @@ -100,13 +39,13 @@ export default async function init( }, ])) as InquirerInitOverwriteResponse; if (overwrite.overwrite) { - await createConfigFile(); + await utils.createConfigFile(); } else { // exit without doing anything console.info(MESSAGES.log.noChangesWereMade); } } else { - await createConfigFile(); + await utils.createConfigFile(); } // If the option "login" is true, authorize the user if (options?.login) { diff --git a/src/utils.ts b/src/utils.ts index 47dc2e3..f10029b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,12 +2,75 @@ import fs from 'fs'; import { drive_v3 } from 'googleapis'; +import inquirer, { QuestionCollection } from 'inquirer'; import path from 'path'; -import { Config, DEFAULT_CONFIG } from './constants'; +import { Config, CONFIG_FILE_NAME, DEFAULT_CONFIG } from './constants'; import { C2gError } from './c2g-error'; import { MESSAGES } from './messages'; +/** + * Creates a config file in the current directory based on user input + */ +export async function createConfigFile(): Promise { + // Define the questions to be asked + const questions: QuestionCollection = [ + { + name: 'sourceDir', + type: 'input', + message: MESSAGES.prompt.enterSourceDir, + default: DEFAULT_CONFIG.sourceDir, + validate: (value: string) => { + if (fs.existsSync(value)) { + return true; + } else { + return MESSAGES.prompt.enterValidPath; + } + }, + }, + { + name: 'targetDriveFolderId', + type: 'input', + message: MESSAGES.prompt.enterTargetDriveFolderId, + default: DEFAULT_CONFIG.targetDriveFolderId, + validate: (value: string) => { + if (value.length) { + return true; + } else { + return MESSAGES.prompt.enterValidId; + } + }, + }, + { + name: 'targetIsSharedDrive', + type: 'confirm', + message: MESSAGES.prompt.targetIsSharedDriveYN, + default: DEFAULT_CONFIG.targetIsSharedDrive, + }, + { + name: 'updateExistingGoogleSheets', + type: 'confirm', + message: MESSAGES.prompt.updateExistingGoogleSheetsYN, + default: DEFAULT_CONFIG.updateExistingGoogleSheets, + }, + { + name: 'saveOriginalFilesToDrive', + type: 'confirm', + message: MESSAGES.prompt.saveOriginalFilesToDriveYN, + default: DEFAULT_CONFIG.saveOriginalFilesToDrive, + }, + ]; + + // Prompt the user for input + const answers = (await inquirer.prompt(questions)) as Config; + + // Write the answers to a config file + fs.writeFileSync( + path.join(process.cwd(), CONFIG_FILE_NAME), + JSON.stringify(answers, null, 2), + ); +} + /** * Read the configuration file and return its contents as an object. * @param configFilePath The path to the configuration file