Skip to content

Commit 663bc7e

Browse files
authored
chore!: use pure ES modules (#506)
1 parent a7fc556 commit 663bc7e

11 files changed

+42
-44
lines changed

.eslintrc.js renamed to .eslintrc.cjs

+6
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@ const { overrides } = require('@netlify/eslint-config-node')
22

33
module.exports = {
44
extends: '@netlify/eslint-config-node',
5+
parserOptions: {
6+
sourceType: 'module',
7+
},
8+
rules: {
9+
'import/extensions': [2, 'ignorePackages'],
10+
},
511
overrides: [...overrides],
612
}
File renamed without changes.

functions/npm-diff/compute.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { applyPatch } = require('diff')
1+
import { applyPatch } from 'diff'
22

3-
const { fetchText } = require('./fetch')
3+
import { fetchText } from './fetch.js'
44

55
// Retrieve list of plugins differences
6-
const computeDiffs = async function ({ baseSha, baseRepoUrl, diffUrl }) {
6+
export const computeDiffs = async function ({ baseSha, baseRepoUrl, diffUrl }) {
77
const [baseFile, diffText] = await Promise.all([getBaseFile({ baseSha, baseRepoUrl }), getDiffText(diffUrl)])
88

99
const basePlugins = JSON.parse(baseFile)
@@ -39,12 +39,12 @@ const getDiffedFile = (baseFile, diffText) => {
3939
const getDiffs = (basePlugins, headPlugins) =>
4040
headPlugins.map((headPlugin) => getDiff(basePlugins, headPlugin)).filter(Boolean)
4141

42-
const getDiff = function (basePlugins, { package, version, author }) {
43-
const basePlugin = basePlugins.find((plugin) => plugin.author === author && plugin.package === package)
42+
const getDiff = function (basePlugins, { package: packageName, version, author }) {
43+
const basePlugin = basePlugins.find((plugin) => plugin.author === author && plugin.package === packageName)
4444

4545
// New plugin
4646
if (basePlugin === undefined) {
47-
return { package, version, status: 'added' }
47+
return { package: packageName, version, status: 'added' }
4848
}
4949

5050
// Existing plugin, same version
@@ -53,8 +53,6 @@ const getDiff = function (basePlugins, { package, version, author }) {
5353
}
5454

5555
// Existing plugin, different version
56-
const url = `https://diff.intrinsic.com/${package}/${basePlugin.version}/${version}`
57-
return { package, version, url, status: 'updated' }
56+
const url = `https://diff.intrinsic.com/${packageName}/${basePlugin.version}/${version}`
57+
return { package: packageName, version, url, status: 'updated' }
5858
}
59-
60-
module.exports = { computeDiffs }

functions/npm-diff/fetch.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { env } = require('process')
1+
import { env } from 'process'
22

3-
const fetch = require('node-fetch')
3+
import fetch from 'node-fetch'
44

55
// Make an HTTP request with a JSON request/response
6-
const fetchJson = async (url, errorPrefix, { headers, body, ...options } = {}) => {
6+
export const fetchJson = async (url, errorPrefix, { headers, body, ...options } = {}) => {
77
const response = await fetch(url, {
88
...options,
99
body: JSON.stringify({ body }),
@@ -21,13 +21,11 @@ const fetchJson = async (url, errorPrefix, { headers, body, ...options } = {}) =
2121
}
2222

2323
// Make a regular HTTP request
24-
const fetchText = async (url, errorPrefix, options) => {
24+
export const fetchText = async (url, errorPrefix, options) => {
2525
const response = await fetch(url, options)
2626
const text = await response.text()
2727
if (!response.ok) {
2828
throw new Error(`${errorPrefix}: ${text}`)
2929
}
3030
return text
3131
}
32-
33-
module.exports = { fetchJson, fetchText }

functions/npm-diff/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const { computeDiffs } = require('./compute')
2-
const { getNewPluginsUrls } = require('./new_urls')
3-
const { upsertComment } = require('./upsert_comment')
4-
const { validatePayload } = require('./validate')
1+
import { computeDiffs } from './compute.js'
2+
import { getNewPluginsUrls } from './new_urls.js'
3+
import { upsertComment } from './upsert_comment.js'
4+
import { validatePayload } from './validate.js'
55

66
// Main function handler.
77
// Add/update a comment on each PR adding/updating a plugin showing the code
88
// difference of the npm package.
9-
const handler = async function (rawEvent) {
9+
export const handler = async function (rawEvent) {
1010
console.log(rawEvent)
1111

1212
try {
@@ -60,5 +60,3 @@ const performDiff = async function ({
6060
const diffUrls = await getNewPluginsUrls(diffs)
6161
await upsertComment(diffUrls, commentsUrl)
6262
}
63-
64-
module.exports = { handler }

functions/npm-diff/new_urls.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const pacote = require('pacote')
1+
import pacote from 'pacote'
22

33
// Add npm URLs to new plugins.
4-
const getNewPluginsUrls = (diffs) => Promise.all(diffs.map(getDiffNewPluginsUrls))
4+
export const getNewPluginsUrls = (diffs) => Promise.all(diffs.map(getDiffNewPluginsUrls))
55

66
const getDiffNewPluginsUrls = async function (diff) {
77
if (diff.status !== 'added') {
@@ -13,5 +13,3 @@ const getDiffNewPluginsUrls = async function (diff) {
1313
} = await pacote.manifest(`${diff.package}@${diff.version}`)
1414
return { ...diff, url: tarball }
1515
}
16-
17-
module.exports = { getNewPluginsUrls }

functions/npm-diff/upsert_comment.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { fetchJson } = require('./fetch')
1+
import { fetchJson } from './fetch.js'
22

33
// Add or update a comment on the GitHub PR displaying the diff
4-
const upsertComment = async (diffUrls, commentsUrl) => {
4+
export const upsertComment = async (diffUrls, commentsUrl) => {
55
const comment = getComment(diffUrls)
66
if (comment === '') {
77
return
@@ -52,5 +52,3 @@ const UPDATE_TYPES = [
5252
{ status: 'added', header: '#### Added Packages' },
5353
{ status: 'updated', header: '#### Updated Packages' },
5454
]
55-
56-
module.exports = { upsertComment }

functions/npm-diff/validate.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { createHmac } = require('crypto')
2-
const { env } = require('process')
1+
import { createHmac } from 'crypto'
2+
import { env } from 'process'
33

44
// Validate Function event payload
5-
const validatePayload = (rawEvent) => {
5+
export const validatePayload = (rawEvent) => {
66
const failedValidation = VALIDATIONS.find(({ test }) => test(rawEvent))
77
if (failedValidation === undefined) {
88
return
@@ -37,5 +37,3 @@ const VALIDATIONS = [
3737
]
3838

3939
const REQUIRED_HEADERS = ['x-hub-signature', 'x-github-event', 'x-github-delivery']
40-
41-
module.exports = { validatePayload }

index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
'use strict'
22

3-
const pluginsList = require('./site/plugins.json')
3+
import { readFileSync } from 'fs'
4+
import { fileURLToPath } from 'url'
45

5-
const pluginsUrl = 'https://list-v2--netlify-plugins.netlify.app/plugins.json'
6+
const PLUGINS_FILE = fileURLToPath(new URL('./site/plugins.json', import.meta.url))
67

7-
module.exports = { pluginsList, pluginsUrl }
8+
// TODO: replace with a JSON import once this is supported without any
9+
// experimental flag
10+
export const pluginsList = JSON.parse(readFileSync(PLUGINS_FILE))
11+
export const pluginsUrl = 'https://list-v2--netlify-plugins.netlify.app/plugins.json'

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"name": "@netlify/plugins-list",
33
"version": "5.0.0",
44
"description": "List of Netlify plugins",
5-
"main": "index.js",
5+
"type": "module",
6+
"exports": "./index.js",
7+
"main": "./index.js",
68
"files": [
79
"site/plugins.json"
810
],
@@ -21,8 +23,8 @@
2123
"test:ci": "nyc -r lcovonly -r text -r json ava"
2224
},
2325
"config": {
24-
"eslint": "--ignore-path .gitignore --cache --format=codeframe --max-warnings=0 \"{functions,test,.github}/**/*.{js,md,html}\" \"*.{js,md,html}\" \".*.{js,md,html}\"",
25-
"prettier": "--ignore-path .gitignore --loglevel=warn \"{functions,test,.github}/**/*.{js,md,yml,json,html}\" \"*.{js,yml,json,html}\" \".*.{js,yml,json,html}\" \"!package-lock.json\""
26+
"eslint": "--ignore-path .gitignore --cache --format=codeframe --max-warnings=0 \"{functions,test,.github}/**/*.{cjs,mjs,js,md,html}\" \"*.{cjs,mjs,js,md,html}\" \".*.{cjs,mjs,js,md,html}\"",
27+
"prettier": "--ignore-path .gitignore --loglevel=warn \"{functions,test,.github}/**/*.{cjs,mjs,js,md,yml,json,html}\" \"*.{cjs,mjs,js,yml,json,html}\" \".*.{cjs,mjs,js,yml,json,html}\" \"!package-lock.json\""
2628
},
2729
"husky": {
2830
"hooks": {

test/main.mjs renamed to test/main.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/* eslint-disable max-lines */
2-
import { URL } from 'url'
3-
42
import test from 'ava'
53
import got from 'got'
64
import isPlainObj from 'is-plain-obj'

0 commit comments

Comments
 (0)