Skip to content

Instantly share code, notes, and snippets.

@xiaoxiangmoe
Last active February 18, 2022 08:47
Show Gist options
  • Save xiaoxiangmoe/d99ed02c12456430474ce4e557be942f to your computer and use it in GitHub Desktop.
Save xiaoxiangmoe/d99ed02c12456430474ce4e557be942f to your computer and use it in GitHub Desktop.
.zsh_cn-nodejs-env-setup.sh
function cn-nodejs-env-setup() {
local script='
const https = require("https");
const { exit } = require("process");
const url = "https://raw.githubusercontent.com/cnpm/binary-mirror-config/master/package.json";
https
.get(url, (res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
let json = JSON.parse(body);
const envs = json.mirrors.china.ENVS;
const script = Object.entries(envs)
.map(([name, url]) => `export ${name}=${url};`)
.join("\n");
console.log(script);
// do something with JSON
} catch (error) {
console.error(error.message);
}
});
})
.on("error", (error) => {
console.error(error.message);
exit(1);
});
'
local result=$(node --eval=$script)
if [ "$?" -eq 0 ];then
echo $result
eval $result
fi
}
function cn-nodejs-env-unset() {
local script='
const https = require("https");
const { exit } = require("process");
const url = "https://raw.githubusercontent.com/cnpm/binary-mirror-config/master/package.json";
https
.get(url, (res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
let json = JSON.parse(body);
const envs = json.mirrors.china.ENVS;
const script = Object.entries(envs)
.map(([name, url]) => `unset ${name};`)
.join("\n");
console.log(script);
// do something with JSON
} catch (error) {
console.error(error.message);
}
});
})
.on("error", (error) => {
console.error(error.message);
exit(1);
});
'
local result=$(node --eval=$script)
if [ "$?" -eq 0 ];then
echo $result
eval $result
fi
}
@xiaoxiangmoe
Copy link
Author

xiaoxiangmoe commented Feb 18, 2022

copy this file to ~/.zsh_cn-nodejs-env-setup.sh

add source $HOME/.zsh_cn-nodejs-env-setup.sh in your .zshrc or .bashrc file.

run cn-nodejs-env-setup or cn-nodejs-env-unset when you need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment