From 986716b3c3dceecb8f17fea276ef4f406a013a04 Mon Sep 17 00:00:00 2001 From: Kiwi Date: Fri, 29 Jun 2018 12:25:40 -0300 Subject: [PATCH] lint js files, cleanup scripts (#1713) --- .gitignore | 1 + .vscode/settings.json | 5 +- config/jest.config.js | 21 +++---- config/jest/cssTransform.js | 6 +- config/jest/fileTransform.js | 4 +- config/postcss.config.js | 19 +++--- doczrc.js | 4 +- loaders/locales-loader.js | 110 +++++++++++++++++++++++++--------- package-lock.json | 28 ++++++--- package.json | 5 +- scripts/build.js | 15 ++--- scripts/start.js | 14 ----- src/core/client/.babelrc.js | 12 ++-- src/core/client/tsconfig.json | 10 +++- src/core/client/tslint.json | 12 +++- tsconfig.json | 8 ++- tslint.json | 28 +++++++-- 17 files changed, 191 insertions(+), 111 deletions(-) diff --git a/.gitignore b/.gitignore index f9359967b..318868371 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ yarn.lock coverage .idea/ +.vs .docz *.swp *.DS_STORE diff --git a/.vscode/settings.json b/.vscode/settings.json index 499d0f3a9..841afbd57 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,5 +11,6 @@ ".vscode": true, "package-lock.json": true }, - "tslint.autoFixOnSave": true -} + "tslint.autoFixOnSave": true, + "tslint.jsEnable": true +} \ No newline at end of file diff --git a/config/jest.config.js b/config/jest.config.js index 39cb8dbdb..682a2c8a4 100644 --- a/config/jest.config.js +++ b/config/jest.config.js @@ -3,29 +3,26 @@ const paths = require("./paths"); module.exports = { rootDir: "../", roots: ["/src", "/scripts"], - collectCoverageFrom: [ - "src/**/*.{js,jsx,mjs,ts,tsx}" - ], + collectCoverageFrom: ["src/**/*.{js,jsx,mjs,ts,tsx}"], coveragePathIgnorePatterns: ["/node_modules/"], - setupFiles: [ - "/config/polyfills.js" - ], + setupFiles: ["/config/polyfills.js"], testMatch: [ "**/__tests__/**/*.{js,jsx,mjs,ts,tsx}", - "**/*.(spec|test).{js,jsx,mjs,ts,tsx}" + "**/*.(spec|test).{js,jsx,mjs,ts,tsx}", ], testEnvironment: "node", testURL: "http://localhost", transform: { "^.+\\.(js|jsx|mjs|ts|tsx)$": "/node_modules/ts-jest", "^.+\\.css$": "/config/jest/cssTransform.js", - "^(?!.*\\.(js|jsx|mjs|css|json|ftl)$)": "/config/jest/fileTransform.js" + "^(?!.*\\.(js|jsx|mjs|css|json|ftl)$)": + "/config/jest/fileTransform.js", }, transformIgnorePatterns: [ - "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|ts|tsx)$" + "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|ts|tsx)$", ], moduleNameMapper: { - "^react-native$": "react-native-web" + "^react-native$": "react-native-web", }, moduleFileExtensions: [ "web.js", @@ -36,6 +33,6 @@ module.exports = { "node", "mjs", "ts", - "tsx" + "tsx", ], -} +}; diff --git a/config/jest/cssTransform.js b/config/jest/cssTransform.js index 8f6511481..606cc276b 100644 --- a/config/jest/cssTransform.js +++ b/config/jest/cssTransform.js @@ -1,14 +1,14 @@ -'use strict'; +"use strict"; // This is a custom Jest transformer turning style imports into empty objects. // http://facebook.github.io/jest/docs/en/webpack.html module.exports = { process() { - return 'module.exports = {};'; + return "module.exports = {};"; }, getCacheKey() { // The output is always the same. - return 'cssTransform'; + return "cssTransform"; }, }; diff --git a/config/jest/fileTransform.js b/config/jest/fileTransform.js index 9e4047d35..d61964957 100644 --- a/config/jest/fileTransform.js +++ b/config/jest/fileTransform.js @@ -1,6 +1,6 @@ -'use strict'; +"use strict"; -const path = require('path'); +const path = require("path"); // This is a custom Jest transformer turning file imports into filenames. // http://facebook.github.io/jest/docs/en/webpack.html diff --git a/config/postcss.config.js b/config/postcss.config.js index aac3e8543..4de2da6b2 100644 --- a/config/postcss.config.js +++ b/config/postcss.config.js @@ -5,28 +5,31 @@ const kebabCase = require("lodash/kebabCase"); const mapKeys = require("lodash/mapKeys"); const flat = require("flat"); const flexbugsFixes = require("postcss-flexbugs-fixes"); -const paths = require('./paths'); +const paths = require("./paths"); delete require.cache[paths.appThemeVariables]; const variables = require(paths.appThemeVariables); -const flatKebabVariables = mapKeys(flat(variables, {delimiter: "-"}), (_, k) => kebabCase(k)); +const flatKebabVariables = mapKeys( + flat(variables, { delimiter: "-" }), + (_, k) => kebabCase(k) +); module.exports = { // Necessary for external CSS imports to work // https://github.com/facebookincubator/create-react-app/issues/2677 - ident: 'postcss', + ident: "postcss", plugins: [ precss({ variables: flatKebabVariables }), fontMagician(), flexbugsFixes, autoprefixer({ browsers: [ - '>1%', - 'last 4 versions', - 'Firefox ESR', - 'not ie < 9', // React doesn't support IE8 anyway + ">1%", + "last 4 versions", + "Firefox ESR", + "not ie < 9", // React doesn't support IE8 anyway ], - flexbox: 'no-2009', + flexbox: "no-2009", }), ], }; diff --git a/doczrc.js b/doczrc.js index 0ec8c90af..19542a6b6 100644 --- a/doczrc.js +++ b/doczrc.js @@ -37,7 +37,9 @@ export default { }, ], }); - config.resolve.plugins = [new TsconfigPathsPlugin({ extensions, configFile: paths.appTsconfig })]; + config.resolve.plugins = [ + new TsconfigPathsPlugin({ extensions, configFile: paths.appTsconfig }), + ]; // fs.writeFileSync(path.resolve(__dirname, "tmp"), stringify(config, null, 2)); return config; }, diff --git a/loaders/locales-loader.js b/loaders/locales-loader.js index f77fce1a4..4288a461f 100644 --- a/loaders/locales-loader.js +++ b/loaders/locales-loader.js @@ -1,11 +1,11 @@ -const loaderUtils = require('loader-utils'); -const fs = require('fs'); -const path = require('path'); -const camelCase = require('lodash/camelCase'); -const upperFirst = require('lodash/upperFirst'); -const memoize = require('lodash/memoize'); +const loaderUtils = require("loader-utils"); +const fs = require("fs"); +const path = require("path"); +const camelCase = require("lodash/camelCase"); +const upperFirst = require("lodash/upperFirst"); +const memoize = require("lodash/memoize"); -const pascalCase = (x) => upperFirst(camelCase(x)); +const pascalCase = x => upperFirst(camelCase(x)); /** * Default values for every param that can be passed in the loader query. @@ -15,12 +15,12 @@ const DEFAULT_QUERY_VALUES = { pathToLocales: null, // Default locale if non could be negotiated. - defaultLocale: 'en-US', + defaultLocale: "en-US", // Fallback locale if a translation was not found. // If not set, will use the text that is already // in the code base. - fallbackLocale: '', + fallbackLocale: "", // If set, restrict to this list of available locales. availableLocales: null, @@ -33,11 +33,11 @@ const DEFAULT_QUERY_VALUES = { // Target specifies the prefix for fluent files to be loaded. ${target}-xyz.ftl and ${†arget}.ftl are // loaded into the locales. - target: '', + target: "", }; function getFiles(target, pathToLocale, context) { - const {pathToLocales, commonFiles} = context; + const { pathToLocales, commonFiles } = context; const common = []; const suffixes = []; @@ -55,13 +55,22 @@ function getFiles(target, pathToLocale, context) { } }); - return {common, suffixes}; + return { common, suffixes }; } function generateTarget(target, context) { - const {defaultLocale, fallbackLocale, pathToLocales, locales, commonFiles, bundled} = context; + const { + defaultLocale, + fallbackLocale, + pathToLocales, + locales, + commonFiles, + bundled, + } = context; const getLocalePath = locale => path.join(pathToLocales, locale); - const getLocaleFiles = memoize(locale => getFiles(target, getLocalePath(locale), context)); + const getLocaleFiles = memoize(locale => + getFiles(target, getLocalePath(locale), context) + ); const loadables = locales.filter(local => !bundled.includes(local)); return ` @@ -74,34 +83,54 @@ function generateTarget(target, context) { }; // Bundled locales are directly available in the main bundle. - ${bundled.map(locale => ` + ${bundled + .map( + locale => ` { var suffixes = ${JSON.stringify(getLocaleFiles(locale).suffixes)}; var contents = []; - ${getLocaleFiles(locale).common.map(file => ` + ${getLocaleFiles(locale) + .common.map( + file => ` contents.push(require('${getLocalePath(locale)}/${file}')); - `).join("\n")} - contents = contents.concat(suffixes.map(function(suffix) { return require(\`${getLocalePath(locale)}/${target}\${suffix}\`); })); + ` + ) + .join("\n")} + contents = contents.concat(suffixes.map(function(suffix) { return require(\`${getLocalePath( + locale + )}/${target}\${suffix}\`); })); ret.bundled[${JSON.stringify(locale)}] = contents.join("\\n"); } - `).join("\n")} + ` + ) + .join("\n")} // Loadables are in a separate bundle, that can be easily loaded. - ${loadables.map(locale => ` + ${loadables + .map( + locale => ` ret.loadables[${JSON.stringify(locale)}] = function() { var suffixes = ${JSON.stringify(getLocaleFiles(locale).suffixes)}; var promises = []; - ${getLocaleFiles(locale).common.map(file => ` + ${getLocaleFiles(locale) + .common.map( + file => ` promises.push( import( - /* webpackChunkName: ${JSON.stringify(`${target}-locale-${locale}`)}, webpackMode: "lazy" */ + /* webpackChunkName: ${JSON.stringify( + `${target}-locale-${locale}` + )}, webpackMode: "lazy" */ '${getLocalePath(locale)}/${file}' ) ); - `).join("\n")} + ` + ) + .join("\n")} promises = promises.concat(suffixes.map(function(suffix) { return import( - /* webpackChunkName: ${JSON.stringify(`${target}-locale-${locale}`)}, webpackMode: "lazy-once" */ + /* webpackChunkName: ${JSON.stringify( + `${target}-locale-${locale}` + )}, webpackMode: "lazy-once" */ \`${getLocalePath(locale)}/${target}\${suffix}\` ) })); @@ -109,14 +138,28 @@ function generateTarget(target, context) { return modules.map(function(m){return m.default}).join("\\n"); }); }; - `).join("\n")} + ` + ) + .join("\n")} module.exports = ret; `; } module.exports = function(source) { - const options = Object.assign({}, DEFAULT_QUERY_VALUES, loaderUtils.getOptions(this)); - const {pathToLocales, defaultLocale, fallbackLocale, availableLocales, target, bundled, commonFiles} = options; + const options = Object.assign( + {}, + DEFAULT_QUERY_VALUES, + loaderUtils.getOptions(this) + ); + const { + pathToLocales, + defaultLocale, + fallbackLocale, + availableLocales, + target, + bundled, + commonFiles, + } = options; let locales = fs.readdirSync(pathToLocales); if (availableLocales) { @@ -129,7 +172,9 @@ module.exports = function(source) { } if (fallbackLocale && !locales.includes(fallbackLocale)) { - throw new Error(`fallbackLocale ${fallbackLocale} not in available locales`); + throw new Error( + `fallbackLocale ${fallbackLocale} not in available locales` + ); } if (!pathToLocales) { throw new Error(`pathToLocales is required`); @@ -142,7 +187,14 @@ module.exports = function(source) { throw new Error(`defaultLocale ${defaultLocale} not in available locales`); } - const context = {pathToLocales, defaultLocale, fallbackLocale, commonFiles, locales, bundled}; + const context = { + pathToLocales, + defaultLocale, + fallbackLocale, + commonFiles, + locales, + bundled, + }; this.cacheable(); return generateTarget(target, context); diff --git a/package-lock.json b/package-lock.json index c61ddd7fb..d11b39f23 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8255,12 +8255,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -8275,17 +8277,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -8402,7 +8407,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -8414,6 +8420,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -8428,6 +8435,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -8435,12 +8443,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -8459,6 +8469,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -8539,7 +8550,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -8551,6 +8563,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -8672,6 +8685,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", diff --git a/package.json b/package.json index 2ccf6c47c..7ab9f3d07 100644 --- a/package.json +++ b/package.json @@ -16,10 +16,11 @@ "compile:css-types": "tcm src/core/client/", "compile:relay-stream": "relay-compiler --src ./src/core/client/stream --schema ./src/core/server/graph/tenant/schema/schema.graphql --language typescript --artifactDirectory ./src/core/client/stream/__generated__ --no-watchman", "start:development": "NODE_ENV=development ts-node -r tsconfig-paths/register src/index.ts", - "lint-fix": "tslint --fix --project ./tsconfig.json && tslint --fix --project ./src/core/client/tsconfig.json", + "lint-fix": "npm run lint:server -- --fix && npm run lint:client -- --fix && npm run lint:scripts -- --fix", "lint": "npm-run-all --parallel lint:*", "lint:server": "tslint --project ./tsconfig.json", "lint:client": "tslint --project ./src/core/client/tsconfig.json", + "lint:scripts": "tslint config/**/*.js scripts/**/*.js", "docz:watch": "docz dev" }, "author": "", @@ -137,4 +138,4 @@ "webpack-hot-client": "^4.0.3", "webpack-manifest-plugin": "^2.0.3" } -} +} \ No newline at end of file diff --git a/scripts/build.js b/scripts/build.js index 670f338aa..ebbefbfa7 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -22,7 +22,6 @@ const fs = require("fs-extra"); const webpack = require("webpack"); const config = require("../config/webpack.config.prod"); const paths = require("../config/paths"); -const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles"); const formatWebpackMessages = require("react-dev-utils/formatWebpackMessages"); const printHostingInstructions = require("react-dev-utils/printHostingInstructions"); const FileSizeReporter = require("react-dev-utils/FileSizeReporter"); @@ -39,15 +38,12 @@ const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024; // Treat warnings as errors when we are in CI // TODO: This is currently turned off until we have // an optimized build. -const treatWarningsAsErrors = false && process.env.CI && +const treatWarningsAsErrors = + false && + process.env.CI && (typeof process.env.CI !== "string" || process.env.CI.toLowerCase() !== "false"); -// Warn and crash if required files are missing -if (!checkRequiredFiles([paths.appStreamHtml, paths.appStreamIndex])) { - process.exit(1); -} - // First, read the current file sizes in build directory. // This lets us display how much they changed later. measureFileSizesBeforeBuild(paths.appDist) @@ -117,10 +113,7 @@ function build(previousFileSizes) { } return reject(new Error(messages.errors.join("\n\n"))); } - if ( - treatWarningsAsErrors && - messages.warnings.length - ) { + if (treatWarningsAsErrors && messages.warnings.length) { console.log( chalk.yellow( "\nTreating warnings as errors because process.env.CI = true.\n" + diff --git a/scripts/start.js b/scripts/start.js index 6567c9db6..9f56e5ab5 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -20,26 +20,16 @@ const fs = require("fs"); const chalk = require("chalk"); const webpack = require("webpack"); const WebpackDevServer = require("webpack-dev-server"); -const clearConsole = require("react-dev-utils/clearConsole"); -const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles"); const { choosePort, createCompiler, prepareProxy, prepareUrls, } = require("react-dev-utils/WebpackDevServerUtils"); -const openBrowser = require("react-dev-utils/openBrowser"); const paths = require("../config/paths"); const config = require("../config/webpack.config.dev"); const createDevServerConfig = require("../config/webpackDevServer.config"); -const isInteractive = process.stdout.isTTY; - -// Warn and crash if required files are missing -if (!checkRequiredFiles([paths.appStreamHtml, paths.appStreamIndex])) { - process.exit(1); -} - const PORT = parseInt(process.env.DEV_SERVER_PORT, 10) || 8080; const HOST = process.env.HOST || "0.0.0.0"; @@ -85,11 +75,7 @@ choosePort(HOST, PORT) if (err) { return console.log(err); } - if (isInteractive) { - clearConsole(); - } console.log(chalk.cyan("Starting the development server...\n")); - openBrowser(urls.localUrlForBrowser); }); ["SIGINT", "SIGTERM"].forEach(function(sig) { diff --git a/src/core/client/.babelrc.js b/src/core/client/.babelrc.js index 49631c56f..6650ad9c9 100644 --- a/src/core/client/.babelrc.js +++ b/src/core/client/.babelrc.js @@ -1,14 +1,12 @@ module.exports = { presets: [ ["@babel/env", { targets: "last 2 versions, ie 11", modules: false }], - "@babel/react" - ], - plugins: [ - "@babel/syntax-dynamic-import", + "@babel/react", ], + plugins: ["@babel/syntax-dynamic-import"], env: { - "production": { - "plugins": [], + production: { + plugins: [], }, }, -} +}; diff --git a/src/core/client/tsconfig.json b/src/core/client/tsconfig.json index 6c8d28198..dac6724f0 100644 --- a/src/core/client/tsconfig.json +++ b/src/core/client/tsconfig.json @@ -6,7 +6,13 @@ "jsx": "preserve", "noEmit": true, "strictNullChecks": true, - "lib": ["dom", "es7", "scripthost", "es2015", "esnext.asynciterable"], + "lib": [ + "dom", + "es7", + "scripthost", + "es2015", + "esnext.asynciterable" + ], "baseUrl": "./", "paths": { "talk-admin/*": [ @@ -38,4 +44,4 @@ "exclude": [ "node_modules" ] -} +} \ No newline at end of file diff --git a/src/core/client/tslint.json b/src/core/client/tslint.json index e0719ff97..9abc9b921 100644 --- a/src/core/client/tslint.json +++ b/src/core/client/tslint.json @@ -5,10 +5,16 @@ ], "rules": { "jsx-no-multiline-js": false, - "jsx-boolean-value": [true, "never"] + "jsx-boolean-value": [ + true, + "never" + ] }, "jsRules": { "jsx-no-multiline-js": false, - "jsx-boolean-value": [true, "never"] + "jsx-boolean-value": [ + true, + "never" + ] } -} +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index e63068a3d..a4c173d20 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,7 +18,11 @@ "outDir": "dist", // See https://github.com/prismagraphql/graphql-request/issues/26 for why we // have to include "dom" here. - "lib": ["es6", "esnext.asynciterable", "dom"], + "lib": [ + "es6", + "esnext.asynciterable", + "dom" + ], "baseUrl": "./", "paths": { "talk-server/*": [ @@ -36,4 +40,4 @@ "node_modules", "./src/core/client" ] -} +} \ No newline at end of file diff --git a/tslint.json b/tslint.json index a4c11fb1b..68001f8c6 100644 --- a/tslint.json +++ b/tslint.json @@ -7,11 +7,18 @@ "rules": { "prettier": true, "object-literal-sort-keys": false, - "interface-name": [true, "never-prefix"], + "interface-name": [ + true, + "never-prefix" + ], "no-switch-case-fall-through": true, "member-ordering": false, - "variable-name": [true, - "ban-keywords", "check-format", "allow-leading-underscore", "allow-pascal-case" + "variable-name": [ + true, + "ban-keywords", + "check-format", + "allow-leading-underscore", + "allow-pascal-case" ] }, "jsRules": { @@ -19,8 +26,17 @@ "object-literal-sort-keys": false, "no-switch-case-fall-through": true, "member-ordering": false, - "variable-name": [true, - "ban-keywords", "check-format", "allow-leading-underscore", "allow-pascal-case" + "variable-name": [ + true, + "ban-keywords", + "check-format", + "allow-leading-underscore", + "allow-pascal-case" + ] + }, + "linterOptions": { + "exclude": [ + "**/node_modules/**/*" ] } -} +} \ No newline at end of file