From 1dda1bda526d5d319449aa9233ca99881821425f Mon Sep 17 00:00:00 2001 From: Roberto Hernandez Date: Fri, 20 Sep 2024 09:10:47 -0600 Subject: [PATCH] feat: adjusting the init command to work without parameters --- src/handlers/init.test.ts | 32 +++++++++++++++++++++++++++--- src/handlers/init.ts | 41 ++++++++++++++++++++++++++++++++++++++- src/index.ts | 2 +- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/handlers/init.test.ts b/src/handlers/init.test.ts index f68efd5..68d3cf2 100644 --- a/src/handlers/init.test.ts +++ b/src/handlers/init.test.ts @@ -3,16 +3,19 @@ jest.mock('../util/platform/getPlatformInfo', () => jest.fn()); jest.mock('../util/fs/writeToJsonFile', () => jest.fn()); jest.mock('../util/fs/executeScript', () => jest.fn()); jest.mock('../util/project/installDependencies', () => jest.fn()); +jest.mock('inquirer'); import fs from 'fs'; import git from 'simple-git'; import log from '../lib/log.js'; -import init from './init.js'; +import inquirer from 'inquirer'; +import ProgressBar from 'progress'; +import installDependencies from '../util/project/installDependencies.js'; import getPlatformInfo from '../util/platform/getPlatformInfo.js'; import writeToJsonFile from '../util/fs/writeToJsonFile.js'; import executeScript from '../util/fs/executeScript.js'; -import installDependencies from '../util/project/installDependencies.js'; -import ProgressBar from 'progress'; +import init, { DIRECTORY, questions } from './init.js'; +import { EXIT_ERROR } from '../lib/constants.js'; const root = '/home/uname/Projects/cornflake'; @@ -39,6 +42,29 @@ describe('init', () => { progressMock.tick.mockClear(); }); + it('should execute the returned function', async () => { + await init(progress)(); + expect(logMock).toHaveBeenCalledWith( + 'error', + 'Unable to determine the project name. Please provide a valid project name.', + EXIT_ERROR, + ); + }); + + it('should prompt for the name if not provided', async () => { + expect.assertions(1); + const mockPrompt = jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ + name: 'cornflake', + platform: 'drupal', + targetDirectory: root, + }); + + await init(progress)(); + questions[DIRECTORY].default = root; + expect(mockPrompt).toHaveBeenCalledWith(questions); + mockPrompt.mockRestore(); + }); + it('can detect the platform, and use information about the platform to autodetect the target directory and Emulsify starter', async () => { expect.assertions(3); await init(progress)('cornflake'); diff --git a/src/handlers/init.ts b/src/handlers/init.ts index 2120c79..49e97d2 100644 --- a/src/handlers/init.ts +++ b/src/handlers/init.ts @@ -2,6 +2,8 @@ import { join } from 'path'; import { existsSync, promises as fs } from 'fs'; import simpleGit from 'simple-git'; import ProgressBar from 'progress'; +import inquirer from 'inquirer'; +import { AnyQuestion } from 'inquirer/dist/cjs/types/types.js'; import type { EmulsifyProjectConfiguration, @@ -25,6 +27,28 @@ import { EXIT_ERROR } from '../lib/constants.js'; const git = simpleGit(); +export const DIRECTORY = 1; +export const questions: AnyQuestion[] = [ + { + type: 'input', + name: 'name', + message: 'Project name:', + default: 'emulsifyTheme', + }, + { + type: 'input', + name: 'targetDirectory', + message: 'Target directory:', + default: './', + }, + { + type: 'input', + name: 'platform', + message: 'Platform:', + default: 'drupal', + }, +]; + /** * Handler for the initialization command. * @@ -36,7 +60,7 @@ const git = simpleGit(); */ export default function init(progress: InstanceType) { return async ( - name: string, + name?: string, targetDirectory?: string, options?: InitHandlerOptions, ): Promise => { @@ -44,6 +68,21 @@ export default function init(progress: InstanceType) { const { name: autoPlatformName, emulsifyParentDirectory } = (await getPlatformInfo()) || {}; + if (typeof name === 'undefined') { + questions[DIRECTORY].default = emulsifyParentDirectory; + const response = await inquirer.prompt(questions as any); + if (response?.targetDirectory) targetDirectory = response.targetDirectory; + if (response?.platform) options = { platform: response.platform }; + if (response?.name) name = response.name; + } + if (!name) { + return log( + 'error', + 'Unable to determine the project name. Please provide a valid project name.', + EXIT_ERROR, + ); + } + // If no platform name is given, and none can be detected, exit and error. const platformName = (options?.platform || autoPlatformName) as | Platform diff --git a/src/index.ts b/src/index.ts index 73a8627..8fcca44 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,7 +16,7 @@ program ); program - .command('init [path]', { + .command('init [name] [path]', { isDefault: true, }) .description('Initialize an Emulsify project')