]> git.ipfire.org Git - thirdparty/vuejs/create-vue.git/commitdiff
feat: use separate commands for linting and formatting
authorHaoqun Jiang <haoqunjiang@gmail.com>
Wed, 22 Feb 2023 13:11:04 +0000 (21:11 +0800)
committerHaoqun Jiang <haoqunjiang@gmail.com>
Wed, 22 Feb 2023 13:11:04 +0000 (21:11 +0800)
Closes #190

index.ts
utils/renderEslint.ts

index 0fc6bb4544d181b4487925ff446d91fbd50eeb55..bfd82c71dd060c98e87fffca3a0cefd3ab674236 100755 (executable)
--- a/index.ts
+++ b/index.ts
@@ -450,7 +450,7 @@ async function init() {
   }
   console.log(`  ${bold(green(getCommand(packageManager, 'install')))}`)
   if (needsPrettier) {
-    console.log(`  ${bold(green(getCommand(packageManager, 'lint')))}`)
+    console.log(`  ${bold(green(getCommand(packageManager, 'format')))}`)
   }
   console.log(`  ${bold(green(getCommand(packageManager, 'dev')))}`)
   console.log()
index b6033a7cbf0036e6a57b8545037fe182d37b715d..762f0f88ac39797cf9b04d8383e1eefe450791d5 100644 (file)
@@ -45,19 +45,27 @@ export default function renderEslint(
     additionalDependencies
   })
 
+  const scripts: Record<string, string> = {
+    // Note that we reuse .gitignore here to avoid duplicating the configuration
+    lint: needsTypeScript
+      ? 'eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore'
+      : 'eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore'
+  }
+
+  // Theoretically, we could add Prettier without requring ESLint.
+  // But it doesn't seem to be a good practice, so we just leave it here.
+  if (needsPrettier) {
+    // Default to only format the `src/` directory to avoid too much noise, and
+    // the need for a `.prettierignore` file.
+    // Users can still append any paths they'd like to format to the command,
+    // e.g. `npm run format cypress/`.
+    scripts.format = 'prettier --write src/'
+  }
+
   // update package.json
   const packageJsonPath = path.resolve(rootDir, 'package.json')
   const existingPkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
-  const updatedPkg = sortDependencies(
-    deepMerge(deepMerge(existingPkg, pkg), {
-      scripts: {
-        // Note that we reuse .gitignore here to avoid duplicating the configuration
-        lint: needsTypeScript
-          ? 'eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore'
-          : 'eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore'
-      }
-    })
-  )
+  const updatedPkg = sortDependencies(deepMerge(deepMerge(existingPkg, pkg), { scripts }))
   fs.writeFileSync(packageJsonPath, JSON.stringify(updatedPkg, null, 2) + '\n', 'utf-8')
 
   // write to .eslintrc.cjs, .prettierrc.json, etc.