-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (106 loc) · 3 KB
/
index.js
File metadata and controls
115 lines (106 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
'use strict'
const chalk = require('chalk')
// 检查node版本要求
function checkNodeVersion() {
const pkg = require('./package.json')
const nodeVer = process.version
const requiredVer = pkg.engines.node
if (
parseInt(nodeVer.replace(/^[\D]+/, ''), 10) <
parseInt(requiredVer.replace(/^[\D]+/, ''), 10)
) {
console.error(
chalk.red(
`You are using Node ${nodeVer} , but this tool requires Node ${requiredVer}.\nPlease upgrade your Node version first.\n`
)
)
process.exit(1)
}
}
// 如果node版本过低,依赖的包会抛异常
checkNodeVersion()
const update = require('./lib/update')
const { clear, isAccessible } = require('./lib/hosts')
const { flush: flushDNSService } = require('./lib/dns')
const { confirm, logger } = require('./lib/utils')
const { downloadDomainsData } = require('./lib/domains')
// 运行主逻辑
async function run({ c, u }) {
// 检查文件读写权限
const hasPerm = await isAccessible()
if (c) {
// 执行清除操作
// 必须要有权限
logger.info('Preparing to clear the hosts of GitHub...\n')
if (!hasPerm) {
throw 'Administrator permission required to modify the hosts file.\n Please run as administrator again.'
}
await clear(await downloadDomainsData(u))
await flushDNSService().catch(() => {
logger.warn('Maybe you need to restart your computer.\n')
})
logger.success('Cleared Successfully.\n')
} else {
// 执行更新操作
let doUpdate = true
if (!hasPerm) {
doUpdate = await confirm(
chalk.yellow(
`Administrator permission required to modify the hosts file.\n Do you want to go on ${chalk.red(
'without admin permission'
)} ?`
)
)
if (doUpdate) {
console.log('')
}
}
if (doUpdate) {
// 如果没有权限,只拷贝hosts内容到剪贴板
// 如果有权限,则直接更新hosts文件
return await update(hasPerm, await downloadDomainsData(u))
}
console.log('')
}
}
// 初始化命令行提示信息
const yargs = require('yargs')
.usage('github-hosts [options]')
.option('help', {
alias: 'h',
type: 'boolean',
describe: 'Show help',
})
.option('version', {
alias: 'v',
type: 'boolean',
describe: 'Show version number',
})
.option('clear', {
alias: 'c',
type: 'boolean',
default: false,
describe: 'Clear the hosts of GitHub',
})
.option('data-url', {
alias: 'u',
type: 'string',
describe: 'Remote url for domains data',
})
//
const argv = yargs.argv
if (argv.h) {
// 显示帮助信息
yargs.showHelp('log')
} else {
const printError = (err) =>
logger.error(err instanceof Error ? err.message : err)
// 捕获全局异常,防止程序依赖的包抛出未知错误而导致退出
process.on('uncaughtException', printError)
// 执行脚本
run(argv)
.then((msg = 'Good bye!') => logger.info(msg))
.catch(printError)
.then(() => console.log(''))
}