Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit f3e61cd

Browse files
authored
feat: add lintfix and prettier scripts (#829)
fix lint-staged coverage fix #827 to avoid eslint conflicts by reordering prettier in lintfix
1 parent b734105 commit f3e61cd

File tree

7 files changed

+233
-108
lines changed

7 files changed

+233
-108
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
###
2+
# Place your Prettier ignore content here
3+
4+
###
5+
# .gitignore content is duplicated here due to https://github.com/prettier/prettier/issues/8506
6+
7+
# Created by .ignore support plugin (hsz.mobi)
8+
### Node template
9+
# Logs
10+
/logs
11+
*.log
12+
npm-debug.log*
13+
yarn-debug.log*
14+
yarn-error.log*
15+
16+
# Runtime data
17+
pids
18+
*.pid
19+
*.seed
20+
*.pid.lock
21+
22+
# Directory for instrumented libs generated by jscoverage/JSCover
23+
lib-cov
24+
25+
# Coverage directory used by tools like istanbul
26+
coverage
27+
28+
# nyc test coverage
29+
.nyc_output
30+
31+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
32+
.grunt
33+
34+
# Bower dependency directory (https://bower.io/)
35+
bower_components
36+
37+
# node-waf configuration
38+
.lock-wscript
39+
40+
# Compiled binary addons (https://nodejs.org/api/addons.html)
41+
build/Release
42+
43+
# Dependency directories
44+
node_modules/
45+
jspm_packages/
46+
47+
# TypeScript v1 declaration files
48+
typings/
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Optional REPL history
57+
.node_repl_history
58+
59+
# Output of 'npm pack'
60+
*.tgz
61+
62+
# Yarn Integrity file
63+
.yarn-integrity
64+
65+
# dotenv environment variables file
66+
.env
67+
68+
# parcel-bundler cache (https://parceljs.org/)
69+
.cache
70+
71+
# next.js build output
72+
.next
73+
74+
# nuxt.js build output
75+
.nuxt
76+
77+
# Nuxt generate
78+
dist
79+
80+
# vuepress build output
81+
.vuepress/dist
82+
83+
# Serverless directories
84+
.serverless
85+
86+
# IDE / Editor
87+
.idea
88+
89+
# Service worker
90+
sw.*
91+
92+
# macOS
93+
.DS_Store
94+
95+
# Vim swap files
96+
*.swp
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
_
1+
_

packages/cna-template/template/nuxt/package.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,27 @@ module.exports = {
1212

1313
// Linter
1414
const eslint = linter.includes('eslint')
15-
const lintStaged = eslint && linter.includes('lintStaged')
15+
const lintStaged = linter.includes('lintStaged')
1616
const stylelint = linter.includes('stylelint')
1717
const prettier = linter.includes('prettier')
1818
const commitlint = linter.includes('commitlint')
1919
const lintScripts = {
2020
eslint: '<%= pmRun %> lint:js',
21-
stylelint: '<%= pmRun %> lint:style'
21+
stylelint: '<%= pmRun %> lint:style',
22+
prettier: '<%= pmRun %> lint:prettier'
23+
}
24+
const lintfixScripts = {
25+
// prettier before eslint to avoid conflicting rules like no-return-assign
26+
// without having to use prettier via eslint (plugin:prettier/recommended)
27+
prettier: 'prettier --write --list-different .',
28+
eslint: "<%= pmRun %> lint:js <%= pm === 'npm' ? '-- ' : '' %>--fix",
29+
stylelint: "<%= pmRun %> lint:style <%= pm === 'npm' ? '-- ' : '' %>--fix"
2230
}
2331

2432
if (!eslint) {
33+
lintStaged && delete pkg['lint-staged']["*.{js,<%= typescript ? 'ts,' : '' %>vue}"]
2534
delete lintScripts.eslint
35+
delete lintfixScripts.eslint
2636
delete pkg.scripts['lint:js']
2737
delete pkg.devDependencies['@nuxtjs/eslint-config']
2838
delete pkg.devDependencies['@nuxtjs/eslint-module']
@@ -36,15 +46,20 @@ module.exports = {
3646
delete pkg.devDependencies['lint-staged']
3747
}
3848
if (!stylelint) {
39-
lintStaged && delete pkg['lint-staged']['*.{css,vue}']
49+
lintStaged && delete pkg['lint-staged']['*.{css,scss,sass,html,vue}']
4050
delete lintScripts.stylelint
51+
delete lintfixScripts.stylelint
4152
delete pkg.scripts['lint:style']
4253
delete pkg.devDependencies['@nuxtjs/stylelint-module']
4354
delete pkg.devDependencies.stylelint
4455
delete pkg.devDependencies['stylelint-config-standard']
4556
delete pkg.devDependencies['stylelint-config-prettier']
4657
}
4758
if (!prettier) {
59+
lintStaged && delete pkg['lint-staged']['*.**']
60+
delete pkg.scripts['lint:prettier']
61+
delete lintScripts.prettier
62+
delete lintfixScripts.prettier
4863
delete pkg.devDependencies['eslint-config-prettier']
4964
delete pkg.devDependencies['stylelint-config-prettier']
5065
delete pkg.devDependencies.prettier
@@ -53,15 +68,25 @@ module.exports = {
5368
delete pkg.devDependencies['@commitlint/config-conventional']
5469
delete pkg.devDependencies['@commitlint/cli']
5570
}
56-
if (!lintStaged && !commitlint) {
57-
delete pkg.devDependencies.husky
58-
delete pkg.scripts.prepare
59-
}
6071

6172
const lintScript = Object.values(lintScripts).join(' && ')
6273
if (lintScript) {
6374
pkg.scripts.lint = lintScript
6475
}
76+
const lintfixScript = Object.values(lintfixScripts).join(' && ')
77+
if (lintfixScript) {
78+
pkg.scripts.lintfix = lintfixScript
79+
}
80+
81+
if (!lintStaged && !commitlint) {
82+
delete pkg.devDependencies.husky
83+
delete pkg.scripts.prepare
84+
} else {
85+
// Move prepare to make it the last script
86+
const prepare = pkg.scripts.prepare
87+
delete pkg.scripts.prepare
88+
pkg.scripts.prepare = prepare
89+
}
6590

6691
// Modules
6792
if (!features.includes('axios')) {

packages/cna-template/template/nuxt/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
"build": "nuxt build",
55
"start": "nuxt start",
66
"generate": "nuxt generate",
7-
"lint:js": "eslint --ext \".js,.vue\" --ignore-path .gitignore .",
8-
"lint:style": "stylelint \"**/*.{vue,css}\" --ignore-path .gitignore",
7+
"lint:js": "eslint --ext \".js<%= typescript ? ',.ts' : '' %>,.vue\" --ignore-path .gitignore .",
8+
"lint:style": "stylelint \"**/*.{css,scss,sass,html,vue}\" --ignore-path .gitignore",
9+
"lint:prettier": "prettier --check .",
910
"prepare": "husky install"
1011
},
1112
"lint-staged": {
12-
"*.{js,vue}": "eslint",
13-
"*.{css,vue}": "stylelint"
13+
"*.{js,<%= typescript ? 'ts,' : '' %>vue}": "eslint --cache",
14+
"*.{css,scss,sass,html,vue}": "stylelint",
15+
"*.**": "prettier --check --ignore-unknown"
1416
},
1517
"dependencies": {
1618
"@nuxt/content": "^1.15.1",

packages/create-nuxt-app/lib/saofile.js

+4-17
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ module.exports = {
103103
files: '*',
104104
filters: {
105105
'_.eslintrc.js': 'linter.includes("eslint")',
106+
'_.prettierignore': 'linter.includes("prettier")',
106107
'_.prettierrc': 'linter.includes("prettier")',
107108
'_jsconfig.json': 'devTools.includes("jsconfig.json")',
108109
'tsconfig.json': 'language.includes("ts")',
@@ -119,6 +120,7 @@ module.exports = {
119120
patterns: {
120121
gitignore: '.gitignore',
121122
'_package.json': 'package.json',
123+
'_.prettierignore': '.prettierignore',
122124
'_.prettierrc': '.prettierrc',
123125
'_.eslintrc.js': '.eslintrc.js',
124126
'_jsconfig.json': 'jsconfig.json',
@@ -167,23 +169,8 @@ module.exports = {
167169

168170
await this.npmInstall({ npmClient: this.answers.pm })
169171

170-
if (this.answers.linter.includes('eslint')) {
171-
const options = ['run', 'lint:js', '--', '--fix']
172-
if (this.answers.pm === 'yarn') {
173-
options.splice(2, 1)
174-
}
175-
spawn.sync(this.answers.pm, options, {
176-
cwd: this.outDir,
177-
stdio: 'inherit'
178-
})
179-
}
180-
181-
if (this.answers.linter.includes('stylelint')) {
182-
const options = ['run', 'lint:style', '--', '--fix']
183-
if (this.answers.pm === 'yarn') {
184-
options.splice(2, 1)
185-
}
186-
spawn.sync(this.answers.pm, options, {
172+
if (['eslint', 'stylelint', 'prettier'].some(linter => this.answers.linter.includes(linter))) {
173+
spawn.sync(this.answers.pm, ['run', 'lintfix'], {
187174
cwd: this.outDir,
188175
stdio: 'inherit'
189176
})

0 commit comments

Comments
 (0)