visit-github 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Readme.md ADDED
@@ -0,0 +1,24 @@
1
+ ## 访问GitHub
2
+
3
+
4
+ ### 注意
5
+ > 1. windows 使用时请时使用`管理员权限`打开命令行工具
6
+ > 2. 如果您的hosts文件特别重要,请自己手动备份
7
+
8
+ #### 介绍
9
+ > 本项目还处于测试阶段,在本机测试已通过,目前只可以在windows下使用
10
+
11
+ #### 安装
12
+ ```
13
+ npm install visit-github -g
14
+ ```
15
+
16
+ #### 使用
17
+
18
+ > visit-github 两条功能命令
19
+ 1. `zx-github` 更新备份hosts文件,只备份第一次的hosts文件
20
+ 2. `zx-github -r` 恢复备份的hosts文件
21
+
22
+
23
+ #### 感谢
24
+ 使用了[GitHub520](https://github.com/521xueweihan/GitHub520)提供的更新的hosts接口
package/bin/index.js ADDED
@@ -0,0 +1,12 @@
1
+ #! /usr/bin/env node
2
+
3
+ const os = require('os'),
4
+ platform = os.platform(),
5
+ win = require('../lib/win'),
6
+ program = require('../util/commander')()
7
+
8
+ const platforms = {
9
+ win32: win
10
+ }
11
+
12
+ platforms[platform](program.opts().reset)
package/lib/win.js ADDED
@@ -0,0 +1,12 @@
1
+ const { init, reset } = require('../util/index'),
2
+ MIN_DEFAULT_PATH = 'C:/Windows/System32/drivers/etc/'
3
+
4
+ const win = function (isReset) {
5
+ if(isReset) {
6
+ reset(MIN_DEFAULT_PATH, 'hosts_zx_backups')
7
+ }else {
8
+ init(MIN_DEFAULT_PATH, 'hosts_zx_temporary')
9
+ }
10
+ }
11
+
12
+ module.exports = win
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "visit-github",
3
+ "version": "0.0.1",
4
+ "description": "替换系统中的hosts文件",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "zx-github": "bin/index.js"
8
+ },
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": [],
13
+ "author": "zxhou",
14
+ "license": "ISC",
15
+ "dependencies": {
16
+ "commander": "^9.3.0",
17
+ "iconv-lite": "^0.6.3"
18
+ }
19
+ }
@@ -0,0 +1,17 @@
1
+ module.exports = function () {
2
+ const { program } = require('commander'),
3
+ op = require('../package.json')
4
+
5
+ program
6
+ .version(op.version, '-v, --version', '输出当前版本')
7
+ .option('-r, --reset', '恢复原来的hosts文件')
8
+ .helpOption('-h, --help', '显示命令帮助')
9
+ .addHelpText('after', `
10
+ 示例:
11
+ $ zx-github 备份原来hosts文件,替换hosts
12
+ $ zx-github -r, --rest 恢复原来的hosts文件`
13
+ )
14
+ .parse(process.argv)
15
+
16
+ return program
17
+ }
package/util/index.js ADDED
@@ -0,0 +1,82 @@
1
+ const fs = require('fs'),
2
+ https = require('https'),
3
+ iconv = require('iconv-lite'),
4
+ process = require('child_process')
5
+
6
+ const isExists = path => {
7
+ try {
8
+ const o = fs.statSync(path)
9
+ return o.isFile()
10
+ }catch(err) {
11
+ return false
12
+ }
13
+ }
14
+
15
+ const download = (path, n) => {
16
+ https.get('https://raw.hellogithub.com/hosts', res => {
17
+ let str = ''
18
+ res.on('data', chunk => (str += chunk))
19
+ res.on('end', () => {
20
+ try {
21
+ fs.writeFile(path + n, str, err => {
22
+ if(err) return console.log('写入临时文件出错')
23
+ rename(path, n, 'hosts', '替换下载hosts文件时出错')
24
+ console.log('替换hosts文件成功')
25
+ runShellFlushDns()
26
+ })
27
+ }catch(err) {
28
+ console.log(err)
29
+ }
30
+ })
31
+ }).on('error', (e) => {
32
+ console.error(`Got error: ${e.message}`)
33
+ })
34
+ }
35
+
36
+ const reset = (path, n) => {
37
+ const isOk = isExists(path + n)
38
+ if(isOk) {
39
+ if(isExists(path + 'hosts')) {
40
+ fs.unlinkSync(path + 'hosts')
41
+ }
42
+ rename(path, 'hosts_zx_backups', 'hosts', '还原系统hosts文件时出错')
43
+ console.log('还原系统hosts文件成功')
44
+ runShellFlushDns()
45
+ }else {
46
+ console.log('没有找到备份文件,无法恢复')
47
+ }
48
+ }
49
+
50
+ const init = (path, n) => {
51
+ if(!isExists(path + 'hosts_zx_backups')) {
52
+ rename(
53
+ path,
54
+ 'hosts',
55
+ 'hosts_zx_backups',
56
+ '备份系统hosts文件时出错'
57
+ )
58
+ }
59
+ setTimeout(download.bind(null, path, n), 500)
60
+ }
61
+
62
+ const rename = (root, path, nPath, info) => {
63
+ const isOk = isExists(root + path)
64
+ if(isOk) {
65
+ try {
66
+ fs.renameSync(root + path, root + nPath)
67
+ }catch (err) {
68
+ console.log(info)
69
+ }
70
+ }else {
71
+ console.log(path + '文件不存在')
72
+ }
73
+ }
74
+
75
+ const runShellFlushDns = () => {
76
+ process.exec('ipconfig /flushdns', { encoding: 'binary' }, (err, stdout) => {
77
+ if(err) return
78
+ console.log(iconv.decode(Buffer.from(stdout, 'binary'), 'cp936'))
79
+ })
80
+ }
81
+
82
+ module.exports = { init, reset }