Skip to content

Commit

Permalink
feat: 新增 是否初始化 husky 选项;优化 commitlint、cz-conventional-changelog、husky…
Browse files Browse the repository at this point in the history
…、lint-staged 等的配置和依赖
  • Loading branch information
CaoMeiYouRen committed Dec 17, 2021
1 parent 41f4905 commit e769a02
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ export interface InitAnswers {
* 是否初始化 贡献指南,仅开源的情况下初始化
*/
isInitContributing: boolean
/**
* 是否初始化 husky
*/
isInitHusky: boolean
}
9 changes: 9 additions & 0 deletions src/plopfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ module.exports = function (plop: NodePlopAPI) {
return answers.isOpenSource
},
},
{
type: 'confirm',
name: 'isInitHusky',
message: '是否初始化 husky?',
default: false,
when(answers: InitAnswers) {
return answers.isOpenSource
},
},
{
type: 'confirm',
name: 'isInitReadme',
Expand Down
111 changes: 104 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export async function initProject(answers: InitAnswers) {
}

export async function init(projectPath: string, answers: InitAnswers) {
const { isOpenSource, isRemoveDependabot, gitRemoteUrl, isInitReadme, isInitContributing } = answers
const { isOpenSource, isRemoveDependabot, gitRemoteUrl, isInitReadme, isInitContributing, isInitHusky } = answers

try {
await asyncExec('git --version', {
Expand Down Expand Up @@ -160,6 +160,9 @@ export async function init(projectPath: string, answers: InitAnswers) {
if (info.licenseName === 'MIT') {
await initLicense(projectPath, info)
}
if (isInitHusky) {
await initHusky(projectPath, info)
}
}
await initGithubWorkflows(projectPath, info)
}
Expand All @@ -180,6 +183,10 @@ export async function init(projectPath: string, answers: InitAnswers) {
process.exit(1)
}

await asyncExec('git add .', {
cwd: projectPath,
})

if (newPkg?.scripts?.lint) {
await asyncExec(`${PACKAGE_MANAGER} run lint`, {
cwd: projectPath,
Expand All @@ -189,6 +196,7 @@ export async function init(projectPath: string, answers: InitAnswers) {
await asyncExec('git add .', {
cwd: projectPath,
})

await asyncExec('git commit -m "chore: init"', {
cwd: projectPath,
})
Expand All @@ -199,7 +207,7 @@ export async function init(projectPath: string, answers: InitAnswers) {
}

export async function getGitUserName() {
const username = (await asyncExec('git config user.name')) as string
const username = (await asyncExec('git config user.name')) as string || ''
return username.trim()
}

Expand All @@ -222,7 +230,7 @@ export async function initProjectJson(projectPath: string, answers: InitAnswers)
const homepage = `${repositoryUrl}#readme`
const issuesUrl = `${repositoryUrl}/issues`
const gitUrl = `git+${repositoryUrl}.git`
const nodeVersion = await getLtsNodeVersion() || '12'
const nodeVersion = await getLtsNodeVersion() || '16'
const node = Number(nodeVersion) - 4 // lts 减 4 为最旧支持的版本
const pkgPath = path.join(projectPath, 'package.json')
const pkg: IPackage = await fs.readJSON(pkgPath)
Expand All @@ -248,6 +256,14 @@ export async function initProjectJson(projectPath: string, answers: InitAnswers)
bugs: {
url: issuesUrl,
},
devDependencies: {
'conventional-changelog-cli': '^2.1.1',
'conventional-changelog-cmyr-config': `^${await getNpmPackageVersion('conventional-changelog-cmyr-config')}`,
...pkg?.devDependencies,
},
changelog: {
language: 'zh',
},
}
}
const newPkg = Object.assign({}, pkg, pkgData, extData)
Expand Down Expand Up @@ -480,16 +496,23 @@ async function initGithubWorkflows(projectPath: string, projectInfos: any) {
try {
const { isPublishToNpm } = projectInfos
const files = ['.github/workflows/test.yml']

if (isPublishToNpm) {
const releaseYml = '.github/workflows/release.yml'
const dir = path.join(projectPath, '.github/workflows')
if (!await fs.pathExists(dir)) {
await fs.mkdirp(dir)
}
const releaseYml = '.github/workflows/release.yml'
if (isPublishToNpm) { // 如果需要发布到 npm 则说明需要自动 release
files.push(releaseYml)
const oldReleaseYml = path.join(projectPath, '.github/release.yml')
if (await fs.pathExists(oldReleaseYml)) {
await fs.remove(oldReleaseYml)
}
} else { // 否则就移除 release.yml
const oldReleaseYml = path.join(projectPath, releaseYml)
if (await fs.pathExists(oldReleaseYml)) {
await fs.remove(oldReleaseYml)
}
}

files.forEach(async (file) => {
const templatePath = path.join(__dirname, '../templates/', file)
const newPath = path.join(projectPath, file)
Expand All @@ -505,6 +528,75 @@ async function initGithubWorkflows(projectPath: string, projectInfos: any) {
}
}

/**
* 初始化 husky
* @param projectPath
* @param projectInfos
*/
async function initHusky(projectPath: string, projectInfos: any) {
const loading = ora('正在初始化 husky ……').start()
try {
const files = ['.husky/commit-msg', '.husky/pre-commit']
const dir = path.join(projectPath, '.husky')
if (!await fs.pathExists(dir)) {
await fs.mkdirp(dir)
}
files.forEach(async (file) => {
const templatePath = path.join(__dirname, '../templates/', file)
const newPath = path.join(projectPath, file)
if (await fs.pathExists(newPath)) {
await fs.remove(newPath)
}
await fs.copyFile(templatePath, newPath)
})

const extnames = ['js', 'ts']
const pkgPath = path.join(projectPath, 'package.json')
const pkg: IPackage = await fs.readJSON(pkgPath)
if (pkg?.dependencies?.vue) {
extnames.push('vue')
}
if (pkg?.dependencies?.react) {
extnames.push('jsx', 'tsx')
}
const keyname = `*.{${extnames.join(',')}}`
const devDependencies = {
'@commitlint/cli': '^15.0.0',
'@commitlint/config-conventional': '^15.0.0',
commitizen: '^4.2.3',
'cz-conventional-changelog': '^3.3.0',
husky: '^7.0.4',
'lint-staged': '^12.1.2',
}
const pkgData: IPackage = {
devDependencies: {
...devDependencies,
...pkg?.devDependencies,
},
husky: undefined,
config: {
commitizen: {
path: 'cz-conventional-changelog',
},
},
'lint-staged': {
[keyname]: [
'npm run lint',
'git add',
],
},
}

const newPkg = Object.assign({}, pkg, pkgData)
await fs.writeFile(pkgPath, JSON.stringify(newPkg, null, 2))

loading.succeed('husky 初始化成功!')
} catch (error) {
loading.fail('husky 初始化失败!')
console.error(error)
}
}


/**
* 根据 github name 获取作者网站
Expand Down Expand Up @@ -534,6 +626,11 @@ async function getLtsNodeVersion(): Promise<string> {
}
}

async function getNpmPackageVersion(name: string) {
const version = (await asyncExec(`${PACKAGE_MANAGER} view ${name} version`)) as string || ''
return version.trim()
}

/**
* 修复 markdown 格式
* @param markdown
Expand Down
4 changes: 4 additions & 0 deletions templates/.husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"
4 changes: 4 additions & 0 deletions templates/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install lint-staged
4 changes: 2 additions & 2 deletions templates/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
1. Clone 本项目

```sh
git clone <%= repositoryUrl %>
git clone <%= repositoryUrl %>.git
```

2. 安装依赖
Expand Down Expand Up @@ -74,4 +74,4 @@

5. 推送到分支 ( `git push origin feat/your_feature`)

6. [打开一个新的 Pull Request](<%= repositoryUrl %>/compare?expand=1)
6. [打开一个新的 Pull Request](<%= repositoryUrl %>/compare?expand=1)

0 comments on commit e769a02

Please sign in to comment.