Skip to content

Commit

Permalink
Added chalk UI logging, bumped version.
Browse files Browse the repository at this point in the history
  • Loading branch information
dconnolly committed Feb 24, 2014
1 parent 51f85f0 commit f871ed0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 55 deletions.
37 changes: 22 additions & 15 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Q = require('q');
var fs = require('fs');
var request = require('request');
var nopt = require('nopt');
var chalk = require('chalk');
var getChromecastBackgrounds = require('./index');

var read = Q.denodeify(fs.readFile);
Expand Down Expand Up @@ -33,13 +34,14 @@ var writeInlineMarkdown = function(filename, backgrounds) {

var updateSize = function(sizeString, backgrounds) {
var sizeRegex = /\/s\d+.*?\//;
_(backgrounds).each(function(backgroundEntry) {
_.each(backgrounds, function(backgroundEntry) {
backgroundEntry.url = backgroundEntry.url.replace(sizeRegex, '/'+sizeString+'/');
});
};

var downloadImages = function(backgrounds, directory) {
var promises = [];
console.log(backgrounds);
fs.existsSync(directory) || fs.mkdirSync(directory);
_.each(backgrounds, function(backgroundEntry) {
var deferred = Q.defer();
Expand All @@ -48,13 +50,11 @@ var downloadImages = function(backgrounds, directory) {
request(backgroundEntry.url)
.pipe(fs.createWriteStream(filename))
.on('close', function() {
console.log(filename);
console.log(chalk.grey(filename));
deferred.resolve();
});
});
Q.all(promises).done(function() {
console.log('All done!');
});
return Q.all(promises);
};

var options = nopt({
Expand All @@ -71,35 +71,42 @@ var options = nopt({
});

if (options.help) {
console.log('chromecast-backgrounds --download=<directory> --size=<size> --save=<file> --writemd=<file>');
console.log(chalk.yellow('chromecast-backgrounds --download=<directory> --size=<size> --save=<file> --writemd=<file>'));
return;
}

console.log(chalk.underline('Parsing Chromecast Home...\n'));

getChromecastBackgrounds().then(function(backgrounds) {
if (options.size) {
console.log('Updating sizes to ', options.size);
backgrounds = updateSize(options.size, backgrounds);
console.log(chalk.underline('Updating sizes to', options.size));
updateSize(options.size, backgrounds);
}
if (options.load) {
console.log('Loading previous backgrounds from ', options.load);
console.log(chalk.underline('Loading previous backgrounds from', options.load));
var backgroundsFromJSON;
read(options.load, 'utf8').then(function(backgroundsJSON) {
backgroundsFromJSON = JSON.parse(backgroundsJSON);
backgrounds = _.uniq(_.union(backgrounds, backgroundsFromJSON), 'url');
});
}
if (options.save) {
console.log('Writing backgrounds JSON to ', options.save);
console.log(chalk.underline('Writing backgrounds JSON to', options.save));
saveObjectToFile(options.save, backgrounds);
}
if (options.writemd) {
console.log('Writing backgrounds as inline markdown to ', options.writemd);
console.log(chalk.underline('Writing backgrounds as inline markdown to', options.writemd));
writeInlineMarkdown(options.writemd, backgrounds);
}
if (options.download) {
downloadImages(backgrounds, options.download);
}
if (options.verbose) {
console.log(backgrounds);
console.log(chalk.grey(JSON.stringify(backgrounds)));
}
if (options.download) {
console.log(chalk.underline('Downloading background images...\n'));
downloadImages(backgrounds, options.download).done(function() {
console.log(chalk.green('\n✓ Done!'));
});
} else {
console.log(chalk.green('\n✓ Done!'));
}
});
82 changes: 42 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
{
"name": "chromecast-backgrounds",
"version": "0.1.0",
"description": "Get all those beautiful backgrounds images from the Chromecast home view.",
"license": "MIT",
"bin": {
"chromecast-backgrounds": "cli.js"
},
"repository": "dconnolly/chromecast-backgrounds",
"author": {
"name": "Deirdre Connolly",
"email": "[email protected]"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {},
"files": [
"index.js",
"cli.js"
],
"keywords": [
"chromecast",
"backgrounds",
"images",
"chrome"
],
"devDependencies": {
"grunt-contrib-jshint": "~0.7.0",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt-contrib-watch": "~0.5.0",
"load-grunt-tasks": "~0.2.0",
"time-grunt": "~0.2.0",
"jshint-stylish": "~0.1.3"
},
"dependencies": {
"request": "~2.33.0",
"lodash": "~2.4.1",
"q": "~1.0.0",
"nopt": "~2.2.0"
}
"name": "chromecast-backgrounds",
"version": "0.2.0",
"description": "Get all those beautiful backgrounds images from the Chromecast home view.",
"license": "MIT",
"bin": {
"chromecast-backgrounds": "cli.js"
},
"repository": "dconnolly/chromecast-backgrounds",
"author": {
"name": "Deirdre Connolly",
"email": "[email protected]"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {},
"files": [
"index.js",
"cli.js"
],
"keywords": [
"chromecast",
"backgrounds",
"images",
"chrome",
"cli"
],
"devDependencies": {
"grunt-contrib-jshint": "~0.7.0",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt-contrib-watch": "~0.5.0",
"load-grunt-tasks": "~0.2.0",
"time-grunt": "~0.2.0",
"jshint-stylish": "~0.1.3"
},
"dependencies": {
"request": "~2.33.0",
"lodash": "~2.4.1",
"q": "~1.0.0",
"nopt": "~2.2.0",
"chalk": "~0.4.0"
}
}

0 comments on commit f871ed0

Please sign in to comment.