Skip to content

Commit

Permalink
refactor(language): encapsulated the prompt with the scaffolding manager
Browse files Browse the repository at this point in the history
  • Loading branch information
travi committed Jul 30, 2024
1 parent 3cf9dbb commit 493d939
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
7 changes: 6 additions & 1 deletion src/language/scaffolder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export default function (languagePlugins, chosenLanguage, options) {
import {questionNames} from '../prompts/question-names.js';
import promptForLanguageDetails from './prompt.js';

export default async function (languagePlugins, decisions, options) {
const {[questionNames.PROJECT_LANGUAGE]: chosenLanguage} = await promptForLanguageDetails(languagePlugins, decisions);

const plugin = languagePlugins[chosenLanguage];

if (plugin) return plugin.scaffold(options);
Expand Down
14 changes: 12 additions & 2 deletions src/language/scaffolder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@ import {describe, expect, it, vi} from 'vitest';
import any from '@travi/any';
import {when} from 'jest-when';

import * as languagePrompt from './prompt.js';
import {questionNames} from '../prompts/question-names.js';
import scaffold from './scaffolder.js';

vi.mock('./prompt.js');

describe('language scaffolder', () => {
it('should scaffold the chosen language', async () => {
const options = any.simpleObject();
const chosenLanguage = any.word();
const scaffolderResult = any.simpleObject();
const decisions = any.simpleObject();
const chosenLanguageScaffolder = vi.fn();
const plugins = {...any.simpleObject(), [chosenLanguage]: {scaffold: chosenLanguageScaffolder}};
when(languagePrompt.default)
.calledWith(plugins, decisions)
.mockResolvedValue({[questionNames.PROJECT_LANGUAGE]: chosenLanguage});
when(chosenLanguageScaffolder).calledWith(options).mockResolvedValue(scaffolderResult);

expect(await scaffold(plugins, chosenLanguage, options)).toEqual(scaffolderResult);
expect(await scaffold(plugins, decisions, options)).toEqual(scaffolderResult);
});

it('should not result in an error when choosing a language without a defined scaffolder', async () => {
await scaffold(any.simpleObject(), any.word(), any.simpleObject());
when(languagePrompt.default).mockResolvedValue({[questionNames.PROJECT_LANGUAGE]: any.word()});

await scaffold(any.simpleObject(), any.simpleObject(), any.simpleObject());
});
});
6 changes: 2 additions & 4 deletions src/scaffolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {reportResults} from '@form8ion/results-reporter';
import {scaffold as scaffoldReadme} from '@form8ion/readme';
import {info} from '@travi/cli-messages';

import {prompt as promptForLanguageDetails, scaffold as scaffoldLanguage} from './language/index.js';
import {scaffold as scaffoldLanguage} from './language/index.js';
import {scaffold as scaffoldGit} from './vcs/git/git.js';
import {scaffold as scaffoldLicense} from './license/index.js';
import scaffoldDependencyUpdater from './dependency-updater/scaffolder.js';
Expand Down Expand Up @@ -45,11 +45,9 @@ export async function scaffold(options) {
{projectRoot, vcs: vcsResults.vcs}
);

const {[questionNames.PROJECT_LANGUAGE]: projectLanguage} = await promptForLanguageDetails(languages, decisions);

const language = await scaffoldLanguage(
languages,
projectLanguage,
decisions,
{projectRoot, projectName, vcs: vcsResults.vcs, visibility, license: chosenLicense || 'UNLICENSED', description}
);

Expand Down
24 changes: 3 additions & 21 deletions src/scaffolder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {when} from 'jest-when';
import {scaffold as scaffoldGit} from './vcs/git/git.js';
import * as licenseScaffolder from './license/scaffolder.js';
import scaffoldLanguage from './language/scaffolder.js';
import * as languagePrompt from './language/prompt.js';
import * as dependencyUpdaterScaffolder from './dependency-updater/scaffolder.js';
import * as optionsValidator from './options-validator.js';
import * as prompts from './prompts/questions.js';
Expand All @@ -28,7 +27,6 @@ vi.mock('./readme');
vi.mock('./vcs/git/git.js');
vi.mock('./license/scaffolder');
vi.mock('./language/scaffolder');
vi.mock('./language/prompt');
vi.mock('./dependency-updater/scaffolder');
vi.mock('./options-validator');
vi.mock('./prompts/questions');
Expand All @@ -44,7 +42,6 @@ describe('project scaffolder', () => {
const description = any.string();
const homepage = any.url();
const license = any.string();
const projectLanguage = any.word();
const licenseBadge = any.url();
const languages = any.simpleObject();
const vcsHosts = any.simpleObject();
Expand Down Expand Up @@ -111,9 +108,6 @@ describe('project scaffolder', () => {
[coreQuestionNames.COPYRIGHT_YEAR]: year,
[coreQuestionNames.VISIBILITY]: visibility
});
when(languagePrompt.default)
.calledWith(languages, decisions)
.mockResolvedValue({[questionNames.PROJECT_LANGUAGE]: projectLanguage});
when(scaffoldGit)
.calledWith(gitRepoShouldBeInitialized, projectPath, projectName, description, vcsHosts, visibility, decisions)
.mockResolvedValue(vcsResults);
Expand Down Expand Up @@ -154,7 +148,6 @@ describe('project scaffolder', () => {
[questionNames.GIT_REPO]: gitRepoShouldBeInitialized
});
when(scaffoldGit).mockResolvedValue(vcsResults);
languagePrompt.default.mockResolvedValue({});

await scaffold();

Expand All @@ -166,7 +159,6 @@ describe('project scaffolder', () => {
const emptyOptions = {};
when(optionsValidator.validate).calledWith(emptyOptions).mockReturnValue({plugins: {}});
when(prompts.promptForBaseDetails).calledWith(projectPath, undefined).mockResolvedValue({});
languagePrompt.default.mockResolvedValue({});
scaffoldGit.mockResolvedValue({});

await scaffold(emptyOptions);
Expand Down Expand Up @@ -207,7 +199,6 @@ describe('project scaffolder', () => {
scaffoldLanguage.mockResolvedValue(languageResults);
dependencyUpdaterScaffolder.default.mockResolvedValue({badges: dependencyUpdaterBadges});
licenseScaffolder.default.mockResolvedValue({badges: licenseBadges});
languagePrompt.default.mockResolvedValue({});
when(scaffoldGit).mockResolvedValue(vcsResults);

await scaffold(options);
Expand All @@ -218,7 +209,6 @@ describe('project scaffolder', () => {
it('should not scaffold the git repo if not requested', async () => {
when(optionsValidator.validate).calledWith(options).mockReturnValue({plugins: {}});
prompts.promptForBaseDetails.mockResolvedValue({[questionNames.GIT_REPO]: false});
languagePrompt.default.mockResolvedValue({});
scaffoldReadme.mockResolvedValue();
scaffoldGit.mockResolvedValue({});

Expand Down Expand Up @@ -258,10 +248,7 @@ describe('project scaffolder', () => {
[coreQuestionNames.LICENSE]: license,
[coreQuestionNames.DESCRIPTION]: description
});
when(languagePrompt.default)
.calledWith(languages, decisions)
.mockResolvedValue({[questionNames.PROJECT_LANGUAGE]: projectLanguage});
when(scaffoldLanguage).calledWith(languages, projectLanguage, {
when(scaffoldLanguage).calledWith(languages, decisions, {
projectName,
projectRoot: projectPath,
visibility,
Expand Down Expand Up @@ -293,9 +280,6 @@ describe('project scaffolder', () => {
[coreQuestionNames.LICENSE]: license,
[coreQuestionNames.DESCRIPTION]: description
});
when(languagePrompt.default)
.calledWith(languages, decisions)
.mockResolvedValue({[questionNames.PROJECT_LANGUAGE]: projectLanguage});
scaffoldLanguage.mockResolvedValue({});
dependencyUpdaterScaffolder.default.mockResolvedValue({});
licenseScaffolder.default.mockResolvedValue({});
Expand All @@ -308,16 +292,15 @@ describe('project scaffolder', () => {
});

it('should pass the license to the language scaffolder as `UNLICENSED` when no license was chosen', async () => {
when(optionsValidator.validate).calledWith(options).mockReturnValue({plugins: {languages}});
when(optionsValidator.validate).calledWith(options).mockReturnValue({plugins: {languages}, decisions});
prompts.promptForBaseDetails.mockResolvedValue({});
languagePrompt.default.mockResolvedValue({[questionNames.PROJECT_LANGUAGE]: projectLanguage});
scaffoldGit.mockResolvedValue(vcsResults);

await scaffold(options);

expect(scaffoldLanguage).toHaveBeenCalledWith(
languages,
projectLanguage,
decisions,
{
license: 'UNLICENSED',
description: undefined,
Expand All @@ -332,7 +315,6 @@ describe('project scaffolder', () => {
it('should not run a verification command when one is not provided', async () => {
when(optionsValidator.validate).calledWith(options).mockReturnValue({plugins: {}});
prompts.promptForBaseDetails.mockResolvedValue({});
languagePrompt.default.mockResolvedValue({});
scaffoldGit.mockResolvedValue({});
scaffoldLanguage.mockResolvedValue({badges: {}, projectDetails: {}});

Expand Down

0 comments on commit 493d939

Please sign in to comment.