applyAfter_Unix.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict';
  6. const fs = require('fs')
  7. const path = require('path')
  8. const {work_path} = require('./paths')
  9. const exec = require('child_process').exec
  10. const platform = process.platform
  11. function forMac(sudo_pswd, callback) {
  12. let cmd_fn = path.join(work_path, '_restart_mDNSResponder.sh');
  13. let cmd = `
  14. p1=/System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
  15. if [ -f $p1 ]; then
  16. launchctl unload -w $p1
  17. launchctl load -w $p1
  18. fi
  19. p2=/System/Library/LaunchDaemons/com.apple.discoveryd.plist
  20. if [ -f p2 ]; then
  21. launchctl unload -w $p2
  22. launchctl load -w $p2
  23. fi
  24. killall -HUP mDNSResponder
  25. `;
  26. fs.writeFileSync(cmd_fn, cmd, 'utf-8');
  27. exec(`echo '${sudo_pswd}' | sudo -S /bin/sh ${cmd_fn}`, function (error, stdout, stderr) {
  28. if (fs.existsSync(cmd_fn)) {
  29. try {
  30. fs.unlink(cmd_fn)
  31. } catch (e) {
  32. alert(e.message)
  33. }
  34. }
  35. // command output is in stdout
  36. if (error) {
  37. console.log(error);
  38. }
  39. console.log(stdout, stderr);
  40. callback();
  41. });
  42. }
  43. module.exports = (sudo_pswd, callback) => {
  44. if (sudo_pswd && platform === 'darwin') {
  45. forMac(sudo_pswd, callback);
  46. } else {
  47. callback()
  48. }
  49. }