From 7b77a0d731ef8ef559048ff3535c13535facfa50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Sat, 1 Dec 2018 14:11:38 +0800 Subject: [PATCH] add prettier lint --- package.json | 4 ++-- scripts/lint-prettier.js | 43 ++++++++++++++++++++++++++++++++++++++++ scripts/prettier.js | 7 ++++--- 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 scripts/lint-prettier.js diff --git a/package.json b/package.json index 1363ec79..46eda13d 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "test": "umi test", "test:component": "umi test ./src/components", "test:all": "node ./tests/run-tests.js", - "prettier": "node node ./scripts/prettier.js", + "prettier": "node ./scripts/prettier.js", "docker:dev": "docker-compose -f ./docker/docker-compose.dev.yml up", "docker:build": "docker-compose -f ./docker/docker-compose.dev.yml build", "docker-prod:dev": "docker-compose -f ./docker/docker-compose.yml up", @@ -100,7 +100,7 @@ }, "lint-staged": { "**/*.{js,jsx,less}": [ - "prettier --write", + "node ./scripts/lint-prettier.js", "git add" ], "**/*.{js,jsx}": "npm run lint-staged:js", diff --git a/scripts/lint-prettier.js b/scripts/lint-prettier.js new file mode 100644 index 00000000..ad825a1a --- /dev/null +++ b/scripts/lint-prettier.js @@ -0,0 +1,43 @@ +/** + * copy to https://github.com/facebook/react/blob/master/scripts/prettier/index.js + * prettier api doc https://prettier.io/docs/en/api.html + *----------*****-------------- + * lint file is prettier + *----------*****-------------- + */ + +const glob = require('glob'); +const prettier = require('prettier'); +const fs = require('fs'); +const prettierConfigPath = require.resolve('../.prettierrc'); + +const files = process.argv.slice(2); + +let didError = false; +let didWarn = false; + +files.forEach(file => { + const options = prettier.resolveConfig.sync(file, { + config: prettierConfigPath, + }); + try { + const fileInfo = prettier.getFileInfo.sync(file); + const input = fs.readFileSync(file, 'utf8'); + const withParserOptions = { + ...options, + parser: fileInfo.inferredParser, + }; + const isPrettier = prettier.check(input, withParserOptions); + if (!isPrettier) { + console.log(`\x1b[31m ${file} is no prettier, please use npm run prettier and git add !`); + didWarn = true; + } + } catch (e) { + didError = true; + } +}); + +if (didWarn || didError) { + process.exit(1); +} +console.log('\x1b[32m lint prettier success!'); diff --git a/scripts/prettier.js b/scripts/prettier.js index 5cbe5c01..b4dbef34 100644 --- a/scripts/prettier.js +++ b/scripts/prettier.js @@ -22,11 +22,11 @@ if (!files.length) { return; } -files.forEach(async file => { +files.forEach(file => { const options = prettier.resolveConfig.sync(file, { config: prettierConfigPath, }); - const fileInfo = await prettier.getFileInfo(file); + const fileInfo = prettier.getFileInfo.sync(file); try { const input = fs.readFileSync(file, 'utf8'); const withParserOptions = { @@ -42,7 +42,8 @@ files.forEach(async file => { didError = true; } }); -console.log('\x1b[32m prettier success!'); + if (didError) { process.exit(1); } +console.log('\x1b[32m prettier success!'); -- GitLab