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: add lintfix and prettier scripts #829

Merged
merged 3 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
96 changes: 96 additions & 0 deletions packages/cna-template/template/_.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
###
# Place your Prettier ignore content here

###
# .gitignore content is duplicated here due to https://github.com/prettier/prettier/issues/8506

# Created by .ignore support plugin (hsz.mobi)
### Node template
# Logs
/logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# Nuxt generate
dist

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless

# IDE / Editor
.idea

# Service worker
sw.*

# macOS
.DS_Store

# Vim swap files
*.swp
2 changes: 1 addition & 1 deletion packages/cna-template/template/nuxt/.husky/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_
_
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of lintfix results thanks to prettier; I think we should integrate it to the template.

39 changes: 32 additions & 7 deletions packages/cna-template/template/nuxt/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ module.exports = {

// Linter
const eslint = linter.includes('eslint')
const lintStaged = eslint && linter.includes('lintStaged')
const lintStaged = linter.includes('lintStaged')
const stylelint = linter.includes('stylelint')
const prettier = linter.includes('prettier')
const commitlint = linter.includes('commitlint')
const lintScripts = {
eslint: '<%= pmRun %> lint:js',
stylelint: '<%= pmRun %> lint:style'
stylelint: '<%= pmRun %> lint:style',
prettier: '<%= pmRun %> lint:prettier'
}
const lintfixScripts = {
// prettier before eslint to avoid conflicting rules like no-return-assign
// without having to use prettier via eslint (plugin:prettier/recommended)
prettier: 'prettier --write --list-different .',
eslint: "<%= pmRun %> lint:js <%= pm === 'npm' ? '-- ' : '' %>--fix",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used EJS templates because there already was <%= pmRun %>, but I don't see a reason not to `use ${variables} instead`; especially as any incorrect templates in this JS cause a compilation error, e.g. if you escape \' within " instead of replacing ' by " and using ' inside. Also the whitespace-trim didn't seem to be very useful here.

stylelint: "<%= pmRun %> lint:style <%= pm === 'npm' ? '-- ' : '' %>--fix"
}

if (!eslint) {
lintStaged && delete pkg['lint-staged']["*.{js,<%= typescript ? 'ts,' : '' %>vue}"]
delete lintScripts.eslint
delete lintfixScripts.eslint
delete pkg.scripts['lint:js']
delete pkg.devDependencies['@nuxtjs/eslint-config']
delete pkg.devDependencies['@nuxtjs/eslint-module']
Expand All @@ -36,15 +46,20 @@ module.exports = {
delete pkg.devDependencies['lint-staged']
}
if (!stylelint) {
lintStaged && delete pkg['lint-staged']['*.{css,vue}']
lintStaged && delete pkg['lint-staged']['*.{css,scss,sass,html,vue}']
delete lintScripts.stylelint
delete lintfixScripts.stylelint
delete pkg.scripts['lint:style']
delete pkg.devDependencies['@nuxtjs/stylelint-module']
delete pkg.devDependencies.stylelint
delete pkg.devDependencies['stylelint-config-standard']
delete pkg.devDependencies['stylelint-config-prettier']
}
if (!prettier) {
lintStaged && delete pkg['lint-staged']['*.**']
delete pkg.scripts['lint:prettier']
delete lintScripts.prettier
delete lintfixScripts.prettier
delete pkg.devDependencies['eslint-config-prettier']
delete pkg.devDependencies['stylelint-config-prettier']
delete pkg.devDependencies.prettier
Expand All @@ -53,15 +68,25 @@ module.exports = {
delete pkg.devDependencies['@commitlint/config-conventional']
delete pkg.devDependencies['@commitlint/cli']
}
if (!lintStaged && !commitlint) {
delete pkg.devDependencies.husky
delete pkg.scripts.prepare
}

const lintScript = Object.values(lintScripts).join(' && ')
if (lintScript) {
pkg.scripts.lint = lintScript
}
const lintfixScript = Object.values(lintfixScripts).join(' && ')
if (lintfixScript) {
pkg.scripts.lintfix = lintfixScript
}

if (!lintStaged && !commitlint) {
delete pkg.devDependencies.husky
delete pkg.scripts.prepare
} else {
// Move prepare to make it the last script
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of moving, we could also create it here.

const prepare = pkg.scripts.prepare
delete pkg.scripts.prepare
pkg.scripts.prepare = prepare
}

// Modules
if (!features.includes('axios')) {
Expand Down
10 changes: 6 additions & 4 deletions packages/cna-template/template/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"build": "nuxt build",
"start": "nuxt start",
"generate": "nuxt generate",
"lint:js": "eslint --ext \".js,.vue\" --ignore-path .gitignore .",
"lint:style": "stylelint \"**/*.{vue,css}\" --ignore-path .gitignore",
"lint:js": "eslint --ext \".js<%= typescript ? ',.ts' : '' %>,.vue\" --ignore-path .gitignore .",
"lint:style": "stylelint \"**/*.{css,scss,sass,html,vue}\" --ignore-path .gitignore",
"lint:prettier": "prettier --check .",
"prepare": "husky install"
},
"lint-staged": {
"*.{js,vue}": "eslint",
"*.{css,vue}": "stylelint"
"*.{js,<%= typescript ? 'ts,' : '' %>vue}": "eslint --cache",
"*.{css,scss,sass,html,vue}": "stylelint",
"*.**": "prettier --check --ignore-unknown"
},
"dependencies": {
"@nuxt/content": "^1.14.0",
Expand Down
21 changes: 4 additions & 17 deletions packages/create-nuxt-app/lib/saofile.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ module.exports = {
files: '*',
filters: {
'_.eslintrc.js': 'linter.includes("eslint")',
'_.prettierignore': 'linter.includes("prettier")',
'_.prettierrc': 'linter.includes("prettier")',
'_jsconfig.json': 'devTools.includes("jsconfig.json")',
'tsconfig.json': 'language.includes("ts")',
Expand All @@ -119,6 +120,7 @@ module.exports = {
patterns: {
gitignore: '.gitignore',
'_package.json': 'package.json',
'_.prettierignore': '.prettierignore',
'_.prettierrc': '.prettierrc',
'_.eslintrc.js': '.eslintrc.js',
'_jsconfig.json': 'jsconfig.json',
Expand Down Expand Up @@ -167,23 +169,8 @@ module.exports = {

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

if (this.answers.linter.includes('eslint')) {
const options = ['run', 'lint:js', '--', '--fix']
if (this.answers.pm === 'yarn') {
options.splice(2, 1)
}
spawn.sync(this.answers.pm, options, {
cwd: this.outDir,
stdio: 'inherit'
})
}

if (this.answers.linter.includes('stylelint')) {
const options = ['run', 'lint:style', '--', '--fix']
if (this.answers.pm === 'yarn') {
options.splice(2, 1)
}
spawn.sync(this.answers.pm, options, {
if (['eslint', 'stylelint', 'prettier'].some(linter => this.answers.linter.includes(linter))) {
spawn.sync(this.answers.pm, ['run', 'lintfix'], {
cwd: this.outDir,
stdio: 'inherit'
})
Expand Down
27 changes: 21 additions & 6 deletions packages/create-nuxt-app/test/snapshots/index.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,7 @@ Generated by [AVA](https://avajs.dev).
'.husky/commit-msg',
'.husky/common.sh',
'.husky/pre-commit',
'.prettierignore',
'.prettierrc',
'README.md',
'commitlint.config.js',
Expand Down Expand Up @@ -2108,17 +2109,20 @@ Generated by [AVA](https://avajs.dev).
'stylelint-config-standard': '^22.0.0',
},
'lint-staged': {
'*.{css,vue}': 'stylelint',
'*.{js,vue}': 'eslint',
'*.**': 'prettier --check --ignore-unknown',
'*.{css,scss,sass,html,vue}': 'stylelint',
'*.{js,vue}': 'eslint --cache',
},
private: true,
scripts: {
build: 'nuxt build',
dev: 'nuxt',
generate: 'nuxt generate',
lint: 'yarn lint:js && yarn lint:style',
lint: 'yarn lint:js && yarn lint:style && yarn lint:prettier',
'lint:js': 'eslint --ext ".js,.vue" --ignore-path .gitignore .',
'lint:style': 'stylelint "**/*.{vue,css}" --ignore-path .gitignore',
'lint:prettier': 'prettier --check .',
'lint:style': 'stylelint "**/*.{css,scss,sass,html,vue}" --ignore-path .gitignore',
lintfix: 'prettier --write --list-different . && yarn lint:js --fix && yarn lint:style --fix',
prepare: 'husky install',
start: 'nuxt start',
},
Expand Down Expand Up @@ -2213,6 +2217,7 @@ Generated by [AVA](https://avajs.dev).
generate: 'nuxt generate',
lint: 'yarn lint:js',
'lint:js': 'eslint --ext ".js,.vue" --ignore-path .gitignore .',
lintfix: 'yarn lint:js --fix',
start: 'nuxt start',
},
}
Expand Down Expand Up @@ -2271,6 +2276,7 @@ Generated by [AVA](https://avajs.dev).
[
'.editorconfig',
'.gitignore',
'.prettierignore',
'.prettierrc',
'README.md',
'components/NuxtLogo.vue',
Expand Down Expand Up @@ -2298,6 +2304,9 @@ Generated by [AVA](https://avajs.dev).
build: 'nuxt build',
dev: 'nuxt',
generate: 'nuxt generate',
lint: 'yarn lint:prettier',
'lint:prettier': 'prettier --check .',
lintfix: 'prettier --write --list-different .',
start: 'nuxt start',
},
}
Expand Down Expand Up @@ -2371,12 +2380,17 @@ Generated by [AVA](https://avajs.dev).
'core-js': '^3.16.2',
nuxt: '^2.15.8',
},
devDependencies: {},
Copy link
Contributor Author

@scscgit scscgit Jul 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the lint-staged was selected alone, which previously didn't install it.

devDependencies: {
husky: '^6.0.0',
'lint-staged': '^10.5.4',
},
'lint-staged': {},
private: true,
scripts: {
build: 'nuxt build',
dev: 'nuxt',
generate: 'nuxt generate',
prepare: 'husky install',
start: 'nuxt start',
},
}
Expand Down Expand Up @@ -2462,7 +2476,8 @@ Generated by [AVA](https://avajs.dev).
dev: 'nuxt',
generate: 'nuxt generate',
lint: 'yarn lint:style',
'lint:style': 'stylelint "**/*.{vue,css}" --ignore-path .gitignore',
'lint:style': 'stylelint "**/*.{css,scss,sass,html,vue}" --ignore-path .gitignore',
lintfix: 'yarn lint:style --fix',
start: 'nuxt start',
},
}
Expand Down
Binary file modified packages/create-nuxt-app/test/snapshots/index.test.js.snap
Binary file not shown.