Skip to content

Instantly share code, notes, and snippets.

View fangbinwei's full-sized avatar
🎯
Focusing

Binwei Fang fangbinwei

🎯
Focusing
View GitHub Profile
@fangbinwei
fangbinwei / api.js
Created April 12, 2021 03:41
axios cancel request vue-router
import axios from 'axios'
/** @type {import('axios').CancelTokenSource) | null } */
export let cancelTokenSource = null
export function setCancelTokenSource (source) {
cancelTokenSource = source
}
axios.interceptors.request.use(function (config) {
// Do something before request is sent
config.cancelToken = cancelTokenSource ? cancelTokenSource.token : null
@fangbinwei
fangbinwei / main.js
Created October 25, 2020 08:06
test registry
// yarn add request
const request = {
get (uri, opts) {
// lazy require
const request = require('util').promisify(require('request'))
const reqOpts = {
method: 'GET',
timeout: 30000,
resolveWithFullResponse: true,
json: true,
@fangbinwei
fangbinwei / test.ts
Last active July 25, 2020 12:58
typescript type from values in array
export const extraKeys = [
'app',
'title',
'package',
'deeplink',
'url',
'logo',
'image',
'type'
] as const
@fangbinwei
fangbinwei / 📊 Weekly development breakdown
Last active July 5, 2024 16:01
📊 Weekly development breakdown
TypeScript 13 hrs 44 mins ███████████████▉░░░░░ 75.9%
JavaScript 2 hrs 11 mins ██▌░░░░░░░░░░░░░░░░░░ 12.1%
JSON 1 hr 48 mins ██░░░░░░░░░░░░░░░░░░░ 9.9%
EJS 7 mins ▏░░░░░░░░░░░░░░░░░░░░ 0.7%
Image (svg) 5 mins ░░░░░░░░░░░░░░░░░░░░░ 0.5%
@fangbinwei
fangbinwei / main.js
Last active May 18, 2020 03:51
get clipboard image on windows
const execa = require('execa')
const path = require('path')
const fs = require('fs')
async function main() {
const scriptPath = path.join(__dirname, 'pc.ps1');
const imagePath = path.resolve(__dirname, 'clipboard.png')
try {
@fangbinwei
fangbinwei / gitRecord.js
Created March 17, 2020 09:03
record latest git info to file
const fs = require('fs')
const path = require('path')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const mkdirp = require('mkdirp')
const outputPath = 'dist/static'
async function main() {
try {
@fangbinwei
fangbinwei / runNpmIfPackageUpdated.js
Created March 17, 2020 09:00
run 'npm install' if package.json was modified
const util = require('util')
const childProcess = require('child_process')
const exec = util.promisify(childProcess.exec)
async function main() {
try {
const { stdout: changeFiles } = await exec(
'git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD'
)
if (changeFiles.includes('package.json')) {
@fangbinwei
fangbinwei / pre-commit.js
Created October 1, 2019 13:08
git pre-commit hooks for detecting 'console.log'
const shell = require('shelljs')
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git');
shell.exit(1);
}
shell.exec('git diff --cached',{silent: true}, async (code, stdout, stderr) => {
if (code !== 0) return
if (!stdout.match(/console\.log/)) return