Skip to content

Commit efc1ef1

Browse files
committed
chore: add eslint, prettier, github action
1 parent d51c0c1 commit efc1ef1

File tree

7 files changed

+1726
-39
lines changed

7 files changed

+1726
-39
lines changed

.eslintrc.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
env: {
3+
commonjs: true,
4+
es6: true,
5+
node: true,
6+
},
7+
extends: 'eslint:recommended',
8+
globals: {
9+
Atomics: 'readonly',
10+
SharedArrayBuffer: 'readonly',
11+
},
12+
parserOptions: {
13+
ecmaVersion: 11,
14+
},
15+
};

.github/workflows/test.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Node CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
tags:
8+
- '*'
9+
10+
pull_request:
11+
types: [opened, synchronize, reopened]
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
strategy:
17+
matrix:
18+
node-version: [12.x]
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- name: Use Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v1
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
- name: Get yarn cache directory path
27+
id: yarn-cache-dir-path
28+
run: echo "::set-output name=dir::$(yarn cache dir)"
29+
- uses: actions/cache@v2
30+
with:
31+
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
32+
key: ${{ runner.os }}-yarn-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}
33+
restore-keys: |
34+
${{ runner.os }}-yarn-
35+
- name: log versions
36+
run: node --version && npm --version && yarn --version
37+
- name: install dependencies
38+
run: yarn --frozen-lockfile
39+
- name: run linter
40+
run: yarn lint
41+
- name: check formatting
42+
run: yarn format:ci

.prettierrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"printWidth": 80,
3+
"tabWidth": 2,
4+
"semi": true,
5+
"singleQuote": true,
6+
"trailingComma": "all",
7+
"useTabs": false,
8+
"overrides": [
9+
{
10+
"files": "*.json",
11+
"options": { "printWidth": 200 }
12+
}
13+
]
14+
}

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { extends: ['@commitlint/config-conventional'] };

package.json

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
22
"name": "@netlify/plugin-lighthouse",
33
"version": "1.0.0",
4-
"description": "",
5-
"main": "index.js",
4+
"description": "Netlify Plugin to run Lighthouse on each build",
5+
"main": "src/index.js",
66
"scripts": {
7-
"local": "node -e 'require(\"./index.js\").onSuccess()'",
8-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"local": "node -e 'require(\"./src/index.js\").onSuccess()'",
8+
"lint": "eslint 'src/**/*.js'",
9+
"format": "prettier --write 'src/**/*.js'",
10+
"format:ci": "prettier --check 'src/**/*.js'",
11+
"tag": "git tag \"v$npm_package_version\" && git push --tags",
12+
"postpublish": "npm run tag"
913
},
1014
"keywords": [
1115
"netlify",
@@ -23,5 +27,17 @@
2327
},
2428
"engines": {
2529
"node": ">=10.13"
30+
},
31+
"devDependencies": {
32+
"@commitlint/cli": "^8.1.0",
33+
"@commitlint/config-conventional": "^8.1.0",
34+
"eslint": "^7.0.0",
35+
"husky": "^4.0.7",
36+
"prettier": "^2.0.0"
37+
},
38+
"husky": {
39+
"hooks": {
40+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
41+
}
2642
}
2743
}

index.js renamed to src/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const httpServer = require("http-server");
2-
const puppeteer = require("puppeteer");
3-
const lighthouse = require("lighthouse");
4-
const chromeLauncher = require("chrome-launcher");
5-
require("dotenv").config();
1+
const httpServer = require('http-server');
2+
const puppeteer = require('puppeteer');
3+
const lighthouse = require('lighthouse');
4+
const chromeLauncher = require('chrome-launcher');
5+
require('dotenv').config();
66

77
const getServer = (url, serveDir) => {
88
if (url) {
@@ -18,13 +18,13 @@ const getServer = (url, serveDir) => {
1818
}
1919

2020
if (!serveDir) {
21-
throw new Error("Empty publish dir");
21+
throw new Error('Empty publish dir');
2222
}
2323

2424
console.log(`Serving and scanning site from directory '${serveDir}'`);
2525
const s = httpServer.createServer({ root: serveDir });
2626
const port = 5000;
27-
const host = "localhost";
27+
const host = 'localhost';
2828
const server = {
2929
listen: (func) => s.listen(port, host, func),
3030
close: () => s.close(),
@@ -35,7 +35,7 @@ const getServer = (url, serveDir) => {
3535
const belowThreshold = (id, expected, results) => {
3636
const category = results.find((c) => c.id === id);
3737
if (!category) {
38-
console.warn("Could not find category", id);
38+
console.warn('Could not find category', id);
3939
}
4040
const actual = category ? category.score : Number.MAX_SAFE_INTEGER;
4141
return actual < expected;
@@ -67,15 +67,15 @@ module.exports = {
6767
},
6868
};
6969

70-
if (typeof thresholds === "string") {
70+
if (typeof thresholds === 'string') {
7171
thresholds = JSON.parse(thresholds);
7272
}
7373

7474
const { server, url } = getServer(auditUrl, serveDir);
7575
const browserFetcher = puppeteer.createBrowserFetcher();
7676
const revisions = await browserFetcher.localRevisions();
7777
if (revisions.length <= 0) {
78-
throw new Error("Could not find local browser");
78+
throw new Error('Could not find local browser');
7979
}
8080
const info = await browserFetcher.revisionInfo(revisions[0]);
8181

@@ -85,7 +85,7 @@ module.exports = {
8585
try {
8686
chrome = await chromeLauncher.launch({
8787
chromePath: info.executablePath,
88-
chromeFlags: ["--headless", "--no-sandbox", "--disable-gpu"],
88+
chromeFlags: ['--headless', '--no-sandbox', '--disable-gpu'],
8989
});
9090
const results = await lighthouse(url, {
9191
port: chrome.port,
@@ -108,7 +108,7 @@ module.exports = {
108108
throw error;
109109
} else {
110110
const categories = Object.values(
111-
results.lhr.categories
111+
results.lhr.categories,
112112
).map(({ title, score, id }) => ({ title, score, id }));
113113

114114
const errors = Object.entries(thresholds)
@@ -122,7 +122,7 @@ module.exports = {
122122
});
123123

124124
if (errors.length > 0) {
125-
throw new Error(errors.join("\n"));
125+
throw new Error(errors.join('\n'));
126126
}
127127
}
128128
} catch (error) {

0 commit comments

Comments
 (0)