diff --git a/package.json b/package.json index 79e512b..f5cf089 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "publikator", - "version": "0.2.0", + "version": "0.3.0", "main": "index.js", "repository": "https://github.com/aengl/publikator.git", "author": "Lynn Smeria ", diff --git a/src/cli.js b/src/cli.js index 05d4a83..01039e1 100644 --- a/src/cli.js +++ b/src/cli.js @@ -41,8 +41,7 @@ program const files = scan.findFilesSync(source); const taggedFiles = await scan.readTags(files); const organisedFiles = await organise.byAlbum(target, taggedFiles); - const releaseInfo = generate.releaseInfo(organisedFiles); - fs.writeFileSync(path.resolve(target, 'releases.yml'), releaseInfo); + generate.generateReleaseInfo(organisedFiles); }); debug(process.argv); diff --git a/src/generate.js b/src/generate.js index 92bf61a..f9815bf 100644 --- a/src/generate.js +++ b/src/generate.js @@ -1,30 +1,49 @@ +const fs = require('fs-extra'); const path = require('path'); const _ = require('lodash'); const yaml = require('js-yaml'); const debug = require('debug')('publikator:generate'); -const tags = require('./tags'); + +/** + * Collects unique values across a number of tracks. + */ +const collect = (tracks, callback) => { + const values = _.uniq(_.flatten(tracks.map(t => callback(t)))).filter( + x => !!x + ); + if (values.length === 0) { + return null; + } + return values.length === 1 ? values[0] : values; +}; + +/** + * Creates release information for a single album. + */ +const getAlbumInfo = tracks => { + return { + artists: collect(tracks, t => t.common.artists || t.common.artist), + album: collect(tracks, t => t.common.album), + bitrate: collect(tracks, t => t.format.bitrate), + trackCount: tracks.length, + tracks, + }; +}; module.exports = { /** * Generates a release YAML with data */ - releaseInfo: taggedFiles => { - debug(`generating release info for ${taggedFiles.length} file(s)`); + generateReleaseInfo: taggedFiles => { const albums = _.groupBy(taggedFiles, file => path.dirname(file.path)); - return yaml.safeDump( - Object.keys(albums).map(key => { - const tracks = albums[key]; - return { - 'track-count': tracks.length, - tracks: tracks.map((track, i) => ({ - path: track.path, - position: i, - common: track.common, - format: track.format, - ...tags.getTags(track, ['foo']), - })), - }; - }) - ); + _.forEach(albums, (albumTracks, albumRoot) => { + debug( + `generating release info for album '${path.basename(albumRoot)}' with ${ + albumTracks.length + } track(s)` + ); + const releaseInfo = yaml.safeDump(getAlbumInfo(albumTracks)); + fs.writeFileSync(path.resolve(albumRoot, 'release.yml'), releaseInfo); + }); }, };