Skip to content

Commit

Permalink
feat: ✨ support multi-line longer description
Browse files Browse the repository at this point in the history
using external-editor module

resolve #40, #43
  • Loading branch information
vivaxy committed Jun 23, 2020
1 parent 000a124 commit 42c431a
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ Default configuration:
"add": true,
"push": true,
"emoji": "code",
"editor": false,
"hooks": {
"postpush": "echo 'after push'"
"postpush": ""
}
}
```
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@
},
"homepage": "/~https://github.com/vivaxy/gacp#readme",
"dependencies": {
"@commitlint/load": "^9.0.0",
"@commitlint/load": "^9.0.1",
"@commitlint/types": "^9.0.1",
"@vivaxy/git": "^4.1.1",
"chalk": "^4.0.0",
"conventional-commit-types": "^3.0.0",
"cosmiconfig": "^6.0.0",
"execa": "^4.0.0",
"external-editor": "^3.1.0",
"figures": "^3.0.0",
"fs-extra": "^9.0.0",
"log-util": "^2.3.0",
Expand All @@ -60,7 +62,6 @@
"devDependencies": {
"@commitlint/cli": "^9.0.0",
"@commitlint/config-conventional": "^9.0.0",
"@types/cosmiconfig": "^5.0.3",
"@types/execa": "^0.9.0",
"@types/fs-extra": "^9.0.1",
"@types/jest": "^26.0.0",
Expand Down
8 changes: 7 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ async function configureYargs() {
choices: ['code', 'emoji'],
default: 'code',
},
editor: {
type: 'boolean',
desc: 'use node external editor in longer description',
default: false,
},
logLevel: {
type: 'number',
desc: 'log level',
Expand Down Expand Up @@ -85,13 +90,14 @@ function notifyUpdate() {
try {
notifyUpdate();
const extraConfigs = await configureYargs();
const { logLevel, cwd, emoji, add, push } = yargs.argv;
const { logLevel, cwd, emoji, add, push, editor } = yargs.argv;
log.setLevel(logLevel as number);
await gacp({
cwd: cwd as string,
add: add as boolean,
push: push as boolean,
emoji: emoji as EMOJI_TYPES,
editor: editor as boolean,
hooks: {
postpush: extraConfigs.hooks.postpush,
},
Expand Down
8 changes: 6 additions & 2 deletions src/gacp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ async function runTasks({
add,
push,
emoji,
editor,
cwd,
hooks,
}: {
add: boolean;
push: boolean;
emoji: EMOJI_TYPES;
editor: boolean;
cwd: string;
hooks: Hooks;
}) {
Expand All @@ -55,7 +57,7 @@ async function runTasks({

// prompt first before performing any actions
if (needsCommit) {
commitMessage = await prompt({ emojiType: emoji });
commitMessage = await prompt({ emojiType: emoji, editor });
debug('commitMessage:', commitMessage);
}

Expand Down Expand Up @@ -100,16 +102,18 @@ export default async function gacp({
add,
push,
emoji,
editor,
hooks,
}: {
cwd: string;
add: boolean;
push: boolean;
emoji: EMOJI_TYPES;
editor: boolean;
hooks: Hooks;
}) {
const startTime = getNow();
await runTasks({ cwd, add, push, emoji, hooks });
await runTasks({ cwd, add, push, emoji, editor, hooks });
const endTime = getNow();
log.success(`Done in ${(endTime - startTime) / 1000}s`);
await flushHistory();
Expand Down
12 changes: 4 additions & 8 deletions src/messages/commitlint-config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// @ts-ignore
import * as load from '@commitlint/load';
import load from '@commitlint/load';
import { QualifiedRules, RuleConfigTuple } from '@commitlint/types';

export type CommitlintRule = [number, string, number];

export async function getCommitlintConfigRules(): Promise<{
[key: string]: CommitlintRule;
}> {
export async function getCommitlintConfigRules(): Promise<QualifiedRules> {
let rules;
try {
const config = await load();
Expand All @@ -17,7 +13,7 @@ export async function getCommitlintConfigRules(): Promise<{
}

export function getRuleValue(
[level, applicable, value]: CommitlintRule = [0, 'never', 0],
[level, applicable, value]: RuleConfigTuple<number> = [0, 'never', 0],
defaultValue: number,
) {
return level === 2 && applicable === 'always' ? value : defaultValue;
Expand Down
20 changes: 10 additions & 10 deletions src/messages/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PATHS } from '../configs';
const { GACPHOME, HISTORY_FILE_NAME } = PATHS;
const historyFile = path.join(GACPHOME, HISTORY_FILE_NAME);

export interface History {
export interface Messages {
type: string;
scope: string;
gitmoji: string;
Expand All @@ -20,9 +20,7 @@ export interface History {
footer: string;
}

let cache: History | null = null;

const defaultCache = {
export const DEFAULT_MESSAGES: Messages = {
type: '',
scope: '',
gitmoji: '',
Expand All @@ -31,32 +29,34 @@ const defaultCache = {
footer: '',
};

let cache: Messages = DEFAULT_MESSAGES;

function debug(...message: any[]) {
log.debug('gacp:history', ...message);
}

export function getHistory(): History {
export function getHistory(): Messages {
debug('reading history');
if (cache) {
// already read from file system
return cache;
}
try {
cache = require(historyFile) as History;
cache = require(historyFile) as Messages;
} catch (e) {
debug('history file not exists');
cache = defaultCache;
cache = DEFAULT_MESSAGES;
}
return cache;
}

export function setHistory(history: History) {
export function setHistory(history: Partial<Messages>) {
debug(`set: ${JSON.stringify(history)}`);
cache = history || {};
cache = { ...cache, ...history };
}

export function clearHistory() {
cache = defaultCache;
cache = DEFAULT_MESSAGES;
}

export async function flushHistory() {
Expand Down
42 changes: 31 additions & 11 deletions src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
* @since 2016-11-23 10:23
* @author vivaxy
*/

import * as log from 'log-util';
import * as wrap from 'word-wrap';
import * as prompts from 'prompts';
import { edit } from 'external-editor';
import { RuleConfigTuple } from '@commitlint/types';

import { getHistory, setHistory, History } from './messages/history';
import {
getHistory,
setHistory,
Messages,
DEFAULT_MESSAGES,
} from './messages/history';

import { getCommitTypes, updateTypesStat } from './messages/commit-types';

import { getGitmojis, updateGitmojisStat } from './messages/gitmojis';
import {
CommitlintRule,
getCommitlintConfigRules,
getRuleValue,
} from './messages/commitlint-config';
Expand All @@ -24,8 +29,10 @@ function debug(...message: any[]) {
}

export default async function prompt({
editor,
emojiType,
}: {
editor: boolean;
emojiType: EMOJI_TYPES;
}) {
const [typeList, gitmojiList] = await Promise.all([
Expand All @@ -35,7 +42,7 @@ export default async function prompt({
const history = getHistory();

function findInitial(list: Array<{ value: string }>, key: string) {
const index = list.findIndex(function({ value }) {
const index = list.findIndex(function ({ value }) {
return value === key;
});
if (index === -1) {
Expand All @@ -51,7 +58,7 @@ export default async function prompt({
if (!input) {
return choices;
}
return choices.filter(function({ title, value }) {
return choices.filter(function ({ title, value }) {
return (
title.toLowerCase().indexOf(input.toLowerCase()) > -1 ||
value.toLowerCase().indexOf(input.toLowerCase()) > -1
Expand Down Expand Up @@ -111,12 +118,25 @@ export default async function prompt({
},
];

const answers = (await prompts(questions, {
onCancel() {
process.exit(0);
},
})) as History;
const answers: Messages = DEFAULT_MESSAGES;

for (const q of questions) {
let answer = {};
if (editor && q.name === 'body') {
answer = {
body: edit(history.body),
};
} else {
answer = await prompts(q, {
onCancel() {
process.exit(0);
},
});
}

debug('got answer', answer);
Object.assign(answers, answer);
}
debug('got answers', answers);
setHistory(answers);

Expand Down Expand Up @@ -148,7 +168,7 @@ export default async function prompt({
};
}

function wrapWords(words: string, rule: CommitlintRule): string {
function wrapWords(words: string, rule?: RuleConfigTuple<number>): string {
const maxLineWidth = getRuleValue(rule, words.length);
return wrap(words, getWrapOptions(maxLineWidth));
}
Expand Down
36 changes: 28 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@
"@commitlint/rules" "^9.0.1"
"@commitlint/types" "^9.0.1"

"@commitlint/load@^9.0.0", "@commitlint/load@^9.0.1":
"@commitlint/load@^9.0.1":
version "9.0.1"
resolved "https://registry.yarnpkg.com/@commitlint/load/-/load-9.0.1.tgz#02ba5795e56da9d6c83dccdc36a6ac940d048c36"
integrity sha512-6ix/pUjVAggmDLTcnpyk0bgY3H9UBBTsEeFvTkHV+WQ6LNIxsQk8SwEOEZzWHUqt0pxqMQeiUgYeSZsSw2+uiw==
Expand Down Expand Up @@ -695,12 +695,6 @@
resolved "https://registry.yarnpkg.com/@types/configstore/-/configstore-4.0.0.tgz#cb718f9507e9ee73782f40d07aaca1cd747e36fa"
integrity sha512-SvCBBPzOIe/3Tu7jTl2Q8NjITjLmq9m7obzjSyb8PXWWZ31xVK6w4T6v8fOx+lrgQnqk3Yxc00LDolFsSakKCA==

"@types/cosmiconfig@^5.0.3":
version "5.0.3"
resolved "https://registry.yarnpkg.com/@types/cosmiconfig/-/cosmiconfig-5.0.3.tgz#880644bb155d4038d3b752159684b777b0a159dd"
dependencies:
"@types/node" "*"

"@types/execa@^0.9.0":
version "0.9.0"
resolved "https://registry.yarnpkg.com/@types/execa/-/execa-0.9.0.tgz#9b025d2755f17e80beaf9368c3f4f319d8b0fb93"
Expand Down Expand Up @@ -1306,6 +1300,11 @@ char-regex@^1.0.2:
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"
integrity sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==

chardet@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==

ci-info@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-2.0.0.tgz#67a9e964be31a51e15e5010d58e6f12834002f46"
Expand Down Expand Up @@ -2113,6 +2112,15 @@ extend@~3.0.2:
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa"
integrity sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==

external-editor@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/external-editor/-/external-editor-3.1.0.tgz#cb03f740befae03ea4d283caed2741a83f335495"
integrity sha512-hMQ4CX1p1izmuLYyZqLMO/qGNw10wSv9QDCPfzXfyFrOaCSSoRfqE1Kf1s5an66J5JZC62NewG+mK49jOCtQew==
dependencies:
chardet "^0.7.0"
iconv-lite "^0.4.24"
tmp "^0.0.33"

extglob@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/extglob/-/extglob-2.0.4.tgz#ad00fe4dc612a9232e8718711dc5cb5ab0285543"
Expand Down Expand Up @@ -2598,7 +2606,7 @@ husky@^4.0.0:
slash "^3.0.0"
which-pm-runs "^1.0.0"

iconv-lite@0.4.24:
iconv-lite@0.4.24, iconv-lite@^0.4.24:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
integrity sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==
Expand Down Expand Up @@ -4052,6 +4060,11 @@ optionator@^0.8.1:
type-check "~0.3.2"
word-wrap "~1.2.3"

os-tmpdir@~1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
integrity sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=

p-cancelable@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/p-cancelable/-/p-cancelable-1.1.0.tgz#d078d15a3af409220c886f1d9a0ca2e441ab26cc"
Expand Down Expand Up @@ -5218,6 +5231,13 @@ through@2, "through@>=2.2.7 <3", through@^2.3.8:
version "2.3.8"
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"

tmp@^0.0.33:
version "0.0.33"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
integrity sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw==
dependencies:
os-tmpdir "~1.0.2"

tmpl@1.0.x:
version "1.0.4"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1"
Expand Down

0 comments on commit 42c431a

Please sign in to comment.