Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support create utils template #1

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
parserOptions: {
project: ['./tsconfig.json'],
},
ignorePatterns: ['.eslintrc.cjs', 'dist', 'types', 'node_modules'],
ignorePatterns: ['.eslintrc.cjs', 'dist', 'types', 'node_modules', 'templates'],
extends: [
// typescript使用此配置
'@compass-aiden/eslint-config/ts',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"typescript": "^5.4.2"
},
"dependencies": {
"@compass-aiden/helpers": "^0.1.1",
"@compass-aiden/helpers": "^0.2.0",
"@compass-aiden/telegram": "^2.1.0",
"axios": "^1.6.7",
"chalk": "^5.3.0",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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

23 changes: 23 additions & 0 deletions src/utils/batch-compile-templates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { readFileSync } from 'fs';
import { dirname } from 'path';
import handlebars from 'handlebars';
import { createFileSync, createFolder, isFileOrFolderExists } from '@compass-aiden/helpers/cjs';

/**
* @description 使用handlebars批量编译模板
* @param temps 模板的源路径与目标路径
* @param tempData 模板的上下文数据
*/
export default async function batchCompileTemplates(temps: [string, string][], tempData: Record<string, any>) {
const tasks = temps.map(async (temp) => {
if (temp && temp[0] && temp[1]) {
const compileTemp = handlebars.compile(readFileSync(temp[0], 'utf-8'));
const targetDirPath = dirname(temp[1]);
if (!isFileOrFolderExists(targetDirPath)) {
await createFolder(targetDirPath);
}
createFileSync(temp[1], compileTemp(tempData));
}
});
await Promise.all(tasks);
}
14 changes: 7 additions & 7 deletions src/utils/create-turbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ export default async function createTurbo(options?: { pkgManager?: PkgManager })
await createFolder(projectPath, {});
// 创建workspaces配置
if (opt.pkgManager === 'pnpm') {
createFile('package.json', PACKAGE_FILE, execOptions);
createFile('pnpm-workspace.yaml', PNPM_WORKSPACES_FILE, execOptions);
await createFile('package.json', PACKAGE_FILE, execOptions);
await createFile('pnpm-workspace.yaml', PNPM_WORKSPACES_FILE, execOptions);
} else {
createFile('package.json', WORKSPACE_PACKAGE_FILE, execOptions);
await createFile('package.json', WORKSPACE_PACKAGE_FILE, execOptions);
}
createFolder('apps', execOptions);
createFolder('packages', execOptions);
createFile('turbo.json', TURBO_JSON_FILE, execOptions);
createFile('.gitignore', GITIGNORE_FILE, execOptions);
await createFolder('apps', execOptions);
await createFolder('packages', execOptions);
await createFile('turbo.json', TURBO_JSON_FILE, execOptions);
await createFile('.gitignore', GITIGNORE_FILE, execOptions);
loading.succeed(chalk.green('创建完成'));
}
53 changes: 43 additions & 10 deletions src/utils/create-utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { fileURLToPath } from 'url';
import { join, dirname } from 'path';
import { readFileSync } from 'fs';
import inquirer from 'inquirer';
import ora from 'ora';
import chalk from 'chalk';
import handlebars from 'handlebars';
import { createFile, createFolder, isFileOrFolderExists } from '@compass-aiden/helpers/cjs';
import { createFolder, isFileOrFolderExists, copyFolderSync } from '@compass-aiden/helpers/cjs';
import batchCompileTemplates from './batch-compile-templates';

// eslint-disable-next-line @typescript-eslint/naming-convention, no-underscore-dangle
const __filename = fileURLToPath(import.meta.url);
Expand All @@ -14,7 +13,8 @@ const __dirname = dirname(__filename);
const utilsTempBasePath = join(__dirname, '../templates/utils');

export default async function createUtils() {
const {
/* eslint-disable prefer-const */
let {
projectPath,
projectDescription,
enabledEslint,
Expand All @@ -23,6 +23,8 @@ export default async function createUtils() {
enabledTypedoc,
enabledPrettyQuick,
enabledCommitlint,
enabledGithubActions,
/* eslint-enable prefer-const */
} = await inquirer.prompt([
{
type: 'input',
Expand Down Expand Up @@ -72,6 +74,12 @@ export default async function createUtils() {
message: '是否启用Commitlint在代码提交时校验提交信息',
defaut: true,
},
{
type: 'confirm',
name: 'enabledGithubActions',
message: '是否启用Github actions CICD执行自动化任务',
defaut: true,
},
]);
const loading = ora();
loading.start(chalk.cyan('开始创建项目文件\n'));
Expand All @@ -80,14 +88,37 @@ export default async function createUtils() {
return;
}
const enabledGithooks = enabledPrettyQuick || enabledCommitlint;
if (enabledPrettyQuick) {
enabledPrettier = true;
}
const pathArr = projectPath.split('/');
const projectName = pathArr.pop();
await createFolder(projectPath, {});
// 输出package.json文件
const packageTemplate = handlebars.compile(readFileSync(join(utilsTempBasePath, 'package.json.handlebars'), 'utf-8'));
createFile(
join(projectPath, 'package.json'),
packageTemplate({
await batchCompileTemplates(
[
[join(utilsTempBasePath, 'package.json.handlebars'), join(projectPath, 'package.json')],
[join(utilsTempBasePath, 'tsconfig.json.handlebars'), join(projectPath, 'tsconfig.json')],
[join(utilsTempBasePath, 'tsconfig.web.json.handlebars'), join(projectPath, 'tsconfig.web.json')],
[join(utilsTempBasePath, 'tsconfig.node.json.handlebars'), join(projectPath, 'tsconfig.node.json')],
[join(utilsTempBasePath, 'rollup.config.js'), join(projectPath, 'rollup.config.js')],
[join(utilsTempBasePath, 'README.md.handlebars'), join(projectPath, 'README.md')],
[join(utilsTempBasePath, '.gitignore'), join(projectPath, '.gitignore')],
enabledJest && [join(utilsTempBasePath, 'tsconfig.test.json'), join(projectPath, 'tsconfig.test.json')],
enabledJest && [join(utilsTempBasePath, 'jest.config.cjs'), join(projectPath, 'jest.config.cjs')],
enabledCommitlint && [join(utilsTempBasePath, 'commitlint.config.js'), join(projectPath, 'commitlint.config.js')],
enabledPrettier && [join(utilsTempBasePath, '.prettierrc.json'), join(projectPath, '.prettierrc.json')],
enabledPrettier && [join(utilsTempBasePath, '.prettierignore'), join(projectPath, '.prettierignore')],
enabledEslint && [join(utilsTempBasePath, '.eslintrc.cjs.handlebars'), join(projectPath, '.eslintrc.cjs')],
enabledGithubActions && [
join(utilsTempBasePath, '.github/workflows/lint-and-test.yml.handlebars'),
join(projectPath, '.github/workflows/lint-and-test.yml'),
],
enabledGithubActions && [
join(utilsTempBasePath, '.github/workflows/publish-and-deploy.yml.handlebars'),
join(projectPath, '.github/workflows/publish-and-deploy.yml'),
],
],
{
projectDescription,
enabledEslint,
enabledPrettier,
Expand All @@ -97,7 +128,9 @@ export default async function createUtils() {
enabledCommitlint,
enabledGithooks,
projectName,
}),
enabledGithubActions,
},
);
copyFolderSync(join(utilsTempBasePath, 'src'), join(projectPath, 'src'));
loading.succeed(chalk.green('创建完成'));
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export { default as createElectron } from './create-electron';
export { default as createNest } from './create-nest';
export { default as createNext } from './create-next';
export { default as createUtils } from './create-utils';
export { default as batchCompileTemplates } from './batch-compile-templates';
13 changes: 13 additions & 0 deletions templates/utils/.eslintrc.cjs.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
parserOptions: {
project: ['./tsconfig.*.json'],
},
ignorePatterns: ['.eslintrc.cjs', 'dist', 'types'],
extends: [
// typescript使用此配置
'@compass-aiden/eslint-config/ts',
{{#if enabledPrettier}}
'plugin:prettier/recommended',
{{/if}}
],
};
59 changes: 59 additions & 0 deletions templates/utils/.github/workflows/lint-and-test.yml.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Lint and test

on:
pull_request:
branches:
- '**'

jobs:
lint:
name: Lint and test code
runs-on: ubuntu-latest
steps:
# 签出分支
- name: Checkout Repository
uses: actions/checkout@v3
with:
fetch-depth: 2

# 设置node环境
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 18

# 设置pnpm
- uses: pnpm/action-setup@v2
with:
version: 8.7.1

# 设置 pnpm 缓存文件
- name: Cache pnpm
uses: actions/cache@v3
with:
path: |
node_modules
key: pnpm-cache-$\{{ runner.os }}-$\{{ hashFiles('pnpm-lock.yaml') }}

# 恢复项目依赖
- name: Install
env:
# HUSKY: 0
SKIP_INSTALL_SIMPLE_GIT_HOOKS: 1
SKIP_SIMPLE_GIT_HOOKS: 1
run: |
pnpm install

{{#if enabledEslint}}
# 检查项目
- name: Run lint
run: |
pnpm lint
{{/if}}

{{#if enabledJest}}
# 测试项目
- name: Run test
run: |
pnpm test
{{/if}}
110 changes: 110 additions & 0 deletions templates/utils/.github/workflows/publish-and-deploy.yml.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
name: Publish and deploy

on:
push:
branches:
- master

permissions:
contents: write
pages: write
id-token: write

concurrency:
group: 'deploy'
cancel-in-progress: false

jobs:
lint:
name: Publish and deploy
runs-on: ubuntu-latest
steps:
# 签出分支
- uses: actions/checkout@v4

# 设置 git
- name: Setup git
run: |
git config --local user.email github_bot@users.noreply.github.com
git config --local user.name github_bot

# 设置node环境
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 18

# 设置pnpm
- uses: pnpm/action-setup@v2
with:
version: 8.7.1

# 设置 pnpm 缓存文件
- name: Cache pnpm
uses: actions/cache@v3
with:
path: |
node_modules
key: pnpm-cache-$\{{ runner.os }}-$\{{ hashFiles('pnpm-lock.yaml') }}

# 恢复项目依赖
- name: Install
env:
# HUSKY: 0
SKIP_INSTALL_SIMPLE_GIT_HOOKS: 1
SKIP_SIMPLE_GIT_HOOKS: 1
run: |
pnpm install --frozen-lockfile

# 同步版本及推送标签
- name: Bump version and push tag
id: tag_version
uses: mathieudutour/github-tag-action@v6.2
with:
github_token: $\{{ secrets.GITHUB_TOKEN }}

# 更新npm版本
- name: Updated version
run: |
npm pkg set version=$\{{ steps.tag_version.outputs.new_version }}
git add -A
git commit -m "chore: published tag $\{{ steps.tag_version.outputs.new_tag }}"

# 构建项目
- name: Run build
env:
NODE_ENV: production
run: |
pnpm build
{{#if enabledTypedoc}}
pnpm build:doc
{{/if}}

# 创建Github release
- name: Create a GitHub release
uses: ncipollo/release-action@v1
with:
tag: $\{{ steps.tag_version.outputs.new_tag }}
name: Release $\{{ steps.tag_version.outputs.new_tag }}
body: $\{{ steps.tag_version.outputs.changelog }}

# 发布npm包
- name: Publish
uses: JS-DevTools/npm-publish@v3
with:
token: $\{{ secrets.NPM_AUTH_TOKEN }}

# 推送变更至仓库
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: $\{{ secrets.GITHUB_TOKEN }}
branch: $\{{ github.ref }}

{{#if enabledTypedoc}}
# 部署文档
- name: Deploy GitHub Pages site
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: ./docs
{{/if}}
Loading
Loading