auto-terminal-profile 3.0.2 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ buy_me_a_coffee: patrikcsak
package/actions/enable.js CHANGED
@@ -1,24 +1,58 @@
1
1
  import {config} from '../config.js';
2
2
  import {packageJson} from '../constants/index.js';
3
- import {enableAutomaticSwitching} from '../functions/index.js';
3
+ import {
4
+ enableAutomaticSwitching,
5
+ getCurrentMode,
6
+ isTerminalOpen,
7
+ setTerminalProfile,
8
+ } from '../functions/index.js';
4
9
 
5
- export async function enable({darkProfile, lightProfile}) {
6
- if (!darkProfile && !config.darkProfile) {
7
- throw new Error(
8
- `Dark profile must be specified with --dark-profile or previously set with \`${packageJson.name} set-dark-mode\``,
9
- );
10
+ /**
11
+ * @param {string} mode
12
+ */
13
+ function undefinedProfileMessage(mode) {
14
+ return `${mode} profile must be specified with --${mode}-profile or previously set with \`${packageJson.name} set-${mode}-mode\``;
15
+ }
16
+
17
+ /**
18
+ * @param {object} parameters
19
+ * @param {string|undefined} parameters.darkProfile
20
+ * @param {string|undefined} parameters.lightProfile
21
+ */
22
+ export async function enable(parameters) {
23
+ if (
24
+ [parameters.darkProfile, config.darkProfile].every(
25
+ (value) => value === undefined,
26
+ )
27
+ ) {
28
+ throw new Error(undefinedProfileMessage('dark'));
29
+ }
30
+
31
+ if (
32
+ [parameters.lightProfile, config.lightProfile].every(
33
+ (value) => value === undefined,
34
+ )
35
+ ) {
36
+ throw new Error(undefinedProfileMessage('light'));
10
37
  }
11
38
 
12
- if (!lightProfile && !config.lightProfile) {
13
- throw new Error(
14
- `Light profile must be specified with --light-profile or previously set with \`${packageJson.name} set-light-mode\``,
15
- );
39
+ if (parameters.darkProfile !== undefined) {
40
+ config.darkProfile = parameters.darkProfile;
16
41
  }
17
42
 
18
- if (darkProfile) config.darkProfile = darkProfile;
19
- if (lightProfile) config.lightProfile = lightProfile;
43
+ if (parameters.lightProfile !== undefined) {
44
+ config.lightProfile = parameters.lightProfile;
45
+ }
20
46
 
21
47
  await enableAutomaticSwitching();
22
48
 
23
- console.log('Automatic switching enabled');
49
+ if (await isTerminalOpen()) {
50
+ const mode = await getCurrentMode();
51
+
52
+ if (parameters[`${mode}Profile`] !== undefined) {
53
+ await setTerminalProfile(config[`${mode}Profile`]);
54
+ }
55
+ }
56
+
57
+ console.log('automatic switching enabled');
24
58
  }
@@ -1,7 +1,21 @@
1
1
  import {config} from '../config.js';
2
+ import {
3
+ getCurrentMode,
4
+ isAutomaticSwitchingEnabled,
5
+ isTerminalOpen,
6
+ setTerminalProfile,
7
+ } from '../functions/index.js';
2
8
 
3
- export function setModeProfile({mode, profile}) {
9
+ export async function setModeProfile({mode, profile}) {
4
10
  config[`${mode}Profile`] = profile;
5
11
 
12
+ if ((await isAutomaticSwitchingEnabled()) && (await isTerminalOpen())) {
13
+ const currentMode = await getCurrentMode();
14
+
15
+ if (currentMode === mode) {
16
+ await setTerminalProfile(profile);
17
+ }
18
+ }
19
+
6
20
  console.log(`${mode} mode profile set to '${profile}'`);
7
21
  }
@@ -1,10 +1,15 @@
1
- import process from 'node:process';
2
- import {setTerminalProfile, setTerminalDefaultProfile} from 'terminal-profile';
3
1
  import {config} from '../config.js';
4
- import {isTerminalOpen} from '../functions/index.js';
2
+ import {
3
+ getCurrentMode,
4
+ isAutomaticSwitchingEnabled,
5
+ isTerminalOpen,
6
+ setTerminalProfile,
7
+ } from '../functions/index.js';
5
8
 
6
9
  export async function updateProfile() {
7
- if (!(await isTerminalOpen())) return;
10
+ if (!(await isAutomaticSwitchingEnabled()) || !(await isTerminalOpen())) {
11
+ return;
12
+ }
8
13
 
9
14
  if (!config.darkProfile) {
10
15
  throw new Error('Dark profile not set');
@@ -14,11 +19,8 @@ export async function updateProfile() {
14
19
  throw new Error('Light profile not set');
15
20
  }
16
21
 
17
- const mode = process.env.DARKMODE === '1' ? 'dark' : 'light';
22
+ const mode = await getCurrentMode();
18
23
  const profile = config[`${mode}Profile`];
19
24
 
20
- await Promise.all([
21
- setTerminalProfile(profile),
22
- setTerminalDefaultProfile(profile),
23
- ]);
25
+ await setTerminalProfile(profile);
24
26
  }
package/changelog.md CHANGED
@@ -5,19 +5,40 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [v3.0.2](https://github.com/patrik-csak/terminal-profile/compare/v3.0.1...v3.0.2) – 2024-02-21
8
+ ## [5.0.0](https://github.com/patrik-csak/auto-terminal-profile/compare/v4.0.0...v5.0.0) – 2024-11-27
9
+
10
+ ### Changed
11
+
12
+ - **BREAKING**: `enable` and `set-(dark|light)-profile` now set the Terminal profile
13
+ - Updated dependencies
14
+
15
+ ## [4.0.0](https://github.com/patrik-csak/auto-terminal-profile/compare/v3.0.2...v4.0.0) – 2024-03-25
16
+
17
+ ### Added
18
+
19
+ - Documentation explaining how to automatically update Terminal profile when opening Terminal
20
+
21
+ ### Changed
22
+
23
+ - **BREAKING**: `update-profile` gets appearance mode from OS instead of from `DARKMODE` environment variable
24
+
25
+ ## [3.0.2](https://github.com/patrik-csak/auto-terminal-profile/compare/v3.0.1...v3.0.2) – 2024-02-21
9
26
 
10
27
  ### Fixed
11
28
 
12
29
  - Fix Terminal opening when closed
13
30
 
14
- ## [v3.0.1](https://github.com/patrik-csak/terminal-profile/compare/v3.0.0...v3.0.1) – 2024-02-20
31
+ ## [3.0.1](https://github.com/patrik-csak/auto-terminal-profile/compare/v3.0.0...v3.0.1) – 2024-02-20
15
32
 
16
33
  ### Fixed
17
34
 
18
35
  - Fix broken post-install compilation step
19
36
 
20
- ## [v3.0.0](https://github.com/patrik-csak/terminal-profile/compare/v2.1.0...v3.0.0) – 2024-02-19
37
+ ## [3.0.0](https://github.com/patrik-csak/auto-terminal-profile/compare/v2.1.0...v3.0.0) – 2024-02-19
38
+
39
+ ### Changed
40
+
41
+ - **BREAKING**: `update-profile` gets appearance mode from `DARKMODE` environment variable instead of from OS
21
42
 
22
43
  ### Removed
23
44
 
@@ -27,13 +48,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
27
48
 
28
49
  - Fix issue causing `auto-terminal-profile` to silently fail on macOS Sonoma
29
50
 
30
- ## [v2.1.0](https://github.com/patrik-csak/terminal-profile/compare/v2.0.0...v2.1.0) – 2023-07-04
51
+ ## [2.1.0](https://github.com/patrik-csak/auto-terminal-profile/compare/v2.0.0...v2.1.0) – 2023-07-04
31
52
 
32
53
  ### Changed
33
54
 
34
55
  - Update dependencies
35
56
 
36
- ## [2.0.0](https://github.com/patrik-csak/terminal-profile/compare/v1.1.1...v2.0.0) – 2023-05-10
57
+ ## [2.0.0](https://github.com/patrik-csak/auto-terminal-profile/compare/v1.1.1...v2.0.0) – 2023-05-10
37
58
 
38
59
  ### Added
39
60
 
package/config.js CHANGED
@@ -12,7 +12,7 @@ class Config {
12
12
  };
13
13
 
14
14
  /**
15
- * @return {string}
15
+ * @return {string|undefined}
16
16
  */
17
17
  get darkProfile() {
18
18
  return config_.get(this.keys.darkProfile);
@@ -26,7 +26,7 @@ class Config {
26
26
  }
27
27
 
28
28
  /**
29
- * @return {string}
29
+ * @return {string|undefined}
30
30
  */
31
31
  get lightProfile() {
32
32
  return config_.get(this.keys.lightProfile);
@@ -3,7 +3,7 @@ import {execa} from 'execa';
3
3
  import {launchAgentPlistFilePath} from '../constants/index.js';
4
4
 
5
5
  export async function disableAutomaticSwitching() {
6
- await execa('launchctl', ['unload', '-w', launchAgentPlistFilePath]);
6
+ await execa`launchctl unload -w ${launchAgentPlistFilePath}`;
7
7
 
8
8
  await unlink(launchAgentPlistFilePath);
9
9
  }
@@ -9,5 +9,5 @@ export async function enableAutomaticSwitching() {
9
9
  await getLaunchAgentPlistFileContents(),
10
10
  );
11
11
 
12
- await execa('launchctl', ['load', '-w', launchAgentPlistFilePath]);
12
+ await execa`launchctl load -w ${launchAgentPlistFilePath}`;
13
13
  }
@@ -0,0 +1,8 @@
1
+ import darkMode from 'dark-mode';
2
+
3
+ /**
4
+ * @returns {Promise<'dark' | 'light'>} Current macOS appearance mode
5
+ */
6
+ export async function getCurrentMode() {
7
+ return (await darkMode.isEnabled()) ? 'dark' : 'light';
8
+ }
@@ -1,5 +1,7 @@
1
1
  export {enableAutomaticSwitching} from './enable-automatic-switching.js';
2
2
  export {disableAutomaticSwitching} from './disable-automatic-switching.js';
3
+ export {getCurrentMode} from './get-current-mode.js';
3
4
  export {getLaunchAgentPlistFileContents} from './get-launch-agent-plist-file-contents.js';
4
5
  export {isAutomaticSwitchingEnabled} from './is-automatic-switching-enabled.js';
5
6
  export {isTerminalOpen} from './is-terminal-open.js';
7
+ export {setTerminalProfile} from './set-terminal-profile.js';
@@ -6,7 +6,7 @@ import {launchAgentPlistFilePath} from '../constants/index.js';
6
6
  * @return {Promise<boolean>}
7
7
  */
8
8
  async function isDarkModeNotifyRunning() {
9
- const {stdout} = await execa('launchctl', ['list']);
9
+ const {stdout} = await execa`launchctl list`;
10
10
 
11
11
  return stdout.includes('ke.bou.dark-mode-notify');
12
12
  }
@@ -0,0 +1,11 @@
1
+ import {
2
+ setTerminalDefaultProfile as setDefaultProfile,
3
+ setTerminalProfile as setProfile,
4
+ } from 'terminal-profile';
5
+
6
+ /**
7
+ * @param {string} profile
8
+ */
9
+ export async function setTerminalProfile(profile) {
10
+ await Promise.all([setDefaultProfile(profile), setProfile(profile)]);
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-terminal-profile",
3
- "version": "3.0.2",
3
+ "version": "5.0.0",
4
4
  "description": "Automatically switch macOS Terminal profile when system dark / light mode changes",
5
5
  "keywords": [
6
6
  "macos",
@@ -10,6 +10,7 @@
10
10
  "profile"
11
11
  ],
12
12
  "repository": "github:patrik-csak/auto-terminal-profile",
13
+ "funding": "https://buymeacoffee.com/patrikcsak",
13
14
  "license": "MIT",
14
15
  "author": "Patrik Csak <p@trikcsak.com> (https://patrikcsak.com)",
15
16
  "type": "module",
@@ -21,9 +22,10 @@
21
22
  },
22
23
  "dependencies": {
23
24
  "commander": "^12.0.0",
24
- "conf": "^12.0.0",
25
+ "conf": "^13.0.1",
26
+ "dark-mode": "^4.0.0",
25
27
  "env-paths": "^3.0.0",
26
- "execa": "^8.0.1",
28
+ "execa": "^9.5.1",
27
29
  "pupa": "^3.1.0",
28
30
  "read-package-up": "^11.0.0",
29
31
  "run-applescript": "^7.0.0",
@@ -32,7 +34,7 @@
32
34
  },
33
35
  "devDependencies": {
34
36
  "sort-package-json": "^2.7.0",
35
- "xo": "^0.57.0"
37
+ "xo": "^0.59.3"
36
38
  },
37
39
  "engines": {
38
40
  "node": ">=18 <=20"
package/readme.md CHANGED
@@ -1,35 +1,51 @@
1
- # `auto-terminal-profile`
1
+ # Auto Terminal Profile
2
2
 
3
- Automatically switch the macOS Terminal profile based on the system-wide dark / light appearance mode
3
+ Automatically switch [macOS Terminal](https://en.wikipedia.org/wiki/Terminal_%28macOS%29) profiles when the [dark / light appearance mode](https://support.apple.com/guide/mac-help/use-a-light-or-dark-appearance-mchl52e1c2d2/mac) changes
4
4
 
5
5
  ![auto-terminal-profile demonstration screen recording](./documentation/demo.gif)
6
6
 
7
7
  ## Prerequisites
8
8
 
9
- - [Node.js](https://nodejs.org/en/) 18–20
9
+ - [Node.js](https://nodejs.org/) 18–20
10
10
 
11
11
  ## Installation
12
12
 
13
- ```sh
13
+ ```shell
14
14
  npm install --global auto-terminal-profile
15
15
  ```
16
16
 
17
17
  ## Usage
18
18
 
19
+ ### Enable automatic profile switching
20
+
19
21
  To get started, enable automatic profile switching and set your preferred dark and light mode profiles:
20
22
 
21
- ```sh
23
+ ```shell
22
24
  auto-terminal-profile enable \
23
25
  --dark-profile='One Dark' \
24
26
  --light-profile='One Light'
25
27
  ```
26
28
 
27
- For more information view the help output:
29
+ ### Switch profile on Terminal startup
28
30
 
29
- ```sh
30
- auto-terminal-profile --help
31
+ Auto Terminal Profile only runs if Terminal is running, so the profile can fall out of sync if the macOS appearance mode changes while Terminal isn&rsquo;t running.
32
+
33
+ To sync the Terminal profile to the current macOS appearance mode once:
34
+
35
+ ```shell
36
+ auto-terminal-profile update-profile
37
+ ```
38
+
39
+ To sync the Terminal profile to the current macOS appearance mode when Terminal app is opened, you can add that line to your shell startup script (e.g. `.zshrc`), but it will increase the startup time of new shell sessions.
40
+
41
+ ### Disable automatic profile switching
42
+
43
+ ```shell
44
+ auto-terminal-profile disable
31
45
  ```
32
46
 
33
- ## Acknowledgements
47
+ ### Help
34
48
 
35
- Thanks [Bouke van der Bijl](https://bou.ke) for his project [dark-mode-notify](https://github.com/bouk/dark-mode-notify), which this project depends on
49
+ ```shell
50
+ auto-terminal-profile --help
51
+ ```