Created
November 16, 2014 20:35
-
-
Save adnasa/5e84fcfa3ae8463f0a30 to your computer and use it in GitHub Desktop.
Quick play with nodejs fs module
This file contains hidden or 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
var fs = require('fs'); | |
var _ = require('underscore')._; | |
var artists = [{ | |
name: 'Raphaeel Saadiq', | |
album: [ | |
'Instant Vintage' | |
] | |
}, { | |
name: 'Dr Dre', | |
album: [ | |
'The Chronic' | |
] | |
}]; | |
var getHeadlineString = function(name) { | |
var k = "="; | |
var string = ""; | |
length = name.length; | |
for (var i = 0; i < (length+2); i++) { | |
string += k; | |
}; | |
return string; | |
} | |
var artistAlbumsToMarkdown = function() { | |
fs.readdir('discographies', function(error) { | |
if (error) { | |
fs.mkdir('discographies', function() { | |
artistAlbumsToMarkdown(); | |
}); | |
} else { | |
var fileContent = ""; | |
_.each(artists, function(artist, index) { | |
var artistString = ""; | |
artistString += artist.name + "\n" + getHeadlineString(artist.name) + "\n"; | |
_.each(artist.album, function(album, index) { | |
var count = (index+1); | |
var albumString = "\n" + count + " - " + album; | |
artistString += albumString; | |
}); | |
artistString += "\n\n---\n\n"; | |
fileContent += artistString; | |
}); | |
var artistNames = _.pluck(artists, 'name').join('_'); | |
var file = "discographies/" + artistNames + ".md"; | |
fs.appendFile(file, fileContent); | |
} | |
}); | |
} | |
artistAlbumsToMarkdown(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment