Create release info for albums

This commit is contained in:
Lynn Smeria 2018-08-20 13:13:19 +02:00
parent 15483c0354
commit 7654aaa91a
3 changed files with 39 additions and 21 deletions

View file

@ -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 <ae@cephea.de>",

View file

@ -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);

View file

@ -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);
});
},
};