script: cleanup & reduce useless prettier files (#15467)

This commit is contained in:
ZYSzys 2019-03-18 10:46:17 +08:00 committed by 偏右
parent b1ec61a2da
commit 554fdd4a3c
8 changed files with 96 additions and 158 deletions

View File

@ -3,15 +3,19 @@ import { mount } from 'enzyme';
import List from '..';
describe('List Item Layout', () => {
const data = [{
const data = [
{
key: 1,
href: 'http://ant.design',
title: `ant design`,
avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
description: 'Ant Design, a design language for background applications, is refined by Ant UED Team.',
content: 'We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
description:
'Ant Design, a design language for background applications, is refined by Ant UED Team.',
content:
'We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
extra: 'extra',
}];
},
];
it('horizontal itemLayout List which contains string nodes should not be flex container', () => {
const wrapper = mount(
@ -22,9 +26,14 @@ describe('List Item Layout', () => {
I am <span>ant</span> design list item
</List.Item>
)}
/>
/>,
);
expect(wrapper.find('.ant-list-item').at(0).hasClass('ant-list-item-no-flex')).toBe(true);
expect(
wrapper
.find('.ant-list-item')
.at(0)
.hasClass('ant-list-item-no-flex'),
).toBe(true);
});
it('horizontal itemLayout List should be flex container defaultly', () => {
@ -39,9 +48,14 @@ describe('List Item Layout', () => {
/>
</List.Item>
)}
/>
/>,
);
expect(wrapper.find('.ant-list-item').at(0).hasClass('ant-list-item-no-flex')).toBe(false);
expect(
wrapper
.find('.ant-list-item')
.at(0)
.hasClass('ant-list-item-no-flex'),
).toBe(false);
});
it('vertical itemLayout List should be flex container when there is extra node', () => {
@ -57,9 +71,14 @@ describe('List Item Layout', () => {
/>
</List.Item>
)}
/>
/>,
);
expect(wrapper.find('.ant-list-item').at(0).hasClass('ant-list-item-no-flex')).toBe(false);
expect(
wrapper
.find('.ant-list-item')
.at(0)
.hasClass('ant-list-item-no-flex'),
).toBe(false);
});
it('vertical itemLayout List should not be flex container when there is not extra node', () => {
@ -75,9 +94,14 @@ describe('List Item Layout', () => {
/>
</List.Item>
)}
/>
/>,
);
expect(wrapper.find('.ant-list-item').at(0).hasClass('ant-list-item-no-flex')).toBe(true);
expect(
wrapper
.find('.ant-list-item')
.at(0)
.hasClass('ant-list-item-no-flex'),
).toBe(true);
});
it('horizontal itemLayout List should accept extra node', () => {
@ -92,7 +116,7 @@ describe('List Item Layout', () => {
/>
</List.Item>
)}
/>
/>,
);
expect(wrapper.render()).toMatchSnapshot();
});

View File

@ -232,17 +232,17 @@
"lint-staged": {
"components/**/*.tsx": [
"npm run lint-staged:ts",
"node ./scripts/pre-prettier.js",
"node ./scripts/prettier.js --pre",
"git add"
],
"{tests,site,scripts,components}/**/*.{js,jsx}": [
"npm run lint-staged:es",
"node ./scripts/pre-prettier.js",
"node ./scripts/prettier.js --pre",
"git add"
],
"{site,components}/**/*.less": [
"stylelint --syntax less",
"node ./scripts/pre-prettier.js",
"node ./scripts/prettier.js --pre",
"git add"
],
"components/*/demo/*.md": [

View File

@ -1,48 +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
*----------*****--------------
* lint file is prettier
*----------*****--------------
*/
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);
if (fileInfo.ignored) {
return;
}
const input = fs.readFileSync(file, 'utf8');
const withParserOptions = {
...options,
parser: fileInfo.inferredParser,
};
const isPrettier = prettier.check(input, withParserOptions);
if (!isPrettier) {
// eslint-disable-next-line no-console
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);
}
// eslint-disable-next-line no-console
console.log('\x1b[32m lint prettier success!');

View File

@ -1,47 +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
*----------*****--------------
* pre commit prettier file
*----------*****--------------
*/
const prettier = require('prettier');
const fs = require('fs');
const prettierConfigPath = require.resolve('../.prettierrc');
const files = process.argv.slice(2);
let didError = false;
files.forEach(file => {
const options = prettier.resolveConfig.sync(file, {
config: prettierConfigPath,
});
try {
const fileInfo = prettier.getFileInfo.sync(file);
if (fileInfo.ignored) {
return;
}
const input = fs.readFileSync(file, 'utf8');
const withParserOptions = {
...options,
parser: fileInfo.inferredParser,
};
const output = prettier.format(input, withParserOptions);
if (output !== input) {
fs.writeFileSync(file, output, 'utf8');
// eslint-disable-next-line no-console
console.log(`\x1b[34m ${file} is prettier`);
}
} catch (e) {
didError = true;
}
});
if (didError) {
process.exit(1);
}
// eslint-disable-next-line no-console
console.log('\x1b[32m prettier success!');

View File

@ -9,13 +9,20 @@
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 didError = false;
let files = [];
if (program.pre) {
files = program.args;
} else {
const ignoreFiles = [
'**/node_modules/**',
'build/**',
@ -28,28 +35,21 @@ const ignoreFiles = [
'_site/**',
];
// get all ts files
const tsFiles = glob.sync('**/*.ts*', {
// get all ts, js, less files
['**/*.ts*', '**/*.js*', '**/*.less'].forEach(pattern => {
const matchFiles = glob.sync(pattern, {
ignore: ignoreFiles,
});
files = files.concat(tsFiles);
// get all js files
const jsFiles = glob.sync('**/*.js*', {
ignore: ignoreFiles,
files = files.concat(matchFiles);
});
files = files.concat(jsFiles);
// get all less files
const lessFiles = glob.sync('**/*.less', {
ignore: ignoreFiles,
});
files = files.concat(lessFiles);
}
if (!files.length) {
return;
}
let didError = false;
let didWarn = false;
files.forEach(file => {
const options = prettier.resolveConfig.sync(file, {
config: prettierConfigPath,
@ -64,19 +64,28 @@ files.forEach(file => {
...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(`\x1b[34m ${file} is prettier`);
console.log(chalk.blue(`${file} is prettier`));
}
}
} catch (e) {
didError = true;
}
});
if (didError) {
if (didWarn || didError) {
process.exit(1);
}
// eslint-disable-next-line no-console
console.log('\x1b[32m prettier success!');
console.log(chalk.green('prettier success!'));