Skip to content

Commit 0b657d5

Browse files
committed
feat: add eslint
1 parent 193c773 commit 0b657d5

File tree

79 files changed

+372
-241
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+372
-241
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
max_line_length = 120
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true

.eslintignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
### Node ###
2+
3+
.next
4+
/next.config.js
5+
node_modules
6+
src/cypress
7+
src/lib/helpers/logger.js
8+
src/lib/helpers/asyncForEach.js
9+
src/lib/templates
10+
src/tests
11+
test/sample/node_modules
12+
test/sample/out_functions
13+
test/sample/netlify/functions
14+
test/sample/my-publish-dir
15+
test/sample/.next
16+
test/sample/.netlify

.eslintrc.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const { overrides } = require('@netlify/eslint-config-node')
2+
3+
const customOverrides = [
4+
{
5+
files: ['*.js'],
6+
rules: {
7+
complexity: 0,
8+
'fp/no-let': 0,
9+
'fp/no-loops': 0,
10+
'fp/no-mutation': 0,
11+
'fp/no-mutating-methods': 0,
12+
'id-length': 0,
13+
'max-statements': 0,
14+
'no-await-in-loop': 0,
15+
'node/exports-style': 0,
16+
'node/global-require': 0,
17+
'node/no-unpublished-require': 0,
18+
'node/prefer-global/process': 0,
19+
'no-magic-numbers': 0,
20+
'no-promise-executor-return': 0,
21+
'no-prototype-builtins': 0,
22+
'no-shadow': 0,
23+
'prefer-regex-literals': 0,
24+
'promise/prefer-await-to-callbacks': 0,
25+
'unicorn/filename-case': 0,
26+
'unicorn/no-array-push-push': 0,
27+
},
28+
},
29+
]
30+
31+
module.exports = {
32+
extends: '@netlify/eslint-config-node',
33+
rules: {},
34+
overrides: [...overrides, ...customOverrides],
35+
}

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.github/workflows/test.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ jobs:
2626
uses: actions/setup-node@v2
2727
with:
2828
node-version: ${{ matrix.node-version }}
29-
- run: npm install
29+
- run: npm ci
30+
- name: Linting
31+
run: npm run format:ci
32+
if: "${{ matrix.node-version == '15.x' }}"
3033
- run: npm test

index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
const fs = require('fs')
2-
const path = require('path')
32
const util = require('util')
4-
const makeDir = require('make-dir')
3+
54
const findUp = require('find-up')
6-
const nextOnNetlify = require('./src/index.js')
5+
const makeDir = require('make-dir')
76

8-
const validateNextUsage = require('./helpers/validateNextUsage')
7+
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
8+
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
99
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
1010
const getNextConfig = require('./helpers/getNextConfig')
11-
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
12-
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
11+
const validateNextUsage = require('./helpers/validateNextUsage')
12+
const nextOnNetlify = require('./src/index.js')
1313

1414
const pWriteFile = util.promisify(fs.writeFile)
1515

package.json

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111
"!src/tests"
1212
],
1313
"scripts": {
14-
"format": "prettier --write .",
1514
"build": "next build test/sample",
15+
"format": "run-s format:check-fix:*",
16+
"format:ci": "run-s format:check:*",
17+
"format:check-fix:lint": "run-e format:check:lint format:fix:lint",
18+
"format:check:lint": "cross-env-shell eslint $npm_package_config_eslint",
19+
"format:fix:lint": "cross-env-shell eslint --fix $npm_package_config_eslint",
20+
"format:check-fix:prettier": "run-e format:check:prettier format:fix:prettier",
21+
"format:check:prettier": "cross-env-shell prettier --check $npm_package_config_prettier",
22+
"format:fix:prettier": "cross-env-shell prettier --write $npm_package_config_prettier",
1623
"test:plugin": "npm run build && jest",
1724
"test:src": "jest --config src/tests/jest.config.js",
1825
"test": "npm run test:plugin && npm run test:src",
@@ -26,6 +33,10 @@
2633
"prepublishOnly:install": "npm ci",
2734
"prepublishOnly:test": "npm test"
2835
},
36+
"config": {
37+
"eslint": "--cache --format=codeframe --max-warnings=0 \"{src,scripts,tests,.github}/**/*.{js,md,html}\" \"*.{js,md,html}\" \".*.{js,md,html}\"",
38+
"prettier": "--loglevel=warn \"{src,scripts,tests,.github}/**/*.{js,md,yml,json,html}\" \"*.{js,yml,json,html}\" \".*.{js,yml,json,html}\" \"!package-lock.json\""
39+
},
2940
"repository": {
3041
"type": "git",
3142
"url": "git+https://github.com/netlify/netlify-plugin-nextjs.git"
@@ -77,7 +88,7 @@
7788
"husky": {
7889
"hooks": {
7990
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
80-
"pre-commit": "prettier --check ."
91+
"pre-push": "npm run format"
8192
}
8293
},
8394
"engines": {

src/cypress/plugins/buildProject.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { join } = require('path')
2+
23
const execa = require('execa')
34

45
// Build the given NextJS project

src/cypress/plugins/clearProject.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { join } = require('path')
2+
23
const { emptyDirSync } = require('fs-extra')
34

45
// Clear the project

src/cypress/plugins/copyFixture.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { join } = require('path')
2+
23
const { copySync } = require('fs-extra')
34

45
// Copy the fixture files from fixtures/ to the project folder

src/cypress/plugins/deployProject.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
const { join } = require('path')
2+
3+
const execa = require('execa')
14
const { removeSync } = require('fs-extra')
25
const waitOn = require('wait-on')
3-
const execa = require('execa')
4-
const { join } = require('path')
6+
57
const getBaseUrl = require('./getBaseUrl')
68

79
// Deploy the project locally, using netlify dev
@@ -62,7 +64,7 @@ const deployProject = ({ project }, config) => {
6264
return deployLocally({ project }, config)
6365
}
6466
// Deployment on Netlify
65-
else if (config.env.DEPLOY == 'netlify') {
67+
if (config.env.DEPLOY == 'netlify') {
6668
return deployOnNetlify({ project }, config)
6769
}
6870
}

src/cypress/plugins/getBaseUrl.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { join } = require('path')
2+
23
const { readJsonSync } = require('fs-extra')
34

45
// Set baseurl, either localhost:8888 or based on deployed Netlify URL
@@ -8,7 +9,7 @@ const getBaseUrl = ({ project }, config) => {
89
return 'http://localhost:8888/'
910
}
1011
// Deployment on Netlify
11-
else if (config.env.DEPLOY == 'netlify') {
12+
if (config.env.DEPLOY == 'netlify') {
1213
const { deploy_url } = readJsonSync(join(config.buildsFolder, project, 'deployment.json'))
1314
return deploy_url
1415
}

src/cypress/plugins/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
const { join } = require('path')
77

8+
const buildProject = require('./buildProject')
9+
const clearDeployment = require('./clearDeployment')
810
const clearProject = require('./clearProject')
911
const copyFixture = require('./copyFixture')
10-
const buildProject = require('./buildProject')
1112
const deployProject = require('./deployProject')
1213
const getBaseUrl = require('./getBaseUrl')
13-
const clearDeployment = require('./clearDeployment')
1414

1515
const tasks = [clearProject, copyFixture, buildProject, deployProject, getBaseUrl, clearDeployment]
1616

src/index.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
const { normalize } = require('path')
2-
const debounceFn = require('debounce-fn')
2+
33
const chokidar = require('chokidar')
4+
const debounceFn = require('debounce-fn')
45
const execa = require('execa')
56

7+
const { NETLIFY_PUBLISH_PATH, NETLIFY_FUNCTIONS_PATH, SRC_FILES } = require('./lib/config')
68
const { logTitle } = require('./lib/helpers/logger')
7-
8-
const prepareFolders = require('./lib/steps/prepareFolders')
9-
const copyPublicFiles = require('./lib/steps/copyPublicFiles')
109
const copyNextAssets = require('./lib/steps/copyNextAssets')
11-
const setupPages = require('./lib/steps/setupPages')
10+
const copyPublicFiles = require('./lib/steps/copyPublicFiles')
11+
const prepareFolders = require('./lib/steps/prepareFolders')
12+
const setupHeaders = require('./lib/steps/setupHeaders')
1213
const setupImageFunction = require('./lib/steps/setupImageFunction')
14+
const setupPages = require('./lib/steps/setupPages')
1315
const setupRedirects = require('./lib/steps/setupRedirects')
14-
const setupHeaders = require('./lib/steps/setupHeaders')
15-
const { NETLIFY_PUBLISH_PATH, NETLIFY_FUNCTIONS_PATH, SRC_FILES } = require('./lib/config')
1616

1717
const build = async (functionsPath, publishPath) => {
1818
const trackNextOnNetlifyFiles = prepareFolders({
@@ -45,8 +45,8 @@ const watch = (functionsPath, publishPath) => {
4545
try {
4646
execa.sync('next', ['build'], { stdio: 'inherit' })
4747
await build(functionsPath, publishPath)
48-
} catch (e) {
49-
console.log(e)
48+
} catch (error) {
49+
console.log(error)
5050
}
5151
},
5252
{
@@ -57,13 +57,13 @@ const watch = (functionsPath, publishPath) => {
5757
chokidar.watch(SRC_FILES).on('all', runBuild)
5858
}
5959

60-
/** options param:
61-
* {
62-
* functionsDir: string to path
63-
* publishDir: string to path
64-
* watch: { directory: string to path }
65-
* }
66-
*/
60+
// options param:
61+
// {
62+
// functionsDir: string to path
63+
// publishDir: string to path
64+
// watch: { directory: string to path }
65+
// }
66+
//
6767

6868
const nextOnNetlify = async (options = {}) => {
6969
const functionsPath = normalize(options.functionsDir || NETLIFY_FUNCTIONS_PATH)

src/lib/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { join } = require('path')
2-
const getNextDistDir = require('./helpers/getNextDistDir')
2+
33
const getNextSrcDirs = require('./helpers/getNextSrcDir')
44

55
// This is where next-on-netlify will place all static files.

src/lib/constants/regex.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const CATCH_ALL_REGEX = /\/\[\.{3}(.*)\](.json)?$/
2-
const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)\]{2}(.json)?$/
3-
const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)\]/g
1+
const CATCH_ALL_REGEX = /\/\[\.{3}(.*)](.json)?$/
2+
const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)]{2}(.json)?$/
3+
const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)]/g
44

55
module.exports = {
66
CATCH_ALL_REGEX,

src/lib/helpers/addDefaultLocaleRedirect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const addDefaultLocaleRedirect = async (redirects, route, target, additionalPara
1111
if (!defaultLocale) return
1212

1313
const routePieces = route.split('/')
14-
const routeLocale = routePieces[1]
14+
const [, routeLocale] = routePieces
1515
if (routeLocale === defaultLocale) {
1616
const nakedRoute = route === `/${routeLocale}` ? '/' : route.replace(`/${routeLocale}`, '')
1717
redirects.push({

src/lib/helpers/addLocaleRedirects.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const getI18n = require('./getI18n')
2-
const getDataRouteForRoute = require('./getDataRouteForRoute')
31
const asyncForEach = require('./asyncForEach')
2+
const getDataRouteForRoute = require('./getDataRouteForRoute')
3+
const getI18n = require('./getI18n')
44

55
const addLocaleRedirects = async (redirects, route, target) => {
66
const i18n = await getI18n()

src/lib/helpers/copyDynamicImportChunks.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
const { join } = require('path')
2+
23
const { copySync, readdirSync } = require('fs-extra')
3-
const { logTitle } = require('../helpers/logger')
4+
45
const getNextDistDir = require('./getNextDistDir')
6+
const { logTitle } = require('./logger')
57

68
// Check if there are dynamic import chunks and copy to the necessary function dir
79
const copyDynamicImportChunks = async (functionPath) => {
810
const nextDistDir = await getNextDistDir()
911
const chunksPath = join(nextDistDir, 'serverless')
1012
const files = readdirSync(chunksPath)
11-
const chunkRegex = new RegExp(/^(\.?[-_$~A-Z0-9a-z]+){1,}\.js$/g)
12-
const excludeFiles = ['init-server.js.js', 'on-error-server.js.js']
13+
const chunkRegex = new RegExp(/^(\.?[-$~\w]+)+\.js$/g)
14+
const excludeFiles = new Set(['init-server.js.js', 'on-error-server.js.js'])
1315
files.forEach((file) => {
14-
if (!excludeFiles.includes(file) && chunkRegex.test(file)) {
16+
if (!excludeFiles.has(file) && chunkRegex.test(file)) {
1517
logTitle('💼 Copying dynamic import chunks to', functionPath)
1618
copySync(join(chunksPath, file), join(functionPath, file), {
1719
overwrite: false,

src/lib/helpers/getDataRouteForRoute.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { join } = require('path')
2+
23
const { readFileSync } = require('fs-extra')
4+
35
const getFilePathForRoute = require('./getFilePathForRoute')
46
const getNextDistDir = require('./getNextDistDir')
57

src/lib/helpers/getNetlifyRoutes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const getNetlifyRoutes = (nextRoute) => {
1313
// If the route is an optional catch-all route, we need to add a second
1414
// Netlify route for the base path (when no parameters are present).
1515
// The file ending must be present!
16-
if (nextRoute.match(OPTIONAL_CATCH_ALL_REGEX)) {
16+
if (OPTIONAL_CATCH_ALL_REGEX.test(nextRoute)) {
1717
let netlifyRoute = nextRoute.replace(OPTIONAL_CATCH_ALL_REGEX, '$2')
1818

1919
// When optional catch-all route is at top-level, the regex on line 19 will

src/lib/helpers/getNextDistDir.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Get the NextJS distDir specified in next.config.js
22
const { join } = require('path')
3+
34
const getNextConfig = require('../../../helpers/getNextConfig')
45

56
const getNextDistDir = async () => {

src/lib/helpers/getNextSrcDir.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const { join } = require('path')
22

3-
const getNextSrcDirs = () => {
4-
return ['pages', 'src', 'public', 'styles'].map((dir) => join('.', dir))
5-
}
3+
const getNextSrcDirs = () => ['pages', 'src', 'public', 'styles'].map((dir) => join('.', dir))
64

75
module.exports = getNextSrcDirs

src/lib/helpers/getPagesManifest.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { join } = require('path')
2+
23
const { existsSync, readJSONSync } = require('fs-extra')
4+
35
const getNextDistDir = require('./getNextDistDir')
46

57
const getPagesManifest = async () => {

src/lib/helpers/getPrerenderManifest.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
const { join } = require('path')
2+
23
const { readJSONSync } = require('fs-extra')
4+
35
const getNextConfig = require('../../../helpers/getNextConfig')
4-
const getNextDistDir = require('./getNextDistDir')
5-
const getDataRouteForRoute = require('./getDataRouteForRoute')
6+
67
const asyncForEach = require('./asyncForEach')
8+
const getDataRouteForRoute = require('./getDataRouteForRoute')
9+
const getNextDistDir = require('./getNextDistDir')
710

811
const transformManifestForI18n = async (manifest) => {
912
const { routes } = manifest
1013
const newRoutes = {}
11-
await asyncForEach(Object.entries(routes), async ([route, { dataRoute, srcRoute, ...params }]) => {
12-
const isDynamicRoute = !!srcRoute
14+
await asyncForEach(Object.entries(routes), async ([route, { srcRoute, ...params }]) => {
15+
const isDynamicRoute = Boolean(srcRoute)
1316
if (isDynamicRoute) {
1417
newRoutes[route] = routes[route]
1518
} else {
16-
const locale = route.split('/')[1]
19+
const [, locale] = route.split('/')
1720
const routeWithoutLocale = `/${route.split('/').slice(2, route.split('/').length).join('/')}`
1821
newRoutes[route] = {
1922
dataRoute: await getDataRouteForRoute(routeWithoutLocale, locale),

src/lib/helpers/getRoutesManifest.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { join } = require('path')
2+
23
const { readJSONSync } = require('fs-extra')
4+
35
const getNextDistDir = require('./getNextDistDir')
46

57
const getRoutesManifest = async () => {

0 commit comments

Comments
 (0)