Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 29, 2018
1 parent 9296fd7 commit 16bc00f
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 127 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -3,4 +3,3 @@ language: node_js
node_js:
- '10'
- '8'
- '6'
2 changes: 1 addition & 1 deletion appveyor.yml
@@ -1,6 +1,6 @@
environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '10'
install:
- ps: Install-Product node $env:nodejs_version
- npm install --global npm@latest
Expand Down
145 changes: 64 additions & 81 deletions lib/linux.js
@@ -1,12 +1,13 @@
'use strict';
const util = require('util');
const path = require('path');
const homeDir = require('os').homedir();
const childProcess = require('child_process');
const fileSystem = require('fs');
const pify = require('pify');
const fs = require('fs');

const cp = pify(childProcess);
const fs = pify(fileSystem);
const execFile = util.promisify(childProcess.execFile);
const exec = util.promisify(childProcess.exec);
const readFile = util.promisify(fs.readFile);

const appsList = [
{
Expand Down Expand Up @@ -111,7 +112,7 @@ const appsList = [

let availableApps;

function setAvailableApps() {
async function setAvailableApps() {
const availableAppsDict = {};

availableApps = [];
Expand All @@ -124,104 +125,92 @@ function setAvailableApps() {
// `which` all commands and expect stdout to return a positive
const whichCmd = `which -a ${names.join('; which -a ')}`;

return cp.exec(`echo $(${whichCmd})`).then(stdout => {
stdout = stdout.trim();
let {stdout} = await exec(`echo $(${whichCmd})`);
stdout = stdout.trim();

if (!stdout) {
throw new Error('None of the apps were found');
}
if (!stdout) {
throw new Error('None of the apps were found');
}

// It may return aliases so we only want the real path
// and only the executable name. i.e. 'foo' from /bin/foo
stdout = stdout.split('\n');
// It may return aliases so we only want the real path
// and only the executable name. i.e. 'foo' from /bin/foo
const lines = stdout.split('\n');

stdout.forEach(el => {
// It's an alias
if (el[0] !== path.sep) {
return;
}
for (let line of lines) {
// It's an alias
if (line[0] !== path.sep) {
return;
}

el = el.split(path.sep).pop();
line = line.split(path.sep).pop();

availableApps.push(availableAppsDict[el]);
});
});
availableApps.push(availableAppsDict[line]);
}
}

function isCinnamon() {
async function isCinnamon() {
const args = ['writable', 'org.cinnamon.desktop.background', 'picture-uri'];

return cp.execFile('gsettings', args)
.then(out => out.trim() === 'true')
.catch(() => {});
try {
const {stdout} = await execFile('gsettings', args);
return stdout.trim() === 'true';
} catch (_) {}
}

function isMATE() {
async function isMATE() {
const args = ['writable', 'org.mate.background', 'picture-filename'];

return cp.execFile('gsettings', args)
.then(out => out.trim() === 'true')
.catch(() => {});
try {
const {stdout} = await execFile('gsettings', args);
return stdout.trim() === 'true';
} catch (_) {}
}

exports.get = function get() {
exports.get = async function get() {
if (!availableApps) {
return setAvailableApps().then(get);
await setAvailableApps();
return get();
}

const app = availableApps.find(app => app.get);

if (app.cmd === 'nitrogen') {
const configFile = path.join(homeDir, '.config/nitrogen/bg-saved.cfg');
return fs.readFile(configFile, 'utf8').then(config =>
config.trim().split('\n').find(line => line.startsWith('file=')).replace('file=', '')
);
const config = await readFile(configFile, 'utf8');
return config.trim().split('\n').find(line => line.startsWith('file=')).replace('file=', '');
}

if (app.cmd === 'gsettings') {
const getArgs = app.get.slice();
return isCinnamon()
.then(cinnamon => {
if (cinnamon) {
getArgs[1] = 'org.cinnamon.desktop.background';
}
})
.then(() => isMATE())
.then(mate => {
if (mate) {
getArgs.splice(1, 2, 'org.mate.background', 'picture-filename');
}
})
.then(() => cp.execFile(app.cmd, getArgs))
.then(stdout => {
stdout = stdout.trim();
const getArgs = app.get.slice();

if (typeof app.transform === 'function') {
return app.transform(stdout);
}
if (app.cmd === 'gsettings') {
if (await isCinnamon()) {
getArgs[1] = 'org.cinnamon.desktop.background';
}

return stdout;
});
if (await isMATE()) {
getArgs.splice(1, 2, 'org.mate.background', 'picture-filename');
}
}

return cp.execFile(app.cmd, app.get).then(stdout => {
stdout = stdout.trim();
let {stdout} = await execFile(app.cmd, getArgs);
stdout = stdout.trim();

if (typeof app.transform === 'function') {
return app.transform(stdout);
}
if (typeof app.transform === 'function') {
return app.transform(stdout);
}

return stdout;
});
return stdout;
};

exports.set = function set(imagePath) {
exports.set = async function set(imagePath) {
if (typeof imagePath !== 'string') {
return Promise.reject(new TypeError('Expected a string'));
throw new TypeError('Expected a string');
}

if (!availableApps) {
return setAvailableApps().then(() => set(imagePath));
await setAvailableApps();
await set(imagePath);
return;
}

const app = availableApps.find(app => app.set);
Expand All @@ -230,20 +219,14 @@ exports.set = function set(imagePath) {
params[params.length - 1] = params[params.length - 1].replace('%s', path.resolve(imagePath));

if (app.cmd === 'gsettings') {
return isCinnamon()
.then(cinnamon => {
if (cinnamon) {
params[1] = 'org.cinnamon.desktop.background';
}
})
.then(() => isMATE())
.then(mate => {
if (mate) {
params.splice(1, 3, 'org.mate.background', 'picture-filename', params[3].replace(/^file:\/\//, ''));
}
})
.then(() => cp.execFile(app.cmd, params));
if (await isCinnamon()) {
params[1] = 'org.cinnamon.desktop.background';
}

if (await isMATE()) {
params.splice(1, 3, 'org.mate.background', 'picture-filename', params[3].replace(/^file:\/\//, ''));
}
}

return cp.execFile(app.cmd, params);
await execFile(app.cmd, params);
};
42 changes: 24 additions & 18 deletions lib/macos.js
@@ -1,31 +1,37 @@
'use strict';
const util = require('util');
const path = require('path');
const childProcess = require('child_process');
const pify = require('pify');

const execFile = pify(childProcess.execFile);
const execFile = util.promisify(childProcess.execFile);

// Binary source → https://github.com/sindresorhus/macos-wallpaper
const bin = path.join(__dirname, 'macos-wallpaper');

exports.get = () => execFile(bin, ['get']).then(x => x.trim());
exports.get = async () => {
const {stdout} = await execFile(bin, ['get']);
return stdout.trim();
};

exports.set = (imagePath, opts) => {
exports.set = async (imagePath, options) => {
if (typeof imagePath !== 'string') {
return Promise.reject(new TypeError('Expected a string'));
}

opts = opts || {};

const args = ['set', path.resolve(imagePath)];

if (opts.screen) {
args.push('--screen', opts.screen);
}

if (opts.scale) {
args.push('--scale', opts.scale);
throw new TypeError('Expected a string');
}

return execFile(bin, args);
options = {
screen: 'all',
scale: 'auto',
...options
};

const args = [
'set',
path.resolve(imagePath),
'--screen',
options.screen,
'--scale',
options.scale
];

await execFile(bin, args);
};
15 changes: 9 additions & 6 deletions lib/win.js
@@ -1,19 +1,22 @@
'use strict';
const util = require('util');
const path = require('path');
const childProcess = require('child_process');
const pify = require('pify');

const execFile = pify(childProcess.execFile);
const execFile = util.promisify(childProcess.execFile);

// Binary source → https://github.com/sindresorhus/win-wallpaper
const bin = path.join(__dirname, 'win-wallpaper.exe');

exports.get = () => execFile(bin).then(x => x.trim());
exports.get = async () => {
const {stdout} = await execFile(bin);
return stdout.trim();
};

exports.set = imagePath => {
exports.set = async imagePath => {
if (typeof imagePath !== 'string') {
return Promise.reject(new TypeError('Expected a string'));
throw new TypeError('Expected a string');
}

return execFile(bin, [path.resolve(imagePath)]);
await execFile(bin, [path.resolve(imagePath)]);
};
11 changes: 4 additions & 7 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "wallpaper",
"version": "3.0.0",
"description": "Get or set the desktop wallpaper",
"description": "Manage the desktop wallpaper",
"license": "MIT",
"repository": "sindresorhus/wallpaper",
"author": {
Expand All @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -30,11 +30,8 @@
"picture",
"photo"
],
"dependencies": {
"pify": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^0.25.0",
"xo": "^0.23.0"
}
}
11 changes: 5 additions & 6 deletions readme.md
Expand Up @@ -17,14 +17,12 @@ $ npm install wallpaper
```js
const wallpaper = require('wallpaper');

wallpaper.set('unicorn.jpg').then(() => {
console.log('done');
});
(async () => {
await wallpaper.set('unicorn.jpg');

wallpaper.get().then(imagePath => {
console.log(imagePath);
await wallpaper.get();
//=> '/Users/sindresorhus/unicorn.jpg'
});
})();
```


Expand Down Expand Up @@ -84,6 +82,7 @@ Scaling method.
- [wallpaper-cli](https://github.com/sindresorhus/wallpaper-cli) - CLI for this module
- [macos-wallpaper](https://github.com/sindresorhus/macos-wallpaper) - macOS binary used in this module
- [win-wallpaper](https://github.com/sindresorhus/win-wallpaper) - Windows binary used in this module
- [trash](https://github.com/sindresorhus/trash) - Move files and directories to the trash


## License
Expand Down
10 changes: 5 additions & 5 deletions test.js
@@ -1,12 +1,12 @@
import path from 'path';
import test from 'ava';
import m from '.';
import wallpaper from '.';

test('main', async t => {
const orignalImagePath = await m.get();
const orignalImagePath = await wallpaper.get();

await m.set('fixture.jpg');
t.is(await m.get(), path.resolve('fixture.jpg'));
await wallpaper.set('fixture.jpg');
t.is(await wallpaper.get(), path.resolve('fixture.jpg'));

await m.set(orignalImagePath);
await wallpaper.set(orignalImagePath);
});

0 comments on commit 16bc00f

Please sign in to comment.