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

Commit

Permalink
refactor: 👌 do dependency injection of config
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed May 23, 2018
1 parent d9d0113 commit abab6cd
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 147 deletions.
File renamed without changes.
83 changes: 83 additions & 0 deletions src/createPrompter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const fs = require('fs');
const inquirer = require('inquirer');
const wrap = require('word-wrap');
const appRoot = require('app-root-path');
const {
createPackagesQuestion,
createQuestions
} = require('./questions');
const LimitedInput = require('./LimitedInput');
const {
getAllPackages,
getChangedPackages
} = require('./lernaUtils');

const MAX_LINE_WIDTH = 72;

inquirer.registerPrompt('limitedInput', LimitedInput);

const IS_LERNA_PROJECT = fs.existsSync(appRoot.resolve('lerna.json'));

const makeAffectsLine = function (answers) {
const selectedPackages = answers.packages;

if (selectedPackages && selectedPackages.length) {
return `\naffects: ${selectedPackages.join(', ')}`;
}

return '';
};

module.exports = (config) => {
const prompter = {
prompter (cz, commit) {
let promptQuestions = createQuestions(config);

if (IS_LERNA_PROJECT) {
const allPackages = getAllPackages().map((pkg) => pkg.name);
const changedPackages = getChangedPackages();

promptQuestions = promptQuestions.concat(createPackagesQuestion(allPackages, changedPackages));
}

return inquirer.prompt(promptQuestions)
.then((answers) => {
const wrapOptions = {
indent: '',
trim: true,
width: MAX_LINE_WIDTH
};

const emoji = config.types[answers.type].emoji;
const emojiPrefix = emoji ? emoji + ' ' : '';
const head = answers.type + ': ' + emojiPrefix + answers.subject;
const affectsLine = makeAffectsLine(answers);

// Wrap these lines at MAX_LINE_WIDTH character
const body = wrap(answers.body + affectsLine, wrapOptions);
const breaking = wrap(answers.breaking, wrapOptions);
const footer = wrap(answers.footer, wrapOptions);

let msg;

msg = head;

if (body) {
msg += '\n\n' + body;
}

if (breaking) {
msg += '\n\nBREAKING CHANGE: ' + breaking;
}

if (footer) {
msg += '\n\nIssues: ' + footer;
}

return commit(msg);
});
}
};

return prompter;
};
82 changes: 3 additions & 79 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,4 @@
const fs = require('fs');
const inquirer = require('inquirer');
const wrap = require('word-wrap');
const appRoot = require('app-root-path');
const defaults = require('./defaults');
const {
makePackagesQuestion,
questions
} = require('./prompt/questions');
const LimitedInput = require('./prompt/LimitedInput');
const {
getAllPackages,
getChangedPackages
} = require('./lernaUtils');
const createPrompter = require('./createPrompter');
const config = require('./defaults');

const MAX_LINE_WIDTH = 72;

inquirer.registerPrompt('limitedInput', LimitedInput);

const IS_LERNA_PROJECT = fs.existsSync(appRoot.resolve('lerna.json'));

const makeAffectsLine = function (answers) {
const selectedPackages = answers.packages;

if (selectedPackages && selectedPackages.length) {
return `\naffects: ${selectedPackages.join(', ')}`;
}

return '';
};

module.exports = {
prompter (cz, commit) {
let promptQuestions = questions;

if (IS_LERNA_PROJECT) {
const allPackages = getAllPackages().map((pkg) => pkg.name);
const changedPackages = getChangedPackages();

promptQuestions = promptQuestions.concat(makePackagesQuestion(allPackages, changedPackages));
}

return inquirer.prompt(promptQuestions)
.then((answers) => {
const wrapOptions = {
indent: '',
trim: true,
width: MAX_LINE_WIDTH
};

const emoji = defaults.types[answers.type].emoji;
const emojiPrefix = emoji ? emoji + ' ' : '';
const head = answers.type + ': ' + emojiPrefix + answers.subject;
const affectsLine = makeAffectsLine(answers);

// Wrap these lines at MAX_LINE_WIDTH character
const body = wrap(answers.body + affectsLine, wrapOptions);
const breaking = wrap(answers.breaking, wrapOptions);
const footer = wrap(answers.footer, wrapOptions);

let msg;

msg = head;

if (body) {
msg += '\n\n' + body;
}

if (breaking) {
msg += '\n\nBREAKING CHANGE: ' + breaking;
}

if (footer) {
msg += '\n\nIssues: ' + footer;
}

return commit(msg);
});
}
};
module.exports = createPrompter(config);
68 changes: 0 additions & 68 deletions src/prompt/questions.js

This file was deleted.

68 changes: 68 additions & 0 deletions src/questions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const pad = require('pad-right');

const typeToListItem = ({description, emoji, value}) => ({
name: (emoji || '❔') + ' ' + pad(value + ':', 12, ' ') + description,
value
});

const createQuestions = (config) => {
const MIN_SUBJECT_LENGTH_ERROR_MESSAGE = `The subject must have at least ${config.minMessageLength} characters`;
const questions = [
{
choices: config.list.map((type) => typeToListItem(config.types[type])),
message: 'Select the type of change that you\'re committing:',
name: 'type',
type: 'list'
},
{
filter: (input) => {
let subject;

subject = input.trim();
while (subject.endsWith('.')) {
subject = subject.substr(0, subject.length - 1).trim();
}

return subject;
},
leadingLabel: (answers) => `${answers.type}:`,

// Minus 3 chars are for emoji + space.
maxLength: config.maxMessageLength - 3,
message: 'Write a short, imperative mood description of the change:',
name: 'subject',
type: 'limitedInput',
validate: (input) => input.length >= config.minMessageLength || MIN_SUBJECT_LENGTH_ERROR_MESSAGE
},
{
message: 'Provide a longer description of the change:\n',
name: 'body',
type: 'input'
},
{
message: 'List any breaking changes:\n BREAKING CHANGE:',
name: 'breaking',
type: 'input'
},
{
message: 'Reference any task that this commit closes:\n Issues:',
name: 'footer',
type: 'input'
}
];

return questions;
};

const createPackagesQuestion = (allPackages, changedPackages) => ({
choices: allPackages,
default: changedPackages,
message: `The packages that this commit has affected (${changedPackages.length} detected)\n`,
name: 'packages',
type: 'checkbox'
});

module.exports = {
createPackagesQuestion,
createQuestions
};

0 comments on commit abab6cd

Please sign in to comment.