<commit_before>var Bluebird = require('bluebird')
var promisify = Bluebird.promisify

var createReadStream = require('fs').createReadStream
var createWriteStream = require('fs').createWriteStream
var path = require('path')

var log = require('npmlog')
var md = require('flac-metadata')
var mkdirp = promisify(require('mkdirp'))
var moment = require('moment')
var Comment = md.data.MetaDataBlockVorbisComment
var FLACProcessor = md.Processor

var version = require('../../package.json').version
var nowish = moment().format('YYYYMMDD')

var VENDOR = 'testing packard ' + version + ' ' + nowish
var EMPTY_TRACK = path.resolve(__dirname, '../fixtures/empty.flac')

function makeAlbum (root, tracks) {
  return Bluebird.map(tracks, function (track) {
    return mkdirp(path.dirname(track.file.path)).then(function () {
      return new Bluebird(function (resolve, reject) {
        var processor = new FLACProcessor()
        var source = createReadStream(EMPTY_TRACK)
        var sink = createWriteStream(track.file.path)

        var mdbVorbis
        processor.on('preprocess', function (mdb) {
          if (mdb.type === FLACProcessor.MDB_TYPE_VORBIS_COMMENT) {
            log.silly('makeAlbum.preprocess', track.file.path, 'removing existing tags')
            mdb.remove()
          }

          var tags = [
            'ARTIST=' + track.artist.name,
            'TITLE=' + track.name,
            'ALBUM=' + track.album.name,
            'TRACKNUMBER=' + track.index,
            'DATE=' + track.date
          ]
          if (track.tags && track.tags.genre) {
            tags.push('GENRE=' + track.tags.genre)
          }

          if (mdb.isLast) {
            mdb.isLast = false
            mdbVorbis = Comment.create(
              true,
              VENDOR,
              tags
            )
            log.silly('makeAlbum.preprocess', track.file.path, 'created', mdbVorbis)
          }
        })

        processor.on('postprocess', function () {
          if (mdbVorbis) {
            log.silly('makeAlbum.postprocess', track.file.path, 'publishing tags')
            this.push(mdbVorbis.publish())
          }
        })

        source
          .pipe(processor)
          .pipe(sink)
          .on('error', reject)
          .on('close', function () {
            resolve(track.file.path)
          })
      })
    })
  }, { concurrency: 1 }) // FLAC writer gets confused without this
}

module.exports = { makeAlbum: makeAlbum }<commit_msg>test: FLAC metadata writing had a race<commit_after>
var createReadStream = require('fs').createReadStream
var createWriteStream = require('fs').createWriteStream
var dirname = require('path').dirname
var join = require('path').join

var log = require('npmlog')
var moment = require('moment')
var Bluebird = require('bluebird')
var FLACProcessor = require('flac-metadata').Processor
var VorbisComment = require('flac-metadata').data.MetaDataBlockVorbisComment

var promisify = Bluebird.promisify
var mkdirp = promisify(require('mkdirp'))

var version = require('../../package.json').version
var nowish = moment().format('YYYYMMDD')

var VENDOR = 'testing packard ' + version + ' ' + nowish
var EMPTY_TRACK = join(__dirname, '../fixtures/empty.flac')

function makeAlbum (root, tracks) {
  return Bluebird.map(tracks, function (track) {
    return mkdirp(dirname(track.file.path)).then(function () {
      return new Bluebird(function (resolve, reject) {
        var source = createReadStream(EMPTY_TRACK)
        var processor = new FLACProcessor()
        var sink = createWriteStream(track.file.path)

        processor.on('preprocess', function (mdb) {
          if (mdb.type === FLACProcessor.MDB_TYPE_VORBIS_COMMENT) {
            log.silly('makeAlbum.preprocess', track.file.path, 'removing existing tags')
            mdb.remove()
          } else if (mdb.removed || mdb.isLast) {
            var tags = [
              'ARTIST=' + track.artist.name,
              'TITLE=' + track.name,
              'ALBUM=' + track.album.name,
              'TRACKNUMBER=' + track.index,
              'DATE=' + track.date
            ]
            if (track.tags && track.tags.genre) {
              tags.push('GENRE=' + track.tags.genre)
            }

            var mdbVorbis = VorbisComment.create(mdb.isLast, VENDOR, tags)
            log.silly('makeAlbum.preprocess', track.file.path, 'created', mdbVorbis)
            this.push(mdbVorbis.publish())
          }
        })

        source
          .pipe(processor)
          .on('error', reject)
          .pipe(sink)
          .on('error', reject)
          .on('finish', function () { resolve(track.file.path) })
      })
    })
  }, { concurrency: 1 }) // FLACProcessor has internal state confused by concurrency
}

module.exports = { makeAlbum: makeAlbum }
{'fraction_non_alphanumeric': 0.13322161709258484, 'fraction_numerical': 0.00041893590280687055, 'mean_word_length': 2.4131522516082917, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3900333', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1429, 'n_words': 292}