Skip to content

Commit

Permalink
Move createConfigFile to utils.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ttsukagoshi committed Sep 18, 2023
1 parent 25b5d2f commit b15a96b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 67 deletions.
71 changes: 5 additions & 66 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<void> {
// 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.
Expand All @@ -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) {
Expand Down
65 changes: 64 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
// 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
Expand Down

0 comments on commit b15a96b

Please sign in to comment.