Skip to content

Commit

Permalink
feat: support pull cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiden-FE committed Mar 25, 2024
1 parent 3200686 commit 6b31f9c
Show file tree
Hide file tree
Showing 40 changed files with 518 additions and 1,062 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,23 @@

`compass update` Cli更新检查

`compass create` 创建项目, `compass create --help` 可以获得更多可选项说明, 支持创建的项目如下:
`compass pull` 通过拉取模板来创建项目, `compass pull --help` 可以获得更多可选项说明,支持拉取的模板如下:

| Name | Description |
| :-------------------------------------------------------------------: | :---------------------: |
| [utils](https://github.com/Aiden-FE/compass-template/tree/temp/utils) | Utils实用程序工具库模板 |

`compass plugin` 向项目添加或删除插件, `compass plugin --help` 可以获得更多可选项说明,支持创建的插件如下:

| Name | Description |
| :---------: | :---------------------------------------------------------------------------------: |
| Githooks | 使用SimpleGitHooks基于githooks对项目添加自动化任务 |
| Prettier | Prettier 代码格式化 |
| eslint | Eslint 基于Airbnb规范对代码进行检查 |
| prettyquick | PrettyQuick 在Commit前仅对变更文件进行快速格式化,该插件依赖于githooks及prettier插件 |
| commitlint | Commitlint 提交信息格式校验,该插件依赖于githooks插件 |

`compass create` 创建各官方标准项目, `compass create --help` 可以获得更多可选项说明, 支持创建的项目如下:

| Name | Description |
| :------: | :----------------: |
Expand All @@ -22,9 +38,6 @@
| Uniapp | Uniapp 跨端项目 |
| Electron | Electron桌面端项目 |
| Nest | Nest后端项目 |
| Utils | 实用程序工具库 |

`compass plugin` 向项目添加或删除插件, `compass plugin --help` 可以获得更多可选项说明

## Contributes

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"@types/adm-zip": "^0.5.5",
"@types/figlet": "^1.5.8",
"@types/inquirer": "^9.0.7",
"@types/node": "^20.11.27",
Expand Down Expand Up @@ -74,6 +75,7 @@
"@compass-aiden/helpers": "^0.2.0",
"@compass-aiden/telegram": "^2.1.0",
"@inquirer/prompts": "^4.3.0",
"adm-zip": "^0.5.12",
"axios": "^1.6.7",
"chalk": "^5.3.0",
"commander": "^12.0.0",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

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

24 changes: 3 additions & 21 deletions src/commands/create.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,12 @@ import {
createUniapp,
createVue,
selectPkgManager,
createUtils,
createAngular,
} from '@/utils';
import { PkgManager } from '@/interfaces';

interface CommandOptions {
projectType?:
| 'turboMonorepo'
| 'uniapp'
| 'vue'
| 'react'
| 'electron'
| 'nest'
| 'next'
| 'utils'
| 'angular'
| string;
projectType?: 'turboMonorepo' | 'uniapp' | 'vue' | 'react' | 'electron' | 'nest' | 'next' | 'angular' | string;
pkgManager?: PkgManager;
}

Expand All @@ -39,10 +28,10 @@ interface CommandOptions {
export default (program: Command) => {
program
.command('create')
.description('快速创建项目')
.description('快速创建官方标准项目')
.option(
'-T, --project-type [projectType]',
'需要创建的项目类型\n\t\t\t\t\t- vue\tVue项目\n\t\t\t\t\t- react\tReact项目\n\t\t\t\t\t- angular\tAngular项目\n\t\t\t\t\t- next\tNext SSR项目\n\t\t\t\t\t- turboMonorepo\tTurbo monorepo项目\n\t\t\t\t\t- uniapp\tUniapp跨端项目\n\t\t\t\t\t- electron\tElectron桌面端项目\n\t\t\t\t\t- nest\tNest后端项目\n\t\t\t\t\t- utils\t实用程序工具库',
'需要创建的项目类型\n\t\t\t\t\t- vue\tVue项目\n\t\t\t\t\t- react\tReact项目\n\t\t\t\t\t- angular\tAngular项目\n\t\t\t\t\t- next\tNext SSR项目\n\t\t\t\t\t- turboMonorepo\tTurbo monorepo项目\n\t\t\t\t\t- uniapp\tUniapp跨端项目\n\t\t\t\t\t- electron\tElectron桌面端项目\n\t\t\t\t\t- nest\tNest后端项目',
)
.option('-M, --pkg-manager [pkgManager]', '指定npm管理器. npm,yarn,pnpm')
.action(async (options: CommandOptions) => {
Expand Down Expand Up @@ -86,10 +75,6 @@ export default (program: Command) => {
name: 'Nest 后端项目',
value: 'nest',
},
{
name: 'Utils 实用程序库',
value: 'utils',
},
],
},
]);
Expand All @@ -115,9 +100,6 @@ export default (program: Command) => {
if (projectType === 'next') {
await createNext();
}
if (projectType === 'utils') {
await createUtils();
}
if (projectType === 'angular') {
await createAngular();
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as updateCommand } from './update.cmd';
export { default as createCommand } from './create.cmd';
export { default as pluginCommand } from './plugin.cmd';
export { default as pullCommand } from './pull.cmd';
53 changes: 53 additions & 0 deletions src/commands/pull.cmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Command } from 'commander';
import { input, select } from '@inquirer/prompts';
import { join, dirname } from 'path';
import { isFileOrFolderExists } from '@compass-aiden/helpers/cjs';
import { SELECT_TEMPLATE } from '@/constants';
import { Logger, pullUtilsTemplate } from '@/utils';

export default (program: Command) => {
program
.command('pull')
.description('通过拉取模板来创建项目')
.option('-N, --name [name]', '项目路径, 如: new-project, ./temp/new-project')
.option('-T, --template-type [tempType]', '需要拉取的模板类型\n\t\t\t\t\t- utils\tUtils实用程序工具库模板')
.action(async (options) => {
const tempType =
options.templateType ||
(await select({
message: '请选择要拉取的模板类型',
choices: SELECT_TEMPLATE,
}));
const tempConfig = SELECT_TEMPLATE.find((item) => item.value === tempType);
if (!tempConfig) {
throw new Error('Not found template');
}

let projectPath =
options.name ||
(await input({
message: '请输入项目路径',
default: './new-project',
}));

projectPath = projectPath.startsWith('/') ? projectPath : join(process.cwd(), projectPath);
const projectName = projectPath.replace(join(dirname(projectPath), './'), '');

if (isFileOrFolderExists(projectPath)) {
Logger.error('❌ 目标路径已经存在');
return;
}

if (tempType === 'utils') {
await pullUtilsTemplate({
projectPath,
templateData: await tempConfig.getTemplateVars({
projectName,
}),
});
return;
}

Logger.error('❌ 暂不支持的模板类型');
});
};
3 changes: 2 additions & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './templates';

/** ESlint 插件 */
// eslint-disable-next-line import/prefer-default-export
export const ESLINT_PLUGINS = [
{
name: 'Next TS',
Expand Down
69 changes: 69 additions & 0 deletions src/constants/templates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { input, confirm } from '@inquirer/prompts';

/* eslint-disable import/prefer-default-export */
export const SELECT_TEMPLATE = [
{
name: '自定义模板',
value: 'custom',
getTemplateVars: async (options?: any) => options || {},
},
{
name: 'Utils 实用程序工具库模板',
value: 'utils',
getTemplateVars: async (options?: any) => {
const result = {
projectName: await input({
message: '请输入项目名称',
default: options.projectName,
}),
projectDescription:
options?.projectDescription ||
(await input({
message: '请输入项目描述',
})),
enabledEslint:
options?.enabledEslint ||
(await confirm({
message: '是否启用 eslint 代码检查插件',
default: true,
})),
enabledPrettier:
options?.enabledEslint ||
(await confirm({
message: '是否启用 prettier 代码格式化插件',
default: true,
})),
enabledJest:
options?.enabledJest ||
(await confirm({
message: '是否启用 jest 单测插件',
default: true,
})),
enabledTypedoc:
options?.enabledTypedoc ||
(await confirm({
message: '是否启用 typedoc 生成文档',
default: true,
})),
enabledPrettyQuick:
options?.enabledPrettyQuick ||
(await confirm({
message: '是否启用 pretty-quick 插件,代码提交前快速格式化变更文件.依赖enabledGithooks开启',
default: true,
})),
enabledCommitlint:
options?.enabledPrettyQuick ||
(await confirm({
message: '是否启用 commitlint 插件,代码提交前对提交信息进行校验.依赖enabledGithooks开启',
default: true,
})),
enabledGithooks: false,
};

return {
...result,
enabledGithooks: result.enabledPrettyQuick || result.enabledCommitlint || false,
};
},
},
];
23 changes: 21 additions & 2 deletions src/http/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import Telegram, { HttpTelegramResInterceptor, HttpTelegramErrorInterceptor } from '@compass-aiden/telegram';
import { Logger } from '@/utils';
import Telegram, {
HttpTelegramResInterceptor,
HttpTelegramErrorInterceptor,
HttpTelegramReqInterceptor,
} from '@compass-aiden/telegram';

const defaultResInterceptor: HttpTelegramResInterceptor = (data, res) => {
if (res.status >= 200 && res.status < 300) {
Expand All @@ -11,12 +16,26 @@ const defaultErrorInterceptor: HttpTelegramErrorInterceptor = (error) => {
throw new Error(`[${error.response?.status || 'Api Error'}]: ${error.message || error}`);
};

const Api = new Telegram()
const defaultRequestInterceptor: HttpTelegramReqInterceptor = (req) => {
if (req.customMeta?.debug) {
Logger.info('Request debug:');
Logger.log(req);
}
return req;
};

const Api = new Telegram({
interceptors: {
response: defaultResInterceptor,
responseError: defaultErrorInterceptor,
},
})
.register('github', {
baseURL: 'https://api.github.com',
interceptors: {
response: defaultResInterceptor,
responseError: defaultErrorInterceptor,
request: defaultRequestInterceptor,
},
})
.register('npm', {
Expand Down
49 changes: 47 additions & 2 deletions src/http/github.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import Api from './api';
import { RepositoryInfo, GithubReleaseInfo } from './interfaces';
import { RepositoryInfo, GithubReleaseInfo, ContentOfGithubRepo } from './interfaces';

/**
* @description 获取存储库已发布版本列表
* @param repoInfo
* @param token
*/
// eslint-disable-next-line import/prefer-default-export
export function getRepoReleasesFromGithub(repoInfo: Pick<RepositoryInfo, 'author' | 'repository'>, token?: string) {
return Api.chain()
.domain('github')
Expand All @@ -21,3 +20,49 @@ export function getRepoReleasesFromGithub(repoInfo: Pick<RepositoryInfo, 'author
.path('repository', repoInfo.repository)
.request<GithubReleaseInfo[]>();
}

/**
* @description 获取存储库
* @param repoInfo
* @param token
*/
export function getRepoFromGithub(repoInfo: RepositoryInfo, token?: string) {
return Api.chain()
.domain('github')
.get(`/repos/${repoInfo.author}/${repoInfo.repository}/zipball/${repoInfo.branch || ''}`)
.path('author', repoInfo.author)
.path('repository', repoInfo.repository)
.path('branch', repoInfo.branch || '')
.config({
headers: {
accept: 'application/vnd.github+json',
Authorization: token ? `Bearer ${token}` : undefined,
},
responseType: 'arraybuffer',
timeout: 1000 * 60,
})
.request();
}

/**
* @description 获取仓库内容
* @param repoInfo
*/
export function getRepoContentsFromGithub(repoInfo: RepositoryInfo, token?: string) {
return Api.chain()
.domain('github')
.get('/repos/:author/:repository/contents/:repoPath')
.path('author', repoInfo.author)
.path('repository', repoInfo.repository)
.path('repoPath', repoInfo.repoPath || '')
.searchParams({
ref: repoInfo.branch,
})
.config({
headers: {
accept: 'application/vnd.github+json',
Authorization: token ? `Bearer ${token}` : undefined,
},
})
.request<ContentOfGithubRepo[] | ContentOfGithubRepo>();
}
Loading

0 comments on commit 6b31f9c

Please sign in to comment.