Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 3, 2021
1 parent e870356 commit 82a4726
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 48 deletions.
4 changes: 1 addition & 3 deletions .gitattributes
@@ -1,3 +1 @@
* text=auto
*.js text eol=lf
dark-mode binary
* text=auto eol=lf
9 changes: 2 additions & 7 deletions .github/workflows/main.yml
Expand Up @@ -10,15 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 6
- 4
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
33 changes: 24 additions & 9 deletions index.js
@@ -1,17 +1,32 @@
'use strict';
const runJxa = require('run-jxa');
import {runJxa} from 'run-jxa';

const prop = `Application('System Events').appearancePreferences.darkMode`;
const property = 'Application(\'System Events\').appearancePreferences.darkMode';

exports.enable = () => runJxa(`${prop} = true`);
exports.disable = () => runJxa(`${prop} = false`);
const darkMode = {};

exports.toggle = force => {
darkMode.enable = async () => {
await runJxa(`${property} = true`);
};

darkMode.disable = async () => {
await runJxa(`${property} = false`);
};

darkMode.toggle = async force => {
if (typeof force === 'boolean') {
return force ? exports.enable() : exports.disable();
// eslint-disable-next-line unicorn/prefer-ternary
if (force) {
await darkMode.enable();
} else {
await darkMode.disable();
}

return;
}

return runJxa(`${prop} = !${prop}()`);
await runJxa(`${property} = !${property}()`);
};

exports.isDark = () => runJxa(`return ${prop}()`);
darkMode.isDark = async () => runJxa(`return ${property}()`);

export default darkMode;
2 changes: 1 addition & 1 deletion license
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
17 changes: 11 additions & 6 deletions package.json
Expand Up @@ -4,13 +4,16 @@
"description": "Control the macOS dark mode",
"license": "MIT",
"repository": "sindresorhus/node-dark-mode",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=4"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -23,13 +26,15 @@
"dark",
"mode",
"control",
"toggle"
"toggle",
"automate",
"automation"
],
"dependencies": {
"run-jxa": "^1.0.0"
"run-jxa": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^3.15.0",
"xo": "^0.46.3"
}
}
16 changes: 7 additions & 9 deletions readme.md
Expand Up @@ -8,22 +8,20 @@

## Install

```
$ npm install dark-mode
```sh
npm install dark-mode
```

## Usage

```js
const darkMode = require('dark-mode');
import darkMode from 'dark-mode';

darkMode.enable().then(() => {
console.log('Enabled dark mode');
});
await darkMode.enable();
console.log('Enabled dark mode');

darkMode.toggle().then(() => {
console.log('Toggled between dark and light mode');
});
await darkMode.toggle();
console.log('Toggled between dark and light mode');
```

## API
Expand Down
26 changes: 13 additions & 13 deletions test.js
@@ -1,27 +1,27 @@
import test from 'ava';
import m from '.';
import darkMode from './index.js';

let isDarkInitially;

test.before(async () => {
isDarkInitially = await m.isDark();
isDarkInitially = await darkMode.isDark();
});

test.after(async () => {
await m.toggle(isDarkInitially);
await darkMode.toggle(isDarkInitially);
});

test(async t => {
const dark = await m.isDark();
await m.toggle();
t.not(dark, await m.isDark());
test('main', async t => {
const dark = await darkMode.isDark();
await darkMode.toggle();
t.not(dark, await darkMode.isDark());

await m.disable();
t.false(await m.isDark());
await darkMode.disable();
t.false(await darkMode.isDark());

await m.enable();
t.true(await m.isDark());
await darkMode.enable();
t.true(await darkMode.isDark());

await m.toggle(false);
t.false(await m.isDark());
await darkMode.toggle(false);
t.false(await darkMode.isDark());
});

0 comments on commit 82a4726

Please sign in to comment.