build.js (1351B)
1 #!/usr/bin/env node 2 3 'use strict'; 4 5 const fs = require('fs'); 6 const path = require('path'); 7 const mimeScore = require('mime-score'); 8 9 let db = require('mime-db'); 10 let chalk = require('chalk'); 11 12 const STANDARD_FACET_SCORE = 900; 13 14 const byExtension = {}; 15 16 // Clear out any conflict extensions in mime-db 17 for (let type in db) { 18 let entry = db[type]; 19 entry.type = type; 20 21 if (!entry.extensions) continue; 22 23 entry.extensions.forEach(ext => { 24 if (ext in byExtension) { 25 const e0 = entry; 26 const e1 = byExtension[ext]; 27 e0.pri = mimeScore(e0.type, e0.source); 28 e1.pri = mimeScore(e1.type, e1.source); 29 30 let drop = e0.pri < e1.pri ? e0 : e1; 31 let keep = e0.pri >= e1.pri ? e0 : e1; 32 drop.extensions = drop.extensions.filter(e => e !== ext); 33 34 console.log(`${ext}: Keeping ${chalk.green(keep.type)} (${keep.pri}), dropping ${chalk.red(drop.type)} (${drop.pri})`); 35 } 36 byExtension[ext] = entry; 37 }); 38 } 39 40 function writeTypesFile(types, path) { 41 fs.writeFileSync(path, JSON.stringify(types)); 42 } 43 44 // Segregate into standard and non-standard types based on facet per 45 // https://tools.ietf.org/html/rfc6838#section-3.1 46 const types = {}; 47 48 Object.keys(db).sort().forEach(k => { 49 const entry = db[k]; 50 types[entry.type] = entry.extensions; 51 }); 52 53 writeTypesFile(types, path.join(__dirname, '..', 'types.json'));