Skip to content

Commit

Permalink
feat: adjusting the init command to work without parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
robherba committed Sep 20, 2024
1 parent 8df13a7 commit 1dda1bd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
32 changes: 29 additions & 3 deletions src/handlers/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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');
Expand Down
41 changes: 40 additions & 1 deletion src/handlers/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,6 +27,28 @@ import { EXIT_ERROR } from '../lib/constants.js';

const git = simpleGit();

export const DIRECTORY = 1;
export const questions: AnyQuestion<String>[] = [
{
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.
*
Expand All @@ -36,14 +60,29 @@ const git = simpleGit();
*/
export default function init(progress: InstanceType<typeof ProgressBar>) {
return async (
name: string,
name?: string,
targetDirectory?: string,
options?: InitHandlerOptions,
): Promise<void> => {
// Load information about the project and platform.
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
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ program
);

program
.command('init <name> [path]', {
.command('init [name] [path]', {
isDefault: true,
})
.description('Initialize an Emulsify project')
Expand Down

0 comments on commit 1dda1bd

Please sign in to comment.