applyAfter_Unix.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. echo '${sudo_pswd}' | sudo -S launchctl unload -w $p1
  17. echo '${sudo_pswd}' | sudo -S launchctl load -w $p1
  18. fi
  19. p2=/System/Library/LaunchDaemons/com.apple.discoveryd.plist
  20. if [ -f p2 ]; then
  21. echo '${sudo_pswd}' | sudo -S launchctl unload -w $p2
  22. echo '${sudo_pswd}' | sudo -S launchctl load -w $p2
  23. fi
  24. echo '${sudo_pswd}' | sudo -S killall -HUP mDNSResponder
  25. `;
  26. fs.writeFileSync(cmd_fn, cmd, 'utf-8');
  27. exec(`/bin/sh ${cmd_fn}`, function (error, stdout, stderr) {
  28. fs.unlink(cmd_fn)
  29. // command output is in stdout
  30. if (error) {
  31. console.log(error);
  32. }
  33. console.log(stdout, stderr);
  34. callback();
  35. });
  36. }
  37. module.exports = (sudo_pswd, callback) => {
  38. if (sudo_pswd && platform === 'darwin') {
  39. forMac(sudo_pswd, callback);
  40. } else {
  41. callback()
  42. }
  43. }