Last active
February 18, 2022 08:47
-
-
Save xiaoxiangmoe/d99ed02c12456430474ce4e557be942f to your computer and use it in GitHub Desktop.
.zsh_cn-nodejs-env-setup.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
copy this file to
~/.zsh_cn-nodejs-env-setup.shadd
source $HOME/.zsh_cn-nodejs-env-setup.shin your.zshrcor.bashrcfile.run
cn-nodejs-env-setuporcn-nodejs-env-unsetwhen you need.