diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 7ead2dd..0000000 --- a/.babelrc +++ /dev/null @@ -1,9 +0,0 @@ -{ - "presets": [ - ["env", { - "targets": { - "node": "4" - } - }] - ] -} diff --git a/.gitignore b/.gitignore index 5c829f8..aae09ec 100644 --- a/.gitignore +++ b/.gitignore @@ -127,6 +127,5 @@ $RECYCLE.BIN/ # End of https://www.gitignore.io/api/macos,windows,linux,node -lib package-lock.json -yarn.lock \ No newline at end of file +yarn.lock diff --git a/index.js b/index.js new file mode 100644 index 0000000..33b4ee3 --- /dev/null +++ b/index.js @@ -0,0 +1,19 @@ +const types = require('conventional-changelog-metahub/types'); +const aliases = require('conventional-changelog-metahub/aliases'); +const commitizen = require('commitizen'); +const _ = require('lodash'); +const questions = require('./lib/questions'); +const format = require('./lib/format-commit'); + +const config = _.merge(commitizen.configLoader.load(), { + 'cz-conventional-commit': {maxSubjectLength: 72, bodyLineLength: 100, emoji: false}, +})['cz-conventional-commit']; + +module.exports = { + prompter(cz, commit) { + return cz + .prompt(questions({types: types.types, aliases}, config)) + .then(answers => format(answers, {types: types.types, aliases}, config)) + .then(commitMessage => commit(commitMessage)); + }, +}; diff --git a/src/format-commit.js b/lib/format-commit.js similarity index 82% rename from src/format-commit.js rename to lib/format-commit.js index fbb2d6b..681da95 100644 --- a/src/format-commit.js +++ b/lib/format-commit.js @@ -1,5 +1,5 @@ -import wrap from 'word-wrap'; -import {truncate} from 'lodash'; +const wrap = require('word-wrap'); +const _ = require('lodash'); /** * Create a formatted commit message based on answers provided by user. @@ -26,8 +26,7 @@ import {truncate} from 'lodash'; * @param {boolean} config.emoji `true` to add emoji at the end of the commit message * @return {string} formatted commit */ -export default function formatCommit(answers, options, config) { - const {types, aliases} = options; +module.exports = (answers, options, config) => { const wrapOptions = { trim: true, newline: '\n', @@ -37,12 +36,12 @@ export default function formatCommit(answers, options, config) { }; // Parentheses are only needed when a scope is present const scope = answers.scope ? answers.scope.trim() : ''; - const isAlias = !Object.prototype.hasOwnProperty.call(types, answers.type); - const type = isAlias ? aliases[answers.type].type : answers.type; - const emoji = config.emoji ? (isAlias ? aliases[answers.type].emoji : types[type].emoji) : ''; + const isAlias = !Object.prototype.hasOwnProperty.call(options.types, answers.type); + const type = isAlias ? options.aliases[answers.type].type : answers.type; + const emoji = config.emoji ? (isAlias ? options.aliases[answers.type].emoji : options.types[type].emoji) : ''; // Limit head to maxSubjectLength (including the trailing emoji) // We use the String.length function to determine the length of the emoji even it returns an innacurate value (2 chars per emoji), because most likely a commit hook or linter veryfyingt he head length will also use String.length. - const head = `${truncate(`${type}${scope ? `(${scope})` : ''}: ${answers.subject.trim()}`, { + const head = `${_.truncate(`${type}${scope ? `(${scope})` : ''}: ${answers.subject.trim()}`, { length: config.maxSubjectLength - (config.emoji ? emoji.length + 1 : 0), })}${config.emoji ? ` ${emoji}` : ''}`; const body = wrap(answers.body, wrapOptions); @@ -56,4 +55,4 @@ export default function formatCommit(answers, options, config) { .join('\n\n'); return [head, body, footer].filter(x => x).join('\n\n'); -} +}; diff --git a/src/questions.js b/lib/questions.js similarity index 79% rename from src/questions.js rename to lib/questions.js index 64d0be1..795bce5 100644 --- a/src/questions.js +++ b/lib/questions.js @@ -1,5 +1,5 @@ -import {map, padEnd, maxBy} from 'lodash'; -import {typesOrder} from 'conventional-changelog-metahub/types'; +const _ = require('lodash'); +const types = require('conventional-changelog-metahub/types'); /** * Create the Inquirer.js questions object. @@ -18,14 +18,15 @@ import {typesOrder} from 'conventional-changelog-metahub/types'; * @param {string} config.bodyLineLength length of body lines * @param {boolean} config.emoji `true` to add emoji at the end of the commit message */ -export default function questions(options, config) { - const {types, aliases} = options; - const allTypes = Object.assign({}, types, aliases); - const length = maxBy(Object.keys(allTypes), type => type.length).length + 1; - const choices = map(allTypes, (type, key) => ({ - name: `${padEnd(`${key}:`, length)} ${type.emoji} ${type.description}`, +module.exports = (options, config) => { + const allTypes = Object.assign({}, options.types, options.aliases); + const length = _.maxBy(Object.keys(allTypes), type => type.length).length + 1; + const choices = _.map(allTypes, (type, key) => ({ + name: `${_.padEnd(`${key}:`, length)} ${type.emoji} ${type.description}`, value: key, - })).sort((choice1, choice2) => (typesOrder.indexOf(choice1.value) < typesOrder.indexOf(choice2.value) ? -1 : 1)); + })).sort( + (choice1, choice2) => (types.typesOrder.indexOf(choice1.value) < types.typesOrder.indexOf(choice2.value) ? -1 : 1) + ); return [ {type: 'list', name: 'type', message: "Select the type of change that you're committing:", choices}, @@ -56,7 +57,7 @@ export default function questions(options, config) { }):\n`, default(answers) { if (answers.type === 'initial') { - return aliases[answers.type].description; + return options.aliases[answers.type].description; } return null; }, @@ -85,4 +86,4 @@ export default function questions(options, config) { }, }, ]; -} +}; diff --git a/package.json b/package.json index 9906cbb..62fb96e 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,6 @@ "devDependencies": { "@commitlint/core": "^6.0.2", "ava": "^0.24.0", - "babel-cli": "^6.26.0", - "babel-preset-env": "^1.6.0", "codecov": "^3.0.0", "conventional-commits-parser": "^2.0.0", "emoji-regex": "^6.5.1", @@ -24,7 +22,6 @@ "eslint-plugin-prettier": "^2.4.0", "nyc": "^11.1.0", "prettier": "~1.10.0", - "rimraf": "^2.6.1", "semantic-release": "^12.0.0", "sinon": "^4.0.0", "stringz": "^0.4.0", @@ -34,7 +31,8 @@ "node": ">=4" }, "files": [ - "lib" + "lib", + "index.js" ], "homepage": "https://github.com/vanduynslagerp/cz-conventional-commit#readme", "keywords": [ @@ -47,9 +45,10 @@ "git" ], "license": "MIT", - "main": "lib/index.js", + "main": "index.js", "nyc": { "include": [ + "index.js", "lib/**/*.js" ], "reporter": [ @@ -86,12 +85,11 @@ }, "scripts": { "codecov": "codecov -f coverage/coverage-final.json", - "compile": "rimraf lib && babel src --source-maps --out-dir lib", "lint": "xo", "prepublishOnly": "npm run compile", - "pretest": "npm run compile && npm run lint", + "pretest": "npm run lint", "semantic-release": "semantic-release", - "test": "nyc ava -vv" + "test": "nyc ava -v" }, "xo": { "extends": [ diff --git a/src/index.js b/src/index.js deleted file mode 100644 index fed2aca..0000000 --- a/src/index.js +++ /dev/null @@ -1,19 +0,0 @@ -import {types} from 'conventional-changelog-metahub/types'; -import aliases from 'conventional-changelog-metahub/aliases'; -import {configLoader} from 'commitizen'; -import {merge} from 'lodash'; -import questions from './questions'; -import format from './format-commit'; - -const config = merge(configLoader.load(), { - 'cz-conventional-commit': {maxSubjectLength: 72, bodyLineLength: 100, emoji: false}, -})['cz-conventional-commit']; - -module.exports = { - prompter(cz, commit) { - return cz - .prompt(questions({types, aliases}, config)) - .then(answers => format(answers, {types, aliases}, config)) - .then(commitMessage => commit(commitMessage)); - }, -}; diff --git a/test/integration.test.js b/test/integration.test.js index c8b56f2..5eabe8d 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -4,7 +4,7 @@ import parser from 'conventional-commits-parser'; import {lint, load} from '@commitlint/core'; import {types} from 'conventional-changelog-metahub/types'; import aliases from 'conventional-changelog-metahub/aliases'; -import engine from '../lib/index'; +import engine from '..'; // eslint-disable-next-line ava/no-cb-test test.cb(