Skip to content

Instantly share code, notes, and snippets.

View amitsingh-007's full-sized avatar
:octocat:
Working from home

Amit Singh amitsingh-007

:octocat:
Working from home
View GitHub Profile
@amitsingh-007
amitsingh-007 / getBaseBranchName.js
Created July 4, 2020 06:34
Get base branch name of current pull request raised
/*
* Only works if github cli is installed and logged in
*/
function getPRDetails() {
exec("gh pr view").then(
stdout => {
const response = stdout.toString();
const prDetails = response.match(/into (\S+) from (\S+)/i);
const [, baseBranch, targetBranch] = prDetails;
console.log("Base branch: ", baseBranch);
@amitsingh-007
amitsingh-007 / fetchBaseBranch.js
Created July 4, 2020 06:45
Fetching base branch on CI as well as on local machine
/*
* Need to add refs and then fetch, as Jenkins does not allow to fetch other barnches
*/
async function fetchBaseBranch(baseBranch) {
try {
console.log("Fetching ${baseBranch} from remote");
const addBaseRefs = `git config --add remote.origin.fetch +refs/heads/${baseBranch}:refs/remotes/origin/${baseBranch}`;
const fetchBase = `git fetch --no-tags origin +refs/heads/${baseBranch}:refs/remotes/origin/${baseBranch}`;
const command = `${addBaseRefs} && ${fetchBase}`;
await exec(command);
@amitsingh-007
amitsingh-007 / getCommand.js
Created July 4, 2020 06:56
Construct the command to be executed
const minimist = require("minimist");
const chalk = require("chalk");
const log = console.log;
const error = chalk.bold.red;
const debug = chalk.bold.green;
const info = chalk.rgb(215, 135, 95);
/* argv has all the user arguements
* Visit https://github.com/substack/minimist
@amitsingh-007
amitsingh-007 / execute.js
Created July 4, 2020 07:03
Execute command to run jest command
/*
* Use spawn instead of exec for large ouput buffer
* https://www.hacksparrow.com/nodejs/difference-between-spawn-and-exec-of-node-js-child-rocess.html
*/
const liveOuput = (command, args) => {
const childProcess = spawn(command, args, { shell: true });
childProcess.stdout.on("data", data => {
console.log(data.toString());
});
@amitsingh-007
amitsingh-007 / babel.legacy.config.js
Last active September 15, 2020 17:54
Legacy babel config
const commonPlugins = require("./commonPlugins");
module.exports = api => {
api && api.cache(false);
const presets = [
[
"@babel/preset-env",
{
targets: { browsers: ["safari >= 7"] }, //To support old browsers
useBuiltIns: "usage",
corejs: 2,
@amitsingh-007
amitsingh-007 / babel.modern.config.js
Last active September 15, 2020 17:59
ES6 supporting browser babel config
const commonPlugins = require("./commonPlugins");
module.exports = api => {
api && api.cache(false);
const presets = [
[
"@babel/preset-env",
{
targets: { esmodules: true }, //For ES6 supporting browsers
useBuiltIns: "usage",
corejs: 2,
@amitsingh-007
amitsingh-007 / legacyWebpack.config.js
Last active September 27, 2020 14:51
Legacy Webpack config for ES5 code
const path = require("path");
const legacyBabelConfig = require("babel.legacy.config.js");
module.exports = {
context: path.resolve(__dirname, "src"),
resolve: {
extensions: [".js", ".jsx"],
alias: {...},
modules: [
path.resolve(__dirname, "src"),
@amitsingh-007
amitsingh-007 / modernWebpack.config.js
Last active September 27, 2020 14:50
Modern webpack config for ES6+ code
const path = require("path");
const modernBabelConfig = require("babel.modern.config.js");
module.exports = {
context: path.resolve(__dirname, "src"),
resolve: {
extensions: [".js", ".jsx"],
alias: {...},
modules: [
path.resolve(__dirname, "src"),
@amitsingh-007
amitsingh-007 / BrowserDetection.cs
Last active September 27, 2020 15:53
Detect if request is from modern browser or not
namespace PWAUtils
{
public static class BrowserDetection
{
static readonly Dictionary<string, string> browserMapping = new Dictionary<string, string>
{
//For mobile devices
{ "Chrome Mobile", "71" },
{ "Firefox Mobile", "64" },
{ "Samsung Browser", "8.2" },
@amitsingh-007
amitsingh-007 / LighthouseAggregator.js
Last active October 20, 2020 13:22
Get mean data from multiple lighthouse reports
const glob = require("glob");
const path = require("path");
const configs = [];
glob.sync("./lh-reports/*.json").forEach(file => {
configs.push(require(path.resolve(file)));
});
const stats = {
FCP: { value: 0, unit: "s" },