-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add better error and success reporting * refactoring * update version
- Loading branch information
Showing
13 changed files
with
459 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,60 @@ | ||
#! /usr/bin/env node | ||
|
||
const lib = require('./lib'); | ||
const wallpaper = require('wallpaper'); | ||
const minimist = require('minimist'); | ||
const lib = require('./lib'); | ||
const { boolOptions, minimistAliases, triggerDownload } = require('./lib/args'); | ||
const { log, warn, inform, fail, success } = require('./lib/log'); | ||
const help = require('./lib/help'); | ||
const colors = require('./lib/colors'); | ||
const figures = require('./lib/figures'); | ||
const version = require('./package.json').version; | ||
const reporter = require('./lib/progress-reporter'); | ||
const argv = require('minimist')(process.argv.slice(2), { | ||
boolean: ['help', 'save-config', 'version'], | ||
alias: { | ||
w: 'width', | ||
h: 'height', | ||
d: 'dir', | ||
s: 'save-config', | ||
p: 'photo', | ||
c: 'category', | ||
u: 'user', | ||
l: 'likes', | ||
o: 'collection', | ||
q: 'search', | ||
v: 'version' | ||
} | ||
const version = require('./package.json').version; | ||
|
||
const argv = minimist(process.argv.slice(2), { | ||
boolean: boolOptions, | ||
alias: minimistAliases | ||
}); | ||
|
||
// --help | ||
if (argv.help) { | ||
console.log(help); | ||
log(help); | ||
} | ||
|
||
// --version | ||
if (argv.version) { | ||
console.log('version', version); | ||
log(`version ${version}`); | ||
} | ||
|
||
const options = lib.sanitizeArgs(argv); | ||
const shouldSave = options['save-config']; | ||
const shouldDownload = [ | ||
'photo', | ||
'category', | ||
'user', | ||
'likes', | ||
'collection', | ||
'search', | ||
'random', | ||
'daily', | ||
'weekly', | ||
'featured' | ||
].some(option => options[option] !== undefined); | ||
const args = lib.sanitizeArgs(argv, warn); | ||
const shouldSave = args['save-config']; | ||
const shouldDownload = triggerDownload.some(trigger => args[trigger]); | ||
|
||
if (shouldSave || shouldDownload) { | ||
const promise = lib.readConfig(options); | ||
|
||
if (shouldSave) { | ||
promise.then(opts => lib.saveConfig(opts)) | ||
.catch(error => { | ||
if (error && error.message) { | ||
console.log(colors.magenta(`${figures.cross} ${error.message}`)); | ||
} | ||
}); | ||
} | ||
|
||
if (shouldDownload) { | ||
promise.then(opts => { | ||
const url = lib.createUrl(opts); | ||
|
||
console.log(colors.yellow(`Request ${url}`)); | ||
|
||
return lib.download(opts, url, reporter) | ||
.then(filename => { | ||
console.log(colors.green(`${figures.tick} Image saved to ${filename}`)); | ||
return wallpaper.set(filename); | ||
}) | ||
.then(() => { | ||
console.log('Check it out.'); | ||
}) | ||
.catch(error => { | ||
if (error && error.message) { | ||
console.log(colors.red(`${figures.cross} ${error.message}`)); | ||
} | ||
}); | ||
}); | ||
} | ||
} else if (!argv.help && !argv.version) { | ||
console.log(help); | ||
const promise = lib.readConfig(args); | ||
|
||
if (shouldSave) { | ||
promise | ||
.then(opts => lib.saveConfig(opts)) | ||
.then(config => { | ||
success(`Saved config as:`); | ||
inform(config); | ||
}) | ||
.catch(fail); | ||
} | ||
|
||
if (shouldDownload) { | ||
promise.then(opts => { | ||
const url = lib.createUrl(opts); | ||
|
||
inform(`Request ${url}`); | ||
|
||
return lib | ||
.download(opts, url, reporter) | ||
.then(filename => { | ||
success(`Image saved to ${filename}`); | ||
return wallpaper.set(filename); | ||
}) | ||
.then(() => log('Check it out.')) | ||
.catch(fail); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
const os = require('os'); | ||
const path = require('path'); | ||
|
||
const stringType = arg => typeof arg === 'string'; | ||
const numberType = arg => typeof arg === 'number'; | ||
const boolType = arg => typeof arg === 'boolean'; | ||
const boolOrNumber = arg => boolType(arg) || numberType(arg); | ||
const stringOrNumber = arg => stringType(arg) || numberType(arg); | ||
|
||
// resolve special characters in the dir path | ||
const resolveDir = dir => { | ||
if (dir.startsWith('.') && dir.length > 1) { | ||
return path.join(process.cwd(), dir); | ||
} | ||
if (dir.startsWith('~')) { | ||
return path.join(os.homedir(), dir.substr(1)); | ||
} | ||
return dir; | ||
}; | ||
|
||
const options = { | ||
width: { | ||
alias: 'w', | ||
test: boolOrNumber, | ||
error: 'must be a number (ex: 1920)' | ||
}, | ||
height: { | ||
alias: 'h', | ||
test: boolOrNumber, | ||
error: 'must be a number (ex: 1200)' | ||
}, | ||
dir: { | ||
alias: 'd', | ||
test: stringType, | ||
error: 'must be a valid directory path', | ||
transform: resolveDir | ||
}, | ||
'save-config': { | ||
alias: 's', | ||
bool: true | ||
}, | ||
photo: { | ||
alias: 'p', | ||
test: stringType, | ||
error: | ||
'must be a string. If the photo id begins with a hyphen, try wrapping it in quotes.\n\nexample:\n$ unsplash-wallpaper --photo="-oWyJoSqBRM"\n', | ||
triggerDownload: true | ||
}, | ||
category: { | ||
alias: 'c', | ||
test: stringType, | ||
error: 'must be a string', | ||
triggerDownload: true | ||
}, | ||
user: { | ||
alias: 'u', | ||
test: stringType, | ||
error: 'must be a string', | ||
triggerDownload: true | ||
}, | ||
likes: { | ||
alias: 'l', | ||
test: (arg, args) => | ||
stringType(args.user) ? boolType(arg) : stringType(arg), | ||
error: 'must be a string or left blank when user contains a string', | ||
triggerDownload: true | ||
}, | ||
collection: { | ||
alias: 'o', | ||
test: stringOrNumber, | ||
error: 'must be a string or number', | ||
triggerDownload: true | ||
}, | ||
search: { | ||
alias: 'q', | ||
test: stringOrNumber, | ||
error: 'must be a string or number', | ||
transform: str => str.replace(/\s/g, ','), | ||
triggerDownload: true | ||
}, | ||
version: { | ||
alias: 'v', | ||
bool: true | ||
}, | ||
help: { | ||
bool: true | ||
} | ||
}; | ||
const commands = ['random', 'daily', 'weekly', 'featured']; | ||
|
||
const objToArr = obj => Reflect.ownKeys(obj).map(k => [k, obj[k]]); | ||
const optionsArr = objToArr(options); | ||
const aliases = optionsArr | ||
.map(([key, obj]) => obj.alias) | ||
.filter(alias => alias); | ||
const boolOptions = optionsArr | ||
.filter(([key, obj]) => obj.bool) | ||
.map(([key]) => key); | ||
const minimistAliases = optionsArr.reduce((acc, [key, obj]) => { | ||
if (obj.alias) { | ||
acc[obj.alias] = key; | ||
} | ||
return acc; | ||
}, {}); | ||
const triggerDownload = optionsArr | ||
.filter(([key, obj]) => obj.triggerDownload) | ||
.map(([key]) => key) | ||
.concat(commands); | ||
|
||
module.exports = { | ||
resolveDir, | ||
options, | ||
commands, | ||
aliases, | ||
boolOptions, | ||
minimistAliases, | ||
triggerDownload | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
exports.filter = fn => arr => Array.prototype.filter.call(arr, fn); | ||
exports.map = fn => arr => Array.prototype.map.call(arr, fn); | ||
exports.reduce = (fn, acc) => arr => Array.prototype.reduce.call(arr, fn, acc); | ||
exports.some = fn => arr => Array.prototype.some.call(arr, fn); | ||
exports.every = fn => arr => Array.prototype.every.call(arr, fn); | ||
exports.find = fn => arr => Array.prototype.find.call(arr, fn); | ||
|
||
exports.compose = (...fns) => data => | ||
exports.reduce((acc, fn) => fn(acc), data)(fns); | ||
exports.mergeObjects = arr => | ||
arr.length === 0 ? {} : Object.assign({}, ...arr); | ||
|
||
exports.print = data => { | ||
console.log(data); | ||
return data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = { | ||
dir: '.' | ||
dir: '.' | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.