Skip to content

Instantly share code, notes, and snippets.

View waldekmastykarz's full-sized avatar
🥑
Microsoft 365

Waldek Mastykarz waldekmastykarz

🥑
Microsoft 365
View GitHub Profile
@waldekmastykarz
waldekmastykarz / .zshrc
Created June 23, 2021 11:34
Calculator in zsh
# usage:
# $ = "1+2"
# 3
# quotes are necessary only for some expressions like *, but better to use them all the time just to be safe
function = {
echo "$1" | bc -l
}
@waldekmastykarz
waldekmastykarz / .zshrc
Last active June 24, 2021 12:05
Celsius to Fahrenheit and vice versa in zsh
# usage
# $ deg 10c # Celsius to Fahrenheit
# 50.0
#
# $ deg 115f # Fahrenheit to Celsius
# 46
deg() {
unit=${1:(-1)}
d=${1:0:(-1)}
if [ $unit = c ]
@waldekmastykarz
waldekmastykarz / remove-apipermissions.mjs
Created June 1, 2021 12:02
Removes granted SharePoint API permissions
#!/usr/bin/env zx
$.verbose = false;
console.log('Retrieving granted API permissions...');
const apiPermissions = JSON.parse(await $`m365 spo sp grant list -o json`);
for (let i = 0; i < apiPermissions.length; i++) {
const permission = apiPermissions[i];
console.log(`Removing permission ${permission.Resource}/${permission.Scope} (${permission.ObjectId})...`);
try {
@waldekmastykarz
waldekmastykarz / remove-permissionrequests.mjs
Last active June 1, 2021 12:01
Removes all pending SharePoint API permission requests
#!/usr/bin/env zx
$.verbose = false;
console.log('Retrieving permission requests...');
const permissionRequests = JSON.parse(await $`m365 spo sp permissionrequest list -o json`);
for (let i = 0; i < permissionRequests.length; i++) {
const request = permissionRequests[i];
console.log(`Removing request ${request.Resource}/${request.Scope} (${request.Id})...`);
try {
@waldekmastykarz
waldekmastykarz / cwebp-images.mjs
Created May 24, 2021 14:21
Convert images modified in the last hour to cwebp
#!/usr/bin/env zx
const fs = require('fs'),
path = require('path');
const nonWebPFiles = fs
.readdirSync(process.cwd())
.filter(file => !file.endsWith('.webp'));
const lastHour = new Date();
lastHour.setHours(lastHour.getHours() - 1);
@waldekmastykarz
waldekmastykarz / CacheMiddleware.js
Created April 8, 2021 09:34
Sample cache middleware for Microsoft Graph JS SDK
export function CacheMiddleware(expirationConfig) {
this.nextMiddleware = undefined;
this.expirationConfig = expirationConfig;
const getHeaders = (headers) => {
const h = {};
for (var header of headers.entries()) {
h[header[0]] = header[1];
}
return h;
@waldekmastykarz
waldekmastykarz / profile.sh
Created January 6, 2021 18:10
Profile node application
# https://nodejs.org/en/docs/guides/simple-profiling/
NODE_ENV=production node --prof app.js
node --prof-process isolate-0xnnnnnnnnnnnn-v8.log > processed.txt
@waldekmastykarz
waldekmastykarz / convert-imagetowebp.ps1
Created December 21, 2020 14:33
Convert multiple images to .webp
$now = Get-Date
Get-ChildItem | ? {
($now - $_.LastWriteTime).totalhours -le 1
} | % {
cwebp $_.Name -o $($_.BaseName+".webp")
}
@waldekmastykarz
waldekmastykarz / index.js
Created September 2, 2020 17:44
Reformat docs
const fs = require('fs');
const os = require('os');
const path = require('path');
function readdirR(dir) {
return fs.statSync(dir).isDirectory()
? Array.prototype.concat(...fs.readdirSync(dir).map(f => readdirR(path.join(dir, f))))
: dir;
};
@waldekmastykarz
waldekmastykarz / index.js
Created September 2, 2020 17:43
Remove method from a class in a .ts file
const ts = require('typescript');
const path = require('path');
const fs = require('fs');
function getAsEnumerable(file, node) {
const nodes = [node];
node.getChildren(file).forEach(n => {
nodes.push(...getAsEnumerable(file, n));
});