|
|
@@ -7,17 +7,23 @@
|
|
|
|
|
|
'use strict'
|
|
|
|
|
|
+const fs = require('fs')
|
|
|
const path = require('path')
|
|
|
const exec = require('child_process').exec
|
|
|
+const getPref = require('./actions/getPref')
|
|
|
+const getLang = require('./actions/getLang')
|
|
|
const io = require('./io')
|
|
|
const {sys_host_path, work_path} = require('./paths')
|
|
|
const crypto = require('crypto')
|
|
|
const md5File = require('md5-file')
|
|
|
+const applyAfter_Unix = require('./applyAfter_Unix')
|
|
|
const platform = process.platform
|
|
|
+const svr = require('./svr')
|
|
|
|
|
|
let sudo_pswd = ''
|
|
|
+let lang = null
|
|
|
|
|
|
-function needPswd(str) {
|
|
|
+function needPswd (str) {
|
|
|
str = str.toLowerCase()
|
|
|
|
|
|
console.log('---')
|
|
|
@@ -73,14 +79,36 @@ function apply_Unix (content, callback) {
|
|
|
}
|
|
|
|
|
|
function apply_Win32 (content, callback) {
|
|
|
- // todo
|
|
|
+ // todo 判断写入权限
|
|
|
+
|
|
|
+ try {
|
|
|
+ fs.writeFileSync(sys_host_path, content, 'utf-8')
|
|
|
+ } catch (e) {
|
|
|
+ console.log(e)
|
|
|
+ let msg = e.message
|
|
|
+ msg = `${msg}\n\n${lang.please_run_as_admin}`
|
|
|
+ alert(msg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // todo 刷新 DNS 缓存
|
|
|
+
|
|
|
+ callback()
|
|
|
}
|
|
|
|
|
|
-function tryToApply (...args) {
|
|
|
+function tryToApply (content, callback) {
|
|
|
if (platform !== 'win32') {
|
|
|
- apply_Unix(...args)
|
|
|
+ // unix
|
|
|
+ apply_Unix(content, (e) => {
|
|
|
+ if (e) {
|
|
|
+ callback(e)
|
|
|
+ } else {
|
|
|
+ applyAfter_Unix(sudo_pswd, callback)
|
|
|
+ }
|
|
|
+ })
|
|
|
} else {
|
|
|
- apply_Win32(...args)
|
|
|
+ // win32
|
|
|
+ apply_Win32(content, callback)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -88,18 +116,52 @@ module.exports = (cnt, pswd) => {
|
|
|
if (pswd) {
|
|
|
sudo_pswd = pswd
|
|
|
}
|
|
|
+ let pref
|
|
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- let file_md5 = md5File.sync(sys_host_path)
|
|
|
- let cnt_md5 = crypto.createHash('md5').update(cnt).digest('hex')
|
|
|
+ return Promise.resolve()
|
|
|
+ .then(() => {
|
|
|
+ return getPref()
|
|
|
+ .then(p => {
|
|
|
+ pref = p
|
|
|
+ return p.user_language || 'en'
|
|
|
+ })
|
|
|
+ .then(l => {
|
|
|
+ return getLang(svr, l)
|
|
|
+ })
|
|
|
+ .then(v => lang = v || {})
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ let file_md5 = md5File.sync(sys_host_path)
|
|
|
+ let cnt_md5 = crypto.createHash('md5').update(cnt).digest('hex')
|
|
|
|
|
|
- if (file_md5 === cnt_md5) {
|
|
|
- // 文件相同
|
|
|
- resolve()
|
|
|
- return
|
|
|
- }
|
|
|
+ if (file_md5 === cnt_md5) {
|
|
|
+ // 文件相同
|
|
|
+ resolve()
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- tryToApply(cnt, e => e ? reject(e) : resolve())
|
|
|
- //reject('need_sudo')
|
|
|
- })
|
|
|
+ tryToApply(cnt, e => e ? reject(e) : resolve())
|
|
|
+ //reject('need_sudo')
|
|
|
+ })
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ let after_cmd = pref.after_cmd
|
|
|
+
|
|
|
+ if (after_cmd) {
|
|
|
+ exec(after_cmd, (error, stdout, stderr) => {
|
|
|
+ // command output is in stdout
|
|
|
+ if (error) {
|
|
|
+ reject(`AfterCmdError:\n\n${stderr}`)
|
|
|
+ } else {
|
|
|
+ resolve()
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ } else {
|
|
|
+ resolve()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
}
|