UNPKG

@toktokhan-dev/cli-plugin-commit

Version:
232 lines (218 loc) 8.01 kB
import { defineCommand } from '@toktokhan-dev/cli'; import { $ } from '@toktokhan-dev/node'; import { removeStr, runIfFn } from '@toktokhan-dev/universal'; import clear from 'clear'; import enquirer from 'enquirer'; import { execSync } from 'child_process'; import { readFileSync } from 'fs'; import flow from 'lodash/flow.js'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; const INITIAL_COMMIT_TYPES = [ { name: 'deploy', description: '프로젝트 배포', emoji: '🚀', }, { name: 'chore', description: '자잘한 수정', emoji: '🤖', }, { name: 'docs', description: '문서 관련', emoji: '📝 ', }, { name: 'feat', description: '새로운 기능, 페이지 추가', emoji: '🎸', }, { name: 'fix', description: '버그 수정', emoji: '🐛', }, { name: 'perf', description: '성능 개선', emoji: '👽', }, { name: 'refactor', description: '코드 리팩토링', emoji: '💡', }, { name: 'test', description: '테스트 관련', emoji: '💍', }, { name: 'style', description: '스타일링 관련', emoji: '🎨', }, ]; const getChangesetMd = () => { const isAdded = (message) => message.startsWith('A '); const isModified = (message) => message.startsWith('M '); const isChangesetMd = (path) => path.match(/\.changeset\/.*\.md/); const isSome = (fns) => (val) => fns.some((fn) => fn(val)); const changesetFile = execSync('git status --short') // .toString() .split('\n') .filter(isSome([isAdded, isModified])) .map(flow(removeStr('A '), removeStr('M '))) .find(isChangesetMd); if (!changesetFile) return null; const content = readFileSync(changesetFile, 'utf8'); const [summary, _, ...detail] = removeStr(/---\n(.*)\n---\n\n/g, content).split('\n'); return { summary, detail: detail.join('\n') }; }; const commitPrompt = (config) => __awaiter(void 0, void 0, void 0, function* () { clear(); const workspace = yield (() => __awaiter(void 0, void 0, void 0, function* () { var _a; if (!((_a = config === null || config === void 0 ? void 0 : config.workspaces) === null || _a === void 0 ? void 0 : _a.length)) return []; const { workspace } = yield enquirer.prompt({ type: 'multiselect', name: 'workspace', message: 'Pick WorkSpace(skip by: Enter)', choices: config.workspaces, }); return workspace; }))(); const commitTypes = runIfFn(config.types, INITIAL_COMMIT_TYPES) || INITIAL_COMMIT_TYPES; const chMd = getChangesetMd(); const { type: selectedType, scope, message, detail, breakingChange, } = yield enquirer.prompt([ { type: 'autocomplete', name: 'type', message: 'Pick Commit Type', choices: commitTypes.map(createChoiceItem), }, { type: 'input', name: 'scope', message: 'Pick Change Scope(skip by: Enter)', }, { type: 'input', name: 'message', message: 'What did you do?', initial: chMd ? chMd.summary : undefined, required: true, }, { type: 'input', name: 'breakingChange', message: 'has BREAKING CHANGE for major update?(skip by: Enter)', }, { type: 'input', name: 'detail', initial: chMd ? chMd.detail : undefined, message: 'has detail?(skip by: Enter)', multiline: true, }, ]); const type = commitTypes.find((type) => type.name === selectedType); return { workspace, type: (type === null || type === void 0 ? void 0 : type.name) || '', emoji: (type === null || type === void 0 ? void 0 : type.emoji) || '', scope, message, breakingChange, detail, }; }); const createChoiceItem = ({ name, description, emoji, }) => { return { name, message: (emoji === null || emoji === void 0 ? void 0 : emoji.padEnd(3, ' ')) + (name + ':').padEnd(12, ' ') + description, value: name, }; }; const createCommitMessage = (arg) => { const { emoji, breakingChange, type, message, scope, detail, issueNumber, isCloseIssue, workspace, } = arg; const subject = (() => { const _workspace = (workspace === null || workspace === void 0 ? void 0 : workspace.length) ? workspace.map((txt) => `${txt}-${type}`).join(',') : ''; const mergeStr = (...strs) => strs.join(''); return mergeStr(_workspace || type, breakingChange ? '!' : '', scope ? `(${scope}):` : ':', ' ', `${emoji || ''}${message}`); })(); let details = ''; if (breakingChange) details += `BREAKING CHANGE: ${breakingChange}\n`; if (detail) details += detail; if (issueNumber) { if (isCloseIssue) details += '\nClose '; else details += '\nIssue '; details += '#' + issueNumber; } return { subject, details, }; }; const executeCommit = ({ subject, details }) => { const command = ['commit', '-m', subject]; const conditionalCommand = details ? [...command, '-m', details] : command; $('git', conditionalCommand); }; /** * @category Commands */ const commit = defineCommand({ name: 'commit', description: '대화형 cli 를 통해 일관된 형식의 커밋 메시지 작성을 도와주는 플러그인 입니다.', default: { types: INITIAL_COMMIT_TYPES, workspaces: [], }, cliOptions: [ { alias: 'w', name: 'workspaces', description: '모노래포 사용시, workspace 로 커밋 범위 설정할수 있습니다. ', type: 'string[]', }, ], run: (config) => __awaiter(void 0, void 0, void 0, function* () { yield commitPrompt(config).then(createCommitMessage).then(executeCommit); }), }); export { commit };