Skip to content

Commit

Permalink
feat: support create prettyquick and commitlint plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiden-FE committed Mar 22, 2024
1 parent adea3df commit c1adfc1
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"dependencies": {
"@compass-aiden/helpers": "^0.2.0",
"@compass-aiden/telegram": "^2.1.0",
"@inquirer/prompts": "^4.3.0",
"axios": "^1.6.7",
"chalk": "^5.3.0",
"commander": "^12.0.0",
Expand Down
131 changes: 128 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 35 additions & 6 deletions src/commands/plugin.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ 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 {
addCommitlintPlugin,
addGithooksPlugin,
addPrettierPlugin,
addPrettyQuickPlugin,
removeCommitlintPlugin,
removeGithooksPlugin,
removePrettierPlugin,
removePrettyQuickPlugin,
} from '@/utils';
import { PkgManager } from '@/interfaces';

/**
Expand All @@ -21,7 +30,7 @@ export default (program: Command) => {
.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 代码格式化',
'插件名称\n\t\t\t- githooks\t使用SimpleGitHooks基于githooks对项目添加自动化任务\n\t\t\t- prettier\tPrettier 代码格式化\n\t\t\t- prettyquick\tPrettyQuick 在Commit前仅对变更文件进行快速格式化,该插件依赖于githooks及prettier插件\n\t\t\t- commitlint\tCommitlint 提交信息格式校验,该插件依赖于githooks插件',
)
.option('-P, --path [path]', '项目路径,默认为当前路径')
.action(async ({ name: inputName, type: inputType, path: inputPath }) => {
Expand Down Expand Up @@ -65,10 +74,10 @@ export default (program: Command) => {
name: '使用SimpleGitHooks基于githooks对项目添加自动化任务',
value: 'githooks',
},
{
name: 'Eslint 基于Airbnb规范对代码进行检查',
value: 'eslint',
},
// {
// name: 'Eslint 基于Airbnb规范对代码进行检查',
// value: 'eslint',
// },
{
name: 'Prettier 代码格式化',
value: 'prettier',
Expand Down Expand Up @@ -106,6 +115,26 @@ export default (program: Command) => {
return;
}

if (name === 'prettyquick') {
const action = {
add: addPrettyQuickPlugin,
remove: removePrettyQuickPlugin,
};
const pkgManager: PkgManager = scanDependencyManager({ cwd: basePath });
await action[type]({ pkgManager, cwd: basePath });
return;
}

if (name === 'commitlint') {
const action = {
add: addCommitlintPlugin,
remove: removeCommitlintPlugin,
};
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' 获取更多帮助信息`));
});
Expand Down
75 changes: 75 additions & 0 deletions src/utils/commitlint.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { execSync } from 'child_process';
import { join } from 'path';
import chalk from 'chalk';
import { confirm } from '@inquirer/prompts';
import { createFileSync } from '@compass-aiden/helpers/cjs';
import { PkgManager } from '@/interfaces';
import Logger from './logger';
import { addGithooksPlugin } from './githooks.plugin';
import { deleteFilesSync } from './delete-files-sync';

const COMMITLINT_CONFIG_FILE = `export default {
extends: ['@commitlint/config-conventional'],
};
`;

export async function addCommitlintPlugin(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('开始安装 Commitlint 插件'));

const hasGithooks = execSync('npm pkg get devDependencies.simple-git-hooks', { cwd }).toString().trim();
const isHasGithooks = !(hasGithooks === '{}' || hasGithooks === '');
if (!isHasGithooks) {
loading.stop();

const confirmInstall = await confirm({
message: '未发现前置Githooks插件,是否现在安装?',
default: true,
});

if (!confirmInstall) {
Logger.error(chalk.red('❌ 缺少前置插件安装失败'));
return;
}
await addGithooksPlugin(options);
loading.start(chalk.cyan('开始安装 Commitlint 插件'));
}

execSync(`${pkgManager} add -D @commitlint/cli @commitlint/config-conventional`, execOption);
execSync('npm pkg set simple-git-hooks.commit-msg="npx --no -- commitlint --edit \\$1"', execOption);
createFileSync('commitlint.config.js', COMMITLINT_CONFIG_FILE, { cwd });
execSync('npx simple-git-hooks', execOption);

Logger.success(`Installed @commitlint/cli @commitlint/config-conventional at ${cwd || process.cwd()}`);
Logger.success(`Set 'simple-git-hooks.commit-msg' field at ${cwd || process.cwd()}/package.json`);
Logger.success(`Created file at ${cwd || process.cwd()}/commitlint.config.js`);
Logger.success(`Ran 'npx simple-git-hooks' to update hooks at ${cwd || process.cwd()}`);
loading.succeed(chalk.green('成功安装 Commitlint 插件,将在每次Commit提交时验证提交信息'));
}

export async function removeCommitlintPlugin(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('开始卸载 Commitlint 插件'));

execSync(`${pkgManager} remove @commitlint/cli @commitlint/config-conventional`, execOption);
execSync('npm pkg delete simple-git-hooks.commit-msg', execOption);
deleteFilesSync(join(cwd || './', 'commitlint.config.js'));
execSync('npx simple-git-hooks', execOption);

Logger.success(`Uninstalled @commitlint/cli @commitlint/config-conventional at ${cwd || process.cwd()}`);
Logger.success(`Delete 'simple-git-hooks.commit-msg' field at ${cwd || process.cwd()}/package.json`);
Logger.success(`Delete file at ${cwd || process.cwd()}/commitlint.config.js`);
Logger.success(`Ran 'npx simple-git-hooks' to update hooks at ${cwd || process.cwd()}`);

loading.succeed(chalk.green('成功卸载 Commitlint 插件'));
}
10 changes: 5 additions & 5 deletions src/utils/githooks.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export async function addGithooksPlugin(options?: { pkgManager?: PkgManager; 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`);
Logger.success(`Set 'simple-git-hooks' field at ${cwd || process.cwd()}/package.json`);
Logger.success(`Set 'scripts.prepare' field at ${cwd || process.cwd()}/package.json`);

loading.succeed(
chalk.green('SimpleGitHooks 插件安装完成\n使用文档参考: https://github.com/toplenboren/simple-git-hooks'),
Expand All @@ -29,11 +29,11 @@ export async function removeGithooksPlugin(options?: { pkgManager?: PkgManager;
};
const loading = Logger.createLoading();
loading.start(chalk.cyan('开始移除SimpleGitHooks插件'));
execSync(`${pkgManager} remove -D simple-git-hooks`, { stdio: 'inherit', cwd });
execSync(`${pkgManager} remove 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`);
Logger.success(`Delete 'simple-git-hooks' field at ${cwd || process.cwd()}/package.json`);
Logger.success(`Delete 'scripts.prepare' field at ${cwd || process.cwd()}/package.json`);
loading.succeed(chalk.green('SimpleGitHooks 插件移除完成'));
}
4 changes: 3 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export { default as createAngular } from './create-angular';
// tools
export { default as batchCompileTemplates } from './batch-compile-templates';
export { default as Logger } from './logger';
export * from './delete-files-sync';

// plugin utils
export * from './githooks.plugin';
export * from './prettier.plugin';
export * from './delete-files-sync';
export * from './prettyquick.plugin';
export * from './commitlint.plugin';
6 changes: 3 additions & 3 deletions src/utils/prettier.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export async function addPrettierPlugin(options?: { pkgManager?: PkgManager; 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(`Set 'scripts.format' field 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 插件'));
loading.succeed(chalk.green('成功安装 Prettier 插件,可通过以下命令主动执行格式化:\n\n\tnpm run format'));
}

export async function removePrettierPlugin(options?: { pkgManager?: PkgManager; cwd?: string }) {
Expand All @@ -97,7 +97,7 @@ export async function removePrettierPlugin(options?: { pkgManager?: PkgManager;
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 'scripts.format' field 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 插件'));
Expand Down

0 comments on commit c1adfc1

Please sign in to comment.