visit-github 0.0.1 → 0.0.2

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 CHANGED
@@ -1,24 +1,25 @@
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
- #### 感谢
1
+ ## 访问GitHub
2
+
3
+
4
+ ### 注意
5
+ > 1. windows 使用时请时使用`管理员权限`打开命令行工具
6
+ > 2. 如果您的hosts文件特别重要,请自己手动备份
7
+ > 3. mac 电脑上要使用`sudo zx-github`命令,需要管理员权限
8
+
9
+ #### 介绍
10
+ > 当前项目已经在mac与windows电脑运行成功,余下linux
11
+
12
+ #### 安装
13
+ ```
14
+ npm install visit-github -g
15
+ ```
16
+
17
+ #### 使用
18
+
19
+ > visit-github 两条功能命令
20
+ 1. `zx-github` 更新备份hosts文件,只备份第一次的hosts文件
21
+ 2. `zx-github -r` 恢复备份的hosts文件
22
+
23
+
24
+ #### 感谢
24
25
  使用了[GitHub520](https://github.com/521xueweihan/GitHub520)提供的更新的hosts接口
package/bin/index.js CHANGED
@@ -1,12 +1,12 @@
1
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
-
2
+
3
+ const os = require('os'),
4
+ platform = os.platform(),
5
+ program = require('../util/commander')()
6
+
7
+ const platforms = {
8
+ win32: require('../lib/win'),
9
+ darwin: require('../lib/darwin')
10
+ }
11
+
12
12
  platforms[platform](program.opts().reset)
package/lib/darwin.js ADDED
@@ -0,0 +1,11 @@
1
+ const { init, reset } = require('../util/darwin')
2
+
3
+ const darwin = function (isReset) {
4
+ if(isReset) {
5
+ reset('/etc/', 'hosts_zx_backups')
6
+ }else {
7
+ init('/etc/', 'hosts_zx_temporary')
8
+ }
9
+ }
10
+
11
+ module.exports = darwin
package/lib/win.js CHANGED
@@ -1,12 +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
1
+ const { init, reset } = require('../util/win'),
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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "visit-github",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "替换系统中的hosts文件",
5
5
  "main": "index.js",
6
6
  "bin": {
package/util/commander.js CHANGED
@@ -1,17 +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
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
17
  }
package/util/darwin.js ADDED
@@ -0,0 +1,29 @@
1
+ const { isExists, rename, runShellFlushDns, download, delFile } = require('./index')
2
+
3
+ const init = async (path, n) => {
4
+ if(!isExists(path + 'hosts_zx_backups')) {
5
+ rename(
6
+ path,
7
+ 'hosts',
8
+ 'hosts_zx_backups',
9
+ '备份系统hosts文件时出错'
10
+ )
11
+ }
12
+ await download(path, n) && runShellFlushDns('sudo killall -HUP mDNSResponder')
13
+ }
14
+
15
+ const reset = (path, n) => {
16
+ const isOk = isExists(path + n)
17
+ if(isOk) {
18
+ if(isExists(path + 'hosts')) {
19
+ delFile(path + 'hosts')
20
+ }
21
+ rename(path, 'hosts_zx_backups', 'hosts', '还原系统hosts文件时出错')
22
+ console.log('还原系统hosts文件成功')
23
+ runShellFlushDns('sudo killall -HUP mDNSResponder')
24
+ }else {
25
+ console.log('没有找到备份文件,无法恢复')
26
+ }
27
+ }
28
+
29
+ module.exports = { init, reset }
package/util/index.js CHANGED
@@ -1,82 +1,60 @@
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 }
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
+ return new Promise((resolve, reject) => {
17
+ https.get('https://raw.hellogithub.com/hosts', res => {
18
+ let str = ''
19
+ res.on('data', chunk => (str += chunk))
20
+ res.on('end', () => {
21
+ fs.writeFile(path + n, str, err => {
22
+ if(err){
23
+ reject(false)
24
+ console.log('写入临时文件出错')
25
+ return
26
+ }
27
+ rename(path, n, 'hosts', '替换下载hosts文件时出错')
28
+ console.log('替换hosts文件成功')
29
+ resolve(true)
30
+ })
31
+ })
32
+ }).on('error', (e) => {
33
+ console.error(`Got error: ${e.message}`)
34
+ })
35
+ })
36
+ }
37
+
38
+ const rename = (root, path, nPath, info) => {
39
+ const isOk = isExists(root + path)
40
+ if(isOk) {
41
+ try {
42
+ fs.renameSync(root + path, root + nPath)
43
+ }catch (err) {
44
+ console.log(info)
45
+ }
46
+ }else {
47
+ console.log(path + '文件不存在')
48
+ }
49
+ }
50
+
51
+ const delFile = path => fs.unlinkSync(path)
52
+
53
+ const runShellFlushDns = shell => {
54
+ process.exec(shell, { encoding: 'binary' }, (err, stdout) => {
55
+ if(err) return
56
+ console.log(iconv.decode(Buffer.from(stdout, 'binary'), 'cp936'))
57
+ })
58
+ }
59
+
60
+ module.exports = { isExists, rename, runShellFlushDns, download, delFile }
package/util/win.js ADDED
@@ -0,0 +1,30 @@
1
+ const { isExists, rename, runShellFlushDns, download, delFile } = require('./index')
2
+
3
+ const init = async (path, n) => {
4
+ if(!isExists(path + 'hosts_zx_backups')) {
5
+ rename(
6
+ path,
7
+ 'hosts',
8
+ 'hosts_zx_backups',
9
+ '备份系统hosts文件时出错'
10
+ )
11
+ }
12
+ await download(path, n) && runShellFlushDns('ipconfig /flushdns')
13
+ }
14
+
15
+ const reset = (path, n) => {
16
+ const isOk = isExists(path + n)
17
+ if(isOk) {
18
+ if(isExists(path + 'hosts')) {
19
+ delFile(path + 'hosts')
20
+ }
21
+ rename(path, 'hosts_zx_backups', 'hosts', '还原系统hosts文件时出错')
22
+ console.log('还原系统hosts文件成功')
23
+ runShellFlushDns('ipconfig /flushdns')
24
+ }else {
25
+ console.log('没有找到备份文件,无法恢复')
26
+ }
27
+ }
28
+
29
+
30
+ module.exports = { init, reset }