this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

🚧 wip

+121 -69
+27 -31
cli.js
··· 1 1 #! /usr/bin/env node 2 2 3 - import {readFile, writeFile} from 'node:fs/promises'; 4 - import {fileURLToPath} from 'node:url'; 5 3 import {program} from 'commander'; 6 4 import darkMode from 'dark-mode'; 7 - import envPaths from 'env-paths'; 8 - import {execa} from 'execa'; 9 - import pupa from 'pupa'; 10 5 import {readPackageUp} from 'read-pkg-up'; 11 6 import {setTerminalProfile} from 'terminal-profile'; 12 - import untildify from 'untildify'; 13 7 import {config} from './config.js'; 8 + import { 9 + disableAutomaticSwitching, 10 + enableAutomaticSwitching, 11 + isAutomaticSwitchingEnabled, 12 + } from './functions/index.js'; 14 13 15 14 const {packageJson} = await readPackageUp({cwd: new URL('.', import.meta.url)}); 16 15 17 - const launchAgentPlistFilePath = untildify( 18 - '~/Library/LaunchAgents/ke.bou.dark-mode-notify.plist', 19 - ); 20 - 21 16 program 22 17 .name(packageJson.name) 23 18 .description(packageJson.description) 24 19 .version(packageJson.version); 20 + 21 + program 22 + .command('disable') 23 + .description( 24 + 'Disable automatic macOS Terminal profile switching based on system dark / light mode', 25 + ) 26 + .action(async () => { 27 + await disableAutomaticSwitching() 28 + 29 + console.log('Automatic switching disabled') 30 + }); 25 31 26 32 program 27 33 .command('enable') ··· 52 58 if (darkProfile) config.darkProfile = darkProfile; 53 59 if (lightProfile) config.lightProfile = lightProfile; 54 60 55 - const launchAgentPlistFileContents = pupa( 56 - await readFile(new URL('launch-agent.xml', import.meta.url), { 57 - encoding: 'utf8', 58 - }), 59 - { 60 - autoTerminalProfilePath: fileURLToPath( 61 - new URL('cli.js', import.meta.url), 62 - ), 63 - darkModeNotifyPath: fileURLToPath( 64 - new URL('dark-mode-notify.swift', import.meta.url), 65 - ), 66 - logPath: envPaths(packageJson.name).log, 67 - path: process.env.PATH, 68 - }, 69 - ); 61 + await enableAutomaticSwitching(); 70 62 71 - await writeFile(launchAgentPlistFilePath, launchAgentPlistFileContents); 72 - 73 - await execa('launchctl', ['load', '-w', launchAgentPlistFilePath]); 63 + console.log('Automatic switching enabled') 74 64 }); 75 65 76 66 for (const mode of ['dark', 'light']) { ··· 80 70 .argument('<profile>') 81 71 .action((profile) => { 82 72 config[`${mode}Profile`] = profile; 73 + 74 + console.log(`${mode} mode profile set to '${profile}'`) 83 75 }); 84 76 } 85 77 86 78 program 87 79 .command('status') 88 - .description('Show configuration') 89 - .action(() => { 90 - console.log('automatic switching : (disabled|enabled) (TODO)'); // TODO 80 + .description('Show status and configuration') 81 + .action(async () => { 82 + console.log( 83 + `automatic switching : ${ 84 + (await isAutomaticSwitchingEnabled()) ? 'enabled' : 'disabled' 85 + }`, 86 + ); 91 87 console.log(`dark profile : ${config.darkProfile}`); 92 88 console.log(`light profile : ${config.lightProfile}`); 93 89 });
+2
constants/index.js
··· 1 + export {launchAgentPlistFilePath} from './launch-agent-plist-file-path.js'; 2 + export {packageJson} from './package-json.js';
+5
constants/launch-agent-plist-file-path.js
··· 1 + import untildify from 'untildify'; 2 + 3 + export const launchAgentPlistFilePath = untildify( 4 + '~/Library/LaunchAgents/ke.bou.dark-mode-notify.plist', 5 + );
+5
constants/package-json.js
··· 1 + import {readPackageUp} from 'read-pkg-up'; 2 + 3 + export const {packageJson} = await readPackageUp({ 4 + cwd: new URL('.', import.meta.url), 5 + });
-38
dark-mode-notify.swift
··· 1 - #!/usr/bin/env swift 2 - 3 - // Run as ./notify.swift <program to run when dark mode changes> 4 - // The program will have the DARKMODE env flag set to 1 or 0 5 - // You can also compile with: 6 - // swiftc notify.swift -o notify 7 - // And run the binary directly 8 - // Most credit goes to https://github.com/mnewt/dotemacs/blob/master/bin/dark-mode-notifier.swift 9 - 10 - import Cocoa 11 - 12 - @discardableResult 13 - func shell(_ args: [String]) -> Int32 { 14 - let task = Process() 15 - let isDark = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") == "Dark" 16 - var env = ProcessInfo.processInfo.environment 17 - env["DARKMODE"] = isDark ? "1" : "0" 18 - task.environment = env 19 - task.launchPath = "/usr/bin/env" 20 - task.arguments = args 21 - task.standardError = FileHandle.standardError 22 - task.standardOutput = FileHandle.standardOutput 23 - task.launch() 24 - task.waitUntilExit() 25 - return task.terminationStatus 26 - } 27 - 28 - let args = Array(CommandLine.arguments.suffix(from: 1)) 29 - shell(args) 30 - 31 - DistributedNotificationCenter.default.addObserver( 32 - forName: Notification.Name("AppleInterfaceThemeChangedNotification"), 33 - object: nil, 34 - queue: nil) { (notification) in 35 - shell(args) 36 - } 37 - 38 - NSApplication.shared.run()
+9
functions/disable-automatic-switching.js
··· 1 + import {unlink} from 'node:fs/promises'; 2 + import {execa} from 'execa'; 3 + import {launchAgentPlistFilePath} from '../constants/index.js'; 4 + 5 + export async function disableAutomaticSwitching() { 6 + await execa('launchctl', ['unload', '-w', launchAgentPlistFilePath]); 7 + 8 + await unlink(launchAgentPlistFilePath); 9 + }
+13
functions/enable-automatic-switching.js
··· 1 + import {writeFile} from 'node:fs/promises'; 2 + import {execa} from 'execa'; 3 + import {launchAgentPlistFilePath} from '../constants/index.js'; 4 + import {getLaunchAgentPlistFileContents} from './get-launch-agent-plist-file-contents.js'; 5 + 6 + export async function enableAutomaticSwitching() { 7 + await writeFile( 8 + launchAgentPlistFilePath, 9 + await getLaunchAgentPlistFileContents(), 10 + ); 11 + 12 + await execa('launchctl', ['load', '-w', launchAgentPlistFilePath]); 13 + }
+24
functions/get-launch-agent-plist-file-contents.js
··· 1 + import {readFile} from 'node:fs/promises'; 2 + import {fileURLToPath} from 'node:url'; 3 + import process from 'node:process'; 4 + import pupa from 'pupa'; 5 + import envPaths from 'env-paths'; 6 + import {packageJson} from '../constants/index.js'; 7 + 8 + export async function getLaunchAgentPlistFileContents() { 9 + return pupa( 10 + await readFile(new URL('../templates/launch-agent.xml', import.meta.url), { 11 + encoding: 'utf8', 12 + }), 13 + { 14 + autoTerminalProfilePath: fileURLToPath( 15 + new URL('../cli.js', import.meta.url), 16 + ), 17 + darkModeNotifyPath: fileURLToPath( 18 + new URL('../dark-mode-notify/dark-mode-notify.swift', import.meta.url), 19 + ), 20 + logPath: envPaths(packageJson.name).log, 21 + path: process.env.PATH, 22 + }, 23 + ); 24 + }
+4
functions/index.js
··· 1 + export {enableAutomaticSwitching} from './enable-automatic-switching.js'; 2 + export {disableAutomaticSwitching} from './disable-automatic-switching.js'; 3 + export {getLaunchAgentPlistFileContents} from './get-launch-agent-plist-file-contents.js'; 4 + export {isAutomaticSwitchingEnabled} from './is-automatic-switching-enabled.js';
+32
functions/is-automatic-switching-enabled.js
··· 1 + import {existsSync as exists} from 'node:fs'; 2 + import {execa} from 'execa'; 3 + import {launchAgentPlistFilePath} from '../constants/index.js'; 4 + 5 + /** 6 + * @return {Promise<boolean>} 7 + */ 8 + async function isDarkModeNotifyRunning() { 9 + const {stdout} = await execa('launchctl', ['list']); 10 + 11 + return stdout.includes('ke.bou.dark-mode-notify'); 12 + } 13 + 14 + /** 15 + * @return {Promise<boolean>} 16 + */ 17 + export async function isAutomaticSwitchingEnabled() { 18 + const _isDarkModeNotifyRunning = await isDarkModeNotifyRunning(); 19 + const plistFileExists = exists(launchAgentPlistFilePath); 20 + 21 + if (_isDarkModeNotifyRunning && !plistFileExists) { 22 + throw new Error( 23 + `Automatic switching is running, but the launch agent plist file (${launchAgentPlistFilePath}) doesn't exist`, 24 + ); 25 + } else if (!_isDarkModeNotifyRunning && plistFileExists) { 26 + throw new Error( 27 + `Automatic switching is not running, but the launch agent plist file (${launchAgentPlistFilePath}) exists`, 28 + ); 29 + } 30 + 31 + return _isDarkModeNotifyRunning && plistFileExists; 32 + }
launch-agent.xml templates/launch-agent.xml