Skip to content

Instantly share code, notes, and snippets.

View chengjianhua's full-sized avatar
💦

Jianhua Cheng chengjianhua

💦
View GitHub Profile
@chengjianhua
chengjianhua / utils.mjs
Created March 28, 2023 07:47
Rush Utils
import rushLib from "@microsoft/rush-lib";
import { StringBufferTerminalProvider } from "@rushstack/node-core-library";
import { Terminal } from "@rushstack/node-core-library";
export function getRushConfiguration() {
const config = rushLib.RushConfiguration.loadFromDefaultLocation();
return config;
}
/**
@chengjianhua
chengjianhua / generate-rush-projects-from-lerna.mjs
Last active March 22, 2023 09:38
Generate `projects` list for rush.json
#!/usr/bin/env zx
import "zx/globals";
const pkgsStr = await $`npx lerna list --json --loglevel silent`.quiet();
const pkgs = JSON.parse(pkgsStr);
const transformed = pkgs.map((p) => {
return {
packageName: p.name,
@chengjianhua
chengjianhua / deduplicate-gitignore.mjs
Created March 17, 2023 16:53
Deduplicate rules in .gitignore
#!/usr/bin/env zx
const gitignoreFile = argv._[0];
const content = await fs.readFile(gitignoreFile, { encoding: "utf-8" });
const rulesSet = new Set();
const lines = content.split(`\n`);
const newLines = lines.filter((l) => {
@chengjianhua
chengjianhua / server.js
Created July 29, 2021 06:40
[Close http server]
const http = require("http");
const { promisify } = require("util");
const sleep = promisify(setTimeout);
const server = http.createServer((req, res) => {});
server.listen(6000, async () => {
console.log("listening...");
await sleep(2000);
@chengjianhua
chengjianhua / anal-pkg.js
Created April 9, 2021 09:36
[Analyze prefixes of all dependencies]
const glob = require("glob");
const fs = require("fs-extra");
async function main() {
const pkgFilePaths = glob.sync("./packages/*/package.json");
console.log(pkgFilePaths);
const prefixes = new Set();
@chengjianhua
chengjianhua / test-captureStackTrace.js
Created April 6, 2021 12:50
[test Error.captureStackTrace()]
class CustomError extends Error {
constructor(foo = "bar", ...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params);
// Maintains proper stack trace for where our error was thrown (only available on V8)
// if (Error.captureStackTrace) {
// Error.captureStackTrace(this, CustomError);
// }
@chengjianhua
chengjianhua / openvpn@.service
Created April 5, 2021 17:03
[Linux Systemd Unit File Example] #linux #shell #systemd # service
# 本文件复制自部署机器上 openvpn 生成的 systemd service 文件,目录为
# /lib/systemd/system/openvpn@.service
#
# 其源代码也可以在 https://packages.ubuntu.com/en/xenial-updates/openvpn 中下载
# 源包获得
[Unit]
Description=OpenVPN connection to %i
PartOf=openvpn.service
ReloadPropagatedFrom=openvpn.service
Before=systemd-user-sessions.service
@chengjianhua
chengjianhua / execute-shell-in-jxa.js
Created February 18, 2021 17:35
Execute shell in javascript for automation (osascript) #osx #alfred
ObjC.import("stdlib");
function run(argv) {
const app = Application.currentApplication()
app.includeStandardAdditions = true
app.doShellScript(`open https://www.baidu.com`)
var query = argv[0];
return query;
@chengjianhua
chengjianhua / main.go
Created March 29, 2020 03:43
grpc gateway golang example #grpc #grpc-gateway #golang
package main
import (
"context" // Use "golang.org/x/net/context" for Golang version <= 1.6
"flag"
"net/http"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
#!/usr/bin/env bash
package=$1
if [[ -z "$package" ]]; then
echo "usage: $0 <package-name>"
exit 1
fi
package_split=(${package//\// })
package_name=${package_split[-1]}