From 24ce61d97e7dbbcacfff7f5760e8c5253cc503f0 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Thu, 31 Aug 2023 14:41:48 +0800 Subject: [PATCH] Add nodejs chsrc function --- chsrc.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------ chsrc.h | 2 +- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/chsrc.c b/chsrc.c index 1df4d73..535bdb8 100644 --- a/chsrc.c +++ b/chsrc.c @@ -2,7 +2,7 @@ * File : chsrc.c * Authors : Aoran Zeng * Created on : <2023-08-28> -* Last modified : <2023-08-30> +* Last modified : <2023-08-31> * * chsrc: * @@ -43,6 +43,44 @@ does_the_program_exist (char* check_cmd, char* progname) } +/** + * NodeJS换源 + * + * 参考:https://npmmirror.com/ + */ +void +pl_nodejs_chsrc (char* option) +{ + int selected = 0; char* check_cmd, *prog = NULL; + + if (xy_on_windows) check_cmd = "npm -v >nul 2>nul"; + else check_cmd = "npm -v 1>/dev/null 2>&1"; + + bool exist_b = does_the_program_exist (check_cmd, "npm"); + + if (!exist_b) { + xy_error ("chsrc: 未找到 npm 相关命令,请检查是否存在"); + return; + } + + for (int i=0;iname; + const char* source_abbr = pl_nodejs_sources[selected].mirror->abbr; + const char* source_url = pl_nodejs_sources[selected].url; + + xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); + + char* cmd = xy_2strjoin("npm config set registry ", source_url); + system(cmd); + free(cmd); + + xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); +} + + /** * Python换源 * @@ -76,13 +114,13 @@ pl_python_chsrc (char* option) return; } - for (int i=0;iname; - const char* source_abbr = pl_ruby_sources[selected].mirror->abbr; - const char* source_url = pl_ruby_sources[selected].url; + const char* source_name = pl_python_sources[selected].mirror->name; + const char* source_abbr = pl_python_sources[selected].mirror->abbr; + const char* source_url = pl_python_sources[selected].url; xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); @@ -165,7 +203,7 @@ os_ubuntu_chsrc (char* option) static const char const *pl_ruby[] = {"gem", "ruby", "rb", NULL, chsrcfunc(pl_ruby_chsrc) }, *pl_python[] = {"pip", "python", "py", "pypi", NULL, chsrcfunc(pl_python_chsrc)}, -*pl_nodejs[] = {"npm", "node", "nodejs", "js", NULL}, +*pl_nodejs[] = {"npm", "node", "nodejs", "js", NULL, chsrcfunc(pl_nodejs_chsrc)}, *pl_perl[] = {"perl", "cpan", NULL}, *pl_rust[] = {"rust", "cargo", "crate", "crates", NULL}, diff --git a/chsrc.h b/chsrc.h index b1c68c6..41b33a3 100644 --- a/chsrc.h +++ b/chsrc.h @@ -2,7 +2,7 @@ * File : chsrc.h * Authors : Aoran Zeng * Created on : <2023-08-29> -* Last modified : <2023-08-30> +* Last modified : <2023-08-31> * * chsrc: *