Skip to content
This repository has been archived by the owner on Feb 3, 2025. It is now read-only.

Commit

Permalink
feat: prettier and eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
irfan-maulana-tkp committed Feb 18, 2020
1 parent 95cc25e commit 5841548
Show file tree
Hide file tree
Showing 13 changed files with 2,552 additions and 203 deletions.
19 changes: 19 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
extends: [
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
'plugin:prettier/recommended', // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
],
parserOptions: {
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
sourceType: 'module', // Allows for the use of imports
},
rules: {
'@typescript-eslint/ban-ts-ignore': 'warn',
'@typescript-eslint/camelcase': 'warn',
'@typescript-eslint/no-var-requires': 'warn'
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
},
};
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
trailingComma: 'all',
singleQuote: true,
semi: true,
bracketSpacing: true,
printWidth: 120,
};
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Web Perf Comparison for E-Commerce in Indonesia",
"main": "index.js",
"scripts": {
"lint": "eslint ./src --ext .ts -c .eslintrc.js",
"test": "echo \"Error: no test specified\" && exit 1",
"test:gist": "ts-node ./src/server/test-gist.ts",
"test:lh": "ts-node ./src/server/test-lh.ts",
Expand All @@ -28,8 +29,31 @@
},
"devDependencies": {
"@types/node-fetch": "2.5.4",
"@typescript-eslint/eslint-plugin": "^2.20.0",
"@typescript-eslint/parser": "^2.20.0",
"chalk": "3.0.0",
"cli-table3": "0.5.1",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
"husky": "^4.2.3",
"lint-staged": "^10.0.7",
"prettier": "^1.19.1",
"ts-node-dev": "1.0.0-pre.44"
},
"lint-staged": {
"*.ts": [
"yarn lint --fix",
"git add"
],
"*.{json,md,yml}": [
"prettier --config .prettierrc.js --write",
"git add"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
36 changes: 18 additions & 18 deletions src/server/ecommerce.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import { EcommerceItem } from './types';

const data: EcommerceItem[] = [
{
name: 'Tokopedia',
urlDesktop: 'https://www.tokopedia.com/',
urlMobile: 'https://m.tokopedia.com/',
{
name: 'Tokopedia',
urlDesktop: 'https://www.tokopedia.com/',
urlMobile: 'https://m.tokopedia.com/',
logo: 'https://ecs7.tokopedia.net/assets-tokopoints/prod/images/2018/08/logo%402x.png',
},
},
{
name: 'Shopee',
urlDesktop: 'https://shopee.co.id/',
urlMobile: 'https://shopee.co.id/?d=mobile',
urlDesktop: 'https://shopee.co.id/',
urlMobile: 'https://shopee.co.id/?d=mobile',
logo: 'https://deo.shopeemobile.com/shopee/shopee-pcmall-live-sg/assets/ca5d12864c12916c05640b36e47ac5c9.png',
},
{
name: 'Bukalapak',
urlDesktop: 'https://www.bukalapak.com/',
urlMobile: 'https://m.bukalapak.com/',
urlDesktop: 'https://www.bukalapak.com/',
urlMobile: 'https://m.bukalapak.com/',
logo: 'https://s2.bukalapak.com/images/logo/single_320x51_12_red_03052016.png',
},
{
name: 'Lazada',
urlDesktop: 'https://www.lazada.co.id/',
urlMobile: 'https://www.lazada.co.id/?d=mobile',
urlDesktop: 'https://www.lazada.co.id/',
urlMobile: 'https://www.lazada.co.id/?d=mobile',
logo: 'https://laz-img-cdn.alicdn.com/images/ims-web/TB1Hs8GaMFY.1VjSZFnXXcFHXXa.png',
},
{
name: 'Blibli',
urlDesktop: 'https://www.blibli.com/',
urlMobile: 'https://www.blibli.com/?d=mobile',
urlDesktop: 'https://www.blibli.com/',
urlMobile: 'https://www.blibli.com/?d=mobile',
logo: 'https://upload.wikimedia.org/wikipedia/id/0/06/Blibli-logo.png',
},
{
name: 'JD',
urlDesktop: 'https://www.jd.id/',
urlMobile: 'https://m.jd.id/',
urlDesktop: 'https://www.jd.id/',
urlMobile: 'https://m.jd.id/',
logo: 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTf8H6_qPS6d2dqXq81J2Zzjfa3yv7UvFGJN78FxaIP3v-YyS8T',
},
{
name: 'Blanja',
urlDesktop: 'https://www.blanja.com/',
urlMobile: 'https://m.blanja.com/',
urlDesktop: 'https://www.blanja.com/',
urlMobile: 'https://m.blanja.com/',
logo: 'https://s2.blanja.com/static/public/prime/img/common/[email protected]',
},
]
];

export default data;
24 changes: 12 additions & 12 deletions src/server/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import fs from 'fs';
import path from 'path';
import appRootDir from 'app-root-dir';

export const writeFile = (filename, content) => {
return new Promise(function (resolve, reject) {
fs.writeFile(filename, content, 'utf-8', function (err) {
if (err) reject(err);
else resolve(content);
});
});
}
export const writeFile = (filename, content): Promise<any> => {
return new Promise(function(resolve, reject) {
fs.writeFile(filename, content, 'utf-8', function(err) {
if (err) reject(err);
else resolve(content);
});
});
};

export const writeNewReport = (content: any) => {
const reportFileDir = path.join(path.resolve(appRootDir.get()), `/reports/output.json`);
writeFile(reportFileDir, JSON.stringify(content));
}
export const writeNewReport = (content: any): void => {
const reportFileDir = path.join(path.resolve(appRootDir.get()), `/reports/output.json`);
writeFile(reportFileDir, JSON.stringify(content));
};
63 changes: 31 additions & 32 deletions src/server/gists.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { modifyLatestData } from './utils';
import { writeNewReport } from './file';

const { Octokit } = require("@octokit/rest");
const { Octokit } = require('@octokit/rest');

const octokit = new Octokit({
auth: process.env.GIST_TOKEN
auth: process.env.GIST_TOKEN,
});

export const updateGist = async (name: string, device: string, response: any) => {
const lastData = await octokit.gists.get({
gist_id: process.env.GIST_ID,
});
const FILENAME = 'webperf-ecommerce-id.json';

if (lastData) {
console.log(`> [GIST] - get existing data from ${FILENAME}...\n`);
}

try {
// @ts-ignore
const objectData = JSON.parse(lastData.data.files[FILENAME].content);
const newValue = modifyLatestData(objectData, response, name, device);

writeNewReport(newValue);

await octokit.gists.update({
gist_id: process.env.GIST_ID,
files: {
[FILENAME]: {
content: JSON.stringify(newValue),
filename: FILENAME
},
}
})

} catch (e) {
console.error('> Error gist', e);
}
}
const lastData = await octokit.gists.get({
gist_id: process.env.GIST_ID,
});
const FILENAME = 'webperf-ecommerce-id.json';

if (lastData) {
console.log(`> [GIST] - get existing data from ${FILENAME}...\n`);
}

try {
// @ts-ignore
const objectData = JSON.parse(lastData.data.files[FILENAME].content);
const newValue = modifyLatestData(objectData, response, name, device);

writeNewReport(newValue);

await octokit.gists.update({
gist_id: process.env.GIST_ID,
files: {
[FILENAME]: {
content: JSON.stringify(newValue),
filename: FILENAME,
},
},
});
} catch (e) {
console.error('> Error gist', e);
}
};
88 changes: 44 additions & 44 deletions src/server/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,57 @@ import { median } from './utils';
const NUMBER_OF_RUN = 5;

const run = async (name: string, url: string, device: string): Promise<any | null> => {
let results: any[] = [];
const tableLog = new Table({
head: ['Perf', 'TTFB', 'FCP', 'TTI']
});
const results: any[] = [];
const tableLog = new Table({
head: ['Perf', 'TTFB', 'FCP', 'TTI'],
});

for(let i = 0; i < NUMBER_OF_RUN; i++) {
const response = await runLH(name, url, device);
if (response) {
results.push(response);
// @ts-ignore
tableLog.push([response.perf, response.ttfb, response.fcp, response.tti]);
}
}
for (let i = 0; i < NUMBER_OF_RUN; i++) {
const response = await runLH(name, url, device);
if (response) {
results.push(response);
// @ts-ignore
tableLog.push([response.perf, response.ttfb, response.fcp, response.tti]);
}
}

console.log(`\nPerformance Result for ${name} - ${device}`);
console.log(tableLog.toString());
console.log(`\n`);
console.log(`\nPerformance Result for ${name} - ${device}`);
console.log(tableLog.toString());
console.log(`\n`);

const report = median(results, 'perf');
updateGist(name, device, report);
}
const report = median(results, 'perf');
updateGist(name, device, report);
};

const readData = () => {
let isSecretNotFound = false;
if (process.env.LIGHTHOUSE_API_KEY) {
console.log(`> Found env LIGHTHOUSE_API_KEY`);
} else {
isSecretNotFound = true;
console.error(`> env LIGHTHOUSE_API_KEY not found`);
}
let isSecretNotFound = false;
if (process.env.LIGHTHOUSE_API_KEY) {
console.log(`> Found env LIGHTHOUSE_API_KEY`);
} else {
isSecretNotFound = true;
console.error(`> env LIGHTHOUSE_API_KEY not found`);
}

if (process.env.GIST_TOKEN) {
console.log(`> Found env GIST_TOKEN`);
} else {
isSecretNotFound = true;
console.error(`> env GIST_TOKEN not found`);
}
if (process.env.GIST_TOKEN) {
console.log(`> Found env GIST_TOKEN`);
} else {
isSecretNotFound = true;
console.error(`> env GIST_TOKEN not found`);
}

if (process.env.GIST_ID) {
console.log(`> Found env GIST_ID`);
} else {
isSecretNotFound = true;
console.error(`> env GIST_ID not found`);
}
if (process.env.GIST_ID) {
console.log(`> Found env GIST_ID`);
} else {
isSecretNotFound = true;
console.error(`> env GIST_ID not found`);
}

if (!isSecretNotFound) {
data.map(async (item: EcommerceItem) => {
await run(item.name, item.urlMobile, 'mobile');
await run(item.name, item.urlDesktop, 'desktop');
})
}
}
if (!isSecretNotFound) {
data.map(async (item: EcommerceItem) => {
await run(item.name, item.urlMobile, 'mobile');
await run(item.name, item.urlDesktop, 'desktop');
});
}
};

readData();
Loading

0 comments on commit 5841548

Please sign in to comment.