Integrate eslint & precommit to codebase (#815)

* add eslint, precommit & refactor

* fix exit code 127

* modify contributing.md & add package-lock

* use .eslintrc.js
This commit is contained in:
Endilie Yacop Sucipto 2018-07-01 12:27:31 +08:00 committed by GitHub
parent 97eaaad744
commit 21dcea2a31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 4836 additions and 2139 deletions

View file

@ -40,7 +40,7 @@ jobs:
- run: *yarn
- save-cache: *save-yarn-cache
- run:
name: Check Prettier
name: Check Prettier & ESLint
command: yarn ci-check
- run:
name: Run Test Suites

31
.eslintrc.js Normal file
View file

@ -0,0 +1,31 @@
const OFF = 0;
const WARNING = 1;
const ERROR = 2;
module.exports = {
extends: [
'fbjs', // eslint-config-fbjs
'prettier' // eslint-config-prettier
],
overrides: [
{
files: ['lib/**/*.js'],
rules: {
// allow console
'no-console': OFF,
// require radix argument in parseInt
'radix': ERROR,
// disallow unused vars
'no-unused-vars': ERROR,
// almost certainly a bug
'no-duplicate-case': ERROR,
}
},
{
files: ['examples/**/*.js'],
rules: {
'no-unused-vars': OFF
}
}
]
};

1
.gitignore vendored
View file

@ -4,6 +4,7 @@
node_modules
.eslintcache
lib/core/metadata.js
lib/core/MetadataBlog.js
lib/pages/

View file

@ -93,7 +93,7 @@ Please make sure the following is done when submitting a pull request:
1. Fork [the repository](https://github.com/facebook/docusaurus) and create your branch from `master`.
1. Add the copyright notice to the top of any code new files you've added.
1. Describe your [**test plan**](#test-plan) in your pull request description. Make sure to [test your changes](https://github.com/facebook/Docusaurus/blob/master/admin/testing-changes-on-Docusaurus-itself.md)!
1. Make sure your code lints (`npm run prettier`).
1. Make sure your code lints (`npm run prettier && npm run lint`).
1. Make sure our Jest tests pass (`npm run test`).
1. If you haven't already, [sign the CLA](https://code.facebook.com/cla).

View file

@ -56,9 +56,9 @@ beforeAll(() => {
});
});
function afterAll() {
afterAll(() => {
clearBuildFolder();
}
});
test('Build folder exists', function() {
return fs.stat(buildDir).then(function(status) {
@ -73,7 +73,6 @@ test('Generated HTML for each Markdown resource', function() {
metadata.push(path.basename());
});
inputMarkdownFiles.forEach(function(file) {
const path = filepath.create(file);
const data = fs.readFileSync(file, 'utf8');
const frontmatter = fm(data);
expect(metadata).toContain(frontmatter.attributes.id + '.html');

View file

@ -5,11 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/
const Metadata = require('./metadata.js');
const React = require('react');
const Container = require('./Container.js');
const SideNav = require('./nav/SideNav.js');
const siteConfig = require(process.cwd() + '/siteConfig.js');
const readCategories = require('../server/readCategories.js');
class DocsSidebar extends React.Component {

View file

@ -6,12 +6,9 @@
*/
const React = require('react');
const fs = require('fs');
const Head = require('./Head.js');
const translation = require('../server/translation.js');
const CWD = process.cwd();
// Component used to provide same head, header, footer, other scripts to all pages
class Redirect extends React.Component {
render() {
@ -28,13 +25,9 @@ class Redirect extends React.Component {
this.props.config.url +
this.props.config.baseUrl +
(this.props.url || 'index.html');
let latestVersion;
const redirect = this.props.redirect || false;
if (fs.existsSync(CWD + '/versions.json')) {
latestVersion = require(CWD + '/versions.json')[0];
}
return (
<html>
<Head

View file

@ -10,8 +10,6 @@
const React = require('react');
const renderMarkdown = require('./renderMarkdown.js');
const CWD = process.cwd();
class Remarkable extends React.Component {
content() {
if (this.props.source) {

View file

@ -7,7 +7,6 @@
const React = require('react');
const fs = require('fs');
const classNames = require('classnames');
const HeaderNav = require('./nav/HeaderNav.js');
const Head = require('./Head.js');

View file

@ -125,11 +125,11 @@ shell.exec('git rm -rf .');
shell.cd('../..');
fromPath = path.join('build', `${PROJECT_NAME}`);
toPath = path.join('build', `${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`);
const fromPath = path.join('build', `${PROJECT_NAME}`);
const toPath = path.join('build', `${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`);
// In github.io case, project is deployed to root. Need to not recursively
// copy the deployment-branch to be.
excludePath = `${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`;
const excludePath = `${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`;
// cannot use shell.cp because it doesn't support copying dotfiles and we
// need to copy directories like .circleci, for example

View file

@ -11,7 +11,6 @@ const chalk = require('chalk');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const readMetadata = require('./server/readMetadata.js');
const metadataUtils = require('./server/metadataUtils.js');
const CWD = process.cwd();

View file

@ -5,17 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const Feed = require('feed');
const chalk = require('chalk');
const CWD = process.cwd();
const siteConfig = require(CWD + '/siteConfig.js');
const blogFolder = path.resolve('../blog/');
const readMetadata = require('./readMetadata.js');
const blogRootURL = siteConfig.url + siteConfig.baseUrl + 'blog';
const siteImageURL =
siteConfig.url + siteConfig.baseUrl + siteConfig.headerIcon;
@ -23,17 +16,6 @@ const utils = require('../core/utils');
const renderMarkdown = require('../core/renderMarkdown.js');
/****************************************************************************/
let readMetadata;
let Metadata;
readMetadata = require('./readMetadata.js');
readMetadata.generateMetadataDocs();
Metadata = require('../core/metadata.js');
/****************************************************************************/
module.exports = function(type) {
console.log('feed.js triggered...');

View file

@ -6,7 +6,7 @@
*/
async function execute() {
const extractTranslations = require('../write-translations.js');
require('../write-translations.js');
const metadataUtils = require('./metadataUtils');
@ -15,7 +15,6 @@ async function execute() {
const fs = require('fs-extra');
const readMetadata = require('./readMetadata.js');
const path = require('path');
const color = require('color');
const getTOC = require('../core/getTOC.js');
const React = require('react');
const mkdirp = require('mkdirp');
@ -321,9 +320,11 @@ async function execute() {
// create sitemap
if (MetadataBlog.length > 0 || Object.keys(Metadata).length > 0) {
let targetFile = join(buildDir, 'sitemap.xml');
sitemap(xml => {
writeFileAndCreateFolder(targetFile, xml);
sitemap((err, xml) => {
if (!err) {
const targetFile = join(buildDir, 'sitemap.xml');
writeFileAndCreateFolder(targetFile, xml);
}
});
}
@ -454,7 +455,6 @@ async function execute() {
fs.writeFileSync(mainCss, css);
// compile/copy pages from user
let pagesArr = [];
files = glob.sync(join(CWD, 'pages', '**'));
files.forEach(file => {
// Why normalize? In case we are on Windows.

View file

@ -10,7 +10,6 @@ const CWD = process.cwd();
const path = require('path');
const fs = require('fs');
const glob = require('glob');
const chalk = require('chalk');
const metadataUtils = require('./metadataUtils');
@ -180,8 +179,6 @@ function generateMetadataDocs() {
const docsDir = path.join(CWD, '../', getDocsPath());
let files = glob.sync(CWD + '/../' + getDocsPath() + '/**');
files.forEach(file => {
let language = 'en';
const extension = path.extname(file);
if (extension === '.md' || extension === '.markdown') {
@ -359,7 +356,7 @@ function generateMetadataBlog() {
});
const sortedMetadatas = metadatas.sort(
(a, b) => parseInt(b.seconds) - parseInt(a.seconds)
(a, b) => parseInt(b.seconds, 10) - parseInt(a.seconds, 10)
);
fs.writeFileSync(

View file

@ -11,14 +11,11 @@ function execute(port, options) {
const metadataUtils = require('./metadataUtils');
const env = require('./env.js');
const translation = require('./translation');
const express = require('express');
const React = require('react');
const request = require('request');
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const color = require('color');
const getTOC = require('../core/getTOC');
const {
blogRouting,
@ -284,10 +281,13 @@ function execute(port, options) {
});
app.get(sitemapRouting(siteConfig.baseUrl), (req, res) => {
res.set('Content-Type', 'application/xml');
sitemap(xml => {
res.send(xml);
sitemap((err, xml) => {
if (err) {
res.status(500).send('Sitemap error');
} else {
res.set('Content-Type', 'application/xml');
res.send(xml);
}
});
});

View file

@ -6,11 +6,7 @@
*/
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const Feed = require('feed');
const chalk = require('chalk');
const glob = require('glob');
const CWD = process.cwd();
@ -104,9 +100,6 @@ module.exports = function(callback) {
});
sm.toXML((err, xml) => {
if (err) {
return 'An error has occurred.';
}
callback(xml);
callback(err, xml);
});
};

View file

@ -9,7 +9,6 @@ const CWD = process.cwd();
const glob = require('glob');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const metadataUtils = require('./metadataUtils');

5822
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -14,15 +14,26 @@
"url": "https://github.com/facebook/Docusaurus.git"
},
"scripts": {
"ci-check": "yarn prettier:diff",
"ci-check": "yarn lint && yarn prettier:diff",
"format:source": "prettier --config .prettierrc --write \"lib/**/*.js\"",
"format:examples": "prettier --config .prettierrc --write \"examples/**/*.js\"",
"lint": "eslint --cache \"lib/**/*.js\" \"examples/**/*.js\"",
"nit:source": "prettier --config .prettierrc --list-different \"lib/**/*.js\"",
"nit:examples": "prettier --config .prettierrc --list-different \"examples/**/*.js\"",
"precommit": "lint-staged",
"prettier": "yarn format:source && yarn format:examples",
"prettier:diff": "yarn nit:source && yarn nit:examples",
"test": "jest"
},
"lint-staged": {
"linters": {
"{lib,examples}/**/*.js": [
"yarn lint --fix",
"yarn prettier",
"git add"
]
}
},
"dependencies": {
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
@ -35,7 +46,7 @@
"chalk": "^2.1.0",
"classnames": "^2.2.6",
"color": "^2.0.1",
"commander": "^2.11.0",
"commander": "^2.16.0",
"crowdin-cli": "^0.3.0",
"cssnano": "^3.10.0",
"escape-string-regexp": "^1.0.5",
@ -75,11 +86,22 @@
"docusaurus-feed": "./lib/generate-feed.js"
},
"devDependencies": {
"babel-eslint": "^8.2.5",
"eslint": "^5.0.1",
"eslint-config-fbjs": "^2.0.1",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-babel": "^5.1.0",
"eslint-plugin-flowtype": "^2.49.3",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.10.0",
"eslint-plugin-relay": "^0.0.23",
"filepath": "^1.1.0",
"front-matter": "^2.3.0",
"glob-promise": "^3.3.0",
"husky": "^0.14.3",
"jest": "^21.2.1",
"prettier": "^1.13.5",
"lint-staged": "^7.2.0",
"prettier": "^1.13.7",
"rimraf": "^2.6.2"
}
}

1004
yarn.lock

File diff suppressed because it is too large Load diff