use prettier cli

This commit is contained in:
afc163 2019-05-07 14:57:25 +08:00
parent c4dcc0a827
commit 66f81550e4
No known key found for this signature in database
GPG Key ID: 738F973FCE5C6B48
3 changed files with 24 additions and 93 deletions

View File

@ -1,4 +1,26 @@
**/*.svg
package.json
.umi
.umi-production
.umi-production
AUTHORS.txt
lib/
es/
dist/
_site/
coverage/
CNAME
LICENSE
yarn.lock
netlify.toml
yarn-error.log
*.sh
*.snap
components/*/*.js
components/*/*.jsx
.gitignore
.npmignore
.prettierignore
.DS_Store
.editorconfig
.eslintignore
**/*.yml

View File

@ -230,7 +230,7 @@
"lint-staged:ts": "tsc && node node_modules/tslint/bin/tslint",
"lint-staged:es": "eslint ./.*.js ./webpack.config.js",
"lint-staged:demo": "cross-env RUN_ENV=DEMO eslint --ext '.md'",
"prettier": "node ./scripts/prettier.js"
"prettier": "prettier -c --write '**/*'"
},
"pre-commit": [
"lint-staged"

View File

@ -1,91 +0,0 @@
/**
* copy to https://github.com/facebook/react/blob/master/scripts/prettier/index.js
* prettier api doc https://prettier.io/docs/en/api.html
*----------*****--------------
* prettier all js and all ts.
*----------*****--------------
*/
const glob = require('glob');
const prettier = require('prettier');
const fs = require('fs');
const chalk = require('chalk');
const program = require('commander');
program
.option('--pre', 'pre-prettier')
.option('--lint', 'lint-prettier')
.parse(process.argv);
const prettierConfigPath = require.resolve('../.prettierrc');
let files = [];
if (program.pre) {
files = program.args;
} else {
const ignoreFiles = [
'**/node_modules/**',
'build/**',
'es/**',
'lib/**',
'**/**.snap',
'**/**.map',
'**/components/style/color/**',
'**/dist/**',
'_site/**',
];
// get all ts, js, less files
['**/*.ts*', '**/*.js*', '**/*.less', '**/*.md', '**/*.html'].forEach(pattern => {
const matchFiles = glob.sync(pattern, {
ignore: ignoreFiles,
});
files = files.concat(matchFiles);
});
}
if (!files.length) {
return;
}
let didError = false;
let didWarn = false;
files.forEach(file => {
const options = prettier.resolveConfig.sync(file, {
config: prettierConfigPath,
});
const fileInfo = prettier.getFileInfo.sync(file);
if (fileInfo.ignored) {
return;
}
try {
const input = fs.readFileSync(file, 'utf8');
const withParserOptions = {
...options,
parser: fileInfo.inferredParser,
};
if (program.lint) {
const isPrettier = prettier.check(input, withParserOptions);
if (!isPrettier) {
// eslint-disable-next-line no-console
console.log(chalk.red(`${file} is no prettier, please use npm run prettier and git add !`));
didWarn = true;
}
} else {
const output = prettier.format(input, withParserOptions);
if (output !== input) {
fs.writeFileSync(file, output, 'utf8');
// eslint-disable-next-line no-console
console.log(chalk.blue(`${file} is prettier`));
}
}
} catch (e) {
didError = true;
}
});
if (didWarn || didError) {
process.exit(1);
}
// eslint-disable-next-line no-console
console.log(chalk.green('prettier success!'));