Skip to content

Commit

Permalink
fix: dnd doesn't work on macOS Big Sur
Browse files Browse the repository at this point in the history
  • Loading branch information
arodik committed Feb 20, 2021
1 parent 7c0511e commit 475435f
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 12 deletions.
39 changes: 30 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
"easytimer.js": "^4.3.1",
"home-dir": "^1.0.0",
"lowdb": "^1.0.0",
"luxon": "^1.25.0",
"luxon": "^1.26.0",
"semver": "^7.3.4",
"shortid": "^2.2.16",
"yargs": "^16.1.1"
"yargs": "^16.2.0"
}
}
2 changes: 1 addition & 1 deletion src/commands/start.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Timer } = require("easytimer.js");
const dialog = require("dialog");
const doNotDisturb = require("@sindresorhus/do-not-disturb");
const doNotDisturb = require("../dnd").getDndProvider();
const {clearLineAndWrite} = require("../helpers/cli");
const {startCountdownTimer} = require("../helpers/timer");
const TimerSession = require("../db/session");
Expand Down
21 changes: 21 additions & 0 deletions src/dnd/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const os = require('os');
const semverParse = require('semver/functions/parse');
const LegacyDndProvider = require("./legacy-dnd");
const ShellBigSurDndProvider = require("./shell-bigsur-dnd");

function isBigSurOrNewer() {
const { major } = semverParse(os.release())
return major >= 20;
}

function getDndProvider() {
if (isBigSurOrNewer()) {
return new ShellBigSurDndProvider();
}

return new LegacyDndProvider();
}

module.exports = {
getDndProvider,
}
13 changes: 13 additions & 0 deletions src/dnd/legacy-dnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const doNotDisturb = require("@sindresorhus/do-not-disturb");

class LegacyDndProvider {
enable() {
return doNotDisturb.enable();
}

disable() {
return doNotDisturb.disable();
}
}

module.exports = LegacyDndProvider;
4 changes: 4 additions & 0 deletions src/dnd/shell-bigsur-dnd/check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env zsh

# https://github.com/sindresorhus/do-not-disturb/issues/9#issuecomment-768492417
plutil -extract dnd_prefs xml1 -o - /Users/"$USER"/Library/Preferences/com.apple.ncprefs.plist | xmllint --xpath "string(//data)" - | base64 --decode | plutil -convert xml1 - -o - | xmllint --xpath 'boolean(//key[text()="userPref"]/following-sibling::dict/key[text()="enabled"])' -
21 changes: 21 additions & 0 deletions src/dnd/shell-bigsur-dnd/disable.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env zsh

# https://github.com/sindresorhus/do-not-disturb/issues/9#issuecomment-768492417
defaults read /Users/"$USER"/Library/Preferences/com.apple.ncprefs.plist >/dev/null
DND_HEX_DATA=$(plutil -extract dnd_prefs xml1 -o - /Users/"$USER"/Library/Preferences/com.apple.ncprefs.plist | xmllint --xpath "string(//data)" - | base64 --decode | plutil -convert xml1 - -o - | plutil -remove userPref - -o - | plutil -convert binary1 - -o - | xxd -p | tr -d '\n')
defaults write com.apple.ncprefs.plist dnd_prefs -data "$DND_HEX_DATA"
PROCESS_LIST=(
#cfprefsd
usernoted
#NotificationCenter
)
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "" ]]; then continue; fi
i="$line"
#echo "$i"
if [[ $(ps aux | grep "$i" | grep -v grep | awk '{print $2;}') != "" ]]; then
killall "$i" && sleep 0.1 && while [[ $(ps aux | grep "$i" | grep -v grep | awk '{print $2;}') == "" ]]; do sleep 0.5; done
else
:
fi
done <<<"$(printf "%s\n" "${PROCESS_LIST[@]}")"
29 changes: 29 additions & 0 deletions src/dnd/shell-bigsur-dnd/enable.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env zsh

# https://github.com/sindresorhus/do-not-disturb/issues/9#issuecomment-768492417
defaults read /Users/"$USER"/Library/Preferences/com.apple.ncprefs.plist >/dev/null
DND_HEX_DATA=$(plutil -extract dnd_prefs xml1 -o - /Users/"$USER"/Library/Preferences/com.apple.ncprefs.plist | xmllint --xpath "string(//data)" - | base64 --decode | plutil -convert xml1 - -o - | plutil -insert userPref -xml "
<dict>
<key>date</key>
<date>$(date -u +"%Y-%m-%dT%H:%M:%SZ")</date>
<key>enabled</key>
<true/>
<key>reason</key>
<integer>1</integer>
</dict> " - -o - | plutil -convert binary1 - -o - | xxd -p | tr -d '\n')
defaults write com.apple.ncprefs.plist dnd_prefs -data "$DND_HEX_DATA"
PROCESS_LIST=(
#cfprefsd
usernoted
#NotificationCenter
)
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "" ]]; then continue; fi
i="$line"
#echo "$i"
if [[ $(ps aux | grep "$i" | grep -v grep | awk '{print $2;}') != "" ]]; then
killall "$i" && sleep 0.1 && while [[ $(ps aux | grep "$i" | grep -v grep | awk '{print $2;}') == "" ]]; do sleep 0.5; done
else
:
fi
done <<<"$(printf "%s\n" "${PROCESS_LIST[@]}")"
28 changes: 28 additions & 0 deletions src/dnd/shell-bigsur-dnd/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const util = require('util');
const path = require('path');
const execFile = util.promisify(require('child_process').execFile);

const execShellScript = filename => execFile(path.resolve(__dirname, filename));

class ShellBigsurDndProvider {
async enable() {
if (await this._isDnDEnabled()) {
return;
}
return execShellScript('./enable.sh');
}

async disable() {
if (!await this._isDnDEnabled()) {
return;
}
return execShellScript('./disable.sh');
}

async _isDnDEnabled() {
const { stdout } = await execShellScript('./check.sh');
return stdout === 'true';
}
}

module.exports = ShellBigsurDndProvider;

0 comments on commit 475435f

Please sign in to comment.