Skip to content

Instantly share code, notes, and snippets.

@blakehaswell
Created January 20, 2014 05:34
Show Gist options
  • Save blakehaswell/8515343 to your computer and use it in GitHub Desktop.
Save blakehaswell/8515343 to your computer and use it in GitHub Desktop.
Little Node script which gets a list of teams from a CSV file, then copies and renames icons for those teams.
var q = require('q');
var copy = q.denodeify(require('fs-extra').copy);
var exists = require('fs-extra').existsSync;
var csv = require('csv');
var reduce = require('underscore').reduce;
function getTeams() {
var deferred = q.defer();
var teams = [];
csv()
.from('./teams.csv', { columns: ['sport', 'id', 'name', 'shortName'] })
.transform(function (row, index) {
teams.push(transformRow(row));
})
.on('end', function () {
deferred.resolve(teams);
});
return deferred.promise;
}
function transformRow(row) {
return {
id: row.id,
sport: row.sport.toLowerCase(),
shortName: row.shortName.toLowerCase()
};
}
function copyTeamIcons(teams) {
reduce(teams, function (copyTeamIcon, team) {
return copyTeamIcon.then(function () {
var src = './orig/' + team.sport + '/' + team.shortName + '.png';
var dest = './result/' + team.sport + '/' + team.id + '.png';
if (!exists(src)) {
return q();
}
console.log('Copying icon for team: ' + team.shortName);
return copy(src, dest);
});
}, q());
}
getTeams().then(copyTeamIcons);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment