Skip to content

Commit

Permalink
feat: 新增 初始化 stylelint 相关配置
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Jan 9, 2023
1 parent da14116 commit 652c46d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface Package {
private: boolean
license: string
main: string
bin: Record<string, string>
bin?: Record<string, string>
files: string[]
scripts: Record<string, string>
devDependencies: Record<string, string>
Expand All @@ -15,6 +15,7 @@ export interface Package {
homepage: string
bugs: Record<string, string>
config: Record<string, unknown>
type: string
[k: string]: unknown
}

Expand Down
1 change: 1 addition & 0 deletions src/plopfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = function (plop: NodePlopAPI) {
message: '请选择项目模板',
choices() {
return [
'vite4',
'vite3',
'vite2-vue2',
'vite2',
Expand Down
63 changes: 63 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ async function init(projectPath: string, answers: InitAnswers) {

await initTsconfig(projectPath)

await initStylelint(projectPath)

await sortProjectJson(projectPath)

await initDependabot(projectPath, answers)
Expand Down Expand Up @@ -914,6 +916,67 @@ async function initHusky(projectPath: string) {
console.error(error)
}
}

/**
* 初始化 stylelint 相关配置
* @param projectPath
*/
async function initStylelint(projectPath: string) {
const loading = ora('正在初始化 stylelint ……').start()
try {
const pkg: IPackage = await getProjectJson(projectPath)

const extnames = ['html', 'css', 'scss', 'sass']
if (pkg?.dependencies?.vue) {
extnames.push('vue')
} else if (pkg?.dependencies?.react) {
extnames.push('jsx', 'tsx')
} else {
loading.stopAndPersist({
text: '非前端项目,无需初始化 stylelint',
})
return
}

const files = ['.stylelintignore']
if (pkg.type === 'module') {
files.push('.stylelintrc.cjs')
} else {
files.push('.stylelintrc.js')
}
await copyFilesFromTemplates(projectPath, files)

const devDependencies = {
'postcss-html': '^1.5.0',
sass: '^1.57.1',
stylelint: '^14.16.1',
'stylelint-config-cmyr': '^0.2.1',
'stylelint-config-rational-order': '^0.1.2',
'stylelint-config-standard': '^29.0.0',
'stylelint-order': '^6.0.1',
'stylelint-scss': '^4.3.0',
}

const pkgData: IPackage = {
scripts: {
'lint:css': `stylelint src/**/*.{${extnames.join(',')}} --fix --custom-syntax postcss-html`,
...pkg?.scripts,
},
devDependencies: {
...devDependencies,
...pkg?.devDependencies,
},
}

await saveProjectJson(projectPath, pkgData)

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

/**
* 初始化 Commitizen 相关配置
* @param projectPath
Expand Down
6 changes: 6 additions & 0 deletions templates/.stylelintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/dist/
/test/
*.min.css
*.js
*.ts
_*
11 changes: 11 additions & 0 deletions templates/.stylelintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
extends: [
'stylelint-config-cmyr',
],
plugins: [
'stylelint-scss',
'stylelint-order',
],
rules: {
},
}
11 changes: 11 additions & 0 deletions templates/.stylelintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
extends: [
'stylelint-config-cmyr',
],
plugins: [
'stylelint-scss',
'stylelint-order',
],
rules: {
},
}

0 comments on commit 652c46d

Please sign in to comment.