Skip to content

Commit

Permalink
Feat: better reporting (#11)
Browse files Browse the repository at this point in the history
* add better error and success reporting

* refactoring

* update version
  • Loading branch information
cuth authored Jul 2, 2017
1 parent fe0e4df commit 932610c
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 318 deletions.
3 changes: 1 addition & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
Expand Down
111 changes: 42 additions & 69 deletions cli.js
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);
});
}
}
118 changes: 118 additions & 0 deletions lib/args.js
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
};
7 changes: 0 additions & 7 deletions lib/colors.js

This file was deleted.

16 changes: 16 additions & 0 deletions lib/compose.js
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;
};
2 changes: 1 addition & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
dir: '.'
dir: '.'
};
11 changes: 0 additions & 11 deletions lib/figures.js

This file was deleted.

10 changes: 7 additions & 3 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ module.exports = `
-w, --width {Number}
Set the width of desired download.
Set the width of desired image.
example:
$ unsplash-wallpaper --width 2880 --save-config
-h, --height {Number}
Set the height of desired download.
Set the height of desired image.
example:
$ unsplash-wallpaper --width 2880 --height 1800 --save-config
-d, --dir {String} or "."
Expand All @@ -54,6 +58,7 @@ module.exports = `
Get a specific image by the photo ID.
example:
$ unsplash-wallpaper -p WLUHO9A_xik
$ unsplash-wallpaper --photo="-oWyJoSqBRM"
-c, --category {CATEGORY NAME}
Expand Down Expand Up @@ -86,5 +91,4 @@ module.exports = `
$ unsplash-wallpaper -q nature,water
-v, --version
--help
`;
Loading

0 comments on commit 932610c

Please sign in to comment.