Skip to content

Instantly share code, notes, and snippets.

Avatar
😃

David Wells DavidWells

😃
View GitHub Profile
View custom-oclif-help-class.js
const { Help } = require('@oclif/plugin-help')
const chalk = require('chalk')
// https://github.com/oclif/plugin-help/blob/master/src/index.ts
module.exports = class MyHelpClass extends Help {
constructor(config, opts) {
super(config, opts)
const commandSlice = process.argv
console.log('commandSlice', commandSlice)
// Show all commands at root help
@DavidWells
DavidWells / command-with-listr.js
Created Sep 28, 2020
Oclif command with listr
View command-with-listr.js
const execa = require('execa')
const Listr = require('listr')
const { Observable } = require('rxjs')
const { Command, flags } = require('@oclif/command')
const timeout = ms => new Promise(res => setTimeout(res, ms))
class AddCommand extends Command {
async run() {
@DavidWells
DavidWells / extend-segment-plugin.js
Last active Sep 25, 2020
Adding segment group functionality to analytics npm package. See the docs https://getanalytics.io/plugins/segment/#adding-group-functionality
View extend-segment-plugin.js
import Analytics from 'analytics'
import segmentPlugin from '@analytics/segment'
const originalSegmentInstance = segmentPlugin({
writeKey: '123-xyz'
})
// Extend originalSegmentInstance with custom methods
const enchancedSegmentInstance = Object.assign({}, originalSegmentInstance, {
methods: {
@DavidWells
DavidWells / serverless-function-reference-formats.yml
Created Sep 19, 2020
Various ways to reference function name and ARN in serverless.yml
View serverless-function-reference-formats.yml
function:
foo:
handler: index.handler
# Ways to reference function in serverless.yml
FunctionName: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${self:service.name}-${self:provider.stage}-foo
FunctionName: !GetAtt FooLambdaFunction.Arn
FunctionName: !Sub "${AWS::StackName}-foo"
FunctionName: ${self:service.name}-${self:provider.stage}-foo
@DavidWells
DavidWells / using-cloudformation-metadata.yml
Created Sep 14, 2020
Example of using Metadata 'AWS::CloudFormation::Interface' to group stack params in CF deploy UI
View using-cloudformation-metadata.yml
# via https://mechanicalrock.github.io/2020/03/17/cloudformation-tricks-part-1.html
Parameters:
Name:
Type: String
Description: >-
The name of the application.
This should include a stage qualifier if this is a nonproduction instance.
Stage qualifiers should only include 3-5 characters
AllowedPattern: ^[a-z]{3,}(-[a-z]{3,5})?$
MinLength: 3
View locked-down-serverless-framework-policy.json
// From https://serverless-stack.com/chapters/customize-the-serverless-iam-policy.html
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudformation:Describe*",
"cloudformation:List*",
"cloudformation:Get*",
@DavidWells
DavidWells / cognito-lite.js
Created Sep 3, 2020
Add cognito auth with amplify without entire framework
View cognito-lite.js
// via https://github.com/aws-amplify/amplify-js/discussions/5554
import {Auth} from "@aws-amplify/auth";
....
Auth.configure({
// REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab',
// REQUIRED - Amazon Cognito Region
region: 'XX-XXXX-X',
@DavidWells
DavidWells / parse-url-no-libs.js
Created Aug 23, 2020
Parse URL in browser / node with no dependancies
View parse-url-no-libs.js
function parseUrl(url) {
const match = url.match(/^(https?)?(?:[\:\/]*)([a-z0-9\.-]*)(?:\:([0-9]+))?(\/[^?#]*)?(?:\?([^#]*))?(?:#(.*))?$/i)
return {
protocol: match[1] || '',
host: match[2] || '',
port: match[3] || '',
path: match[4] || '',
query: match[5] || '',
fragment: match[6] || '',
}
@DavidWells
DavidWells / refresh-token.js
Created Aug 23, 2020 — forked from pfulop/refresh-token.js
how to refresh amplify token
View refresh-token.js
const refreshToken = async () => {
const refreshPromise = new Promise(async (resolve) => {
const user = await Auth.currentAuthenticatedUser();
const session = await Auth.currentSession();
user.refreshSession(session.refreshToken, async (res, newSession) => {
if (newSession) {
await Auth.currentUserCredentials();
}
resolve('ok');
@DavidWells
DavidWells / cypress-tests-on-netlify-url.json
Last active Aug 13, 2020
Run Cypress tests on live netlify URL
View cypress-tests-on-netlify-url.json
{
"scripts": {
"installCLI": "npm install netlify-cli",
"build": "react-scripts build && npm run deployTestSite",
"deployTestSite": "netlify deploy --json | jq {siteURL} | cypress test {siteURL}",
"test": "react-scripts test",
"deploy": "netlify deploy --dir build --prod"
},
}
You can’t perform that action at this time.