Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
feat(app-types): scaffolded the chosen app type
Browse files Browse the repository at this point in the history
for #185
  • Loading branch information
travi committed Mar 31, 2019
1 parent ce40520 commit 7362abf
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 17 deletions.
14 changes: 8 additions & 6 deletions src/project-type/application.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import chalk from 'chalk';
import chooseApplicationType from './prompt';
import scaffoldChosenApplicationType from './choice-scaffolder';

export default async function ({applicationTypes}) {
export default async function ({applicationTypes, projectRoot}) {
console.log(chalk.blue('Scaffolding Application Details')); // eslint-disable-line no-console

await chooseApplicationType({types: applicationTypes});
const chosenType = await chooseApplicationType({types: applicationTypes});
const results = await scaffoldChosenApplicationType(applicationTypes, chosenType, {projectRoot});

return {
scripts: {start: './lib/index.js'},
devDependencies: [],
dependencies: [],
vcsIgnore: {files: [], directories: []}
scripts: {start: './lib/index.js', ...results.scripts},
dependencies: results.dependencies,
devDependencies: results.devDependencies,
vcsIgnore: {files: results.vcsIgnore.files, directories: results.vcsIgnore.directories}
};
}
7 changes: 7 additions & 0 deletions src/project-type/choice-scaffolder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function (typeScaffolders, chosenType, options) {
const typeScaffolder = typeScaffolders[chosenType];

if (typeScaffolder) return typeScaffolder(options);

return {scripts: {}, dependencies: [], devDependencies: [], vcsIgnore: {files: [], directories: []}};
}
8 changes: 5 additions & 3 deletions src/project-type/prompt.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {prompt, Separator} from 'inquirer';

export default function ({types}) {
return prompt([{
name: 'types',
export default async function ({types}) {
const answers = await prompt([{
name: 'type',
type: 'list',
message: 'What type of application is this?',
choices: [...Object.keys(types), new Separator(), 'Other']
}]);

return answers.type;
}
28 changes: 23 additions & 5 deletions test/unit/project-type/application-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {assert} from 'chai';
import any from '@travi/any';
import sinon from 'sinon';
import * as applicationChooser from '../../../src/project-type/prompt';
import * as choiceScaffolder from '../../../src/project-type/choice-scaffolder';
import scaffoldApplication from '../../../src/project-type/application';

suite('application project-type', () => {
Expand All @@ -12,22 +13,39 @@ suite('application project-type', () => {
sandbox = sinon.createSandbox();

sandbox.stub(applicationChooser, 'default');
sandbox.stub(choiceScaffolder, 'default');
});

teardown(() => sandbox.restore());

test('that details specific to an application project-type are scaffolded', async () => {
const applicationTypes = any.simpleObject();
const chosenApplicationType = any.word();
const scaffoldedTypeDependencies = any.listOf(any.string);
const scaffoldedTypeDevDependencies = any.listOf(any.string);
const scaffoldedTypeScripts = any.simpleObject();
const scaffoldedFilesToIgnore = any.listOf(any.string);
const scaffoldedDirectoriesToIgnore = any.listOf(any.string);
const typeScaffoldingResults = {
...any.simpleObject(),
dependencies: scaffoldedTypeDependencies,
devDependencies: scaffoldedTypeDevDependencies,
scripts: scaffoldedTypeScripts,
vcsIgnore: {files: scaffoldedFilesToIgnore, directories: scaffoldedDirectoriesToIgnore}
};
applicationChooser.default.withArgs({types: applicationTypes}).resolves(chosenApplicationType);
choiceScaffolder.default
.withArgs(applicationTypes, chosenApplicationType, {projectRoot})
.resolves(typeScaffoldingResults);

assert.deepEqual(
await scaffoldApplication({projectRoot, applicationTypes}),
{
scripts: {start: './lib/index.js'},
dependencies: [],
devDependencies: [],
vcsIgnore: {files: [], directories: []}
scripts: {start: './lib/index.js', ...scaffoldedTypeScripts},
dependencies: scaffoldedTypeDependencies,
devDependencies: scaffoldedTypeDevDependencies,
vcsIgnore: {files: scaffoldedFilesToIgnore, directories: scaffoldedDirectoriesToIgnore}
}
);
assert.calledWith(applicationChooser.default, {types: applicationTypes});
});
});
24 changes: 24 additions & 0 deletions test/unit/project-type/choice-scaffolder-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {assert} from 'chai';
import any from '@travi/any';
import sinon from 'sinon';
import scaffoldTypeChoice from '../../../src/project-type/choice-scaffolder';

suite('type choice scaffolder', () => {
test('that chosen type is scaffolded', async () => {
const chosenType = any.word();
const options = any.simpleObject();
const results = any.simpleObject();
const chosenTypeScaffolder = sinon.stub();
const typeScaffolders = {...any.simpleObject(), [chosenType]: chosenTypeScaffolder};
chosenTypeScaffolder.withArgs(options).resolves(results);

assert.equal(await scaffoldTypeChoice(typeScaffolders, chosenType, options), results);
});

test('that that choosing a type without a defined scaffolder does not result in an error', async () => {
assert.deepEqual(
await scaffoldTypeChoice(any.simpleObject(), any.string()),
{scripts: {}, dependencies: [], devDependencies: [], vcsIgnore: {files: [], directories: []}}
);
});
});
7 changes: 4 additions & 3 deletions test/unit/project-type/prompt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ suite('project-type prompts', () => {
teardown(() => sandbox.restore());

test('that the choice of application-type is presented', async () => {
const answers = any.simpleObject();
const chosenType = any.word();
const answers = {...any.simpleObject(), type: chosenType};
const types = any.simpleObject();
inquirer.prompt
.withArgs([{
name: 'types',
name: 'type',
type: 'list',
message: 'What type of application is this?',
choices: [...Object.keys(types), new inquirer.Separator(), 'Other']
}])
.resolves(answers);

assert.equal(await prompt({types}), answers);
assert.equal(await prompt({types}), chosenType);
});
});

0 comments on commit 7362abf

Please sign in to comment.