Skip to content

Commit

Permalink
prepare to auto-deploy minified userscripts to gists
Browse files Browse the repository at this point in the history
  • Loading branch information
joshparkerj committed Apr 20, 2022
1 parent 5c86213 commit ad2587e
Show file tree
Hide file tree
Showing 16 changed files with 760 additions and 5 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
- name: Codecov
uses: codecov/codecov-action@v2.1.0
analyze:
name: Analyze
needs: build
runs-on: ubuntu-latest
permissions:
Expand All @@ -49,3 +48,8 @@ jobs:
uses: github/codeql-action/autobuild@v1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
deploy:
needs: analyze
runs-on: ubuntu-latest
steps:
- run: npm run deploy
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ nerdwallet/*
wikipedia/*.css
dist/
coverage/
deploy
44 changes: 44 additions & 0 deletions bitbucket/execution-times.user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// ==UserScript==
// @name Get Test Execution Times
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Make a table of test execution times
// @author Josh Parker
// @match https://bitbucket-pipelines.prod.public.atl-paas.net/Pipelines/home
// @icon https://www.google.com/s2/favicons?sz=64&domain=bitbucket.org
// @grant none
// ==/UserScript==

(function testExecutionTimes() {
const parser = new DOMParser();

const nextSelector = 'button[aria-label=Next]';

const pages = [document];
let { href } = window.location;

const getNext = function getNext(next) {
return new Promise((resolve, reject) => {
if (!next || next.disabled) {
resolve();
}

const match = href.match(/(.*)\/(\d+)$/);

// eslint-disable-next-line no-unused-vars
const [_, url, page] = match;
href = `${url}${Number(page) + 1}`;
fetch(href)
.then((r) => r.text())
.then((text) => parser.parseFromString(text, 'text/html'))
.then((dom) => {
pages.push(dom);
resolve(dom.querySelector(nextSelector));
})
.catch(() => reject());
})
.then((n) => n && getNext(n));
};

getNext(document.querySelector(nextSelector));
}());
9 changes: 8 additions & 1 deletion bundle-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ glob(buildPattern)
const entryFile = files[i];
const outputFile = entryFile.replace('user.js', 'bundle.user.js');
const distFile = `./dist${outputFile.slice(1)}`;
const metaFile = distFile.replace('user.js', 'meta.js');
console.log(entryFile);
console.log(outputFile);
console.log(distFile);
Expand Down Expand Up @@ -57,7 +58,13 @@ glob(buildPattern)
if (writeDistFileErr) {
console.error(writeDistFileErr);
} else {
bundle(i + 1);
writeFile(metaFile, userscriptHeader, (writeMetaFileErr) => {
if (writeMetaFileErr) {
console.error(writeMetaFileErr);
} else {
bundle(i + 1);
}
});
}
});
}
Expand Down
51 changes: 51 additions & 0 deletions bundle-deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { Octokit } = require('@octokit/core');
const { createActionAuth } = require('@octokit/auth-action');
const globber = require('glob');
const { readFile } = require('fs');

const pattern = './@(dist)/**/*.@(user|meta).js';

const glob = (globPattern) => (new Promise((r) => { globber(globPattern, (_, s) => r(s)); }));

const octokit = new Octokit({
authStrategy: createActionAuth,
});

glob(pattern)
.then((files) => {
const upload = function upload(i = 0) {
if (i < files.length) {
const file = files[i];
readFile(file, (err, contentBuffer) => {
if (err) {
console.error(err);
} else {
const content = contentBuffer.toString();
const gistId = content.match(/downloadURL.*joshparkerj\/([^/]*)/)[1];
const description = content.match(/description\s+(.*)/)[1];
const filename = file.includes('meta') ? content.match(/updateURL.*raw\/[^/]+\/([^/]*)/)[1] : content.match(/downloadURL.*raw\/[^/]+\/([^/]*)/)[1];

console.log(content);
console.log(gistId);
console.log(description);
console.log(filename);
octokit.request(`PATCH /gists/${gistId}`, {
gist_id: gistId,
description,
files: {
[filename]: {
filename,
content,
},
},
})
.then(() => {
upload(i + 1);
});
}
});
}
};

upload();
});
3 changes: 3 additions & 0 deletions codecademy/posers-solved-with-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// Each of the 3 players loses 1 game and at the conclusion of the 3 games each man has $16.
// How much money did each man start with?

// seems like a bit of a nonsense game.
// What if a player doesn't have enough to double the others' money?

const reverseSimGame = (loserOfEachRound, finalDistributionOfCash) => (
loserOfEachRound

Expand Down
149 changes: 149 additions & 0 deletions elevator-saga/challenge-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/* eslint-disable no-param-reassign */
const challenge = {
init: (elevators, floors) => {
const elevatorQueue = [];

const setIndicator = {
up: (e) => { e.goingUpIndicator(true); e.goingDownIndicator(false); },
down: (e) => { e.goingUpIndicator(false); e.goingDownIndicator(true); },
};

const floorButton = (floorNum) => {
const floor = floors.find(({ level }) => level === floorNum);
const { buttonStates: { up, down } } = floor;
if (up.length) {
return 'up';
}

if (down.length) {
return 'down';
}

return null;
};

const go = (elevator, n) => {
const currentFloor = elevator.currentFloor();
const button = floorButton(n);
if (button) setIndicator[button](elevator);
else if (n < currentFloor) setIndicator.down(elevator);
else if (n > currentFloor) setIndicator.up(elevator);

elevator.goToFloor(n);

elevator.destinationQueue = [...(new Set(elevator.destinationQueue))]
.sort((a, b) => (elevator.goingUpIndicator() ? a - b : b - a));

elevator.checkDestinationQueue();
};

const isFull = (e) => Math.round(e.maxPassengerCount() * (1 - e.loadFactor())) <= 1;

const handleFull = (e) => {
if (isFull(e)) {
e.destinationQueue = [];
console.log(e.getPressedFloors());
e.getPressedFloors().forEach((n) => go(e, n));

return true;
}

return false;
};

const serve = (elevator, dir, comp, sorter) => {
const filteredFloors = floors
.filter(({ level }) => (
comp(level, elevator.currentFloor())
&& (floorButton(level) === dir)))
.sort(sorter);

if (filteredFloors.length) {
filteredFloors.forEach(({ level }) => go(elevator, level));

return true;
}

return false;
};

const nextPickup = (e) => {
const floorNum = e.currentFloor();
const dist = (n) => Math.abs(floorNum - n);
const filteredFloors = floors
.filter(({ level }) => floorButton(level))
.sort(({ level: a }, { level: b }) => dist(a) - dist(b));

if (filteredFloors.length === 0) {
return () => false;
}

const { level } = filteredFloors[0];
const button = floorButton(level);

if (level > floorNum) {
return (elevator) => serve(
elevator,
button,
(l, f) => l >= f,
({ level: a }, { level: b }) => (button === 'up' ? a - b : b - a),
);
}

return (elevator) => serve(
elevator,
button,
(l, f) => l <= f,
({ level: a }, { level: b }) => (button === 'up' ? a - b : b - a),
);
};

if (elevators.length > 1) {
elevators.forEach((elevator, i) => {
const maxFloor = Math.max(...floors.map(({ level }) => level));
elevator.goToFloor(Math.round(maxFloor * (i / (elevators.length - 1))));
});
}

elevators.forEach((elevator) => {
elevator.on('idle', () => {
if (!nextPickup(elevator)(elevator)) {
elevatorQueue.push(elevator);
}
});

elevator.on('floor_button_pressed', (floorNum) => {
if (!handleFull(elevator)) {
go(elevator, floorNum);
}
});

elevator.on('passing_floor', (floorNum, dir) => {
if (
(!handleFull(elevator))
&& (floors.find(({ level }) => level === floorNum).buttonStates[dir].length)
) {
go(elevator, floorNum);
}
});

elevator.on('stopped_at_floor', () => {
handleFull(elevator);
});
});

const handleButtonPressed = (n) => {
if (elevatorQueue.length > 0) {
go(elevatorQueue.shift(), n);
}
};

floors.forEach((floor) => {
floor.on('up_button_pressed', () => handleButtonPressed(floor.floorNum()));
floor.on('down_button_pressed', () => handleButtonPressed(floor.floorNum()));
});
},
update: () => { },
};

module.exports = challenge;
30 changes: 30 additions & 0 deletions elevator-saga/js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable max-len */
/* eslint-disable prefer-rest-params */
const func = function func() {
const callback = arguments[arguments.length - 1];
fetch('/api/v1/view?viewType=PRINCIPAL')
.then((r) => r.json())
.then(({
user: {
person: {
givenName, familyName, homeAddress, mobilePhone, birthDate,
},
},
}) => {
const givenNameMatch = givenName === arguments[0];
const familyNameMatch = familyName === arguments[1];
const addressLine1Match = homeAddress?.streetAddressLine1 === arguments[2];
const addressLine2Match = homeAddress?.streetAddressLine2 === arguments[3];
const cityMatch = homeAddress?.citySuburb === arguments[4];
const stateMatch = homeAddress?.stateProvinceRegion === arguments[5];
const phoneMatch = mobilePhone.value.replace(/\D+/g, '') === arguments[6];
const zipMatch = homeAddress.postalCode.startsWith(arguments[7]);
const birthDateMatch = birthDate === arguments[8];
callback(
givenNameMatch && familyNameMatch && addressLine1Match && addressLine2Match && cityMatch && stateMatch && phoneMatch && zipMatch && birthDateMatch,
);
})
.catch((err) => { console.error(err); callback(null); });
};

module.exports = func;
Loading

0 comments on commit ad2587e

Please sign in to comment.