Skip to content

Commit

Permalink
cui: 脚手架编写
Browse files Browse the repository at this point in the history
  • Loading branch information
18236324510 committed Aug 6, 2024
1 parent a4bbfef commit 21babd6
Show file tree
Hide file tree
Showing 12 changed files with 2,766 additions and 1,713 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@commitlint/config-conventional": "^19.2.2",
"husky": "^9.1.4",
"lint-staged": "^15.2.7",
"prettier": "^3.3.3"
"prettier": "^3.3.3",
"typescript": "^5.5.4"
}
}
47 changes: 47 additions & 0 deletions packages/cli/actions/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { InitOptions } from '../type'
import { PROJECT_TYPES } from '../utils/constants'
import inquirer from 'inquirer'
import path from 'path'
import fs from 'fs'
import { execSync } from 'child_process'
import getNpmType from '../utils/npmType'

/**
* 是否添加cui commitlit
*/
const chooseEslintType = async () => {
const { type } = await inquirer.prompt({
type: 'list',
name: 'type',
message: 'cui' as any,
choices: PROJECT_TYPES,
})

return type
}

const init = async (options: InitOptions) => {
const npmType = await getNpmType

const cwd = options?.cwd || process.cwd()
const { cui } = options
const config: Record<string, any> = {}

// 初始化 commitlint
config.cui = await chooseEslintType()
const pkgPath = path.resolve(cwd, 'package.json')
let pkg = JSON.parse(fs.readFileSync(pkgPath, { encoding: 'utf-8' }))
pkg['scripts']['commit:lint'] = 'commitlint --edit'
// 配置husky
console.log('-------------开始安装husky')
execSync(`${npmType} add husky -D`)
console.log('------------------开始初始化husky')
execSync('npx husky init')
console.log('----------------开始配置hooks')
execSync(`echo "${npmType} run commit:lint" > .husky/pre-commit`)
console.log('---------------开始写入package')
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2))
console.log('完成')
}

export default init
19 changes: 19 additions & 0 deletions packages/cli/actions/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PKG_NAME, PKG_VERSION } from '../utils/constants'
import { execSync } from 'child_process'
import getNpmType from '../utils/npmType'

const checkLatestVersion = async () => {
const npmType = await getNpmType
const latestVersion = execSync(`${npmType} view ${PKG_NAME} version`)
.toString('utf-8')
.trim()
return latestVersion
}
const update = async () => {
try {
const latestVersion = await checkLatestVersion()
console.log('当前为最新版' + latestVersion)
} catch (error) {}
}

export default update
24 changes: 24 additions & 0 deletions packages/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 比较当前版本
// 获取当前版本
import { program } from 'commander'
import { PKG_NAME, PKG_VERSION } from './utils/constants'
import init from './actions/init'
const cwd = process.cwd()
program
.version(PKG_VERSION)
.description(
`${PKG_NAME} 是 印客学院前端编码规范工程化 的配套 Lint 工具,提供简单的 CLI 和 Node.js API,让项目能够一键接入、一键扫描、一键修复、一键升级,并为项目配置 git commit 卡点,降低项目实施规范的成本`
)

program
.command('init')
.description(
'一键接入:为项目初始化规范工具和配置,可以根据项目类型和需求进行定制'
)
.option('--vscode', '写入.vscode/setting.json配置')
.action(async (cmd) => {
await init({
cwd,
})
})
program.parse(process.argv)
3 changes: 3 additions & 0 deletions packages/cli/config/commitlint.config.js.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
extends: ['@c1007568349/commitlint'],
}
83 changes: 83 additions & 0 deletions packages/cli/package-lock.json

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

23 changes: 23 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "yunxiang-cli",
"version": "1.0.0",
"description": "",
"bin": "index.ts",
"scripts": {
"test": "ts-node cli.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/command-exists": "^1.2.3",
"@types/node": "^22.1.0"
},
"dependencies": {
"@inquirer/type": "^1.5.1",
"command-exists": "^1.2.9",
"commander": "^12.1.0",
"inquirer": "^10.1.6",
"ora": "^8.0.1"
}
}
21 changes: 21 additions & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */

/* Language and Environment */
"target": "ES5" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,

/* Modules */
"module": "commonjs" /* Specify what module code is generated. */,

"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,

"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"types": ["node"]
}
}
21 changes: 21 additions & 0 deletions packages/cli/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export interface InitOptions {
cwd?: string
// // 是否检查并升级 encode-fe-lint 的版本
// checkVersionUpdate: boolean
// // 是否需要自动重写 lint 配置
// rewriteConfig?: boolean
// // eslint 类型
// eslintType?: string
// // 是否启用 ESLint
// enableESLint?: boolean
// // 是否启用 stylelint
// enableStylelint?: boolean
// // 是否启用 markdownlint
// enableMarkdownlint?: boolean
// // 是否启用 prettier
// enablePrettier?: boolean
// // 是否禁用自动在初始化完成后安装依赖
// disableNpmInstall?: boolean
// 是否使用cui
cui?: string
}
29 changes: 29 additions & 0 deletions packages/cli/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from 'fs'
import path from 'path'
export const pkg = JSON.parse(
fs.readFileSync(path.join(__dirname, '../package.json'), 'utf-8')
)

/**
* 包名
*/
export const PKG_NAME: string = pkg.name

/**
* 包版本号
*/
export const PKG_VERSION: string = pkg.version

/**
* 项目类型
*/
export const PROJECT_TYPES: Array<{ name: string; value: string }> = [
{
name: '不使用cui',
value: '1',
},
{
name: '使用cui',
value: '2',
},
]
9 changes: 9 additions & 0 deletions packages/cli/utils/npmType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { sync as commandExistsSync } from 'command-exists'
// pnpm 类型
const getNpmType: Promise<'npm' | 'pnpm'> = new Promise((resolve) => {
if (!commandExistsSync('pnpm')) {
return resolve('npm')
}
resolve('pnpm')
})
export default getNpmType
Loading

0 comments on commit 21babd6

Please sign in to comment.