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

Commit

Permalink
feat: refactor questions out
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 5, 2018
1 parent 065bfae commit 5212e93
Show file tree
Hide file tree
Showing 21 changed files with 211 additions and 119 deletions.
18 changes: 13 additions & 5 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
const {prompter} = require('.');
const signale = require('signale');
const createState = require('./createState');
const runInteractiveQuestions = require('./runInteractiveQuestions');

const cz = {};
const onCommit = (data) => {
console.log('receibed for commit', data);
const main = async () => {
try {
const state = createState();
const answers = await runInteractiveQuestions(state);

console.log(answers);
} catch (error) {
signale.fatal(error);
}
};

prompter(cz, onCommit);
main();
83 changes: 37 additions & 46 deletions lib/createPrompter.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
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 LimitedInputPrompt = require('./LimitedInputPrompt');
const {
getAllPackages,
getChangedPackages
} = require('./lernaUtils');
const runInteractiveQuestions = require('./runInteractiveQuestions');

const MAX_LINE_WIDTH = 72;

inquirer.registerPrompt('limitedInput', LimitedInputPrompt);

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

/*
const makeAffectsLine = function (answers) {
const selectedPackages = answers.packages;
Expand All @@ -27,55 +19,54 @@ const makeAffectsLine = function (answers) {
return '';
};
*/

module.exports = (config) => {
module.exports = (state) => {
const prompter = {
prompter (cz, commit) {
let promptQuestions = createQuestions(config);
async prompter (cz, commit) {
const answers = await runInteractiveQuestions(state);

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

promptQuestions = promptQuestions.concat(createPackagesQuestion(allPackages, changedPackages));
}
// 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 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);
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);
// 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;
let msg;

msg = head;
msg = head;

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

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

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

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

Expand Down
9 changes: 9 additions & 0 deletions lib/createQuestions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable import/no-dynamic-require, global-require */

const createQuestions = (state) => {
const questions = state.config.questions.map((name) => require('./questions/' + name).createQuestion(state));

return questions.filter(Boolean);
};

module.exports = createQuestions;
13 changes: 13 additions & 0 deletions lib/createState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const appRoot = require('app-root-path');
const getConfig = require('./getConfig');

const createState = () => {
const state = {
config: getConfig(),
root: String(appRoot)
};

return state;
};

module.exports = createState;
13 changes: 12 additions & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const list = [

// https://github.com/angular/angular/blob/master/CONTRIBUTING.md#scope
const scopes = [
null,
'',
'init',
'runner',
'watcher',
Expand All @@ -70,10 +70,21 @@ const scopes = [
'changelog'
];

const questions = [
'type',
'scope',
'title',
'body',
'breaking',
'issues',
// 'lerna'
];

module.exports = {
list,
maxMessageLength: 50,
minMessageLength: 3,
questions,
scopes,
types
};
24 changes: 15 additions & 9 deletions lib/getConfig.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable global-require, import/no-dynamic-require */
const path = require('path');
const defaults = require('./defaults');
const fs = require('fs');
const signale = require('signale');
const defaults = require('./defaults');

const configFiles = [
'.git-cz.json',
'changelog.config.js',
'changelog.config.json'
];
Expand All @@ -12,20 +14,24 @@ const findOverrides = () => {
const dir = process.cwd();

for (const file of configFiles) {
try {
return require(path.join(dir, file));
// eslint-disable-next-line no-empty
} catch (error) {}
const filename = path.join(dir, file);

if (fs.existsSync(filename)) {
return require(filename);
}
}

try {
const changelog = require(path.join(dir, 'package.json')).config.commitizen.changelog;
const pkgFilename = path.join(dir, 'package.json');

if (fs.existsSync(pkgFilename)) {
const changelog = require(pkgFilename).config.commitizen.changelog;

if (changelog) {
return changelog;
}
// eslint-disable-next-line no-empty
} catch (error) {}
} else {
signale.error('package.json not found, at ' + pkgFilename);
}

return {};
};
Expand Down
2 changes: 0 additions & 2 deletions lib/lernaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const getAllPackages = function () {
};

const getChangedPackages = function () {
const shell = require('shelljs');

const changedFiles = shell.exec('git diff --cached --name-only', {silent: true})
.stdout
.split('\n');
Expand Down
55 changes: 0 additions & 55 deletions lib/questions.js

This file was deleted.

9 changes: 9 additions & 0 deletions lib/questions/body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.createQuestion = () => {
const question = {
message: 'Provide a longer description of the change:\n ',
name: 'body',
type: 'input'
};

return question;
};
11 changes: 11 additions & 0 deletions lib/questions/breaking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const chalk = require('chalk');

exports.createQuestion = () => {
const question = {
message: `List any breaking changes:\n ${chalk.red('BREAKING CHANGE')}:`,
name: 'breaking',
type: 'input'
};

return question;
};
File renamed without changes.
11 changes: 11 additions & 0 deletions lib/questions/lerna.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports.createQuestion = (state) => {
const question = {
choices: allPackages,
default: changedPackages,
message: `The packages that this commit has affected (${changedPackages.length} detected)\n`,
name: 'packages',
type: 'checkbox'
};

return question;
};
25 changes: 25 additions & 0 deletions lib/questions/scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
exports.createQuestion = (state) => {
const {scopes} = state.config;

if (!scopes) {
return null;
}

if (!Array.isArray(scopes)) {
throw new TypeError('scopes must be an array of strings.');
}

if (scopes.length < 1) {
return null;
}

const question = {
choices: scopes,
default: 0,
message: 'Select the scope this component affects:\n',
name: 'scope',
type: 'list'
};

return question;
};
26 changes: 26 additions & 0 deletions lib/questions/title.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
exports.createQuestion = (state) => {
const {config} = state;
const minTitleLengthErrorMessage = `The subject must have at least ${config.minMessageLength} characters`;
const question = {
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 || minTitleLengthErrorMessage
};

return question;
};
Loading

0 comments on commit 5212e93

Please sign in to comment.