-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·63 lines (55 loc) · 1.45 KB
/
cli.js
File metadata and controls
executable file
·63 lines (55 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import {pipeline} from 'node:stream/promises';
import meow from 'meow';
import {
getWallpaper,
setWallpaper,
setSolidColorWallpaper,
} from 'wallpaper';
import isUrl from 'is-url-superb';
import {withHttpError, withTimeout} from 'fetch-extras';
import tempfile from 'tempfile';
import isRGBHex from './utilities/is-rgb-hex.js';
const cli = meow(`
Usage
$ wallpaper [file|url|color]
Options
--scale Scaling method: auto fill fit stretch center [Default: auto]
Only available on macOS
Examples
$ wallpaper unicorn.jpg
$ wallpaper https://octodex.github.com/images/dojocat.jpg
$ wallpaper '#ff69b4'
$ wallpaper
/Users/sindresorhus/unicorn.jpg
$ wallpaper codercat.jpg --scale=fit
`, {
importMeta: import.meta,
flags: {
// TODO: Use the `choices` option in `meow` when it supports that.
scale: {
type: 'string',
},
},
});
const input = cli.input[0];
if (input) {
if (isUrl(input)) {
const file = tempfile({extension: path.extname(input).slice(1)});
const fetchWithExtras = withTimeout(withHttpError(fetch), 30_000);
const response = await fetchWithExtras(input);
await pipeline(
response.body,
fs.createWriteStream(file),
);
await setWallpaper(file, cli.flags);
} else if (isRGBHex(input)) {
await setSolidColorWallpaper(input);
} else {
await setWallpaper(input, cli.flags);
}
} else {
console.log(await getWallpaper());
}