-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added plugin command and support for add githook or prettier pl…
…ugin
- Loading branch information
Showing
9 changed files
with
337 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as updateCommand } from './update.cmd'; | ||
export { default as createCommand } from './create.cmd'; | ||
export { default as pluginCommand } from './plugin.cmd'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { join } from 'path'; | ||
import { Command } from 'commander'; | ||
import chalk from 'chalk'; | ||
import inquirer from 'inquirer'; | ||
import { scanDependencyManager } from '@compass-aiden/helpers/cjs'; | ||
import { addGithooksPlugin, addPrettierPlugin, removeGithooksPlugin, removePrettierPlugin } from '@/utils'; | ||
import { PkgManager } from '@/interfaces'; | ||
|
||
/** | ||
* @description Compass 项目插件管理 | ||
* @example | ||
* ```bash | ||
* compass plugin [options] # 添加或删除插件 | ||
* compass plugin --help # 获取更多帮助信息 | ||
* ``` | ||
*/ | ||
export default (program: Command) => { | ||
program | ||
.command('plugin') | ||
.description('项目插件管理') | ||
.option('-T, --type [type]', '操作类型\n\t\t\t- add\t为项目添加插件\n\t\t\t- remove\t为项目移除插件') | ||
.option( | ||
'-N, --name [name]', | ||
'插件名称\n\t\t\t- githooks\t使用SimpleGitHooks基于githooks对项目添加自动化任务\n\t\t\t- prettier\tPrettier 代码格式化', | ||
) | ||
.option('-P, --path [path]', '项目路径,默认为当前路径') | ||
.action(async ({ name: inputName, type: inputType, path: inputPath }) => { | ||
// 确认操作的类型 | ||
let type: 'add' | 'remove' = inputType; | ||
let basePath = inputPath || undefined; | ||
if (basePath) { | ||
basePath = basePath.startsWith('/') ? basePath : join(process.cwd(), basePath); | ||
} | ||
if (!['add', 'remove'].includes(type)) { | ||
const { type: selectedType } = await inquirer.prompt([ | ||
{ | ||
type: 'list', | ||
name: 'type', | ||
message: `请选择您的操作类型`, | ||
choices: [ | ||
{ | ||
name: '为项目添加插件', | ||
value: 'add', | ||
}, | ||
{ | ||
name: '为项目移除插件', | ||
value: 'remove', | ||
}, | ||
], | ||
}, | ||
]); | ||
type = selectedType; | ||
} | ||
|
||
// 确认要操作的插件 | ||
const { name } = inputName | ||
? { name: inputName } | ||
: await inquirer.prompt([ | ||
{ | ||
type: 'list', | ||
name: 'name', | ||
message: `请选择您要${type === 'add' ? '添加' : '删除'}的插件`, | ||
choices: [ | ||
{ | ||
name: '使用SimpleGitHooks基于githooks对项目添加自动化任务', | ||
value: 'githooks', | ||
}, | ||
{ | ||
name: 'Eslint 基于Airbnb规范对代码进行检查', | ||
value: 'eslint', | ||
}, | ||
{ | ||
name: 'Prettier 代码格式化', | ||
value: 'prettier', | ||
}, | ||
{ | ||
name: 'Commitlint 提交信息格式校验,该插件依赖于githooks插件', | ||
value: 'commitlint', | ||
}, | ||
{ | ||
name: 'PrettyQuick 在Commit前仅对变更文件进行快速格式化,该插件依赖于githooks及prettier插件', | ||
value: 'prettyquick', | ||
}, | ||
], | ||
}, | ||
]); | ||
|
||
// 处理选择的插件 | ||
if (name === 'githooks') { | ||
const action = { | ||
add: addGithooksPlugin, | ||
remove: removeGithooksPlugin, | ||
}; | ||
const pkgManager: PkgManager = scanDependencyManager({ cwd: basePath }); | ||
await action[type]({ pkgManager, cwd: basePath }); | ||
return; | ||
} | ||
|
||
if (name === 'prettier') { | ||
const action = { | ||
add: addPrettierPlugin, | ||
remove: removePrettierPlugin, | ||
}; | ||
const pkgManager: PkgManager = scanDependencyManager({ cwd: basePath }); | ||
await action[type]({ pkgManager, cwd: basePath }); | ||
return; | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(chalk.red(`❌ ${name}不是可用的插件名称, 请通过 'compass plugin --help' 获取更多帮助信息`)); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { unlinkSync, existsSync } from 'fs'; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export function deleteFilesSync(filesPath: string | string[]) { | ||
const files = Array.isArray(filesPath) ? filesPath : [filesPath]; | ||
files.forEach((file) => { | ||
if (existsSync(file)) { | ||
unlinkSync(file); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { execSync } from 'child_process'; | ||
import chalk from 'chalk'; | ||
import { PkgManager } from '@/interfaces'; | ||
import Logger from './logger'; | ||
|
||
export async function addGithooksPlugin(options?: { pkgManager?: PkgManager; cwd?: string }) { | ||
const { pkgManager, cwd } = { | ||
pkgManager: 'npm' as PkgManager, | ||
...options, | ||
}; | ||
const loading = Logger.createLoading(); | ||
loading.start(chalk.cyan('开始安装SimpleGitHooks插件')); | ||
execSync(`${pkgManager} add -D simple-git-hooks`, { stdio: 'inherit', cwd }); | ||
execSync('npm pkg set simple-git-hooks={} --json', { stdio: 'inherit', cwd }); | ||
execSync('npm pkg set scripts.prepare="npx simple-git-hooks"', { stdio: 'inherit', cwd }); | ||
Logger.success(`Installed simple-git-hooks at ${cwd || process.cwd()}`); | ||
Logger.success(`Set 'simple-git-hooks' fields at ${cwd || process.cwd()}/package.json`); | ||
Logger.success(`Set 'scripts.prepare' fields at ${cwd || process.cwd()}/package.json`); | ||
|
||
loading.succeed( | ||
chalk.green('SimpleGitHooks 插件安装完成\n使用文档参考: https://github.com/toplenboren/simple-git-hooks'), | ||
); | ||
} | ||
|
||
export async function removeGithooksPlugin(options?: { pkgManager?: PkgManager; cwd?: string }) { | ||
const { pkgManager, cwd } = { | ||
pkgManager: 'npm' as PkgManager, | ||
...options, | ||
}; | ||
const loading = Logger.createLoading(); | ||
loading.start(chalk.cyan('开始移除SimpleGitHooks插件')); | ||
execSync(`${pkgManager} remove -D simple-git-hooks`, { stdio: 'inherit', cwd }); | ||
execSync('npm pkg delete simple-git-hooks', { stdio: 'inherit', cwd }); | ||
execSync('npm pkg delete scripts.prepare', { stdio: 'inherit', cwd }); | ||
Logger.success(`Uninstalled simple-git-hooks at ${cwd || process.cwd()}`); | ||
Logger.success(`Delete 'simple-git-hooks' fields at ${cwd || process.cwd()}/package.json`); | ||
Logger.success(`Delete 'scripts.prepare' fields at ${cwd || process.cwd()}/package.json`); | ||
loading.succeed(chalk.green('SimpleGitHooks 插件移除完成')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import chalk from 'chalk'; | ||
import ora from 'ora'; | ||
|
||
export default class Logger { | ||
static log(...args: unknown[]): void { | ||
// eslint-disable-next-line no-console | ||
console.log(...args); | ||
} | ||
|
||
static info(...args: unknown[]): void { | ||
// eslint-disable-next-line no-console | ||
console.info(...args.map((arg) => chalk.cyan(arg))); | ||
} | ||
|
||
static success(...args: unknown[]): void { | ||
// eslint-disable-next-line no-console | ||
console.log(...args.map((arg) => chalk.green(arg))); | ||
} | ||
|
||
static warn(...args: unknown[]): void { | ||
// eslint-disable-next-line no-console | ||
console.warn(...args.map((arg) => chalk.yellow(arg))); | ||
} | ||
|
||
static error(...args: unknown[]): void { | ||
// eslint-disable-next-line no-console | ||
console.error(...args.map((arg) => chalk.red(arg))); | ||
} | ||
|
||
static createLoading() { | ||
return ora(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { execSync } from 'child_process'; | ||
import { join } from 'path'; | ||
import chalk from 'chalk'; | ||
import { createFileSync } from '@compass-aiden/helpers/cjs'; | ||
import { PkgManager } from '@/interfaces'; | ||
import Logger from './logger'; | ||
import { deleteFilesSync } from './delete-files-sync'; | ||
|
||
const PRETTIERRC_DATA = `{ | ||
"printWidth": 120, | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"semi": true, | ||
"singleQuote": true, | ||
"quoteProps": "as-needed", | ||
"jsxSingleQuote": false, | ||
"trailingComma": "all", | ||
"bracketSpacing": true, | ||
"bracketSameLine": false, | ||
"arrowParens": "always", | ||
"endOfLine": "lf" | ||
} | ||
`; | ||
|
||
const PRETTIERIGNORE_DATA = `# TypeScript v1 declaration files | ||
typings/ | ||
types/ | ||
# Output | ||
dist | ||
coverage | ||
*.tgz | ||
.next | ||
.nuxt | ||
tmp | ||
out-tsc | ||
stats.html | ||
# lockfile | ||
pnpm-lock.yaml | ||
yarn.lock | ||
package-lock.json | ||
# Cache | ||
.npm | ||
.eslintcache | ||
temp | ||
node_modules | ||
`; | ||
|
||
export async function addPrettierPlugin(options?: { pkgManager?: PkgManager; cwd?: string }) { | ||
const { pkgManager, cwd } = { | ||
pkgManager: 'npm' as PkgManager, | ||
...options, | ||
}; | ||
const execOption = { stdio: 'inherit' as const, cwd }; | ||
const loading = Logger.createLoading(); | ||
loading.start(chalk.cyan('开始安装 Prettier 插件')); | ||
|
||
const hasEslint = execSync('npm pkg get devDependencies.eslint', { cwd }).toString().trim(); | ||
const isHasEslint = !(hasEslint === '{}' || hasEslint === ''); | ||
execSync( | ||
`${pkgManager} add -D prettier${isHasEslint ? ' eslint-config-prettier eslint-plugin-prettier' : ''}`, | ||
execOption, | ||
); | ||
execSync('npm pkg set scripts.format="prettier --write src"', execOption); | ||
createFileSync('.prettierrc.json', PRETTIERRC_DATA, { cwd }); | ||
createFileSync('.prettierignore', PRETTIERIGNORE_DATA, { cwd }); | ||
|
||
Logger.success( | ||
`Installed prettier${isHasEslint ? ' eslint-config-prettier eslint-plugin-prettier' : ''} at ${cwd || process.cwd()}`, | ||
); | ||
Logger.success(`Set 'scripts.format' fields at ${cwd || process.cwd()}/package.json`); | ||
Logger.success(`Created file at ${cwd || process.cwd()}/.prettierrc.json`); | ||
Logger.success(`Created file at ${cwd || process.cwd()}/.prettierignore`); | ||
loading.succeed(chalk.green('成功安装 Prettier 插件')); | ||
} | ||
|
||
export async function removePrettierPlugin(options?: { pkgManager?: PkgManager; cwd?: string }) { | ||
const { pkgManager, cwd } = { | ||
pkgManager: 'npm' as PkgManager, | ||
...options, | ||
}; | ||
const execOption = { stdio: 'inherit' as const, cwd }; | ||
const loading = Logger.createLoading(); | ||
loading.start(chalk.cyan('开始移除 Prettier 插件')); | ||
|
||
const hasEslint = execSync('npm pkg get devDependencies.eslint', { cwd }).toString().trim(); | ||
const isHasEslint = !(hasEslint === '{}' || hasEslint === ''); | ||
execSync( | ||
`${pkgManager} remove prettier${isHasEslint ? ' eslint-config-prettier eslint-plugin-prettier' : ''}`, | ||
execOption, | ||
); | ||
execSync('npm pkg delete scripts.format', execOption); | ||
deleteFilesSync([join(cwd || './', '.prettierrc.json'), join(cwd || './', '.prettierignore')]); | ||
|
||
Logger.success( | ||
`Uninstalled prettier${isHasEslint ? ' eslint-config-prettier eslint-plugin-prettier' : ''} at ${cwd || process.cwd()}`, | ||
); | ||
Logger.success(`Delete 'scripts.format' fields at ${cwd || process.cwd()}/package.json`); | ||
Logger.success(`Delete file at ${cwd || process.cwd()}/.prettierrc.json`); | ||
Logger.success(`Delete file at ${cwd || process.cwd()}/.prettierignore`); | ||
loading.succeed(chalk.green('成功移除 Prettier 插件')); | ||
} |