diff --git a/package.json b/package.json index 2174ad9..d92ea05 100644 --- a/package.json +++ b/package.json @@ -1,60 +1,67 @@ { - "name": "branchclear-cli", - "version": "0.0.0-development", - "description": "git分支清理cli工具", - "main": "src/index.js", + "name": "branchclear-cli", + "version": "0.0.0-development", + "description": "git分支清理cli工具", + "main": "src/index.js", "author": "mengshagn918", - "keywords": ["branch","git","clearbranch","clear","branchclear"], - "license": "MIT", - "bin": { - "branchclear": "bin/index.js" - }, - "scripts": { - "cz": "cz", - "semantic-release": "semantic-release" - }, - "dependencies": { - "chalk": "^4.1.0", - "commander": "^6.1.0", - "execa": "^4.0.3", - "husky": "^4.3.0", - "inquirer": "^7.3.3", - "js-yaml": "^3.14.0", - "ora": "^5.1.0" - }, - "devDependencies": { - "@commitlint/cli": "^11.0.0", - "@commitlint/config-conventional": "^11.0.0", - "commitizen": "^4.2.1", - "cz-conventional-changelog": "3.3.0", - "eslint": "^7.10.0", - "lint-staged": "^10.4.0", - "prettier": "^2.1.2", - "semantic-release-cli": "^5.4.0", - "semantic-release": "^17.2.1" - }, - "husky": { - "hooks": { - "pre-commit": "lint-staged", - "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" - } - }, - "lint-staged": { - "*.js": [ - "eslint --fix", - "prettier --config .prettierrc.yml --write" - ], - "*.md": [ - "prettier --config .prettierrc.yml --write" - ] - }, - "config": { - "commitizen": { - "path": "./node_modules/cz-conventional-changelog" - } - }, - "repository": { - "type": "git", - "url": "https://github.com/mengshang918/branchclear-cli.git" - } + "keywords": [ + "branch", + "git", + "clearbranch", + "clear", + "branchclear" + ], + "license": "MIT", + "bin": { + "branchclear": "bin/index.js" + }, + "scripts": { + "cz": "cz", + "semantic-release": "semantic-release" + }, + "dependencies": { + "chalk": "^4.1.0", + "commander": "^6.1.0", + "execa": "^4.0.3", + "husky": "^4.3.0", + "inquirer": "^7.3.3", + "js-yaml": "^3.14.0", + "minimist": "^1.2.5", + "ora": "^5.1.0" + }, + "devDependencies": { + "@commitlint/cli": "^11.0.0", + "@commitlint/config-conventional": "^11.0.0", + "commitizen": "^4.2.1", + "cz-conventional-changelog": "3.3.0", + "eslint": "^7.10.0", + "lint-staged": "^10.4.0", + "prettier": "^2.1.2", + "semantic-release": "^17.2.1", + "semantic-release-cli": "^5.4.0" + }, + "husky": { + "hooks": { + "pre-commit": "lint-staged", + "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" + } + }, + "lint-staged": { + "*.js": [ + "eslint --fix", + "prettier --config .prettierrc.yml --write" + ], + "*.md": [ + "prettier --config .prettierrc.yml --write" + ] + }, + "config": { + "commitizen": { + "path": "./node_modules/cz-conventional-changelog" + } + }, + "repository": { + "type": "git", + "url": "https://github.com/mengshang918/branchclear-cli.git" + } } diff --git a/src/commander.js b/src/commander.js index ba58d19..fc37ea5 100644 --- a/src/commander.js +++ b/src/commander.js @@ -3,11 +3,12 @@ * @Author: jiangxiaowei * @Date: 2020-10-10 11:54:44 * @Last Modified by: jiangxiaowei - * @Last Modified time: 2020-10-10 16:12:42 + * @Last Modified time: 2020-10-21 19:05:30 */ const { program } = require('commander') +const parseArgs = require('minimist') const { version } = require('../package.json') -const { validateStrIsReg } = require('./utils') +const { validateStrIsReg, hasKey } = require('./utils') /** * @param {string} gitUser git用户 @@ -42,12 +43,13 @@ module.exports = (gitUser) => { ) { program.help() } + const args = parseArgs(process.argv.slice(2)) return { answers: { - main, - user, - clearPosition: position, - remoteName: remotename, + ...(hasKey(args, ['m', 'main']) && { main }), + ...(hasKey(args, ['u', 'user']) && { user }), + ...(hasKey(args, ['p', 'position']) && { clearPosition: position }), + ...(hasKey(args, ['remote', 'remotename']) && { remoteName: remotename }), isReg: !!branchreg, ...(!!branchreg && { branchRegStr: branchreg }), isIgnore: !!ignorereg, diff --git a/src/utils.js b/src/utils.js index e732574..c8cc8a7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -3,7 +3,7 @@ * @Author: jiangxiaowei * @Date: 2020-09-29 16:51:11 * @Last Modified by: jiangxiaowei - * @Last Modified time: 2020-10-10 14:05:32 + * @Last Modified time: 2020-10-21 18:59:47 */ /** @@ -38,8 +38,35 @@ const validateReg = (item) => { } } +/** + * 基本数据类型检测 + * @param {unknown} checkVar 待检测的变量 + * @returns {string} 基本数据类型:Object | Array | String | Number | Boolean | Null | Undefined + */ +const typeCheck = (checkVar) => { + const typeClass = Object.prototype.toString.call(checkVar) + + return typeClass.split(' ')[1].split(']')[0] +} + +/** + * 判断obj中是否含有key这个属性 + * @param {object} obj 需要判断的对象 + * @param {string|Array} key 需要判断的key + */ +const hasKey = (obj, key) => { + if (typeCheck(key) === 'Array') { + return key.some((item) => Object.prototype.hasOwnProperty.call(obj, item)) + } else if (typeCheck(key) === 'String') { + return Object.prototype.hasOwnProperty.call(obj, key) + } + return false +} + module.exports = { getBrachList, validateReg, validateStrIsReg, + typeCheck, + hasKey, }